OLD | NEW |
(Empty) | |
| 1 library petitparser.test.json_test; |
| 2 |
| 3 import 'package:test/test.dart'; |
| 4 |
| 5 import 'package:petitparser/json.dart'; |
| 6 |
| 7 void main() { |
| 8 var json = new JsonParser(); |
| 9 group('arrays', () { |
| 10 test('empty', () { |
| 11 expect(json.parse('[]').value, []); |
| 12 }); |
| 13 test('small', () { |
| 14 expect(json.parse('["a"]').value, ['a']); |
| 15 }); |
| 16 test('large', () { |
| 17 expect(json.parse('["a", "b", "c"]').value, ['a', 'b', 'c']); |
| 18 }); |
| 19 test('nested', () { |
| 20 expect(json.parse('[["a"]]').value, [['a']]); |
| 21 }); |
| 22 test('invalid', () { |
| 23 expect(json.parse('[').isFailure, isTrue); |
| 24 expect(json.parse('[1').isFailure, isTrue); |
| 25 expect(json.parse('[1,').isFailure, isTrue); |
| 26 expect(json.parse('[1,]').isFailure, isTrue); |
| 27 expect(json.parse('[1 2]').isFailure, isTrue); |
| 28 expect(json.parse('[]]').isFailure, isTrue); |
| 29 }); |
| 30 }); |
| 31 group('objects', () { |
| 32 test('empty', () { |
| 33 expect(json.parse('{}').value, {}); |
| 34 }); |
| 35 test('small', () { |
| 36 expect(json.parse('{"a": 1}').value, {'a': 1}); |
| 37 }); |
| 38 test('large', () { |
| 39 expect(json.parse('{"a": 1, "b": 2, "c": 3}').value, { |
| 40 'a': 1, |
| 41 'b': 2, |
| 42 'c': 3 |
| 43 }); |
| 44 }); |
| 45 test('nested', () { |
| 46 expect(json.parse('{"obj": {"a": 1}}').value, {'obj': {"a": 1}}); |
| 47 }); |
| 48 test('invalid', () { |
| 49 expect(json.parse('{').isFailure, isTrue); |
| 50 expect(json.parse('{\'a\'').isFailure, isTrue); |
| 51 expect(json.parse('{\'a\':').isFailure, isTrue); |
| 52 expect(json.parse('{\'a\':\'b\'').isFailure, isTrue); |
| 53 expect(json.parse('{\'a\':\'b\',').isFailure, isTrue); |
| 54 expect(json.parse('{\'a\'}').isFailure, isTrue); |
| 55 expect(json.parse('{\'a\':}').isFailure, isTrue); |
| 56 expect(json.parse('{\'a\':\'b\',}').isFailure, isTrue); |
| 57 expect(json.parse('{}}').isFailure, isTrue); |
| 58 }); |
| 59 }); |
| 60 group('literals', () { |
| 61 test('valid true', () { |
| 62 expect(json.parse('true').value, isTrue); |
| 63 }); |
| 64 test('invalid true', () { |
| 65 expect(json.parse('tr').isFailure, isTrue); |
| 66 expect(json.parse('trace').isFailure, isTrue); |
| 67 expect(json.parse('truest').isFailure, isTrue); |
| 68 }); |
| 69 test('valid false', () { |
| 70 expect(json.parse('false').value, isFalse); |
| 71 }); |
| 72 test('invalid false', () { |
| 73 expect(json.parse('fa').isFailure, isTrue); |
| 74 expect(json.parse('falsely').isFailure, isTrue); |
| 75 expect(json.parse('fabulous').isFailure, isTrue); |
| 76 }); |
| 77 test('valid null', () { |
| 78 expect(json.parse('null').value, isNull); |
| 79 }); |
| 80 test('invalid null', () { |
| 81 expect(json.parse('nu').isFailure, isTrue); |
| 82 expect(json.parse('nuclear').isFailure, isTrue); |
| 83 expect(json.parse('nullified').isFailure, isTrue); |
| 84 }); |
| 85 test('valid integer', () { |
| 86 expect(json.parse('0').value, 0); |
| 87 expect(json.parse('1').value, 1); |
| 88 expect(json.parse('-1').value, -1); |
| 89 expect(json.parse('12').value, 12); |
| 90 expect(json.parse('-12').value, -12); |
| 91 expect(json.parse('1e2').value, 100); |
| 92 expect(json.parse('1e+2').value, 100); |
| 93 }); |
| 94 test('invalid integer', () { |
| 95 expect(json.parse('00').isFailure, isTrue); |
| 96 expect(json.parse('01').isFailure, isTrue); |
| 97 }); |
| 98 test('invalid integer', () { |
| 99 expect(json.parse('00').isFailure, isTrue); |
| 100 expect(json.parse('01').isFailure, isTrue); |
| 101 }); |
| 102 test('valid float', () { |
| 103 expect(json.parse('0.0').value, 0.0); |
| 104 expect(json.parse('0.12').value, 0.12); |
| 105 expect(json.parse('-0.12').value, -0.12); |
| 106 expect(json.parse('12.34').value, 12.34); |
| 107 expect(json.parse('-12.34').value, -12.34); |
| 108 expect(json.parse('1.2e-1').value, 1.2e-1); |
| 109 expect(json.parse('1.2E-1').value, 1.2e-1); |
| 110 }); |
| 111 test('invalid float', () { |
| 112 expect(json.parse('.1').isFailure, isTrue); |
| 113 expect(json.parse('0.1.1').isFailure, isTrue); |
| 114 }); |
| 115 test('plain string', () { |
| 116 expect(json.parse('""').value, ''); |
| 117 expect(json.parse('"foo"').value, 'foo'); |
| 118 expect(json.parse('"foo bar"').value, 'foo bar'); |
| 119 }); |
| 120 test('escaped string', () { |
| 121 expect(json.parse('"\\""').value, '"'); |
| 122 expect(json.parse('"\\\\"').value, '\\'); |
| 123 expect(json.parse('"\\b"').value, '\b'); |
| 124 expect(json.parse('"\\f"').value, '\f'); |
| 125 expect(json.parse('"\\n"').value, '\n'); |
| 126 expect(json.parse('"\\r"').value, '\r'); |
| 127 expect(json.parse('"\\t"').value, '\t'); |
| 128 }); |
| 129 test('unicode string', () { |
| 130 expect(json.parse('"\\u0030"').value, '0'); |
| 131 expect(json.parse('"\\u007B"').value, '{'); |
| 132 expect(json.parse('"\\u007d"').value, '}'); |
| 133 }); |
| 134 test('invalid string', () { |
| 135 expect(json.parse('"').isFailure, isTrue); |
| 136 expect(json.parse('"a').isFailure, isTrue); |
| 137 expect(json.parse('"a\\\"').isFailure, isTrue); |
| 138 expect(json.parse('"\\u00"').isFailure, isTrue); |
| 139 expect(json.parse('"\\u000X"').isFailure, isTrue); |
| 140 }); |
| 141 }); |
| 142 group('browser', () { |
| 143 test('Internet Explorer', () { |
| 144 var input = '{"recordset": null, "type": "change", "fromElement": null, "t
oElement": null, ' |
| 145 '"altLeft": false, "keyCode": 0, "repeat": false, "reason": 0, "behavi
orCookie": 0, ' |
| 146 '"contentOverflow": false, "behaviorPart": 0, "dataTransfer": null, "c
trlKey": false, ' |
| 147 '"shiftLeft": false, "dataFld": "", "qualifier": "", "wheelDelta": 0,
"bookmarks": null, ' |
| 148 '"button": 0, "srcFilter": null, "nextPage": "", "cancelBubble": false
, "x": 89, "y": ' |
| 149 '502, "screenX": 231, "screenY": 1694, "srcUrn": "", "boundElements":
{"length": 0}, ' |
| 150 '"clientX": 89, "clientY": 502, "propertyName": "", "shiftKey": false,
"ctrlLeft": ' |
| 151 'false, "offsetX": 25, "offsetY": 2, "altKey": false}'; |
| 152 expect(json.parse(input).isSuccess, isTrue); |
| 153 }); |
| 154 test('FireFox', () { |
| 155 var input = '{"type": "change", "eventPhase": 2, "bubbles": true, "cancela
ble": true, ' |
| 156 '"timeStamp": 0, "CAPTURING_PHASE": 1, "AT_TARGET": 2, "BUBBLING_PHASE
": 3, ' |
| 157 '"isTrusted": true, "MOUSEDOWN": 1, "MOUSEUP": 2, "MOUSEOVER": 4, "MOU
SEOUT": 8, ' |
| 158 '"MOUSEMOVE": 16, "MOUSEDRAG": 32, "CLICK": 64, "DBLCLICK": 128, "KEYD
OWN": 256, ' |
| 159 '"KEYUP": 512, "KEYPRESS": 1024, "DRAGDROP": 2048, "FOCUS": 4096, "BLU
R": 8192, ' |
| 160 '"SELECT": 16384, "CHANGE": 32768, "RESET": 65536, "SUBMIT": 131072, "
SCROLL": ' |
| 161 '262144, "LOAD": 524288, "UNLOAD": 1048576, "XFER_DONE": 2097152, "ABO
RT": 4194304, ' |
| 162 '"ERROR": 8388608, "LOCATE": 16777216, "MOVE": 33554432, "RESIZE": 671
08864, ' |
| 163 '"FORWARD": 134217728, "HELP": 268435456, "BACK": 536870912, "TEXT": 1
073741824, ' |
| 164 '"ALT_MASK": 1, "CONTROL_MASK": 2, "SHIFT_MASK": 4, "META_MASK": 8}'; |
| 165 expect(json.parse(input).isSuccess, isTrue); |
| 166 }); |
| 167 test('WebKit', () { |
| 168 var input = '{"returnValue": true, "timeStamp": 1226697417289, "eventPhase
": 2, "type": ' |
| 169 '"change", "cancelable": false, "bubbles": true, "cancelBubble": false
, "MOUSEOUT": 8, ' |
| 170 '"FOCUS": 4096, "CHANGE": 32768, "MOUSEMOVE": 16, "AT_TARGET": 2, "SEL
ECT": 16384, ' |
| 171 '"BLUR": 8192, "KEYUP": 512, "MOUSEDOWN": 1, "MOUSEDRAG": 32, "BUBBLIN
G_PHASE": 3, ' |
| 172 '"MOUSEUP": 2, "CAPTURING_PHASE": 1, "MOUSEOVER": 4, "CLICK": 64, "DBL
CLICK": 128, ' |
| 173 '"KEYDOWN": 256, "KEYPRESS": 1024, "DRAGDROP": 2048}'; |
| 174 expect(json.parse(input).isSuccess, isTrue); |
| 175 }); |
| 176 }); |
| 177 } |
OLD | NEW |