1 package com.puppycrawl.tools.checkstyle.checks.coding; 2 3 import com.puppycrawl.tools.checkstyle.BaseCheckTestCase; 4 import com.puppycrawl.tools.checkstyle.DefaultConfiguration; 5 6 public class MagicNumberCheckTest 7 extends BaseCheckTestCase 8 { 9 public void testDefault() 10 throws Exception 11 { 12 final DefaultConfiguration checkConfig = 13 createCheckConfig(MagicNumberCheck.class); 14 final String [] expected = { 15 "41:26: '3' is a magic number.", 16 "42:32: '1.5' is a magic number.", 17 "43:27: '3' is a magic number.", 18 "43:31: '4' is a magic number.", 19 "45:29: '3' is a magic number.", 20 "47:23: '3' is a magic number.", 21 "48:26: '1.5' is a magic number.", 22 "50:22: '3' is a magic number.", 23 "50:29: '5' is a magic number.", 24 "50:37: '3' is a magic number.", 25 "54:26: '3' is a magic number.", 26 "55:39: '3' is a magic number.", 27 "60:25: '010' is a magic number.", 28 "61:25: '011' is a magic number.", 29 "63:30: '010L' is a magic number.", 30 "64:30: '011l' is a magic number.", 31 "68:24: '0x10' is a magic number.", 32 "69:24: '0X011' is a magic number.", 33 "71:29: '0x10L' is a magic number.", 34 "72:29: '0X11l' is a magic number.", 35 "85:28: '3' is a magic number.", 36 "92:14: '0xffffffffL' is a magic number.", 37 "100:30: '+3' is a magic number.", 38 "101:29: '-2' is a magic number.", 39 "102:35: '+3.5' is a magic number.", 40 "103:36: '-2.5' is a magic number.", 41 "111:35: '0x80000000' is a magic number.", 42 "112:36: '0x8000000000000000L' is a magic number.", 43 "115:37: '020000000000' is a magic number.", 44 "116:38: '01000000000000000000000L' is a magic number.", 45 "131:20: '378' is a magic number.", 46 }; 47 verify(checkConfig, getPath("InputMagicNumber.java"), expected); 48 } 49 50 public void testIgnoreSome() 51 throws Exception 52 { 53 final DefaultConfiguration checkConfig = 54 createCheckConfig(MagicNumberCheck.class); 55 checkConfig.addAttribute("ignoreNumbers", "0, 1, 3.0, 8, 16"); 56 final String [] expected = { 57 "22:25: '2' is a magic number.", 58 "28:35: '2' is a magic number.", 59 "30:24: '2' is a magic number.", 60 "32:29: '2.0' is a magic number.", 61 "34:29: '2' is a magic number.", 62 "36:17: '2' is a magic number.", 63 "38:19: '2.0' is a magic number.", 64 "42:32: '1.5' is a magic number.", 65 "43:31: '4' is a magic number.", 66 "48:26: '1.5' is a magic number.", 67 "50:29: '5' is a magic number.", 68 "61:25: '011' is a magic number.", 69 "64:30: '011l' is a magic number.", 70 "69:24: '0X011' is a magic number.", 71 "72:29: '0X11l' is a magic number.", 72 "92:14: '0xffffffffL' is a magic number.", 73 "101:29: '-2' is a magic number.", 74 "102:35: '+3.5' is a magic number.", 75 "103:36: '-2.5' is a magic number.", 76 "109:34: '0xffffffff' is a magic number.", 77 "110:36: '0xffffffffffffffffL' is a magic number.", 78 "111:35: '0x80000000' is a magic number.", 79 "112:36: '0x8000000000000000L' is a magic number.", 80 "113:36: '037777777777' is a magic number.", 81 "114:38: '01777777777777777777777L' is a magic number.", 82 "115:37: '020000000000' is a magic number.", 83 "116:38: '01000000000000000000000L' is a magic number.", 84 "131:20: '378' is a magic number.", 85 }; 86 verify(checkConfig, getPath("InputMagicNumber.java"), expected); 87 } 88 89 public void testIgnoreNone() 90 throws Exception 91 { 92 final DefaultConfiguration checkConfig = 93 createCheckConfig(MagicNumberCheck.class); 94 checkConfig.addAttribute("ignoreNumbers", ""); 95 final String [] expected = { 96 "21:24: '1' is a magic number.", 97 "22:25: '2' is a magic number.", 98 "23:26: '0L' is a magic number.", 99 "24:26: '0l' is a magic number.", 100 "25:30: '0D' is a magic number.", 101 "26:30: '0d' is a magic number.", 102 "28:35: '2' is a magic number.", 103 "30:20: '1' is a magic number.", 104 "30:24: '2' is a magic number.", 105 "31:21: '1' is a magic number.", 106 "32:23: '1.0' is a magic number.", 107 "32:29: '2.0' is a magic number.", 108 "34:22: '0' is a magic number.", 109 "34:29: '2' is a magic number.", 110 "36:13: '1' is a magic number.", 111 "36:17: '2' is a magic number.", 112 "38:13: '1.0' is a magic number.", 113 "38:19: '2.0' is a magic number.", 114 "41:26: '3' is a magic number.", 115 "42:32: '1.5' is a magic number.", 116 "43:27: '3' is a magic number.", 117 "43:31: '4' is a magic number.", 118 "45:29: '3' is a magic number.", 119 "47:23: '3' is a magic number.", 120 "48:26: '1.5' is a magic number.", 121 "50:22: '3' is a magic number.", 122 "50:29: '5' is a magic number.", 123 "50:37: '3' is a magic number.", 124 "54:26: '3' is a magic number.", 125 "55:39: '3' is a magic number.", 126 "59:25: '00' is a magic number.", 127 "60:25: '010' is a magic number.", 128 "61:25: '011' is a magic number.", 129 "63:30: '010L' is a magic number.", 130 "64:30: '011l' is a magic number.", 131 "67:23: '0x0' is a magic number.", 132 "68:24: '0x10' is a magic number.", 133 "69:24: '0X011' is a magic number.", 134 "70:28: '0x0L' is a magic number.", 135 "71:29: '0x10L' is a magic number.", 136 "72:29: '0X11l' is a magic number.", 137 "85:28: '3' is a magic number.", 138 "92:14: '0xffffffffL' is a magic number.", 139 "100:30: '+3' is a magic number.", 140 "101:29: '-2' is a magic number.", 141 "102:35: '+3.5' is a magic number.", 142 "103:36: '-2.5' is a magic number.", 143 "109:34: '0xffffffff' is a magic number.", 144 "110:36: '0xffffffffffffffffL' is a magic number.", 145 "111:35: '0x80000000' is a magic number.", 146 "112:36: '0x8000000000000000L' is a magic number.", 147 "113:36: '037777777777' is a magic number.", 148 "114:38: '01777777777777777777777L' is a magic number.", 149 "115:37: '020000000000' is a magic number.", 150 "116:38: '01000000000000000000000L' is a magic number.", 151 "131:20: '378' is a magic number.", 152 }; 153 verify(checkConfig, getPath("InputMagicNumber.java"), expected); 154 } 155 156 public void testIntegersOnly() 157 throws Exception 158 { 159 final DefaultConfiguration checkConfig = 160 createCheckConfig(MagicNumberCheck.class); 161 checkConfig.addAttribute("tokens", "NUM_INT, NUM_LONG"); 162 final String [] expected = { 163 "41:26: '3' is a magic number.", 164 "43:27: '3' is a magic number.", 165 "43:31: '4' is a magic number.", 166 "45:29: '3' is a magic number.", 167 "47:23: '3' is a magic number.", 168 "50:22: '3' is a magic number.", 169 "50:29: '5' is a magic number.", 170 "50:37: '3' is a magic number.", 171 "54:26: '3' is a magic number.", 172 "55:39: '3' is a magic number.", 173 "60:25: '010' is a magic number.", 174 "61:25: '011' is a magic number.", 175 "63:30: '010L' is a magic number.", 176 "64:30: '011l' is a magic number.", 177 "68:24: '0x10' is a magic number.", 178 "69:24: '0X011' is a magic number.", 179 "71:29: '0x10L' is a magic number.", 180 "72:29: '0X11l' is a magic number.", 181 "85:28: '3' is a magic number.", 182 "92:14: '0xffffffffL' is a magic number.", 183 "100:30: '+3' is a magic number.", 184 "101:29: '-2' is a magic number.", 185 "111:35: '0x80000000' is a magic number.", 186 "112:36: '0x8000000000000000L' is a magic number.", 187 "115:37: '020000000000' is a magic number.", 188 "116:38: '01000000000000000000000L' is a magic number.", 189 "131:20: '378' is a magic number.", 190 }; 191 verify(checkConfig, getPath("InputMagicNumber.java"), expected); 192 } 193 194 public void testIgnoreNegativeOctalHex() 195 throws Exception 196 { 197 final DefaultConfiguration checkConfig = 198 createCheckConfig(MagicNumberCheck.class); 199 checkConfig.addAttribute("ignoreNumbers", "-9223372036854775808, -2147483648, -1, 0, 1, 2"); 200 checkConfig.addAttribute("tokens", "NUM_INT, NUM_LONG"); 201 final String [] expected = { 202 "41:26: '3' is a magic number.", 203 "43:27: '3' is a magic number.", 204 "43:31: '4' is a magic number.", 205 "45:29: '3' is a magic number.", 206 "47:23: '3' is a magic number.", 207 "50:22: '3' is a magic number.", 208 "50:29: '5' is a magic number.", 209 "50:37: '3' is a magic number.", 210 "54:26: '3' is a magic number.", 211 "55:39: '3' is a magic number.", 212 "60:25: '010' is a magic number.", 213 "61:25: '011' is a magic number.", 214 "63:30: '010L' is a magic number.", 215 "64:30: '011l' is a magic number.", 216 "68:24: '0x10' is a magic number.", 217 "69:24: '0X011' is a magic number.", 218 "71:29: '0x10L' is a magic number.", 219 "72:29: '0X11l' is a magic number.", 220 "85:28: '3' is a magic number.", 221 "92:14: '0xffffffffL' is a magic number.", 222 "100:30: '+3' is a magic number.", 223 "101:29: '-2' is a magic number.", 224 "131:20: '378' is a magic number.", 225 }; 226 verify(checkConfig, getPath("InputMagicNumber.java"), expected); 227 } 228 } 229 230 | Popular Tags |