DSON 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,390 +1,378 @@
1
- # -*- encoding : utf-8 -*-
2
- require 'DSON'
3
-
4
- require_relative 'lib/example_class'
5
-
6
- PUNCTUATION_MATCH = '(,|\.|!|\\?)'
7
- AND_MATCH = '(and|also)'
8
-
9
- describe 'simple hashes' do
10
-
11
- it 'should be correct with an empty hash' do
12
- dson_hash = Hash.new
13
- dson_string = DSON.such_serialize_wow(dson_hash)
14
-
15
- expect(dson_string).to eq(
16
- 'such wow'
17
- )
18
- end
19
-
20
- it 'should be correct with a hash with one element' do
21
- dson_hash = {
22
- dson: 'awesome'
23
- }
24
- dson_string = DSON.such_serialize_wow(dson_hash)
25
-
26
- expect(dson_string).to eq(
27
- 'such "dson" is "awesome" wow'
28
- )
29
- end
30
-
31
- it 'should be correct with a hash with two elements' do
32
- dson_or_ruby = '("dson" is "awesome"|"ruby" is "great")'
33
-
34
- dson_hash = {
35
- dson: 'awesome',
36
- ruby: 'great'
37
- }
38
- dson_string = DSON.such_serialize_wow(dson_hash)
39
-
40
- expect(dson_string).to match(
41
- /such #{dson_or_ruby}#{PUNCTUATION_MATCH} #{dson_or_ruby} wow/
42
- )
43
- end
44
-
45
- it 'should be correct with a hash with three elements' do
46
- dson_ruby_or_dogescript =
47
- '(' \
48
- '"dson" is "awesome"' \
49
- '|' \
50
- '"ruby" is "great"' \
51
- '|' \
52
- '"dogescript" is "fine"' \
53
- ')'
54
-
55
- dson_hash = {
56
- dson: 'awesome',
57
- ruby: 'great',
58
- dogescript: 'fine'
59
- }
60
- dson_string = DSON.such_serialize_wow(dson_hash)
61
-
62
- regex_string =
63
- 'such' \
64
- ' ' +
65
- dson_ruby_or_dogescript +
66
- PUNCTUATION_MATCH +
67
- ' ' +
68
- dson_ruby_or_dogescript +
69
- PUNCTUATION_MATCH +
70
- ' ' +
71
- dson_ruby_or_dogescript +
72
- ' ' +
73
- 'wow'
74
-
75
- expect(dson_string).to match(
76
- /#{regex_string}/
77
- )
78
- end
79
-
80
- end
81
-
82
- describe 'DSON simple array handling' do
83
-
84
- it 'should correctly handle an empty array' do
85
- dson_array = Array.new
86
- dson_string = DSON.such_serialize_wow(dson_array)
87
-
88
- expect(dson_string).to eq(
89
- 'so many'
90
- )
91
- end
92
-
93
- it 'should correctly handle an array with a single element' do
94
- dson_array = %w(cheese)
95
- dson_string = DSON.such_serialize_wow(dson_array)
96
-
97
- expect(dson_string).to eq(
98
- 'so "cheese" many'
99
- )
100
- end
101
-
102
- it 'should correctly handle an array with two elements' do
103
- dson_array = %w(cheese wine)
104
- dson_string = DSON.such_serialize_wow(dson_array)
105
-
106
- expect(dson_string).to match(
107
- /so "cheese" #{AND_MATCH} "wine" many/
108
- )
109
- end
110
-
111
- it 'should correctly handle an array with three elements' do
112
- dson_array = %w(cheese wine bread)
113
- dson_string = DSON.such_serialize_wow(dson_array)
114
-
115
- expect(dson_string).to match(
116
- /so "cheese" #{AND_MATCH} "wine" #{AND_MATCH} "bread" many/
117
- )
118
- end
119
-
120
- end
121
-
122
- describe 'DSON nested arrays' do
123
-
124
- it 'should correctly handle empty nested arrays' do
125
- dson_array = [[]]
126
- dson_string = DSON.such_serialize_wow(dson_array)
127
-
128
- expect(dson_string).to eq(
129
- 'so so many many'
130
- )
131
- end
132
-
133
- it 'should correctly handle nested arrays with elements' do
134
- dson_array = ['cheese', []]
135
- dson_string = DSON.such_serialize_wow(dson_array)
136
-
137
- expect(dson_string).to match(
138
- /so "cheese" #{AND_MATCH} so many many/
139
- )
140
- end
141
-
142
- it 'should correctly handle a nested array with an element' do
143
- dson_array = [['cheese']]
144
- dson_string = DSON.such_serialize_wow(dson_array)
145
-
146
- expect(dson_string).to eq(
147
- 'so so "cheese" many many'
148
- )
149
- end
150
-
151
- it 'should correctly handle a more complex nested array' do
152
- dson_array = ['cheese', ['cheese', ['cheese']]]
153
- dson_string = DSON.such_serialize_wow(dson_array)
154
-
155
- expect(dson_string).to match(
156
- /so "cheese" #{AND_MATCH} so "cheese" #{AND_MATCH} so "cheese" many many many/
157
- )
158
- end
159
-
160
- end
161
-
162
- describe 'DSON nested hashes' do
163
-
164
- it 'should handle a simple nested hash' do
165
- dson_hash = {
166
- nested: {}
167
- }
168
- dson_string = DSON.such_serialize_wow(dson_hash)
169
-
170
- expect(dson_string).to eq(
171
- 'such "nested" is such wow wow'
172
- )
173
- end
174
-
175
- it 'should handle a further nested hash' do
176
- dson_hash = {
177
- nested: {
178
- further_nested: {
179
- }
180
- }
181
- }
182
- dson_string = DSON.such_serialize_wow(dson_hash)
183
-
184
- expect(dson_string).to eq(
185
- 'such "nested" is such "further_nested" is such wow wow wow'
186
- )
187
- end
188
-
189
- it 'should handle other elements in this hash' do
190
- dson_hash = {
191
- other: 'true',
192
- nested: {
193
- }
194
- }
195
- dson_string = DSON.such_serialize_wow(dson_hash)
196
-
197
- other_or_nested = '("other" is "true"|"nested" is such wow)'
198
-
199
- expect(dson_string).to match(
200
- /such #{other_or_nested}#{PUNCTUATION_MATCH} #{other_or_nested} wow/
201
- )
202
- end
203
-
204
- it 'should handle elements in a nested hash' do
205
- dson_hash = {
206
- other: 'true',
207
- nested: {
208
- element: 'great'
209
- }
210
- }
211
- dson_string = DSON.such_serialize_wow(dson_hash)
212
-
213
- other_or_nested = '("other" is "true"|"nested" is such "element" is "great" wow)'
214
-
215
- expect(dson_string).to match(
216
- /such #{other_or_nested}#{PUNCTUATION_MATCH} #{other_or_nested} wow/
217
- )
218
- end
219
-
220
- it 'should handle multiple elements in the nested hash' do
221
- dson_hash = {
222
- wine: {
223
- white: 'great',
224
- red: 'greater'
225
- }
226
- }
227
- dson_string = DSON.such_serialize_wow(dson_hash)
228
-
229
- white_or_red = '("white" is "great"|"red" is "greater")'
230
-
231
- expect(dson_string).to match(
232
- /such "wine" is such #{white_or_red}#{PUNCTUATION_MATCH} #{white_or_red} wow wow/
233
- )
234
- end
235
-
236
- end
237
-
238
- describe 'DSON hash and array mixes' do
239
-
240
- it 'should handle an array of empty objects' do
241
- dson_array = [
242
- {}, {}
243
- ]
244
- dson_string = DSON.such_serialize_wow(dson_array)
245
-
246
- expect(dson_string).to match(
247
- /so such wow #{AND_MATCH} such wow many/
248
- )
249
- end
250
-
251
- it 'should handle an object with an empty array element' do
252
- dson_hash = {
253
- array: []
254
- }
255
- dson_string = DSON.such_serialize_wow(dson_hash)
256
-
257
- expect(dson_string).to eq(
258
- 'such "array" is so many wow'
259
- )
260
- end
261
-
262
- it 'should handle an object with an array element' do
263
- dson_hash = {
264
- array: %w(olive grape)
265
- }
266
- dson_string = DSON.such_serialize_wow(dson_hash)
267
-
268
- expect(dson_string).to match(
269
- /such "array" is so "olive" #{AND_MATCH} "grape" many wow/
270
- )
271
- end
272
-
273
- it 'should handle an array with an object element' do
274
- dson_array = [
275
- {
276
- first_name: 'Cyril',
277
- surname: 'Figgis'
278
- }
279
- ]
280
- dson_string = DSON.such_serialize_wow(dson_array)
281
-
282
- first_or_last_name = '("first_name" is "Cyril"|"surname" is "Figgis")'
283
-
284
- expect(dson_string).to match(
285
- /so such #{first_or_last_name}#{PUNCTUATION_MATCH} #{first_or_last_name} wow many/
286
- )
287
- end
288
-
289
- end
290
-
291
- describe 'DSON empty' do
292
-
293
- it 'translates a nil object to empty' do
294
- dson_hash = {
295
- value: nil
296
- }
297
- dson_string = DSON.such_serialize_wow(dson_hash)
298
-
299
- expect(dson_string).to eq(
300
- 'such "value" is empty wow'
301
- )
302
- end
303
-
304
- end
305
-
306
- describe 'DSON booleans' do
307
-
308
- it 'translates a true object to yes' do
309
- dson_hash = {
310
- value: true
311
- }
312
- dson_string = DSON.such_serialize_wow(dson_hash)
313
-
314
- expect(dson_string).to eq(
315
- 'such "value" is yes wow'
316
- )
317
- end
318
-
319
- it 'translates a false object to no' do
320
- dson_hash = {
321
- value: false
322
- }
323
- dson_string = DSON.such_serialize_wow(dson_hash)
324
-
325
- expect(dson_string).to eq(
326
- 'such "value" is no wow'
327
- )
328
- end
329
-
330
- end
331
-
332
- describe 'ruby objects' do
333
-
334
- it 'translates a trivial class instance to DSON' do
335
- ruby_object = DSONSpec::TrivialClass.new
336
- dson_string = DSON.such_serialize_wow(ruby_object)
337
-
338
- expect(dson_string).to eq('such wow')
339
- end
340
-
341
- it 'translates an example class instance to DSON' do
342
- ruby_object = DSONSpec::ExampleClass.new('awesome', 'superb')
343
- dson_string = DSON.such_serialize_wow(ruby_object)
344
-
345
- name_or_value = '("name" is "awesome"|"value" is "superb")'
346
-
347
- expect(dson_string).to match(
348
- /such #{name_or_value}#{PUNCTUATION_MATCH} #{name_or_value} wow/
349
- )
350
- end
351
-
352
- end
353
-
354
- # TODO: fix when we can do octals generically
355
- # describe "DSON numbers" do
356
-
357
- # it "doesn't quote numeric values" do
358
- # dson_hash = {
359
- # value: 13
360
- # }
361
- # dson_string = DSON.such_serialize_wow(dson_hash)
362
-
363
- # expect(dson_string).to eq(
364
- # 'such "value" is 13 wow'
365
- # )
366
- # end
367
-
368
- # it "quotes numeric values represented as strings" do
369
- # dson_hash = {
370
- # value: "13"
371
- # }
372
- # dson_string = DSON.such_serialize_wow(dson_hash)
373
-
374
- # expect(dson_string).to eq(
375
- # 'such "value" is "13" wow'
376
- # )
377
- # end
378
-
379
- # it "quotes float values" do
380
- # dson_hash = {
381
- # value: 12.5
382
- # }
383
- # dson_string = DSON.such_serialize_wow(dson_hash)
384
-
385
- # expect(dson_string).to eq(
386
- # 'such "value" is 12.5 wow'
387
- # )
388
- # end
389
-
390
- # end
1
+ # -*- encoding : utf-8 -*-
2
+ require 'DSON'
3
+
4
+ require_relative 'lib/example_class'
5
+
6
+ PUNCTUATION_MATCH = '(,|\.|!|\\?)'
7
+ AND_MATCH = '(and|also)'
8
+
9
+ describe 'simple hashes' do
10
+
11
+ it 'should be correct with an empty hash' do
12
+ dson_hash = Hash.new
13
+ dson_string = DSON.such_serialize_wow(dson_hash)
14
+
15
+ expect(dson_string).to eq(
16
+ 'such wow'
17
+ )
18
+ end
19
+
20
+ it 'should be correct with a hash with one element' do
21
+ dson_hash = {
22
+ dson: 'awesome'
23
+ }
24
+ dson_string = DSON.such_serialize_wow(dson_hash)
25
+
26
+ expect(dson_string).to eq(
27
+ 'such "dson" is "awesome" wow'
28
+ )
29
+ end
30
+
31
+ it 'should be correct with a hash with two elements' do
32
+ dson_or_ruby = '("dson" is "awesome"|"ruby" is "great")'
33
+
34
+ dson_hash = {
35
+ dson: 'awesome',
36
+ ruby: 'great'
37
+ }
38
+ dson_string = DSON.such_serialize_wow(dson_hash)
39
+
40
+ expect(dson_string).to match(
41
+ /such #{dson_or_ruby}#{PUNCTUATION_MATCH} #{dson_or_ruby} wow/
42
+ )
43
+ end
44
+
45
+ it 'should be correct with a hash with three elements' do
46
+ dson_ruby_or_dogescript =
47
+ '(' \
48
+ '"dson" is "awesome"' \
49
+ '|' \
50
+ '"ruby" is "great"' \
51
+ '|' \
52
+ '"dogescript" is "fine"' \
53
+ ')'
54
+
55
+ dson_hash = {
56
+ dson: 'awesome',
57
+ ruby: 'great',
58
+ dogescript: 'fine'
59
+ }
60
+ dson_string = DSON.such_serialize_wow(dson_hash)
61
+
62
+ regex_string =
63
+ 'such' \
64
+ ' ' +
65
+ dson_ruby_or_dogescript +
66
+ PUNCTUATION_MATCH +
67
+ ' ' +
68
+ dson_ruby_or_dogescript +
69
+ PUNCTUATION_MATCH +
70
+ ' ' +
71
+ dson_ruby_or_dogescript +
72
+ ' ' +
73
+ 'wow'
74
+
75
+ expect(dson_string).to match(
76
+ /#{regex_string}/
77
+ )
78
+ end
79
+
80
+ end
81
+
82
+ describe 'DSON simple array handling' do
83
+
84
+ it 'should correctly handle an empty array' do
85
+ dson_array = Array.new
86
+ dson_string = DSON.such_serialize_wow(dson_array)
87
+
88
+ expect(dson_string).to eq(
89
+ 'so many'
90
+ )
91
+ end
92
+
93
+ it 'should correctly handle an array with a single element' do
94
+ dson_array = %w(cheese)
95
+ dson_string = DSON.such_serialize_wow(dson_array)
96
+
97
+ expect(dson_string).to eq(
98
+ 'so "cheese" many'
99
+ )
100
+ end
101
+
102
+ it 'should correctly handle an array with two elements' do
103
+ dson_array = %w(cheese wine)
104
+ dson_string = DSON.such_serialize_wow(dson_array)
105
+
106
+ expect(dson_string).to match(
107
+ /so "cheese" #{AND_MATCH} "wine" many/
108
+ )
109
+ end
110
+
111
+ it 'should correctly handle an array with three elements' do
112
+ dson_array = %w(cheese wine bread)
113
+ dson_string = DSON.such_serialize_wow(dson_array)
114
+
115
+ expect(dson_string).to match(
116
+ /so "cheese" #{AND_MATCH} "wine" #{AND_MATCH} "bread" many/
117
+ )
118
+ end
119
+
120
+ end
121
+
122
+ describe 'DSON nested arrays' do
123
+
124
+ it 'should correctly handle empty nested arrays' do
125
+ dson_array = [[]]
126
+ dson_string = DSON.such_serialize_wow(dson_array)
127
+
128
+ expect(dson_string).to eq(
129
+ 'so so many many'
130
+ )
131
+ end
132
+
133
+ it 'should correctly handle nested arrays with elements' do
134
+ dson_array = ['cheese', []]
135
+ dson_string = DSON.such_serialize_wow(dson_array)
136
+
137
+ expect(dson_string).to match(
138
+ /so "cheese" #{AND_MATCH} so many many/
139
+ )
140
+ end
141
+
142
+ it 'should correctly handle a nested array with an element' do
143
+ dson_array = [['cheese']]
144
+ dson_string = DSON.such_serialize_wow(dson_array)
145
+
146
+ expect(dson_string).to eq(
147
+ 'so so "cheese" many many'
148
+ )
149
+ end
150
+
151
+ it 'should correctly handle a more complex nested array' do
152
+ dson_array = ['cheese', ['cheese', ['cheese']]]
153
+ dson_string = DSON.such_serialize_wow(dson_array)
154
+
155
+ expect(dson_string).to match(
156
+ /so "cheese" #{AND_MATCH} so "cheese" #{AND_MATCH} so "cheese" many many many/
157
+ )
158
+ end
159
+
160
+ end
161
+
162
+ describe 'DSON nested hashes' do
163
+
164
+ it 'should handle a simple nested hash' do
165
+ dson_hash = {
166
+ nested: {}
167
+ }
168
+ dson_string = DSON.such_serialize_wow(dson_hash)
169
+
170
+ expect(dson_string).to eq(
171
+ 'such "nested" is such wow wow'
172
+ )
173
+ end
174
+
175
+ it 'should handle a further nested hash' do
176
+ dson_hash = {
177
+ nested: {
178
+ further_nested: {
179
+ }
180
+ }
181
+ }
182
+ dson_string = DSON.such_serialize_wow(dson_hash)
183
+
184
+ expect(dson_string).to eq(
185
+ 'such "nested" is such "further_nested" is such wow wow wow'
186
+ )
187
+ end
188
+
189
+ it 'should handle other elements in this hash' do
190
+ dson_hash = {
191
+ other: 'true',
192
+ nested: {
193
+ }
194
+ }
195
+ dson_string = DSON.such_serialize_wow(dson_hash)
196
+
197
+ other_or_nested = '("other" is "true"|"nested" is such wow)'
198
+
199
+ expect(dson_string).to match(
200
+ /such #{other_or_nested}#{PUNCTUATION_MATCH} #{other_or_nested} wow/
201
+ )
202
+ end
203
+
204
+ it 'should handle elements in a nested hash' do
205
+ dson_hash = {
206
+ other: 'true',
207
+ nested: {
208
+ element: 'great'
209
+ }
210
+ }
211
+ dson_string = DSON.such_serialize_wow(dson_hash)
212
+
213
+ other_or_nested = '("other" is "true"|"nested" is such "element" is "great" wow)'
214
+
215
+ expect(dson_string).to match(
216
+ /such #{other_or_nested}#{PUNCTUATION_MATCH} #{other_or_nested} wow/
217
+ )
218
+ end
219
+
220
+ it 'should handle multiple elements in the nested hash' do
221
+ dson_hash = {
222
+ wine: {
223
+ white: 'great',
224
+ red: 'greater'
225
+ }
226
+ }
227
+ dson_string = DSON.such_serialize_wow(dson_hash)
228
+
229
+ white_or_red = '("white" is "great"|"red" is "greater")'
230
+
231
+ expect(dson_string).to match(
232
+ /such "wine" is such #{white_or_red}#{PUNCTUATION_MATCH} #{white_or_red} wow wow/
233
+ )
234
+ end
235
+
236
+ end
237
+
238
+ describe 'DSON hash and array mixes' do
239
+
240
+ it 'should handle an array of empty objects' do
241
+ dson_array = [
242
+ {}, {}
243
+ ]
244
+ dson_string = DSON.such_serialize_wow(dson_array)
245
+
246
+ expect(dson_string).to match(
247
+ /so such wow #{AND_MATCH} such wow many/
248
+ )
249
+ end
250
+
251
+ it 'should handle an object with an empty array element' do
252
+ dson_hash = {
253
+ array: []
254
+ }
255
+ dson_string = DSON.such_serialize_wow(dson_hash)
256
+
257
+ expect(dson_string).to eq(
258
+ 'such "array" is so many wow'
259
+ )
260
+ end
261
+
262
+ it 'should handle an object with an array element' do
263
+ dson_hash = {
264
+ array: %w(olive grape)
265
+ }
266
+ dson_string = DSON.such_serialize_wow(dson_hash)
267
+
268
+ expect(dson_string).to match(
269
+ /such "array" is so "olive" #{AND_MATCH} "grape" many wow/
270
+ )
271
+ end
272
+
273
+ it 'should handle an array with an object element' do
274
+ dson_array = [
275
+ {
276
+ first_name: 'Cyril',
277
+ surname: 'Figgis'
278
+ }
279
+ ]
280
+ dson_string = DSON.such_serialize_wow(dson_array)
281
+
282
+ first_or_last_name = '("first_name" is "Cyril"|"surname" is "Figgis")'
283
+
284
+ expect(dson_string).to match(
285
+ /so such #{first_or_last_name}#{PUNCTUATION_MATCH} #{first_or_last_name} wow many/
286
+ )
287
+ end
288
+
289
+ end
290
+
291
+ describe 'DSON empty' do
292
+
293
+ it 'translates a nil object to empty' do
294
+ dson_hash = {
295
+ value: nil
296
+ }
297
+ dson_string = DSON.such_serialize_wow(dson_hash)
298
+
299
+ expect(dson_string).to eq(
300
+ 'such "value" is empty wow'
301
+ )
302
+ end
303
+
304
+ end
305
+
306
+ describe 'DSON booleans' do
307
+
308
+ it 'translates a true object to yes' do
309
+ dson_hash = {
310
+ value: true
311
+ }
312
+ dson_string = DSON.such_serialize_wow(dson_hash)
313
+
314
+ expect(dson_string).to eq(
315
+ 'such "value" is yes wow'
316
+ )
317
+ end
318
+
319
+ it 'translates a false object to no' do
320
+ dson_hash = {
321
+ value: false
322
+ }
323
+ dson_string = DSON.such_serialize_wow(dson_hash)
324
+
325
+ expect(dson_string).to eq(
326
+ 'such "value" is no wow'
327
+ )
328
+ end
329
+
330
+ end
331
+
332
+ describe 'ruby objects' do
333
+
334
+ it 'translates a trivial class instance to DSON' do
335
+ ruby_object = DSONSpec::TrivialClass.new
336
+ dson_string = DSON.such_serialize_wow(ruby_object)
337
+
338
+ expect(dson_string).to eq('such wow')
339
+ end
340
+
341
+ it 'translates an example class instance to DSON' do
342
+ ruby_object = DSONSpec::ExampleClass.new('awesome', 'superb')
343
+ dson_string = DSON.such_serialize_wow(ruby_object)
344
+
345
+ name_or_value = '("name" is "awesome"|"value" is "superb")'
346
+
347
+ expect(dson_string).to match(
348
+ /such #{name_or_value}#{PUNCTUATION_MATCH} #{name_or_value} wow/
349
+ )
350
+ end
351
+
352
+ end
353
+
354
+ describe "DSON numbers" do
355
+
356
+ it 'converts integers to octal' do
357
+ dson_hash = {
358
+ value: 13
359
+ }
360
+ dson_string = DSON.such_serialize_wow(dson_hash)
361
+
362
+ expect(dson_string).to eq(
363
+ 'such "value" is 15 wow'
364
+ )
365
+ end
366
+
367
+ it 'converts floats to octal' do
368
+ dson_hash = {
369
+ value: 18.00001
370
+ }
371
+ dson_string = DSON.such_serialize_wow(dson_hash)
372
+
373
+ expect(dson_string).to eq(
374
+ 'such "value" is 22.8 wow'
375
+ )
376
+ end
377
+
378
+ end