erudite 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,352 +1,366 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe Erudite::Example do
6
- it 'requires some source' do
7
- expect { described_class.new }.to raise_error(ArgumentError)
8
- end
9
-
10
- it 'can be initialized' do
11
- expect(described_class.new(nil)).to be_a(described_class)
12
- end
13
-
14
- it 'sets the source' do
15
- source = double
16
- example = described_class.new(source)
17
- expect(example.source).to eql(source)
18
- end
19
-
20
- it 'uses the default expected outcome' do
21
- example = described_class.new(nil)
22
- expect(example.expected).to be_an(Erudite::Outcome)
23
- expect(example.expected.result).to be(nil)
24
- expect(example.expected.output).to be(nil)
25
- end
26
-
27
- it 'sets the expected result' do
28
- result = double
29
- example = described_class.new(nil, result)
30
- expect(example.expected.result).to eql(result)
31
- end
32
-
33
- it 'sets the expected output' do
34
- output = double
35
- example = described_class.new(nil, nil, output)
36
- expect(example.expected.output).to eql(output)
37
- end
38
-
39
- describe '#binding' do
40
- it 'uses the default binding' do
41
- example = described_class.new(nil)
42
- expect(example.binding).to be_a(Binding)
43
- expect(example.binding.eval('self')).to be(TOPLEVEL_BINDING.eval('self'))
44
- expect(example.binding).to_not be(TOPLEVEL_BINDING)
45
- end
46
-
47
- it 'memoizes the binding' do
48
- example = described_class.new(nil)
49
- binding = example.binding
50
- expect(example.binding).to be(binding)
51
- end
52
-
53
- it 'sets the binding' do
54
- example = described_class.new(nil)
55
- binding = TOPLEVEL_BINDING.dup
56
- example.binding = binding
57
- expect(example.binding).to be(binding)
58
- end
59
- end
60
-
61
- describe '#run!' do
62
- it 'evaluates the source' do
63
- source = ':something'
64
- example = described_class.new(source)
65
- expect(example.run!).to eql(TOPLEVEL_BINDING.eval(source))
66
- end
67
-
68
- it 'evaluates the source in the binding' do
69
- source = 'variable'
70
- example = described_class.new(source)
71
- example.binding.eval('variable = :something')
72
- expect(example.run!).to eql(example.binding.eval(source))
73
- end
74
-
75
- it 'sets the file name' do
76
- example = described_class.new('fail')
77
- begin
78
- example.run!
79
- rescue => error
80
- expect(error.backtrace.first).to_not start_with('<main>:')
81
- end
82
- end
83
-
84
- it 'sets the line number' do
85
- example = described_class.new('fail')
86
- begin
87
- example.run!
88
- rescue => error
89
- expect(error.backtrace.first).to_not end_with(":1:in `<main>'")
90
- end
91
- end
92
- end
93
-
94
- describe '#run' do
95
- it 'returns the result' do
96
- source = ':something'
97
- example = described_class.new(source)
98
- expect(example.run.first).to eql(TOPLEVEL_BINDING.eval(source))
99
- expect(example.run.last).to be(nil)
100
- end
101
-
102
- it 'returns the exception' do
103
- source = 'fail'
104
- example = described_class.new(source)
105
- expect(example.run.first).to be(nil)
106
- expect(example.run.last).to be_a(StandardError)
107
- end
108
- end
109
-
110
- describe '.without_stdio' do
111
- it 'requires a block' do
112
- expect { described_class.without_stdio }.to raise_error(LocalJumpError)
113
- end
114
-
115
- it 'calls the block' do
116
- result = double
117
- expect(described_class.without_stdio { result }.first).to eql(result)
118
- end
119
-
120
- it 'redirects STDIN' do
121
- stdin = $stdin
122
- expect(described_class.without_stdio { $stdin }).to_not eq(stdin)
123
- end
124
-
125
- it 'redirects STDOUT' do
126
- stdout = $stdout
127
- expect(described_class.without_stdio { $stdout }).to_not eq(stdout)
128
- end
129
-
130
- it 'redirects STDERR' do
131
- stderr = $stderr
132
- expect(described_class.without_stdio { $stderr }).to_not eq(stderr)
133
- end
134
-
135
- it 'reconnects STDIN' do
136
- stdin = $stdin
137
- begin
138
- described_class.without_stdio { fail }
139
- rescue
140
- nil
141
- end
142
- expect($stdin).to eq(stdin)
143
- end
144
-
145
- it 'reconnects STDOUT' do
146
- stdout = $stdout
147
- begin
148
- described_class.without_stdio { fail }
149
- rescue
150
- nil
151
- end
152
- expect($stdout).to eq(stdout)
153
- end
154
-
155
- it 'reconnects STDERR' do
156
- stderr = $stderr
157
- begin
158
- described_class.without_stdio { fail }
159
- rescue
160
- nil
161
- end
162
- expect($stderr).to eq(stderr)
163
- end
164
-
165
- it 'returns the redirected IO' do
166
- expect(described_class.without_stdio {}.last).to be_a(StringIO)
167
- end
168
-
169
- it 'handles STDIN' do
170
- result, _ = described_class.without_stdio { gets }
171
- expect(result).to be(nil)
172
- end
173
-
174
- it 'handles STDOUT' do
175
- _, io = described_class.without_stdio { puts 'something' }
176
- expect(io.string).to eql("something\n")
177
- end
178
-
179
- it 'handles STDERR' do
180
- _, io = described_class.without_stdio { warn 'something' }
181
- expect(io.string).to eql("something\n")
182
- end
183
- end
184
-
185
- describe '.format_exception' do
186
- it 'formats the exception' do
187
- exception = StandardError.new('something')
188
- expect(described_class.format_exception(exception))
189
- .to eql("#{exception.class.name}: #{exception.message}")
190
- end
191
- end
192
-
193
- describe '#actual' do
194
- it 'sets the result' do
195
- example = described_class.new('"something"')
196
- expect(example.actual.result).to eql('"something"')
197
- expect(example.actual.output).to eql('')
198
- end
199
-
200
- it 'handles STDIN' do
201
- example = described_class.new('gets')
202
- expect(example.actual.result).to eql('nil')
203
- expect(example.actual.output).to eql('')
204
- end
205
-
206
- it 'handles STDOUT' do
207
- example = described_class.new('puts "something"')
208
- expect(example.actual.result).to eql('nil')
209
- expect(example.actual.output).to eql("something\n")
210
- end
211
-
212
- it 'handles STDERR' do
213
- example = described_class.new('warn "something"')
214
- expect(example.actual.result).to eql('nil')
215
- expect(example.actual.output).to eql("something\n")
216
- end
217
-
218
- it 'handles exceptions' do
219
- example = described_class.new('fail StandardError, "something"')
220
- expect(example.actual.result).to eql('nil')
221
- expect(example.actual.output).to eql("StandardError: something\n")
222
- end
223
-
224
- it 'memoizes the result' do
225
- example = described_class.new('')
226
- actual = example.actual
227
- expect(example.actual).to be(actual)
228
- end
229
- end
230
-
231
- describe '.pattern' do
232
- it 'returns a Regexp' do
233
- pattern = described_class.pattern('something')
234
- expect(pattern).to be_a(Regexp)
235
- end
236
-
237
- it 'escapes meta characters' do
238
- pattern = described_class.pattern('some.thing')
239
- expect(pattern).to eql(/some\.thing/)
240
- end
241
-
242
- it 'replaces "..."' do
243
- pattern = described_class.pattern('some...thing')
244
- expect(pattern).to eql(/some.*?thing/)
245
- end
246
- end
247
-
248
- describe '#valid_result?' do
249
- it 'returns true without an expected result' do
250
- example = described_class.new(nil, nil)
251
- expect(example).to be_valid_result
252
- end
253
-
254
- it 'returns true when the results match' do
255
- example = described_class.new('"something"', '"something"')
256
- expect(example).to be_valid_result
257
- end
258
-
259
- it "returns false when the results don't match" do
260
- example = described_class.new('"something"', '"something else"')
261
- expect(example).to_not be_valid_result
262
- end
263
-
264
- it 'allows wildcards' do
265
- example = described_class.new('Object.new', '#<Object:0x...>')
266
- expect(example).to be_valid_result
267
- end
268
- end
269
-
270
- describe '#valid_output?' do
271
- it 'returns true without expected output' do
272
- example = described_class.new(nil, nil)
273
- expect(example).to be_valid_output
274
- end
275
-
276
- it 'returns true when the output matches' do
277
- example = described_class.new('puts "something"', nil, 'something')
278
- expect(example).to be_valid_output
279
- end
280
-
281
- it "returns false when the output doesn't match" do
282
- example = described_class.new('puts "something"', nil, 'something else')
283
- expect(example).to_not be_valid_output
284
- end
285
-
286
- it 'allows wildcards' do
287
- example = described_class.new('puts Object.new', nil, '#<Object:0x...>')
288
- expect(example).to be_valid_output
289
- end
290
- end
291
-
292
- describe '#pass?' do
293
- it 'realizes the actual outcome' do
294
- example = described_class.new('x = 1')
295
- example.pass?
296
- expect(example.binding.eval('x')).to eql(1)
297
- end
298
-
299
- it 'returns true if both the result and output match' do
300
- example = described_class
301
- .new('p "something"', '"something"', '"something"')
302
- expect(example).to be_pass
303
- end
304
-
305
- it "returns false if the result doesn't match" do
306
- example = described_class
307
- .new('p "something"', '"something else"', '"something"')
308
- expect(example).to_not be_pass
309
- end
310
-
311
- it "returns false if the output doesn't match" do
312
- example = described_class
313
- .new('p "something"', '"something"', '"something else"')
314
- expect(example).to_not be_pass
315
- end
316
- end
317
-
318
- describe '#==' do
319
- it 'returns true when they have the same source, result, and output' do
320
- source = 'x'
321
- result = 'y'
322
- output = 'z'
323
- a = described_class.new(source, result, output)
324
- b = described_class.new(source, result, output)
325
- expect(a).to eq(b)
326
- end
327
-
328
- it "returns false when they don't have the same source" do
329
- result = 'y'
330
- output = 'z'
331
- a = described_class.new('a', result, output)
332
- b = described_class.new('b', result, output)
333
- expect(a).to_not eq(b)
334
- end
335
-
336
- it "returns false when they don't have the same result" do
337
- source = 'x'
338
- output = 'z'
339
- a = described_class.new(source, 'a', output)
340
- b = described_class.new(source, 'b', output)
341
- expect(a).to_not eq(b)
342
- end
343
-
344
- it "returns false when they don't have the same output" do
345
- source = 'x'
346
- result = 'y'
347
- a = described_class.new(source, result, 'a')
348
- b = described_class.new(source, result, 'b')
349
- expect(a).to_not eq(b)
350
- end
351
- end
352
- end
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'English'
5
+
6
+ describe Erudite::Example do
7
+ it 'requires some source' do
8
+ expect { described_class.new }.to raise_error(ArgumentError)
9
+ end
10
+
11
+ it 'can be initialized' do
12
+ expect(described_class.new(nil)).to be_a(described_class)
13
+ end
14
+
15
+ it 'sets the source' do
16
+ source = double
17
+ example = described_class.new(source)
18
+ expect(example.source).to eql(source)
19
+ end
20
+
21
+ it 'uses the default expected outcome' do
22
+ example = described_class.new(nil)
23
+ expect(example.expected).to be_an(Erudite::Outcome)
24
+ expect(example.expected.result).to be(nil)
25
+ expect(example.expected.output).to be(nil)
26
+ end
27
+
28
+ it 'sets the expected result' do
29
+ result = double
30
+ example = described_class.new(nil, result)
31
+ expect(example.expected.result).to eql(result)
32
+ end
33
+
34
+ it 'sets the expected output' do
35
+ output = double
36
+ example = described_class.new(nil, nil, output)
37
+ expect(example.expected.output).to eql(output)
38
+ end
39
+
40
+ describe '#binding' do
41
+ it 'uses the default binding' do
42
+ example = described_class.new(nil)
43
+ expect(example.binding).to be_a(Binding)
44
+ expect(example.binding.eval('self')).to be(TOPLEVEL_BINDING.eval('self'))
45
+ expect(example.binding).to_not be(TOPLEVEL_BINDING)
46
+ end
47
+
48
+ it 'memoizes the binding' do
49
+ example = described_class.new(nil)
50
+ binding = example.binding
51
+ expect(example.binding).to be(binding)
52
+ end
53
+
54
+ it 'sets the binding' do
55
+ example = described_class.new(nil)
56
+ binding = TOPLEVEL_BINDING.dup
57
+ example.binding = binding
58
+ expect(example.binding).to be(binding)
59
+ end
60
+ end
61
+
62
+ describe '#run!' do
63
+ it 'evaluates the source' do
64
+ source = ':something'
65
+ example = described_class.new(source)
66
+ expect(example.run!).to eql(TOPLEVEL_BINDING.eval(source))
67
+ end
68
+
69
+ it 'evaluates the source in the binding' do
70
+ source = 'variable'
71
+ example = described_class.new(source)
72
+ example.binding.eval('variable = :something')
73
+ expect(example.run!).to eql(example.binding.eval(source))
74
+ end
75
+
76
+ it 'sets the file name' do
77
+ example = described_class.new('fail')
78
+ begin
79
+ example.run!
80
+ rescue => error
81
+ expect(error.backtrace.first).to_not start_with('<main>:')
82
+ end
83
+ end
84
+
85
+ it 'sets the line number' do
86
+ example = described_class.new('fail')
87
+ begin
88
+ example.run!
89
+ rescue => error
90
+ expect(error.backtrace.first).to_not end_with(":1:in `<main>'")
91
+ end
92
+ end
93
+ end
94
+
95
+ describe '#run' do
96
+ it 'returns the result' do
97
+ source = ':something'
98
+ example = described_class.new(source)
99
+ expect(example.run.first).to eql(TOPLEVEL_BINDING.eval(source))
100
+ expect(example.run.last).to be(nil)
101
+ end
102
+
103
+ it 'returns the exception' do
104
+ source = 'fail'
105
+ example = described_class.new(source)
106
+ expect(example.run.first).to be(nil)
107
+ expect(example.run.last).to be_a(StandardError)
108
+ end
109
+ end
110
+
111
+ describe '.without_stdio' do
112
+ it 'requires a block' do
113
+ expect { described_class.without_stdio }.to raise_error(LocalJumpError)
114
+ end
115
+
116
+ it 'calls the block' do
117
+ result = double
118
+ expect(described_class.without_stdio { result }.first).to eql(result)
119
+ end
120
+
121
+ it 'redirects STDIN' do
122
+ stdin = $stdin
123
+ expect(described_class.without_stdio { $stdin }.first).to_not be(stdin)
124
+ end
125
+
126
+ it 'redirects STDOUT' do
127
+ stdout = $stdout
128
+ expect(described_class.without_stdio { $stdout }.first).to_not be(stdout)
129
+ end
130
+
131
+ it 'redirects STDERR' do
132
+ stderr = $stderr
133
+ expect(described_class.without_stdio { $stderr }.first).to_not be(stderr)
134
+ end
135
+
136
+ it 'clears ARGV' do
137
+ $ARGV.push(double)
138
+ result = described_class.without_stdio { $ARGV.dup }.first
139
+ $ARGV.pop
140
+ expect(result).to eql([])
141
+ end
142
+
143
+ it 'reconnects STDIN' do
144
+ stdin = $stdin
145
+ begin
146
+ described_class.without_stdio { fail }
147
+ rescue
148
+ nil
149
+ end
150
+ expect($stdin).to be(stdin)
151
+ end
152
+
153
+ it 'reconnects STDOUT' do
154
+ stdout = $stdout
155
+ begin
156
+ described_class.without_stdio { fail }
157
+ rescue
158
+ nil
159
+ end
160
+ expect($stdout).to be(stdout)
161
+ end
162
+
163
+ it 'reconnects STDERR' do
164
+ stderr = $stderr
165
+ begin
166
+ described_class.without_stdio { fail }
167
+ rescue
168
+ nil
169
+ end
170
+ expect($stderr).to be(stderr)
171
+ end
172
+
173
+ it 'restores ARGV' do
174
+ argv = $ARGV.dup
175
+ described_class.without_stdio { $ARGV.push('something') }
176
+ expect($ARGV).to eql(argv)
177
+ end
178
+
179
+ it 'returns the redirected IO' do
180
+ expect(described_class.without_stdio {}.last).to be_a(StringIO)
181
+ end
182
+
183
+ it 'handles STDIN' do
184
+ result, _ = described_class.without_stdio { gets }
185
+ expect(result).to be(nil)
186
+ end
187
+
188
+ it 'handles STDOUT' do
189
+ _, io = described_class.without_stdio { puts 'something' }
190
+ expect(io.string).to eql("something\n")
191
+ end
192
+
193
+ it 'handles STDERR' do
194
+ _, io = described_class.without_stdio { warn 'something' }
195
+ expect(io.string).to eql("something\n")
196
+ end
197
+ end
198
+
199
+ describe '.format_exception' do
200
+ it 'formats the exception' do
201
+ exception = StandardError.new('something')
202
+ expect(described_class.format_exception(exception))
203
+ .to eql("#{exception.class.name}: #{exception.message}")
204
+ end
205
+ end
206
+
207
+ describe '#actual' do
208
+ it 'sets the result' do
209
+ example = described_class.new('"something"')
210
+ expect(example.actual.result).to eql('"something"')
211
+ expect(example.actual.output).to eql('')
212
+ end
213
+
214
+ it 'handles STDIN' do
215
+ example = described_class.new('gets')
216
+ expect(example.actual.result).to eql('nil')
217
+ expect(example.actual.output).to eql('')
218
+ end
219
+
220
+ it 'handles STDOUT' do
221
+ example = described_class.new('puts "something"')
222
+ expect(example.actual.result).to eql('nil')
223
+ expect(example.actual.output).to eql("something\n")
224
+ end
225
+
226
+ it 'handles STDERR' do
227
+ example = described_class.new('warn "something"')
228
+ expect(example.actual.result).to eql('nil')
229
+ expect(example.actual.output).to eql("something\n")
230
+ end
231
+
232
+ it 'handles exceptions' do
233
+ example = described_class.new('fail StandardError, "something"')
234
+ expect(example.actual.result).to eql('nil')
235
+ expect(example.actual.output).to eql("StandardError: something\n")
236
+ end
237
+
238
+ it 'memoizes the result' do
239
+ example = described_class.new('')
240
+ actual = example.actual
241
+ expect(example.actual).to be(actual)
242
+ end
243
+ end
244
+
245
+ describe '.pattern' do
246
+ it 'returns a Regexp' do
247
+ pattern = described_class.pattern('something')
248
+ expect(pattern).to be_a(Regexp)
249
+ end
250
+
251
+ it 'escapes meta characters' do
252
+ pattern = described_class.pattern('some.thing')
253
+ expect(pattern).to eql(/some\.thing/)
254
+ end
255
+
256
+ it 'replaces "..."' do
257
+ pattern = described_class.pattern('some...thing')
258
+ expect(pattern).to eql(/some.*?thing/)
259
+ end
260
+ end
261
+
262
+ describe '#valid_result?' do
263
+ it 'returns true without an expected result' do
264
+ example = described_class.new(nil, nil)
265
+ expect(example).to be_valid_result
266
+ end
267
+
268
+ it 'returns true when the results match' do
269
+ example = described_class.new('"something"', '"something"')
270
+ expect(example).to be_valid_result
271
+ end
272
+
273
+ it "returns false when the results don't match" do
274
+ example = described_class.new('"something"', '"something else"')
275
+ expect(example).to_not be_valid_result
276
+ end
277
+
278
+ it 'allows wildcards' do
279
+ example = described_class.new('Object.new', '#<Object:0x...>')
280
+ expect(example).to be_valid_result
281
+ end
282
+ end
283
+
284
+ describe '#valid_output?' do
285
+ it 'returns true without expected output' do
286
+ example = described_class.new(nil, nil)
287
+ expect(example).to be_valid_output
288
+ end
289
+
290
+ it 'returns true when the output matches' do
291
+ example = described_class.new('puts "something"', nil, 'something')
292
+ expect(example).to be_valid_output
293
+ end
294
+
295
+ it "returns false when the output doesn't match" do
296
+ example = described_class.new('puts "something"', nil, 'something else')
297
+ expect(example).to_not be_valid_output
298
+ end
299
+
300
+ it 'allows wildcards' do
301
+ example = described_class.new('puts Object.new', nil, '#<Object:0x...>')
302
+ expect(example).to be_valid_output
303
+ end
304
+ end
305
+
306
+ describe '#pass?' do
307
+ it 'realizes the actual outcome' do
308
+ example = described_class.new('x = 1')
309
+ example.pass?
310
+ expect(example.binding.eval('x')).to eql(1)
311
+ end
312
+
313
+ it 'returns true if both the result and output match' do
314
+ example = described_class
315
+ .new('p "something"', '"something"', '"something"')
316
+ expect(example).to be_pass
317
+ end
318
+
319
+ it "returns false if the result doesn't match" do
320
+ example = described_class
321
+ .new('p "something"', '"something else"', '"something"')
322
+ expect(example).to_not be_pass
323
+ end
324
+
325
+ it "returns false if the output doesn't match" do
326
+ example = described_class
327
+ .new('p "something"', '"something"', '"something else"')
328
+ expect(example).to_not be_pass
329
+ end
330
+ end
331
+
332
+ describe '#==' do
333
+ it 'returns true when they have the same source, result, and output' do
334
+ source = 'x'
335
+ result = 'y'
336
+ output = 'z'
337
+ a = described_class.new(source, result, output)
338
+ b = described_class.new(source, result, output)
339
+ expect(a).to eq(b)
340
+ end
341
+
342
+ it "returns false when they don't have the same source" do
343
+ result = 'y'
344
+ output = 'z'
345
+ a = described_class.new('a', result, output)
346
+ b = described_class.new('b', result, output)
347
+ expect(a).to_not eq(b)
348
+ end
349
+
350
+ it "returns false when they don't have the same result" do
351
+ source = 'x'
352
+ output = 'z'
353
+ a = described_class.new(source, 'a', output)
354
+ b = described_class.new(source, 'b', output)
355
+ expect(a).to_not eq(b)
356
+ end
357
+
358
+ it "returns false when they don't have the same output" do
359
+ source = 'x'
360
+ result = 'y'
361
+ a = described_class.new(source, result, 'a')
362
+ b = described_class.new(source, result, 'b')
363
+ expect(a).to_not eq(b)
364
+ end
365
+ end
366
+ end