source: imaps-frontend/node_modules/flatted/python/test.py@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.1 KB
Line 
1from flatted import stringify as _stringify, parse
2
3def stringify(value):
4 return _stringify(value, separators=(',', ':'))
5
6assert stringify([None, None]) == '[[null,null]]'
7
8a = []
9o = {}
10
11assert stringify(a) == '[[]]'
12assert stringify(o) == '[{}]'
13
14a.append(a)
15o['o'] = o
16
17assert stringify(a) == '[["0"]]'
18assert stringify(o) == '[{"o":"0"}]'
19
20b = parse(stringify(a))
21assert isinstance(b, list) and b[0] == b
22
23a.append(1)
24a.append('two')
25a.append(True)
26o['one'] = 1
27o['two'] = 'two'
28o['three'] = True
29
30assert stringify(a) == '[["0",1,"1",true],"two"]'
31assert stringify(o) == '[{"o":"0","one":1,"two":"1","three":true},"two"]'
32
33a.append(o)
34o['a'] = a
35
36assert stringify(a) == '[["0",1,"1",true,"2"],"two",{"o":"2","one":1,"two":"1","three":true,"a":"0"}]'
37assert stringify(o) == '[{"o":"0","one":1,"two":"1","three":true,"a":"2"},"two",["2",1,"1",true,"0"]]'
38
39a.append({'test': 'OK'})
40a.append([1, 2, 3])
41
42o['test'] = {'test': 'OK'}
43o['array'] = [1, 2, 3]
44
45assert stringify(a) == '[["0",1,"1",true,"2","3","4"],"two",{"o":"2","one":1,"two":"1","three":true,"a":"0","test":"3","array":"4"},{"test":"5"},[1,2,3],"OK"]'
46assert stringify(o) == '[{"o":"0","one":1,"two":"1","three":true,"a":"2","test":"3","array":"4"},"two",["2",1,"1",true,"0","3","4"],{"test":"5"},[1,2,3],"OK"]'
47
48a2 = parse(stringify(a));
49o2 = parse(stringify(o));
50
51assert a2[0] == a2
52assert o2['o'] == o2
53
54assert a2[1] == 1 and a2[2] == 'two' and a2[3] == True and isinstance(a2[4], dict)
55assert a2[4] == a2[4]['o'] and a2 == a2[4]['o']['a']
56
57str = parse('[{"prop":"1","a":"2","b":"3"},{"value":123},["4","5"],{"e":"6","t":"7","p":4},{},{"b":"8"},"f",{"a":"9"},["10"],"sup",{"a":1,"d":2,"c":"7","z":"11","h":1},{"g":2,"a":"7","b":"12","f":6},{"r":4,"u":"7","c":5}]')
58assert str['b']['t']['a'] == 'sup' and str['a'][1]['b'][0]['c'] == str['b']['t']
59
60oo = parse('[{"a":"1","b":"0","c":"2"},{"aa":"3"},{"ca":"4","cb":"5","cc":"6","cd":"7","ce":"8","cf":"9"},{"aaa":"10"},{"caa":"4"},{"cba":"5"},{"cca":"2"},{"cda":"4"},"value2","value3","value1"]');
61assert oo['a']['aa']['aaa'] == 'value1' and oo == oo['b'] and oo['c']['ca']['caa'] == oo['c']['ca']
62
63print('OK')
Note: See TracBrowser for help on using the repository browser.