| 1 | import x.json2 |
| 2 | |
| 3 | fn test_decode_escaped_string() { |
| 4 | escaped_strings := ['test', 'test\\sd', 'test\nsd', '\ntest', 'test\\"', 'test\\', 'test\u1234ps', |
| 5 | 'test\u1234', '\u1234\\\t"', '', '\uff0f', 'test \uff0f test', '😀', 'text 😀 text'] |
| 6 | |
| 7 | json_string := json2.encode[[]string](escaped_strings) |
| 8 | decoded_strings := json2.decode[[]string](json_string)! |
| 9 | |
| 10 | assert escaped_strings == decoded_strings |
| 11 | } |
| 12 | |
| 13 | fn test_surrogate() { |
| 14 | assert json2.decode[string](r'"\ud83d\ude00"')! == '😀' |
| 15 | assert json2.decode[string](r'"\ud83d\ude00 text"')! == '😀 text' |
| 16 | } |
| 17 | |
| 18 | fn test_invalid_surrogate() { |
| 19 | if x := json2.decode[string](r'"\ud83d"') { |
| 20 | assert false |
| 21 | } else { |
| 22 | if err is json2.JsonDecodeError { |
| 23 | assert err.line == 1 |
| 24 | assert err.character == 1 |
| 25 | assert err.message == 'Data: Expected a trail surrogate after a head surrogate, but got no valid escape sequence.' |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | if x := json2.decode[string](r'"\ud83d\n\n\n\n"') { |
| 30 | assert false |
| 31 | } else { |
| 32 | if err is json2.JsonDecodeError { |
| 33 | assert err.line == 1 |
| 34 | assert err.character == 1 |
| 35 | assert err.message == 'Data: Expected a trail surrogate after a head surrogate, but got no valid escape sequence.' |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | if x := json2.decode[string](r'"\ud83d\ud83d"') { |
| 40 | assert false |
| 41 | } else { |
| 42 | if err is json2.JsonDecodeError { |
| 43 | assert err.line == 1 |
| 44 | assert err.character == 1 |
| 45 | assert err.message == 'Data: Expected a trail surrogate after a head surrogate, but got D83D.' |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | if x := json2.decode[string](r'"\ude00\ud83d"') { |
| 50 | assert false |
| 51 | } else { |
| 52 | if err is json2.JsonDecodeError { |
| 53 | assert err.line == 1 |
| 54 | assert err.character == 1 |
| 55 | assert err.message == 'Data: Got trail surrogate: DE00 before head surrogate.' |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |