ig-serialize 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test.js ADDED
@@ -0,0 +1,168 @@
1
+ #!/usr/bin/env node
2
+ /**********************************************************************
3
+ *
4
+ *
5
+ *
6
+ **********************************************************************/
7
+ ((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define)
8
+ (function(require){ var module={} // make module AMD/node compatible...
9
+ /*********************************************************************/
10
+
11
+ var test = require('ig-test')
12
+
13
+ var eJSON = require('./serialize')
14
+
15
+
16
+ //---------------------------------------------------------------------
17
+ // Test data format:
18
+ // [
19
+ // <data>,
20
+ // // tags (optionsal)
21
+ // json, - if the data is JSON-compatible, value true|false (default: undefined)
22
+ // ...
23
+ // ]
24
+ //
25
+
26
+ var json = true
27
+ var ejson = false
28
+
29
+ // XXX test whitespace handling...
30
+ var setups = test.Setups({
31
+ 'true': function(assert){
32
+ return ['true', json] },
33
+ 'false': function(assert){
34
+ return ['false', json] },
35
+ 'null': function(assert){
36
+ return ['null', json] },
37
+ 'undefined': function(assert){
38
+ return ['undefined'] },
39
+ 'NaN': function(assert){
40
+ return ['NaN'] },
41
+
42
+ number: function(assert){
43
+ return ['123', json] },
44
+ 'number-neg': function(assert){
45
+ return ['-123', json] },
46
+ 'number-zero': function(assert){
47
+ return ['0', json] },
48
+ 'float-a': function(assert){
49
+ return ['0.123', json] },
50
+ 'float-b': function(assert){
51
+ return ['1.23', json] },
52
+ // XXX need a way to test this...
53
+ //'float-a': function(assert){
54
+ // return '.123' },
55
+ //'float-c': function(assert){
56
+ // return '123.' },
57
+ // XXX also test:
58
+ // hex/bin/orc/...
59
+ Infinity: function(assert){
60
+ return ['Infinity'] },
61
+ nInfinity: function(assert){
62
+ return ['-Infinity'] },
63
+
64
+ // XXX also test diffrerent quotations...
65
+ string: function(assert){
66
+ return ['"string"', json] },
67
+
68
+ 'array-empty': function(assert){
69
+ return ['[]', json] },
70
+ 'array-sparse-a': function(assert){
71
+ return ['[<empty>]'] },
72
+ 'array-sparse-b': function(assert){
73
+ return ['["a",<empty>]'] },
74
+ 'array-sparse-c': function(assert){
75
+ return ['[<empty>,"a"]'] },
76
+ 'array-sparse-d': function(assert){
77
+ return ['[<empty>,1,<empty>,<empty>,2,<empty>]'] },
78
+
79
+ 'object-empty': function(assert){
80
+ return ['{}', json] },
81
+
82
+ 'set-empty': function(assert){
83
+ return ['Set([])'] },
84
+
85
+ 'map-empty': function(assert){
86
+ return ['Map([])'] },
87
+
88
+ 'function': function(assert){
89
+ return ['<FUNCTION[14,(function(){})]>'] },
90
+
91
+ // recursive...
92
+ 'array-recursive': function(assert){
93
+ return ['[<RECURSIVE[]>]'] },
94
+ 'object-recursive': function(assert){
95
+ return ['{"r":<RECURSIVE[]>}'] },
96
+ 'set-recursive': function(assert){
97
+ return ['Set([<RECURSIVE[]>])'] },
98
+ 'map-recursive-key': function(assert){
99
+ return ['Map([[<RECURSIVE[]>,"value"]])'] },
100
+ 'map-recursive-value': function(assert){
101
+ return ['Map([["key",<RECURSIVE[]>]])'] },
102
+
103
+ })
104
+
105
+ test.Modifiers({
106
+ // NOTE: we are not simply editing strings as we'll need to also
107
+ // update all the recursion paths which is not trivial...
108
+ 'array-stuffed': function(assert, [setup, json]){
109
+ return [
110
+ eJSON.serialize(
111
+ [ eJSON.deserialize(setup, true) ] ),
112
+ json,
113
+ ] },
114
+ 'object-stuffed': function(assert, [setup, json]){
115
+ return [
116
+ eJSON.serialize(
117
+ { key: eJSON.deserialize(setup, true) } ),
118
+ json,
119
+ ] },
120
+ // NOTE: these explicitly remove JSON compatibility regardless of input...
121
+ 'set-stuffed': function(assert, [setup]){
122
+ return [
123
+ eJSON.serialize(
124
+ new Set([ eJSON.deserialize(setup, true) ]) ),
125
+ ejson,
126
+ ] },
127
+ 'map-key-stuffed': function(assert, [setup]){
128
+ return [
129
+ eJSON.serialize(
130
+ new Map([[eJSON.deserialize(setup, true), "value"]]) ),
131
+ ejson,
132
+ ] },
133
+ 'map-value-stuffed': function(assert, [setup]){
134
+ return [
135
+ eJSON.serialize(
136
+ new Map([["key", eJSON.deserialize(setup, true)]]) ),
137
+ ejson,
138
+ ] },
139
+ })
140
+
141
+ test.Tests({
142
+ 'self-apply': function(assert, [setup]){
143
+ var res
144
+ assert(
145
+ setup == (res = eJSON.serialize( eJSON.deserialize(setup, true) ) ),
146
+ 'serialize(deserialize( setup )) == setup: expected:', setup, 'got:', res) },
147
+
148
+ 'json-caopatiility': function(assert, [setup, json], skipTest){
149
+ if(!json){
150
+ skipTest()
151
+ return }
152
+ var obj = eJSON.deserialize(setup, true)
153
+ assert(
154
+ JSON.stringify(obj) == eJSON.serialize(obj) ) },
155
+ })
156
+
157
+
158
+ //---------------------------------------------------------------------
159
+
160
+ typeof(__filename) != 'undefined'
161
+ && __filename == (require.main || {}).filename
162
+ && test.run()
163
+
164
+
165
+
166
+
167
+ /**********************************************************************
168
+ * vim:set ts=4 sw=4 nowrap : */ return module })