microfiche 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,232 @@
1
+ # _____ _
2
+ # |_ _|__ ___| |_
3
+ # | |/ _ \/ __| __|
4
+ # | | __/\__ \ |_
5
+ # |_|\___||___/\__|
6
+ #
7
+ # for lib/facets/json.rb
8
+ #
9
+ # Extracted Fri Mar 16 13:16:34 EDT 2007
10
+ # Project.rb Test Extraction
11
+ #
12
+
13
+ require 'facets/json.rb'
14
+
15
+
16
+ require 'test/unit'
17
+
18
+ class TC_JSON < Test::Unit::TestCase
19
+
20
+ include JSON
21
+
22
+ class A
23
+ def initialize(a)
24
+ @a = a
25
+ end
26
+
27
+ attr_reader :a
28
+
29
+ def ==(other)
30
+ a == other.a
31
+ end
32
+
33
+ def self.json_create(object)
34
+ new(*object['args'])
35
+ end
36
+
37
+ def to_json(*args)
38
+ {
39
+ 'json_class' => self.class,
40
+ 'args' => [ @a ],
41
+ }.to_json(*args)
42
+ end
43
+ end
44
+
45
+ def setup
46
+ $KCODE = 'UTF8'
47
+ @ary = [1, "foo", 3.14, 4711.0, 2.718, nil, [1,-2,3], false, true]
48
+ @ary_to_parse = ["1", '"foo"', "3.14", "4711.0", "2.718", "null",
49
+ "[1,-2,3]", "false", "true"]
50
+ @hash = {
51
+ 'a' => 2,
52
+ 'b' => 3.141,
53
+ 'c' => 'c',
54
+ 'd' => [ 1, "b", 3.14 ],
55
+ 'e' => { 'foo' => 'bar' },
56
+ 'g' => "\"\0\037",
57
+ 'h' => 1000.0,
58
+ 'i' => 0.001
59
+ }
60
+ @json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' +
61
+ '"g":"\\"\\u0000\\u001f","h":1.0E3,"i":1.0E-3}'
62
+ @json2 = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' +
63
+ '"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
64
+ end
65
+
66
+ def test_parse_value
67
+ assert_equal("", parse('""'))
68
+ assert_equal("\\", parse('"\\\\"'))
69
+ assert_equal('"', parse('"\""'))
70
+ assert_equal('\\"\\', parse('"\\\\\\"\\\\"'))
71
+ assert_equal("\\a\"\b\f\n\r\t\0\037",
72
+ parse('"\\a\"\b\f\n\r\t\u0000\u001f"'))
73
+ for i in 0 ... @ary.size
74
+ assert_equal(@ary[i], parse(@ary_to_parse[i]))
75
+ end
76
+ end
77
+
78
+ def test_parse_array
79
+ assert_equal([], parse('[]'))
80
+ assert_equal([], parse(' [ ] '))
81
+ assert_equal([1], parse('[1]'))
82
+ assert_equal([1], parse(' [ 1 ] '))
83
+ assert_equal(@ary,
84
+ parse('[1,"foo",3.14,47.11e+2,2718.E-3,null,[1,-2,3],false,true]'))
85
+ assert_equal(@ary, parse(%Q{ [ 1 , "foo" , 3.14 \t , 47.11e+2
86
+ , 2718.E-3 ,\n null , [1, -2, 3 ], false , true\n ] }))
87
+ end
88
+
89
+ def test_parse_object
90
+ assert_equal({}, parse('{}'))
91
+ assert_equal({}, parse(' { } '))
92
+ assert_equal({'foo'=>'bar'}, parse('{"foo":"bar"}'))
93
+ assert_equal({'foo'=>'bar'}, parse(' { "foo" : "bar" } '))
94
+ end
95
+
96
+ def test_unparse
97
+ json = unparse(@hash)
98
+ assert_equal(@json2, json)
99
+ parsed_json = parse(json)
100
+ assert_equal(@hash, parsed_json)
101
+ json = unparse({1=>2})
102
+ assert_equal('{"1":2}', json)
103
+ parsed_json = parse(json)
104
+ assert_equal({"1"=>2}, parsed_json)
105
+ end
106
+
107
+ def test_parser_reset
108
+ parser = Parser.new(@json)
109
+ assert_equal(@hash, parser.parse)
110
+ assert_equal(@hash, parser.parse)
111
+ end
112
+
113
+ def test_unicode
114
+ assert_equal '""', ''.to_json
115
+ assert_equal '"\\b"', "\b".to_json
116
+ assert_equal '"\u0001"', 0x1.chr.to_json
117
+ assert_equal '"\u001f"', 0x1f.chr.to_json
118
+ assert_equal '" "', ' '.to_json
119
+ assert_equal "\"#{0x7f.chr}\"", 0x7f.chr.to_json
120
+ utf8 = '© ≠ €!'
121
+ json = '"\u00a9 \u2260 \u20ac!"'
122
+ assert_equal json, utf8.to_json
123
+ assert_equal utf8, parse(json)
124
+ utf8 = "\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"
125
+ json = '"\u3042\u3044\u3046\u3048\u304a"'
126
+ assert_equal json, utf8.to_json
127
+ assert_equal utf8, parse(json)
128
+ utf8 = 'საქართველო'
129
+ json = '"\u10e1\u10d0\u10e5\u10d0\u10e0\u10d7\u10d5\u10d4\u10da\u10dd"'
130
+ assert_equal json, utf8.to_json
131
+ assert_equal utf8, parse(json)
132
+ end
133
+
134
+ def test_comments
135
+ json = <<EOT
136
+ {
137
+ "key1":"value1", // eol comment
138
+ "key2":"value2" /* multi line
139
+ * comment */,
140
+ "key3":"value3" /* multi line
141
+ // nested eol comment
142
+ * comment */
143
+ }
144
+ EOT
145
+ assert_equal(
146
+ { "key1" => "value1", "key2" => "value2", "key3" => "value3" },
147
+ parse(json))
148
+ json = <<EOT
149
+ {
150
+ "key1":"value1" /* multi line
151
+ // nested eol comment
152
+ /* illegal nested multi line comment */
153
+ * comment */
154
+ }
155
+ EOT
156
+ assert_raises(ParserError) { parse(json) }
157
+ json = <<EOT
158
+ {
159
+ "key1":"value1" /* multi line
160
+ // nested eol comment
161
+ closed multi comment */
162
+ and again, throw an Error */
163
+ }
164
+ EOT
165
+ assert_raises(ParserError) { parse(json) }
166
+ json = <<EOT
167
+ {
168
+ "key1":"value1" /*/*/
169
+ }
170
+ EOT
171
+ assert_equal({ "key1" => "value1" }, parse(json))
172
+ end
173
+
174
+ def test_extended_json
175
+ a = A.new(666)
176
+ json = a.to_json
177
+ a_again = JSON.parse(json)
178
+ assert_kind_of a.class, a_again
179
+ assert_equal a, a_again
180
+ end
181
+
182
+ def test_raw_strings
183
+ raw = ''
184
+ raw_array = []
185
+ for i in 0..255
186
+ raw << i
187
+ raw_array << i
188
+ end
189
+ json = raw.to_json_raw
190
+ json_raw_object = raw.to_json_raw_object
191
+ hash = { 'json_class' => 'String', 'raw'=> raw_array }
192
+ assert_equal hash, json_raw_object
193
+ json_raw = <<EOT.chomp
194
+ {\"json_class\":\"String\",\"raw\":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255]}
195
+ EOT
196
+ # "
197
+ assert_equal json_raw, json
198
+ raw_again = JSON.parse(json)
199
+ assert_equal raw, raw_again
200
+ end
201
+
202
+ def test_utf8_mode
203
+ $KCODE = 'NONE'
204
+ utf8 = "© ≠ €! - \001"
205
+ json = "\"© ≠ €! - \\u0001\""
206
+ assert_equal json, utf8.to_json
207
+ assert_equal utf8, parse(json)
208
+ assert JSON.support_unicode?
209
+ $KCODE = 'UTF8'
210
+ utf8 = '© ≠ €!'
211
+ json = '"\u00a9 \u2260 \u20ac!"'
212
+ assert_equal json, utf8.to_json
213
+ assert_equal utf8, parse(json)
214
+ JSON.support_unicode = false
215
+ assert !JSON.support_unicode?
216
+ utf8 = "© ≠ €! - \001"
217
+ json = "\"© ≠ €! - \\u0001\""
218
+ assert_equal json, utf8.to_json
219
+ assert_equal utf8, parse(json)
220
+ end
221
+
222
+ def test_backslash
223
+ json = '"\\\\.(?i:gif|jpe?g|png)$"'
224
+ data = JSON.parse(json)
225
+ assert_equal json, JSON.unparse(data)
226
+ json = '"\\""'
227
+ data = JSON.parse(json)
228
+ assert_equal json, JSON.unparse(data)
229
+ end
230
+ end
231
+
232
+
@@ -0,0 +1,288 @@
1
+ # _____ _
2
+ # |_ _|__ ___| |_
3
+ # | |/ _ \/ __| __|
4
+ # | | __/\__ \ |_
5
+ # |_|\___||___/\__|
6
+ #
7
+ # for lib/facets/xoxo.rb
8
+ #
9
+ # Extracted Fri Mar 16 13:16:34 EDT 2007
10
+ # Project.rb Test Extraction
11
+ #
12
+
13
+ require 'facets/xoxo.rb'
14
+
15
+
16
+ require 'test/unit'
17
+
18
+ class TCXOXO < Test::Unit::TestCase
19
+
20
+ def test_simple_list
21
+ l = ['1', '2', '3']
22
+ html = XOXO.dump(l)
23
+ assert_equal '<ol class="xoxo"><li>1</li><li>2</li><li>3</li></ol>', html
24
+ end
25
+
26
+ def test_nested_list
27
+ l = ['1', ['2', '3']]
28
+ assert_equal '<ol class="xoxo"><li>1</li><li><ol><li>2</li><li>3</li></ol></li></ol>', XOXO.dump(l)
29
+ end
30
+
31
+ def test_hash
32
+ h = {'test' => '1', 'name' => 'Kevin'}
33
+ # Changed since Ruby sorts the hash differently.
34
+ assert_equal '<ol class="xoxo"><li><dl><dt>name</dt><dd>Kevin</dd><dt>test</dt><dd>1</dd></dl></li></ol>', XOXO.dump(h)
35
+ end
36
+
37
+ def test_single_item
38
+ l = 'test'
39
+ assert_equal '<ol class="xoxo"><li>test</li></ol>', XOXO.dump(l)
40
+ end
41
+
42
+ def test_wrap_differs
43
+ l = 'test'
44
+ html = XOXO.dump(l)
45
+ html_wrap = XOXO.dump(l, :html_wrap => true)
46
+ assert_not_equal html, html_wrap
47
+ end
48
+
49
+ def test_wrap_single_item
50
+ l = 'test'
51
+ html = XOXO.dump(l, :html_wrap => true)
52
+ assert_equal <<EOF.strip, html
53
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN
54
+ http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
55
+ <html xmlns="http://www.w3.org/1999/xhtml"><head profile=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><ol class="xoxo"><li>test</li></ol></body></html>
56
+ EOF
57
+ end
58
+
59
+ def test_wrap_item_with_css
60
+ l = 'test'
61
+ html = XOXO.dump(l, :html_wrap => true, :css => 'reaptest.css')
62
+ assert_equal <<EOF.strip, html
63
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN
64
+ http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
65
+ <html xmlns="http://www.w3.org/1999/xhtml"><head profile=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><style type="text/css" >@import "reaptest.css";</style></head><body><ol class="xoxo"><li>test</li></ol></body></html>
66
+ EOF
67
+ end
68
+
69
+ def test_hash_roundtrip
70
+ h = {'test' => '1', 'name' => 'Kevin'}
71
+ assert_equal h, XOXO.load(XOXO.dump(h))
72
+ end
73
+
74
+ def test_hash_with_url_roundtrip
75
+ h = {'url' => 'http://example.com', 'name' => 'Kevin'}
76
+ assert_equal h, XOXO.load(XOXO.dump(h))
77
+ end
78
+
79
+ def test_nested_hash_roundtrip
80
+ h = {'test' => '1', 'inner' => {'name' => 'Kevin'}}
81
+ assert_equal h, XOXO.load(XOXO.dump(h))
82
+ end
83
+
84
+ def test_nested_hash_with_url_roundtrip
85
+ h = {'url' => 'http://example.com', 'inner' => {
86
+ 'url' => 'http://slashdot.org', 'name' => 'Kevin'}}
87
+ assert_equal h, XOXO.load(XOXO.dump(h))
88
+ end
89
+
90
+ def test_list_round_trip
91
+ l = ['3', '2', '1']
92
+ assert_equal l, XOXO.load(XOXO.dump(l))
93
+ end
94
+
95
+ def test_list_of_hashes_round_trip
96
+ l = ['3', {'a' => '2'}, {'b' => '1', 'c' => '4'}]
97
+ assert_equal l, XOXO.load(XOXO.dump(l))
98
+ end
99
+
100
+ def test_list_of_lists_round_trip
101
+ l = ['3', ['a', '2'], ['b', ['1', ['c', '4']]]]
102
+ assert_equal l, XOXO.load(XOXO.dump(l))
103
+ end
104
+
105
+ def test_hashes_of_lists_roundtrip
106
+ h = {
107
+ 'test' => ['1', '2'],
108
+ 'name' => 'Kevin',
109
+ 'nestlist' => ['a', ['b', 'c']],
110
+ 'nestdict' => {'e' => '6', 'f' => '7'}
111
+ }
112
+ assert_equal h, XOXO.load(XOXO.dump(h))
113
+ end
114
+
115
+ def test_xoxo_junk_in_containers
116
+ h = XOXO.load '<ol>bad<li><dl>worse<dt>good</dt><dd>buy</dd> now</dl></li></ol>'
117
+ assert_equal({'good' => 'buy'}, h)
118
+ end
119
+
120
+ def test_xoxo_junk_in_elements
121
+ l = XOXO.load '<ol><li>bad<dl><dt>good</dt><dd>buy</dd></dl>worse</li><li>bag<ol><li>OK</li></ol>fish</li></ol>'
122
+ assert_equal([{'good' => 'buy'}, ['OK']], l)
123
+ end
124
+
125
+ def test_xoxo_with_spaces_and_newlines
126
+ xoxo_sample = <<EOF.strip
127
+ <ol class='xoxo'>
128
+ <li>
129
+ <dl>
130
+ <dt>text</dt>
131
+ <dd>item 1</dd>
132
+ <dt>description</dt>
133
+ <dd> This item represents the main point we're trying to make.</dd>
134
+ <dt>url</dt>
135
+ <dd>http://example.com/more.xoxo</dd>
136
+ <dt>title</dt>
137
+ <dd>title of item 1</dd>
138
+ <dt>type</dt>
139
+ <dd>text/xml</dd>
140
+ <dt>rel</dt>
141
+ <dd>help</dd>
142
+ </dl>
143
+ </li>
144
+ </ol>
145
+ EOF
146
+ h = XOXO.load xoxo_sample
147
+ h2 = {
148
+ 'text' => 'item 1',
149
+ 'description' => " This item represents the main point we're trying to make.",
150
+ 'url' => 'http://example.com/more.xoxo',
151
+ 'title' => 'title of item 1',
152
+ 'type' => 'text/xml',
153
+ 'rel' => 'help'
154
+ }
155
+ assert_equal h2, XOXO.load(xoxo_sample)
156
+ end
157
+
158
+ def test_special_attribute_decoding
159
+ xoxo_sample = <<EOF.strip
160
+ <ol class='xoxo'>
161
+ <li>
162
+ <dl>
163
+ <dt>text</dt>
164
+ <dd>item 1</dd>
165
+ <dt>url</dt>
166
+ <dd>http://example.com/more.xoxo</dd>
167
+ <dt>title</dt>
168
+ <dd>title of item 1</dd>
169
+ <dt>type</dt>
170
+ <dd>text/xml</dd>
171
+ <dt>rel</dt>
172
+ <dd>help</dd>
173
+ </dl>
174
+ </li>
175
+ </ol>
176
+ EOF
177
+ smart_xoxo_sample = <<EOF.strip
178
+ <ol class='xoxo'>
179
+ <li><a href="http://example.com/more.xoxo"
180
+ title="title of item 1"
181
+ type="text/xml"
182
+ rel="help">item 1</a>
183
+ <!-- note how the "text" property is simply the contents of the <a> element -->
184
+ </li>
185
+ </ol>
186
+ EOF
187
+ assert_equal XOXO.load(xoxo_sample), XOXO.load(smart_xoxo_sample)
188
+ end
189
+
190
+ def test_special_attribute_and_dl_decoding
191
+ xoxo_sample = <<EOF.strip
192
+ <ol class="xoxo">
193
+ <li>
194
+ <dl>
195
+ <dt>text</dt>
196
+ <dd>item 1</dd>
197
+ <dt>description</dt>
198
+ <dd> This item represents the main point we're trying to make.</dd>
199
+ <dt>url</dt>
200
+ <dd>http://example.com/more.xoxo</dd>
201
+ <dt>title</dt>
202
+ <dd>title of item 1</dd>
203
+ <dt>type</dt>
204
+ <dd>text/xml</dd>
205
+ <dt>rel</dt>
206
+ <dd>help</dd>
207
+ </dl>
208
+ </li>
209
+ </ol>
210
+ EOF
211
+ smart_xoxo_sample = <<EOF.strip
212
+ <ol class="xoxo">
213
+ <li><a href="http://example.com/more.xoxo"
214
+ title="title of item 1"
215
+ type="text/xml"
216
+ rel="help">item 1</a>
217
+ <!-- note how the "text" property is simply the contents of the <a> element -->
218
+ <dl>
219
+ <dt>description</dt>
220
+ <dd> This item represents the main point we're trying to make.</dd>
221
+ </dl>
222
+ </li>
223
+ </ol>
224
+ EOF
225
+ assert_equal XOXO.load(xoxo_sample), XOXO.load(smart_xoxo_sample)
226
+ end
227
+
228
+ def test_special_attribute_encode
229
+ h = {
230
+ 'url' => 'http://example.com/more.xoxo',
231
+ 'title' => 'sample url',
232
+ 'type' => "text/xml",
233
+ 'rel' => 'help',
234
+ 'text' => 'an example'
235
+ }
236
+ assert_equal '<ol class="xoxo"><li><a href="http://example.com/more.xoxo" title="sample url" rel="help" type="text/xml" >an example</a></li></ol>', XOXO.dump(h)
237
+ end
238
+
239
+ def test_special_attribute_roundtrip_full
240
+ h = {
241
+ 'url' => 'http://example.com/more.xoxo',
242
+ 'title' => 'sample url',
243
+ 'type' => "text/xml",
244
+ 'rel' => 'help',
245
+ 'text' => 'an example'
246
+ }
247
+ assert_equal h, XOXO.load(XOXO.dump(h))
248
+ end
249
+
250
+ def test_special_attribute_roundtrip_no_text
251
+ h = {
252
+ 'url' => 'http://example.com/more.xoxo',
253
+ 'title' => 'sample url',
254
+ 'type' => "text/xml",
255
+ 'rel' => 'help'
256
+ }
257
+ assert_equal h, XOXO.load(XOXO.dump(h))
258
+ end
259
+
260
+ def test_special_attribute_roundtrip_no_text_or_title
261
+ h = {'url' => 'http://example.com/more.xoxo'}
262
+ assert_equal h, XOXO.load(XOXO.dump(h))
263
+ end
264
+
265
+ def test_attention_roundtrip
266
+ kmattn = <<EOF.strip
267
+ <ol class="xoxo"><li><a href="http://www.boingboing.net/" title="Boing Boing Blog" >Boing Boing Blog</a><dl><dt>alturls</dt><dd><ol><li><a href="http://boingboing.net/rss.xml" >xmlurl</a></li></ol></dd><dt>description</dt><dd>Boing Boing Blog</dd></dl></li><li><a href="http://www.financialcryptography.com/" title="Financial Cryptography" >Financial Cryptography</a><dl><dt>alturls</dt><dd><ol><li><a href="http://www.financialcryptography.com/mt/index.rdf" >xmlurl</a></li></ol></dd><dt>description</dt><dd>Financial Cryptography</dd></dl></li><li><a href="http://hublog.hubmed.org/" title="HubLog" >HubLog</a><dl><dt>alturls</dt><dd><ol><li><a href="http://hublog.hubmed.org/index.xml" >xmlurl</a></li><li><a href="http://hublog.hubmed.org/foaf.rdf" >foafurl</a></li></ol></dd><dt>description</dt><dd>HubLog</dd></dl></li></ol>
268
+ EOF
269
+ assert_equal kmattn, XOXO.dump(XOXO.load(kmattn))
270
+ assert_equal XOXO.load(kmattn), XOXO.load(XOXO.dump(XOXO.load(kmattn)))
271
+ assert_equal XOXO.dump(XOXO.load(kmattn)),
272
+ XOXO.dump(XOXO.load(XOXO.dump(XOXO.load(kmattn))))
273
+ end
274
+
275
+ def test_unicode_roundtrip
276
+ unicode = "Tantek \xc3\x87elik and a snowman \xe2\x98\x83"
277
+ assert_equal unicode, XOXO.load(XOXO.dump(unicode))
278
+ end
279
+
280
+ # TBD: Implement proper encodings.
281
+ #
282
+ # def test_utf8_roundtrip
283
+ # end
284
+ # def test_windows1252_roundtrip
285
+ # end
286
+ end
287
+
288
+