oktest 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.
@@ -0,0 +1,379 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ###
4
+ ### $Release: 1.0.0 $
5
+ ### $Copyright: copyright(c) 2011-2021 kuwata-lab.com all rights reserved $
6
+ ### $License: MIT License $
7
+ ###
8
+
9
+ require_relative './initialize'
10
+
11
+
12
+ class Filter_TC < TC
13
+
14
+ describe '.create_from()' do
15
+ def parse_filter_str(str)
16
+ return Oktest::Filter.create_from(str)
17
+ end
18
+ def filter_attrs(ft)
19
+ #return ft.topic_pattern, ft.spec_pattern, ft.tag_pattern, ft.negative
20
+ return ft.instance_eval {
21
+ [@topic_pattern, @spec_pattern, @tag_pattern, @negative]
22
+ }
23
+ end
24
+ #
25
+ it "[!9dzmg] returns filter object." do
26
+ ft = parse_filter_str("topic=*pat*")
27
+ assert ft.is_a?(Oktest::Filter), "should be a filter object."
28
+ end
29
+ it "[!xt364] parses 'topic=...' as filter pattern for topic." do
30
+ ft = parse_filter_str("topic=*pat*")
31
+ assert_eq filter_attrs(ft), ['*pat*', nil, nil, false]
32
+ end
33
+ it "[!53ega] parses 'spec=...' as filter pattern for spec." do
34
+ ft = parse_filter_str("spec=*pat*")
35
+ assert_eq filter_attrs(ft), [nil, '*pat*', nil, false]
36
+ end
37
+ it "[!go6us] parses 'tag=...' as filter pattern for tag." do
38
+ ft = parse_filter_str("tag={exp,old}")
39
+ assert_eq filter_attrs(ft), [nil, nil, '{exp,old}', false]
40
+ end
41
+ it "[!gtpt1] parses 'sid=...' as filter pattern for spec." do
42
+ ft = parse_filter_str("sid=abc123")
43
+ assert_eq filter_attrs(ft), [nil, '\[!abc123\]*', nil, false]
44
+ end
45
+ it "[!cmp6e] raises ArgumentError when invalid argument." do
46
+ assert_exc(ArgumentError, '"abc123": unexpected pattern string.') do
47
+ parse_filter_str("abc123")
48
+ end
49
+ end
50
+ it "[!5hl7z] parses 'xxx!=...' as negative filter pattern." do
51
+ ft = parse_filter_str("topic!=*pat*")
52
+ assert_eq filter_attrs(ft), ['*pat*', nil, nil, true]
53
+ ft = parse_filter_str("spec!=*pat*")
54
+ assert_eq filter_attrs(ft), [nil, '*pat*', nil, true]
55
+ ft = parse_filter_str("tag!={exp,old}")
56
+ assert_eq filter_attrs(ft), [nil, nil, '{exp,old}', true]
57
+ ft = parse_filter_str("sid!=abc123")
58
+ assert_eq filter_attrs(ft), [nil, '\[!abc123\]*', nil, true]
59
+ end
60
+ end
61
+
62
+ def new_filter(topic_pat=nil, spec_pat=nil, tag_pat=nil, negative: false)
63
+ ft = Oktest::Filter.new(topic_pat, spec_pat, tag_pat, negative: negative)
64
+ return ft
65
+ end
66
+
67
+ describe '#_match?()' do
68
+ it "[!h90x3] returns true if str matched to pattern." do
69
+ ft = new_filter()
70
+ assert_eq ft.instance_eval { _match?('foo', 'foo') }, true
71
+ assert_eq ft.instance_eval { _match?('foo', 'f*') }, true
72
+ assert_eq ft.instance_eval { _match?('foo', '*o*') }, true
73
+ assert_eq ft.instance_eval { _match?('foo', '{foo,bar}') }, true
74
+ #
75
+ assert_eq ft.instance_eval { _match?('foo', 'bar') }, false
76
+ assert_eq ft.instance_eval { _match?('foo', 'F*') }, false
77
+ assert_eq ft.instance_eval { _match?('foo', '*x*') }, false
78
+ assert_eq ft.instance_eval { _match?('foo', '{x,y}') }, false
79
+ end
80
+ end
81
+
82
+ describe '#_match_tag?()' do
83
+ it "[!lyo18] returns false if tag is nil." do
84
+ ft = new_filter()
85
+ assert_eq ft.instance_eval { _match_tag?(nil, '*') }, false
86
+ end
87
+ it "[!8lxin] returns true if tag matched to pattern." do
88
+ ft = new_filter()
89
+ assert_eq ft.instance_eval { _match_tag?('foo', 'foo') }, true
90
+ assert_eq ft.instance_eval { _match_tag?('foo', 'f*') }, true
91
+ assert_eq ft.instance_eval { _match_tag?('foo', '*o*') }, true
92
+ assert_eq ft.instance_eval { _match_tag?('foo', '{foo,bar}') }, true
93
+ #
94
+ assert_eq ft.instance_eval { _match_tag?('foo', 'bar') }, false
95
+ assert_eq ft.instance_eval { _match_tag?('foo', 'F*') }, false
96
+ assert_eq ft.instance_eval { _match_tag?('foo', '*x*') }, false
97
+ assert_eq ft.instance_eval { _match_tag?('foo', '{x,y}') }, false
98
+ end
99
+ it "[!7wxmh] supports multiple tag names." do
100
+ ft = new_filter()
101
+ tag = ['foo', 'bar']
102
+ assert_eq ft.instance_eval { _match_tag?(tag, 'foo') }, true
103
+ assert_eq ft.instance_eval { _match_tag?(tag, 'f*') }, true
104
+ assert_eq ft.instance_eval { _match_tag?(tag, '*o*') }, true
105
+ assert_eq ft.instance_eval { _match_tag?(tag, '{fooooo,bar,baz}') }, true
106
+ #
107
+ assert_eq ft.instance_eval { _match_tag?(tag, 'foooo') }, false
108
+ assert_eq ft.instance_eval { _match_tag?(tag, 'F*') }, false
109
+ assert_eq ft.instance_eval { _match_tag?(tag, '*x*') }, false
110
+ assert_eq ft.instance_eval { _match_tag?(tag, '{x,y}') }, false
111
+ end
112
+ end
113
+
114
+ describe '#scope_match?()' do
115
+ it "[!zkq6r] returns true only if tag name matched to pattern." do
116
+ sc = Oktest::ScopeNode.new(nil, 'file.rb', tag: 'foo')
117
+ assert_eq new_filter('*', '*', 'foo').scope_match?(sc), true
118
+ assert_eq new_filter('*', '*', 'f*' ).scope_match?(sc), true
119
+ assert_eq new_filter('*', '*', 'x*' ).scope_match?(sc), false
120
+ #
121
+ sc = Oktest::ScopeNode.new(nil, 'file.rb', tag: nil)
122
+ assert_eq new_filter('*', '*', 'foo').scope_match?(sc), false
123
+ assert_eq new_filter('*', '*', '*' ).scope_match?(sc), false
124
+ end
125
+ end
126
+
127
+ describe '#topic_match?()' do
128
+ it "[!jpycj] returns true if topic target name matched to pattern." do
129
+ to = Oktest::TopicNode.new(nil, Time)
130
+ assert_eq new_filter('Time' , nil, nil).topic_match?(to), true
131
+ assert_eq new_filter('*ime*', nil, nil).topic_match?(to), true
132
+ assert_eq new_filter('*xy*' , nil, nil).topic_match?(to), false
133
+ end
134
+ it "[!6lfp1] returns true if tag name matched to pattern." do
135
+ to = Oktest::TopicNode.new(nil, Time, tag: 'foo')
136
+ [nil, '*bar*'].each do |pat|
137
+ assert_eq new_filter(pat, nil, 'foo' ).topic_match?(to), true
138
+ assert_eq new_filter(pat, nil, 'f*' ).topic_match?(to), true
139
+ assert_eq new_filter(pat, nil, '{foo,bar}').topic_match?(to), true
140
+ assert_eq new_filter(pat, nil, 'fooooo' ).topic_match?(to), false
141
+ end
142
+ end
143
+ end
144
+
145
+ describe '#spec_match?()' do
146
+ it "[!k45p3] returns true if spec description matched to pattern." do
147
+ sp = Oktest::SpecLeaf.new(nil, "sample", tag: 'foo')
148
+ assert_eq new_filter(nil, 'sample', nil).spec_match?(sp), true
149
+ assert_eq new_filter(nil, '*samp*', nil).spec_match?(sp), true
150
+ assert_eq new_filter(nil, '*abc*' , nil).spec_match?(sp), false
151
+ end
152
+ it "[!li3pd] returns true if tag name matched to pattern." do
153
+ sp = Oktest::SpecLeaf.new(nil, "sample", tag: 'foo')
154
+ [nil, '*bar*'].each do |pat|
155
+ assert_eq new_filter(nil, pat, 'foo' ).spec_match?(sp), true
156
+ assert_eq new_filter(nil, pat, 'f*' ).spec_match?(sp), true
157
+ assert_eq new_filter(nil, pat, '{foo,bar}').spec_match?(sp), true
158
+ assert_eq new_filter(nil, pat, 'fooooo' ).spec_match?(sp), false
159
+ end
160
+ end
161
+ end
162
+
163
+ def prepare()
164
+ Oktest.scope do
165
+ topic 'Hello' do
166
+ spec "hello spec", tag: 'new' do ok {"hello"} == "hello" end
167
+ end
168
+ topic 'Topic 832795' do
169
+ topic Integer do
170
+ spec "spec example #1" do ok {1+1} == 2 end
171
+ spec "spec example #2", tag: 'new' do ok {1-1} == 0 end
172
+ end
173
+ topic Float, tag: 'exp' do
174
+ spec "spec example #3" do ok {1.0+1.0} == 2.0 end
175
+ spec "spec example #4" do ok {1.0-1.0} == 0.0 end
176
+ end
177
+ spec "spec example #5", tag: ['exp', 'new'] do ok {1%1} == 0 end
178
+ end
179
+ end
180
+ end
181
+
182
+ def run_filter(topic_pattern, spec_pattern, tag_pattern, negative: false)
183
+ prepare()
184
+ filter = Oktest::Filter.new(topic_pattern, spec_pattern, tag_pattern, negative: negative)
185
+ Oktest.filter(filter)
186
+ reporter = Oktest::VerboseReporter.new()
187
+ sout, serr = capture('', tty: false) do
188
+ Oktest::Runner.new(reporter).start()
189
+ end
190
+ assert_eq serr, ""
191
+ return sout.sub(/^## total:.*\n/, '')
192
+ end
193
+
194
+ def uncolor(s)
195
+ return s.gsub(/\x1b.*?m/, '')
196
+ end
197
+
198
+ describe '#filter_children!()' do
199
+ it "[!osoq2] can filter topics by full name." do
200
+ expected = <<END
201
+ * Hello
202
+ - [pass] hello spec
203
+ END
204
+ sout = run_filter('Hello', nil, nil)
205
+ assert_eq uncolor(sout), expected
206
+ end
207
+ it "[!wzcco] can filter topics by pattern." do
208
+ expected = <<END
209
+ * Topic 832795
210
+ * Integer
211
+ - [pass] spec example #1
212
+ - [pass] spec example #2
213
+ * Float
214
+ - [pass] spec example #3
215
+ - [pass] spec example #4
216
+ - [pass] spec example #5
217
+ END
218
+ sout = run_filter('*832795*', nil, nil)
219
+ assert_eq uncolor(sout), expected
220
+ end
221
+ it "[!mz6id] can filter nested topics." do
222
+ expected = <<END
223
+ * Topic 832795
224
+ * Float
225
+ - [pass] spec example #3
226
+ - [pass] spec example #4
227
+ END
228
+ sout = run_filter('*loat*', nil, nil)
229
+ assert_eq uncolor(sout), expected
230
+ end
231
+ it "[!0kw9c] can filter specs by full name." do
232
+ expected = <<END
233
+ * Hello
234
+ - [pass] hello spec
235
+ END
236
+ sout = run_filter(nil, 'hello spec', nil)
237
+ assert_eq uncolor(sout), expected
238
+ end
239
+ it "[!fd8wt] can filter specs by pattern." do
240
+ expected = <<END
241
+ * Topic 832795
242
+ - [pass] spec example #5
243
+ END
244
+ sout = run_filter(nil, '*#5', nil)
245
+ assert_eq uncolor(sout), expected
246
+ #
247
+ expected = <<END
248
+ * Topic 832795
249
+ * Integer
250
+ - [pass] spec example #1
251
+ - [pass] spec example #2
252
+ * Float
253
+ - [pass] spec example #3
254
+ - [pass] spec example #4
255
+ - [pass] spec example #5
256
+ END
257
+ sout = run_filter(nil, 'spec example*', nil)
258
+ assert_eq uncolor(sout), expected
259
+ end
260
+ it "[!1jphf] can filter specs from nested topics." do
261
+ expected = <<END
262
+ * Topic 832795
263
+ * Float
264
+ - [pass] spec example #4
265
+ END
266
+ sout = run_filter(nil, '*#4', nil)
267
+ assert_eq uncolor(sout), expected
268
+ end
269
+ it "[!eirmu] can filter topics by tag name." do
270
+ expected = <<END
271
+ * Topic 832795
272
+ * Float
273
+ - [pass] spec example #3
274
+ - [pass] spec example #4
275
+ - [pass] spec example #5
276
+ END
277
+ sout = run_filter(nil, nil, 'exp')
278
+ assert_eq uncolor(sout), expected
279
+ end
280
+ it "[!6sq7g] can filter specs by tag name." do
281
+ expected = <<END
282
+ * Hello
283
+ - [pass] hello spec
284
+ * Topic 832795
285
+ * Integer
286
+ - [pass] spec example #2
287
+ - [pass] spec example #5
288
+ END
289
+ sout = run_filter(nil, nil, 'new')
290
+ assert_eq uncolor(sout), expected
291
+ end
292
+ it "[!6to6n] can filter by multiple tag name." do
293
+ expected = <<END
294
+ * Hello
295
+ - [pass] hello spec
296
+ * Topic 832795
297
+ * Integer
298
+ - [pass] spec example #2
299
+ * Float
300
+ - [pass] spec example #3
301
+ - [pass] spec example #4
302
+ - [pass] spec example #5
303
+ END
304
+ sout = run_filter(nil, nil, '{new,exp}')
305
+ assert_eq uncolor(sout), expected
306
+ end
307
+ it "[!r6g6a] supports negative filter by topic." do
308
+ expected = <<END
309
+ * Hello
310
+ - [pass] hello spec
311
+ END
312
+ sout = run_filter('Topic 832795', nil, nil, negative: true)
313
+ assert_eq uncolor(sout), expected
314
+ #
315
+ expected = <<END
316
+ * Hello
317
+ - [pass] hello spec
318
+ * Topic 832795
319
+ - [pass] spec example #5
320
+ END
321
+ sout = run_filter('{Integer,Float}', nil, nil, negative: true)
322
+ assert_eq uncolor(sout), expected
323
+ end
324
+ it "[!doozg] supports negative filter by spec." do
325
+ expected = <<END
326
+ * Topic 832795
327
+ * Integer
328
+ - [pass] spec example #1
329
+ - [pass] spec example #2
330
+ * Float
331
+ - [pass] spec example #3
332
+ - [pass] spec example #4
333
+ - [pass] spec example #5
334
+ END
335
+ sout = run_filter(nil, '*hello*', nil, negative: true)
336
+ assert_eq uncolor(sout), expected
337
+ #
338
+ expected = <<END
339
+ * Hello
340
+ - [pass] hello spec
341
+ END
342
+ sout = run_filter(nil, 'spec example #[1-5]', nil, negative: true)
343
+ assert_eq uncolor(sout), expected
344
+ end
345
+ it "[!ntv44] supports negative filter by tag name." do
346
+ expected = <<END
347
+ * Topic 832795
348
+ * Integer
349
+ - [pass] spec example #1
350
+ * Float
351
+ - [pass] spec example #3
352
+ - [pass] spec example #4
353
+ END
354
+ sout = run_filter(nil, nil, 'new', negative: true)
355
+ assert_eq uncolor(sout), expected
356
+ #
357
+ expected = <<END
358
+ * Hello
359
+ - [pass] hello spec
360
+ * Topic 832795
361
+ * Integer
362
+ - [pass] spec example #1
363
+ - [pass] spec example #2
364
+ END
365
+ sout = run_filter(nil, nil, 'exp', negative: true)
366
+ assert_eq uncolor(sout), expected
367
+ #
368
+ expected = <<END
369
+ * Topic 832795
370
+ * Integer
371
+ - [pass] spec example #1
372
+ END
373
+ sout = run_filter(nil, nil, '{exp,new}', negative: true)
374
+ assert_eq uncolor(sout), expected
375
+ end
376
+
377
+ end
378
+
379
+ end
@@ -0,0 +1,205 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ###
4
+ ### $Release: 1.0.0 $
5
+ ### $Copyright: copyright(c) 2011-2021 kuwata-lab.com all rights reserved $
6
+ ### $License: MIT License $
7
+ ###
8
+
9
+ require_relative './initialize'
10
+
11
+
12
+ class FixtureManager_TC < TC
13
+
14
+ class DummyReporter2 < Oktest::Reporter
15
+ def exit_spec(spec, depth, status, error, parent)
16
+ puts error.inspect if error
17
+ end
18
+ end
19
+
20
+ def run_all(dummy: false)
21
+ reporter = dummy ? DummyReporter2.new : Oktest::Reporter.new
22
+ sout, serr = capture do
23
+ Oktest::Runner.new(reporter).start()
24
+ end
25
+ assert_eq serr, ""
26
+ return sout
27
+ end
28
+
29
+ describe '#get_fixture_values()' do
30
+
31
+ it "[!v587k] resolves fixtures." do
32
+ Oktest.scope do
33
+ fixture(:x) { 10 }
34
+ topic "Parent" do
35
+ fixture(:y) { 20 }
36
+ topic "Child" do
37
+ fixture(:z) { 30 }
38
+ spec "fixture test" do |x, y, z|
39
+ p [x, y, z]
40
+ end
41
+ end
42
+ end
43
+ end
44
+ expected = "[10, 20, 30]\n"
45
+ sout = run_all()
46
+ assert_eq sout, expected
47
+ end
48
+
49
+ it "[!ja2ew] resolves 'this_spec' fixture name as description of current spec." do
50
+ Oktest.scope do
51
+ topic Integer do
52
+ spec "1+1 should be 2." do |this_spec|
53
+ puts "this_spec=#{this_spec.inspect}"
54
+ end
55
+ end
56
+ end
57
+ sout = run_all()
58
+ assert_eq sout, "this_spec=\"1+1 should be 2.\"\n"
59
+ end
60
+
61
+ it "[!w6ffs] resolves 'this_topic' fixture name as target objec of current topic." do
62
+ Oktest.scope do
63
+ topic Integer do
64
+ spec "1+1 should be 2." do |this_topic|
65
+ puts "this_topic=#{this_topic.inspect}"
66
+ end
67
+ end
68
+ end
69
+ sout = run_all()
70
+ assert_eq sout, "this_topic=Integer\n"
71
+ end
72
+
73
+ it "[!np4p9] raises error when loop exists in dependency." do
74
+ Oktest.scope do
75
+ topic "Parent" do
76
+ fixture(:a) {|b| nil }
77
+ fixture(:b) {|c| nil }
78
+ fixture(:c) do |d| nil end
79
+ fixture(:d) do |b| nil end
80
+ topic "Child" do
81
+ spec("spec#1") {|a| p a }
82
+ end
83
+ end
84
+ end
85
+ expected = "\#<Oktest::LoopedDependencyError: fixture dependency is looped: a->b=>c=>d=>b>\n"
86
+ sout = run_all(dummy: true)
87
+ assert_eq sout, expected
88
+ end
89
+
90
+ end
91
+
92
+ describe '#get_fixture_value()' do
93
+
94
+ it "[!2esaf] resolves fixture dependencies." do
95
+ Oktest.scope do
96
+ topic "Parent" do
97
+ fixture(:a) {|b, c| ["A"] + b + c }
98
+ fixture(:b) { ["B"] }
99
+ fixture(:c) do |d| ["C"] + d end
100
+ fixture(:d) do ["D"] end
101
+ topic "Child" do
102
+ spec "spec#1" do |a|
103
+ p a
104
+ end
105
+ spec("spec#2") {|a|
106
+ p a
107
+ }
108
+ end
109
+ end
110
+ end
111
+ expected = <<'END'
112
+ ["A", "B", "C", "D"]
113
+ ["A", "B", "C", "D"]
114
+ END
115
+ sout = run_all()
116
+ assert_eq sout, expected
117
+ end
118
+
119
+ it "[!4xghy] calls fixture block with context object as self." do
120
+ Oktest.scope do
121
+ topic "Parent" do
122
+ fixture(:x) { @x = 10; 20 }
123
+ spec("spec#1") {|x| p @x }
124
+ end
125
+ end
126
+ expected = <<'END'
127
+ 10
128
+ END
129
+ sout = run_all()
130
+ assert_eq sout, expected
131
+ end
132
+
133
+ it "[!8t3ul] caches fixture value to call fixture block only once per spec." do
134
+ Oktest.scope do
135
+ topic "Parent" do
136
+ fixture(:x) { puts "** x called."; 10 }
137
+ fixture(:y) {|x| x + 1 }
138
+ fixture(:z) {|x| x + 2 }
139
+ spec("spec#1") {|y, z|
140
+ p y
141
+ p z
142
+ }
143
+ end
144
+ end
145
+ expected = <<'END'
146
+ ** x called.
147
+ 11
148
+ 12
149
+ END
150
+ sout = run_all()
151
+ assert_eq sout, expected
152
+ end
153
+
154
+ it "[!4chb9] traverses parent topics if fixture not found in current topic." do
155
+ Oktest.scope do
156
+ topic 'Parent' do
157
+ fixture(:x) { 10 }
158
+ topic 'Child' do
159
+ fixture(:y) {|x| x + 1 }
160
+ topic 'GrandChild' do
161
+ fixture(:z) {|x| x + 2 }
162
+ spec("spec#1") do |x, y, z| p "x=#{x}, y=#{y}, z=#{z}" end
163
+ end
164
+ end
165
+ end
166
+ end
167
+ sout = run_all()
168
+ assert_eq sout, "\"x=10, y=11, z=12\"\n"
169
+ end
170
+
171
+ it "[!wt3qk] suports global scope." do
172
+ Oktest.global_scope do
173
+ fixture :gf1592 do {id: "gf1592"} end
174
+ fixture :gf6535 do |gf1592| {id: "gf6535", parent: gf1592} end
175
+ end
176
+ Oktest.global_scope do
177
+ fixture :gf8979 do |gf6535| {id: "gf8979", parent: gf6535} end
178
+ end
179
+ data = nil
180
+ Oktest.scope do
181
+ topic "global fixtures" do
182
+ spec "example" do |gf8979| data = gf8979 end
183
+ end
184
+ end
185
+ _ = run_all()
186
+ assert_eq data, {:id=>"gf8979", :parent=>{:id=>"gf6535", :parent=>{:id=>"gf1592"}}}
187
+ end
188
+
189
+ it "[!nr79z] raises error when fixture not found." do
190
+ Oktest.scope do
191
+ topic "Parent" do
192
+ fixture(:x) { 10 }
193
+ topic "Child" do
194
+ spec("spec#1") {|y| p y }
195
+ end
196
+ end
197
+ end
198
+ expected = "#<Oktest::FixtureNotFoundError: y: fixture not found. (spec: spec#1)>\n"
199
+ sout = run_all(dummy: true)
200
+ assert_eq sout, expected
201
+ end
202
+
203
+ end
204
+
205
+ end