kronk 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_diff.rb CHANGED
@@ -44,62 +44,62 @@ class TestDiff < Test::Unit::TestCase
44
44
  end
45
45
 
46
46
 
47
- def test_ordered_data_string
47
+ def test_ordered_data_string_json
48
48
  expected = <<STR
49
49
  {
50
- "acks" => [
51
- [
52
- 56,
53
- 78
50
+ "acks": [
51
+ [
52
+ 56,
53
+ 78
54
54
  ],
55
- [
56
- "12",
57
- "34"
55
+ [
56
+ "12",
57
+ "34"
58
58
  ]
59
59
  ],
60
- "root" => [
61
- [
62
- "B1",
63
- "B2"
60
+ "root": [
61
+ [
62
+ "B1",
63
+ "B2"
64
64
  ],
65
- [
66
- "A1",
67
- "A2"
65
+ [
66
+ "A1",
67
+ "A2"
68
68
  ],
69
- [
70
- "C1",
71
- "C2",
72
69
  [
73
- "C3a",
74
- "C3b"
70
+ "C1",
71
+ "C2",
72
+ [
73
+ "C3a",
74
+ "C3b"
75
75
  ]
76
76
  ],
77
- {
78
- :tests => [
79
- "D3a",
80
- "D3b"
77
+ {
78
+ ":tests": [
79
+ "D3a",
80
+ "D3b"
81
81
  ],
82
- "test" => [
83
- [
84
- "D1a\\nContent goes here",
85
- "D1b"
82
+ "test": [
83
+ [
84
+ "D1a\\nContent goes here",
85
+ "D1b"
86
86
  ],
87
- "D2"
87
+ "D2"
88
88
  ]
89
89
  }
90
90
  ],
91
- "subs" => [
92
- "a",
93
- "b"
91
+ "subs": [
92
+ "a",
93
+ "b"
94
94
  ],
95
- "tests" => {
96
- :foo => :bar,
97
- "test" => [
98
- [
99
- 1,
100
- 2
95
+ "tests": {
96
+ ":foo": ":bar",
97
+ "test": [
98
+ [
99
+ 1,
100
+ 2
101
101
  ],
102
- 2.123
102
+ 2.123
103
103
  ]
104
104
  }
105
105
  }
@@ -109,69 +109,204 @@ STR
109
109
  end
110
110
 
111
111
 
112
- def test_ordered_data_string_struct
112
+ def test_ordered_data_string_struct_json
113
113
  expected = <<STR
114
114
  {
115
- "acks" => [
116
- [
117
- Fixnum,
118
- Fixnum
115
+ "acks": [
116
+ [
117
+ "Fixnum",
118
+ "Fixnum"
119
119
  ],
120
- [
121
- String,
122
- String
120
+ [
121
+ "String",
122
+ "String"
123
123
  ]
124
124
  ],
125
- "root" => [
126
- [
127
- String,
128
- String
125
+ "root": [
126
+ [
127
+ "String",
128
+ "String"
129
129
  ],
130
- [
131
- String,
132
- String
130
+ [
131
+ "String",
132
+ "String"
133
+ ],
134
+ [
135
+ "String",
136
+ "String",
137
+ [
138
+ "String",
139
+ "String"
140
+ ]
141
+ ],
142
+ {
143
+ ":tests": [
144
+ "String",
145
+ "String"
146
+ ],
147
+ "test": [
148
+ [
149
+ "String",
150
+ "String"
151
+ ],
152
+ "String"
153
+ ]
154
+ }
155
+ ],
156
+ "subs": [
157
+ "String",
158
+ "String"
159
+ ],
160
+ "tests": {
161
+ ":foo": "Symbol",
162
+ "test": [
163
+ [
164
+ "Fixnum",
165
+ "Fixnum"
166
+ ],
167
+ "Float"
168
+ ]
169
+ }
170
+ }
171
+ STR
172
+
173
+ assert_equal expected.strip,
174
+ Kronk::Diff.ordered_data_string(mock_data, true)
175
+ end
176
+
177
+
178
+ def test_ordered_data_string_ruby
179
+ with_config :render_lang => 'ruby' do
180
+ expected = <<STR
181
+ {
182
+ "acks" => [
183
+ [
184
+ 56,
185
+ 78
186
+ ],
187
+ [
188
+ "12",
189
+ "34"
190
+ ]
191
+ ],
192
+ "root" => [
193
+ [
194
+ "B1",
195
+ "B2"
196
+ ],
197
+ [
198
+ "A1",
199
+ "A2"
200
+ ],
201
+ [
202
+ "C1",
203
+ "C2",
204
+ [
205
+ "C3a",
206
+ "C3b"
207
+ ]
208
+ ],
209
+ {
210
+ :tests => [
211
+ "D3a",
212
+ "D3b"
213
+ ],
214
+ "test" => [
215
+ [
216
+ "D1a\\nContent goes here",
217
+ "D1b"
218
+ ],
219
+ "D2"
220
+ ]
221
+ }
222
+ ],
223
+ "subs" => [
224
+ "a",
225
+ "b"
226
+ ],
227
+ "tests" => {
228
+ :foo => :bar,
229
+ "test" => [
230
+ [
231
+ 1,
232
+ 2
233
+ ],
234
+ 2.123
235
+ ]
236
+ }
237
+ }
238
+ STR
239
+
240
+ assert_equal expected.strip, Kronk::Diff.ordered_data_string(mock_data)
241
+ end
242
+ end
243
+
244
+
245
+ def test_ordered_data_string_struct_ruby
246
+ with_config :render_lang => 'ruby' do
247
+ expected = <<STR
248
+ {
249
+ "acks" => [
250
+ [
251
+ Fixnum,
252
+ Fixnum
133
253
  ],
134
- [
135
- String,
136
- String,
137
254
  [
138
255
  String,
139
256
  String
140
- ]
257
+ ]
258
+ ],
259
+ "root" => [
260
+ [
261
+ String,
262
+ String
141
263
  ],
142
- {
143
- :tests => [
264
+ [
144
265
  String,
145
266
  String
146
- ],
147
- "test" => [
267
+ ],
268
+ [
269
+ String,
270
+ String,
148
271
  [
149
272
  String,
150
273
  String
274
+ ]
275
+ ],
276
+ {
277
+ :tests => [
278
+ String,
279
+ String
280
+ ],
281
+ "test" => [
282
+ [
283
+ String,
284
+ String
151
285
  ],
152
- String
286
+ String
153
287
  ]
154
288
  }
155
289
  ],
156
- "subs" => [
157
- String,
158
- String
290
+ "subs" => [
291
+ String,
292
+ String
159
293
  ],
160
- "tests" => {
161
- :foo => Symbol,
162
- "test" => [
163
- [
164
- Fixnum,
165
- Fixnum
294
+ "tests" => {
295
+ :foo => Symbol,
296
+ "test" => [
297
+ [
298
+ Fixnum,
299
+ Fixnum
166
300
  ],
167
- Float
301
+ Float
168
302
  ]
169
303
  }
170
304
  }
171
305
  STR
172
306
 
173
- assert_equal expected.strip,
174
- Kronk::Diff.ordered_data_string(mock_data, true)
307
+ assert_equal expected.strip,
308
+ Kronk::Diff.ordered_data_string(mock_data, true)
309
+ end
175
310
  end
176
311
 
177
312
 
data/test/test_helper.rb CHANGED
@@ -40,3 +40,143 @@ def mock_data
40
40
  "subs"=>["a", "b"]
41
41
  }
42
42
  end
43
+
44
+ MOCK_REQUIRES = Hash.new 0
45
+
46
+ def mock_require str
47
+ MOCK_REQUIRES[str] += 1
48
+ end
49
+
50
+
51
+ def clear_mock_require str
52
+ MOCK_REQUIRES.delete str
53
+ end
54
+
55
+
56
+ alias kernel_require require
57
+ def require str
58
+ if MOCK_REQUIRES[str] > 0
59
+ MOCK_REQUIRES[str] = MOCK_REQUIRES[str] - 1
60
+ return
61
+ end
62
+ kernel_require str
63
+ end
64
+
65
+
66
+ def assert_require req, msg=nil
67
+ assert MOCK_REQUIRES.has_key?(req), msg || "Expected mock require '#{req}'"
68
+ assert_equal 0, MOCK_REQUIRES[req],
69
+ msg || "Expected require '#{req}' #{MOCK_REQUIRES[req]} more times"
70
+ end
71
+
72
+
73
+ $catch_exit = nil
74
+ alias kernel_exit exit
75
+ def exit status=true
76
+ if $catch_exit
77
+ throw :exited, status
78
+ end
79
+ kernel_exit status
80
+ end
81
+
82
+
83
+ def assert_exit num=true
84
+ $catch_exit = true
85
+ status = catch :exited do
86
+ yield if block_given?
87
+ end
88
+ $catch_exit = false
89
+
90
+ assert_equal num,status,
91
+ "Expected exit status #{num.inspect} but got #{status.inspect}"
92
+ end
93
+
94
+
95
+ def expect_compare_output str1, str2=nil, opts={}
96
+ opts = str2 if opts.empty? && Hash === str2
97
+ str2 = str1 if !str2 || Hash === str2
98
+ tim = opts.delete(:times) || 1
99
+
100
+ kronk = Kronk.new opts
101
+ kronk.compare StringIO.new(str1), StringIO.new(str2)
102
+
103
+ $stdout.expects(:puts).with(kronk.diff.formatted).times(tim)
104
+ end
105
+
106
+
107
+ def expect_request_output str, opts={}
108
+ res = Kronk::Response.new str
109
+ tim = opts.delete(:times) || 1
110
+ $stdout.expects(:puts).with(res.stringify opts).times(tim)
111
+ end
112
+
113
+
114
+ def expect_error_output str
115
+ $stderr.expects(:puts).with "\nError: #{str}"
116
+ end
117
+
118
+
119
+ IRB = Module.new
120
+ def with_irb_mock
121
+ mock_require "irb"
122
+
123
+ $stdout.expects(:puts).with "\nHTTP Response is in $http_response"
124
+ $stdout.expects(:puts).with "Response data is in $response\n\n"
125
+ ::IRB.expects :start
126
+
127
+ yield
128
+
129
+ $http_response = nil
130
+ $response = nil
131
+ clear_mock_require 'irb'
132
+ end
133
+
134
+
135
+ def with_config config={}
136
+ old_conf = Kronk.config
137
+ Kronk.instance_variable_set "@config", Kronk.config.merge(config)
138
+ yield
139
+
140
+ ensure
141
+ Kronk.instance_variable_set "@config", old_conf
142
+ end
143
+
144
+
145
+ def expect_request req_method, url, options={}
146
+ uri = URI.parse url
147
+
148
+ resp = Kronk::Response.new(options[:returns] || mock_200_response)
149
+ resp.stubs(:code).returns(options[:status] || '200')
150
+
151
+ http = mock 'http'
152
+ socket = mock 'socket'
153
+ req = mock 'req'
154
+ res = mock 'res'
155
+
156
+ res.stubs(:to_hash).returns Hash.new
157
+
158
+ data = options[:data]
159
+ data &&= Hash === data ? Kronk::Request.build_query(data) : data.to_s
160
+
161
+ headers = options[:headers] || Hash.new
162
+ headers['User-Agent'] ||= Kronk.config[:user_agents]['kronk']
163
+
164
+ req.expects(:start).yields(http).returns res
165
+
166
+ http.expects(:instance_variable_get).with("@socket").returns socket
167
+
168
+ socket.expects(:debug_output=)
169
+
170
+ Kronk::Request::VanillaRequest.expects(:new).
171
+ with(req_method.to_s.upcase, uri.request_uri, headers).returns req
172
+
173
+ Net::HTTP.expects(:new).with(uri.host, uri.port).returns req
174
+
175
+ http.expects(:request).with(req, data).returns res
176
+
177
+ Kronk::Response.expects(:new).returns resp
178
+
179
+ yield http, req, resp if block_given?
180
+
181
+ resp
182
+ end