|
| 1 | +package net.minidev.json; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | + |
| 6 | +import java.io.StringWriter; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +public class JStylerObjTest { |
| 10 | + |
| 11 | + @Test |
| 12 | + public void testIsSpace() { |
| 13 | + assertTrue(JStylerObj.isSpace(' ')); |
| 14 | + assertTrue(JStylerObj.isSpace('\t')); |
| 15 | + assertTrue(JStylerObj.isSpace('\n')); |
| 16 | + assertTrue(JStylerObj.isSpace('\r')); |
| 17 | + |
| 18 | + assertFalse(JStylerObj.isSpace('a')); |
| 19 | + assertFalse(JStylerObj.isSpace('1')); |
| 20 | + assertFalse(JStylerObj.isSpace(':')); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testIsSpecialChar() { |
| 25 | + assertTrue(JStylerObj.isSpecialChar('\b')); |
| 26 | + assertTrue(JStylerObj.isSpecialChar('\f')); |
| 27 | + assertTrue(JStylerObj.isSpecialChar('\n')); |
| 28 | + |
| 29 | + assertFalse(JStylerObj.isSpecialChar('a')); |
| 30 | + assertFalse(JStylerObj.isSpecialChar('\t')); |
| 31 | + assertFalse(JStylerObj.isSpecialChar('\r')); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testIsSpecialOpen() { |
| 36 | + assertTrue(JStylerObj.isSpecialOpen('{')); |
| 37 | + assertTrue(JStylerObj.isSpecialOpen('[')); |
| 38 | + assertTrue(JStylerObj.isSpecialOpen(',')); |
| 39 | + assertTrue(JStylerObj.isSpecialOpen(':')); |
| 40 | + |
| 41 | + assertFalse(JStylerObj.isSpecialOpen('}')); |
| 42 | + assertFalse(JStylerObj.isSpecialOpen(']')); |
| 43 | + assertFalse(JStylerObj.isSpecialOpen('a')); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testIsSpecialClose() { |
| 48 | + assertTrue(JStylerObj.isSpecialClose('}')); |
| 49 | + assertTrue(JStylerObj.isSpecialClose(']')); |
| 50 | + assertTrue(JStylerObj.isSpecialClose(',')); |
| 51 | + assertTrue(JStylerObj.isSpecialClose(':')); |
| 52 | + |
| 53 | + assertFalse(JStylerObj.isSpecialClose('{')); |
| 54 | + assertFalse(JStylerObj.isSpecialClose('[')); |
| 55 | + assertFalse(JStylerObj.isSpecialClose('a')); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testIsSpecial() { |
| 60 | + assertTrue(JStylerObj.isSpecial('{')); |
| 61 | + assertTrue(JStylerObj.isSpecial('[')); |
| 62 | + assertTrue(JStylerObj.isSpecial('}')); |
| 63 | + assertTrue(JStylerObj.isSpecial(']')); |
| 64 | + assertTrue(JStylerObj.isSpecial(',')); |
| 65 | + assertTrue(JStylerObj.isSpecial(':')); |
| 66 | + assertTrue(JStylerObj.isSpecial('\'')); |
| 67 | + assertTrue(JStylerObj.isSpecial('"')); |
| 68 | + |
| 69 | + assertFalse(JStylerObj.isSpecial('a')); |
| 70 | + assertFalse(JStylerObj.isSpecial('1')); |
| 71 | + assertFalse(JStylerObj.isSpecial(' ')); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testIsUnicode() { |
| 76 | + assertTrue(JStylerObj.isUnicode('\u0000')); |
| 77 | + assertTrue(JStylerObj.isUnicode('\u001F')); |
| 78 | + assertTrue(JStylerObj.isUnicode('\u007F')); |
| 79 | + assertTrue(JStylerObj.isUnicode('\u009F')); |
| 80 | + assertTrue(JStylerObj.isUnicode('\u2000')); |
| 81 | + assertTrue(JStylerObj.isUnicode('\u20FF')); |
| 82 | + |
| 83 | + assertFalse(JStylerObj.isUnicode('a')); |
| 84 | + assertFalse(JStylerObj.isUnicode('Z')); |
| 85 | + assertFalse(JStylerObj.isUnicode('1')); |
| 86 | + assertFalse(JStylerObj.isUnicode(' ')); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testIsKeyword() { |
| 91 | + assertTrue(JStylerObj.isKeyword("null")); |
| 92 | + assertTrue(JStylerObj.isKeyword("true")); |
| 93 | + assertTrue(JStylerObj.isKeyword("false")); |
| 94 | + assertTrue(JStylerObj.isKeyword("NaN")); |
| 95 | + |
| 96 | + assertFalse(JStylerObj.isKeyword("NULL")); |
| 97 | + assertFalse(JStylerObj.isKeyword("TRUE")); |
| 98 | + assertFalse(JStylerObj.isKeyword("FALSE")); |
| 99 | + assertFalse(JStylerObj.isKeyword("hello")); |
| 100 | + assertFalse(JStylerObj.isKeyword("a")); |
| 101 | + assertFalse(JStylerObj.isKeyword("ab")); |
| 102 | + assertFalse(JStylerObj.isKeyword("")); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testMPSimpleMustBeProtect() { |
| 107 | + JStylerObj.MustProtect mp = JStylerObj.MP_SIMPLE; |
| 108 | + |
| 109 | + assertFalse(mp.mustBeProtect(null)); |
| 110 | + |
| 111 | + assertTrue(mp.mustBeProtect("")); |
| 112 | + assertTrue(mp.mustBeProtect(" text")); |
| 113 | + assertTrue(mp.mustBeProtect("text ")); |
| 114 | + assertTrue(mp.mustBeProtect(" text ")); |
| 115 | + |
| 116 | + assertTrue(mp.mustBeProtect("123")); |
| 117 | + assertTrue(mp.mustBeProtect("-456")); |
| 118 | + assertTrue(mp.mustBeProtect("0")); |
| 119 | + |
| 120 | + assertTrue(mp.mustBeProtect("null")); |
| 121 | + assertTrue(mp.mustBeProtect("true")); |
| 122 | + assertTrue(mp.mustBeProtect("false")); |
| 123 | + assertTrue(mp.mustBeProtect("NaN")); |
| 124 | + |
| 125 | + assertTrue(mp.mustBeProtect("text{")); |
| 126 | + assertTrue(mp.mustBeProtect("text[")); |
| 127 | + assertTrue(mp.mustBeProtect("text\"")); |
| 128 | + assertTrue(mp.mustBeProtect("text'")); |
| 129 | + |
| 130 | + assertTrue(mp.mustBeProtect("text\n")); |
| 131 | + assertTrue(mp.mustBeProtect("text\t")); |
| 132 | + assertTrue(mp.mustBeProtect("text ")); |
| 133 | + |
| 134 | + assertTrue(mp.mustBeProtect("text\u0001")); |
| 135 | + |
| 136 | + assertFalse(mp.mustBeProtect("hello")); |
| 137 | + assertFalse(mp.mustBeProtect("world")); |
| 138 | + assertFalse(mp.mustBeProtect("test")); |
| 139 | + assertFalse(mp.mustBeProtect("a")); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void testMPTrueMustBeProtect() { |
| 144 | + JStylerObj.MustProtect mp = JStylerObj.MP_TRUE; |
| 145 | + |
| 146 | + assertTrue(mp.mustBeProtect("anything")); |
| 147 | + assertTrue(mp.mustBeProtect("")); |
| 148 | + assertTrue(mp.mustBeProtect("hello")); |
| 149 | + assertTrue(mp.mustBeProtect("123")); |
| 150 | + assertTrue(mp.mustBeProtect(null)); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testMPAggressiveMustBeProtect() { |
| 155 | + JStylerObj.MustProtect mp = JStylerObj.MP_AGGRESIVE; |
| 156 | + |
| 157 | + assertFalse(mp.mustBeProtect(null)); |
| 158 | + |
| 159 | + assertTrue(mp.mustBeProtect("")); |
| 160 | + assertTrue(mp.mustBeProtect(" text")); |
| 161 | + assertTrue(mp.mustBeProtect("text ")); |
| 162 | + |
| 163 | + assertTrue(mp.mustBeProtect("null")); |
| 164 | + assertTrue(mp.mustBeProtect("true")); |
| 165 | + assertTrue(mp.mustBeProtect("false")); |
| 166 | + assertTrue(mp.mustBeProtect("NaN")); |
| 167 | + |
| 168 | + assertTrue(mp.mustBeProtect("123")); |
| 169 | + assertTrue(mp.mustBeProtect("-456")); |
| 170 | + assertTrue(mp.mustBeProtect("3.14")); |
| 171 | + assertTrue(mp.mustBeProtect("1.23e5")); |
| 172 | + assertTrue(mp.mustBeProtect("1.23E-5")); |
| 173 | + assertTrue(mp.mustBeProtect("1.23e+5")); |
| 174 | + |
| 175 | + assertTrue(mp.mustBeProtect("{text")); |
| 176 | + assertTrue(mp.mustBeProtect("text}")); |
| 177 | + assertTrue(mp.mustBeProtect("text,")); |
| 178 | + |
| 179 | + assertTrue(mp.mustBeProtect("text\u0001")); |
| 180 | + |
| 181 | + assertFalse(mp.mustBeProtect("hello")); |
| 182 | + assertFalse(mp.mustBeProtect("world")); |
| 183 | + assertFalse(mp.mustBeProtect("a")); |
| 184 | + assertFalse(mp.mustBeProtect("abc123def")); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + public void testMPAggressiveNumberLike() { |
| 189 | + JStylerObj.MustProtect mp = JStylerObj.MP_AGGRESIVE; |
| 190 | + |
| 191 | + assertTrue(mp.mustBeProtect("0")); |
| 192 | + assertTrue(mp.mustBeProtect("123")); |
| 193 | + assertTrue(mp.mustBeProtect("-456")); |
| 194 | + assertTrue(mp.mustBeProtect("3.14")); |
| 195 | + assertTrue(mp.mustBeProtect("1e5")); |
| 196 | + assertTrue(mp.mustBeProtect("1E5")); |
| 197 | + assertTrue(mp.mustBeProtect("1e+5")); |
| 198 | + assertTrue(mp.mustBeProtect("1e-5")); |
| 199 | + assertTrue(mp.mustBeProtect("1.23e5")); |
| 200 | + assertTrue(mp.mustBeProtect("1.23E-5")); |
| 201 | + |
| 202 | + assertFalse(mp.mustBeProtect("1e")); |
| 203 | + assertFalse(mp.mustBeProtect("1E")); |
| 204 | + assertFalse(mp.mustBeProtect("123abc")); |
| 205 | + assertFalse(mp.mustBeProtect("12.34.56")); |
| 206 | + } |
| 207 | + |
| 208 | + @Test |
| 209 | + public void testEscapeLTStringProtector() { |
| 210 | + JStylerObj.StringProtector protector = JStylerObj.ESCAPE_LT; |
| 211 | + StringWriter writer = new StringWriter(); |
| 212 | + |
| 213 | + protector.escape("hello\"world", writer); |
| 214 | + assertTrue(writer.toString().contains("hello\\\"world")); |
| 215 | + |
| 216 | + writer = new StringWriter(); |
| 217 | + protector.escape("line1\nline2", writer); |
| 218 | + assertTrue(writer.toString().contains("line1\\nline2")); |
| 219 | + |
| 220 | + writer = new StringWriter(); |
| 221 | + protector.escape("tab\there", writer); |
| 222 | + assertTrue(writer.toString().contains("tab\\there")); |
| 223 | + |
| 224 | + writer = new StringWriter(); |
| 225 | + protector.escape("back\\slash", writer); |
| 226 | + assertTrue(writer.toString().contains("back\\\\slash")); |
| 227 | + |
| 228 | + writer = new StringWriter(); |
| 229 | + protector.escape("backspace\b", writer); |
| 230 | + assertTrue(writer.toString().contains("backspace\\b")); |
| 231 | + |
| 232 | + writer = new StringWriter(); |
| 233 | + protector.escape("formfeed\f", writer); |
| 234 | + assertTrue(writer.toString().contains("formfeed\\f")); |
| 235 | + |
| 236 | + writer = new StringWriter(); |
| 237 | + protector.escape("carriage\rreturn", writer); |
| 238 | + assertTrue(writer.toString().contains("carriage\\rreturn")); |
| 239 | + |
| 240 | + writer = new StringWriter(); |
| 241 | + protector.escape("unicode\u0001char", writer); |
| 242 | + assertTrue(writer.toString().contains("unicode\\u0001char")); |
| 243 | + } |
| 244 | + |
| 245 | + @Test |
| 246 | + public void testEscape4WebStringProtector() { |
| 247 | + JStylerObj.StringProtector protector = JStylerObj.ESCAPE4Web; |
| 248 | + StringWriter writer = new StringWriter(); |
| 249 | + |
| 250 | + protector.escape("hello/world", writer); |
| 251 | + assertTrue(writer.toString().contains("hello\\/world")); |
| 252 | + |
| 253 | + writer = new StringWriter(); |
| 254 | + protector.escape("hello\"world", writer); |
| 255 | + assertTrue(writer.toString().contains("hello\\\"world")); |
| 256 | + |
| 257 | + writer = new StringWriter(); |
| 258 | + protector.escape("line1\nline2", writer); |
| 259 | + assertTrue(writer.toString().contains("line1\\nline2")); |
| 260 | + |
| 261 | + writer = new StringWriter(); |
| 262 | + protector.escape("unicode\u0001char", writer); |
| 263 | + assertTrue(writer.toString().contains("unicode\\u0001char")); |
| 264 | + } |
| 265 | + |
| 266 | + @Test |
| 267 | + public void testEscapeUnicodeCharacters() { |
| 268 | + JStylerObj.StringProtector protector = JStylerObj.ESCAPE_LT; |
| 269 | + StringWriter writer = new StringWriter(); |
| 270 | + |
| 271 | + protector.escape("\u0000\u001F\u007F\u009F", writer); |
| 272 | + String result = writer.toString(); |
| 273 | + assertTrue(result.contains("\\u0000")); |
| 274 | + assertTrue(result.contains("\\u001F")); |
| 275 | + assertTrue(result.contains("\\u007F")); |
| 276 | + assertTrue(result.contains("\\u009F")); |
| 277 | + } |
| 278 | + |
| 279 | + @Test |
| 280 | + public void testEscapeUnicodeRange2000() { |
| 281 | + JStylerObj.StringProtector protector = JStylerObj.ESCAPE_LT; |
| 282 | + StringWriter writer = new StringWriter(); |
| 283 | + |
| 284 | + protector.escape("\u2000\u20FF", writer); |
| 285 | + String result = writer.toString(); |
| 286 | + assertTrue(result.contains("\\u2000")); |
| 287 | + assertTrue(result.contains("\\u20FF")); |
| 288 | + } |
| 289 | + |
| 290 | + @Test |
| 291 | + public void testEscapeNormalCharacters() { |
| 292 | + JStylerObj.StringProtector protector = JStylerObj.ESCAPE_LT; |
| 293 | + StringWriter writer = new StringWriter(); |
| 294 | + |
| 295 | + protector.escape("Hello World 123!", writer); |
| 296 | + String result = writer.toString(); |
| 297 | + assertTrue(result.equals("Hello World 123!")); |
| 298 | + } |
| 299 | + |
| 300 | + @Test |
| 301 | + public void testMPSimpleDigitStarting() { |
| 302 | + JStylerObj.MustProtect mp = JStylerObj.MP_SIMPLE; |
| 303 | + |
| 304 | + assertTrue(mp.mustBeProtect("0abc")); |
| 305 | + assertTrue(mp.mustBeProtect("1test")); |
| 306 | + assertTrue(mp.mustBeProtect("9xyz")); |
| 307 | + assertTrue(mp.mustBeProtect("-5abc")); |
| 308 | + |
| 309 | + assertFalse(mp.mustBeProtect("a123")); |
| 310 | + assertFalse(mp.mustBeProtect("test")); |
| 311 | + } |
| 312 | + |
| 313 | + @Test |
| 314 | + public void testConstants() { |
| 315 | + assertTrue(JStylerObj.MP_SIMPLE instanceof JStylerObj.MustProtect); |
| 316 | + assertTrue(JStylerObj.MP_TRUE instanceof JStylerObj.MustProtect); |
| 317 | + assertTrue(JStylerObj.MP_AGGRESIVE instanceof JStylerObj.MustProtect); |
| 318 | + assertTrue(JStylerObj.ESCAPE_LT instanceof JStylerObj.StringProtector); |
| 319 | + assertTrue(JStylerObj.ESCAPE4Web instanceof JStylerObj.StringProtector); |
| 320 | + } |
| 321 | +} |
0 commit comments