oktest 1.3.1 → 1.5.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.
data/test/misc_test.rb CHANGED
@@ -1,61 +1,62 @@
1
1
  # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
2
3
 
3
4
  ###
4
- ### $Release: 1.3.1 $
5
+ ### $Release: 1.5.0 $
5
6
  ### $Copyright: copyright(c) 2011-2024 kuwata-lab.com all rights reserved $
6
7
  ### $License: MIT License $
7
8
  ###
8
9
 
9
- require_relative './initialize'
10
+ require_relative './init'
10
11
 
11
12
 
12
- class Misc_TC < TC
13
+ class Misc__Test
14
+ extend NanoTest
13
15
 
14
- def setup()
15
- @_auto_run = Oktest::Config.auto_run
16
+ def self.test_subject(desc, &b)
17
+ auto_run = Oktest::Config.auto_run
16
18
  Oktest::Config.auto_run = true
17
- end
18
-
19
- def teardown()
20
- Oktest::Config.auto_run = @_auto_run
19
+ super
20
+ ensure
21
+ Oktest::Config.auto_run = auto_run
21
22
  Oktest::THE_GLOBAL_SCOPE.clear_children()
22
23
  end
23
24
 
24
- describe 'Oktest.auto_run?()' do
25
- it "[!7vm4d] returns false if error raised when loading test scripts." do
25
+ test_target 'Oktest.auto_run?()' do
26
+ test_subject "[!7vm4d] returns false if error raised when loading test scripts." do
26
27
  Oktest.scope do
27
28
  end
28
29
  begin
29
30
  1/0
30
31
  rescue => exc
31
- assert_eq Oktest.auto_run?, false
32
+ test_eq? Oktest.auto_run?, false
32
33
  end
33
- assert exc != nil, "exception not raised"
34
+ test_ok? exc != nil, msg: "exception not raised"
34
35
  end
35
- it "[!oae85] returns true if exit() called." do
36
+ test_subject "[!oae85] returns true if exit() called." do
36
37
  Oktest.scope do
37
38
  end
38
39
  #
39
40
  begin
40
41
  exit(0)
41
42
  rescue SystemExit => exc
42
- assert_eq Oktest.auto_run?, true
43
+ test_eq? Oktest.auto_run?, true
43
44
  end
44
- assert exc != nil, "exception not raised"
45
+ test_ok? exc != nil, msg: "exception not raised"
45
46
  end
46
- it "[!rg5aw] returns false if Oktest.scope() never been called." do
47
- assert_eq Oktest::THE_GLOBAL_SCOPE.has_child?, false
48
- assert_eq Oktest.auto_run?, false
47
+ test_subject "[!rg5aw] returns false if Oktest.scope() never been called." do
48
+ test_eq? Oktest::THE_GLOBAL_SCOPE.has_child?, false
49
+ test_eq? Oktest.auto_run?, false
49
50
  end
50
- it "[!0j3ek] returns true if Config.auto_run is enabled." do
51
+ test_subject "[!0j3ek] returns true if Config.auto_run is enabled." do
51
52
  Oktest.scope do
52
53
  end
53
54
  bkup = Oktest::Config.auto_run
54
55
  begin
55
56
  Oktest::Config.auto_run = true
56
- assert_eq Oktest.auto_run?, true
57
+ test_eq? Oktest.auto_run?, true
57
58
  Oktest::Config.auto_run = false
58
- assert_eq Oktest.auto_run?, false
59
+ test_eq? Oktest.auto_run?, false
59
60
  ensure
60
61
  Oktest::Config.auto_run = bkup
61
62
  end
@@ -65,15 +66,16 @@ class Misc_TC < TC
65
66
  end
66
67
 
67
68
 
68
- class Color_TC < TC
69
+ class Color__Test
70
+ extend NanoTest
69
71
 
70
- describe '.status()' do
71
- it "[!yev5y] returns string containing color escape sequence." do
72
- assert_eq Oktest::Color.status(:PASS , "Pass" ), "\e[0;36mPass\e[0m"
73
- assert_eq Oktest::Color.status(:FAIL , "Fail" ), "\e[0;31mFail\e[0m"
74
- assert_eq Oktest::Color.status(:ERROR, "Error"), "\e[1;31mError\e[0m"
75
- assert_eq Oktest::Color.status(:SKIP , "Skip" ), "\e[0;33mSkip\e[0m"
76
- assert_eq Oktest::Color.status(:TODO , "Todo" ), "\e[0;33mTodo\e[0m"
72
+ test_target 'Oktest::Color.status()' do
73
+ test_subject "[!yev5y] returns string containing color escape sequence." do
74
+ test_eq? Oktest::Color.status(:PASS , "Pass" ), "\e[0;36mPass\e[0m"
75
+ test_eq? Oktest::Color.status(:FAIL , "Fail" ), "\e[0;31mFail\e[0m"
76
+ test_eq? Oktest::Color.status(:ERROR, "Error"), "\e[1;31mError\e[0m"
77
+ test_eq? Oktest::Color.status(:SKIP , "Skip" ), "\e[0;33mSkip\e[0m"
78
+ test_eq? Oktest::Color.status(:TODO , "Todo" ), "\e[0;33mTodo\e[0m"
77
79
  end
78
80
  end
79
81
 
data/test/nanot.rb ADDED
@@ -0,0 +1,307 @@
1
+ # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
3
+
4
+
5
+ module NanoTest
6
+ module_function
7
+
8
+ def test_target(target, &b)
9
+ yield
10
+ end
11
+
12
+ def test_subject(subject, &b)
13
+ yield
14
+ print "."
15
+ $nantotest_subject_count += 1
16
+ end
17
+
18
+ $nantotest_subject_count = 0
19
+
20
+ class TestFailed < StandardError
21
+ end
22
+
23
+ def test_ok?(result, msg: nil)
24
+ return true if result
25
+ msg ||= "Test failed."
26
+ raise TestFailed, msg
27
+ end
28
+
29
+ def test_eq?(actual, expected, msg: nil)
30
+ return true if actual == expected
31
+ msg ||= "<ACTUAL> == <EXPECTED> : failed."
32
+ #s1 = " <ACTUAL>: #{actual.inspect}"
33
+ #s2 = " <EXPECTED>: #{expected.inspect}"
34
+ require 'pp' unless defined?(PP)
35
+ s1 = " <ACTUAL>: #{PP.pp(actual, String.new).chomp}"
36
+ s2 = " <EXPECTED>: #{PP.pp(expected, String.new).chomp}"
37
+ raise TestFailed, "#{msg}\n#{s1}\n#{s2}"
38
+ end
39
+
40
+ def test_match?(actual_str, expected_rexp, msg: nil)
41
+ return true if actual_str =~ expected_rexp
42
+ msg ||= "<ACTUAL> =~ <EXPECTED> : failed."
43
+ require 'pp' unless defined?(PP)
44
+ s1 = " <ACTUAL>: #{PP.pp(actual_str, String.new).chomp}"
45
+ s2 = " <EXPECTED>: #{PP.pp(expected_rexp, String.new).chomp}"
46
+ raise TestFailed, "#{msg}\n#{s1}\n#{s2}"
47
+ end
48
+
49
+ def test_exception?(exception_class, &b)
50
+ begin
51
+ yield
52
+ rescue exception_class => exc
53
+ return exc
54
+ else
55
+ raise TestFailed, "Expected #{exception_class.name} to be raised, but not."
56
+ end
57
+ end
58
+
59
+ def capture_output!(stdin="", tty: false, &b)
60
+ require 'stringio' unless defined?(StringIO)
61
+ bkup = [$stdin, $stdout, $stderr]
62
+ $stdin = StringIO.new(stdin)
63
+ $stdout = StringIO.new
64
+ $stderr = StringIO.new
65
+ if tty
66
+ def $stdin.tty? ; true; end
67
+ def $stdout.tty?; true; end
68
+ def $stderr.tty?; true; end
69
+ end
70
+ yield
71
+ stdout_output = $stdout.string
72
+ stderr_output = $stderr.string
73
+ return stdout_output, stderr_output
74
+ ensure
75
+ $stdin, $stdout, $stderr = bkup
76
+ end
77
+
78
+ end
79
+
80
+
81
+ at_exit {
82
+ if $nantotest_subject_count > 0
83
+ puts "\n"
84
+ puts "(#{$nantotest_subject_count} tests)"
85
+ end
86
+ }
87
+
88
+
89
+ if __FILE__ == $0
90
+
91
+ self.extend NanoTest
92
+
93
+ def do_test(desc, &b)
94
+ yield desc
95
+ end
96
+
97
+ ## test_target()
98
+ do_test "test_target() yields block." do |desc|
99
+ called = false
100
+ test_target 'example' do
101
+ called = true
102
+ end
103
+ called == true or fail "Failed: #{desc}"
104
+ end
105
+
106
+ ## test_subject()
107
+ do_test "test_subject() yields block." do |desc|
108
+ called = false
109
+ test_subject 'example' do
110
+ called = true
111
+ end
112
+ called == true or fail "Failed: #{desc}"
113
+ end
114
+
115
+ ## test_ok?()
116
+ do_test "test_ok?() raises nothing if arg is truthy." do |desc|
117
+ test_ok? (1+1) == 2
118
+ end
119
+ do_test "test_ok?() raises TestFailed if arg is falty." do |desc|
120
+ begin
121
+ test_ok? (1+1) == 3
122
+ rescue NanoTest::TestFailed => exc
123
+ expected = "Test failed."
124
+ exc.message == expected or fail "Failed: #{desc}"
125
+ else
126
+ fail "TestFailed should be raised but not: #{desc}"
127
+ end
128
+ end
129
+ do_test "test_ok?() accepts a message string." do |desc|
130
+ msg = "should be equal to 2"
131
+ begin
132
+ test_ok? (1+1) == 3, msg: msg
133
+ rescue NanoTest::TestFailed => exc
134
+ exc.message == msg or fail "Failed: #{desc}"
135
+ else
136
+ fail "TestFailed should be raised but not: #{desc}"
137
+ end
138
+ end
139
+
140
+ ## test_eq?()
141
+ do_test "test_eq?() raises nothing if args are equal." do |desc|
142
+ test_eq? "ABC", "ABC"
143
+ end
144
+ do_test "test_eq?() raises TestFailed if args are not equal." do |desc|
145
+ begin
146
+ test_eq? "ABC", "abc"
147
+ rescue NanoTest::TestFailed => exc
148
+ expected = "<ACTUAL> == <EXPECTED> : failed.\n"\
149
+ " <ACTUAL>: \"ABC\"\n"\
150
+ " <EXPECTED>: \"abc\""
151
+ exc.message == expected or fail "Failed: #{desc}"
152
+ else
153
+ fail "TestFailed should be raised but not: #{desc}"
154
+ end
155
+ end
156
+ do_test "test_eq?() reports actual and expected value in pretty print format." do |desc|
157
+ begin
158
+ actual1 = {
159
+ name: "Alice", email: "alice@gmail.com", gender: "F",
160
+ department: "Sales & Marketing",
161
+ }
162
+ expected1 = {
163
+ name: "Alice", email: "alice@gmail.org", gender: "F",
164
+ department: "Sales & Marketing",
165
+ }
166
+ test_eq? actual1, expected1
167
+ rescue NanoTest::TestFailed => exc
168
+ expected = <<'END'
169
+ <ACTUAL> == <EXPECTED> : failed.
170
+ <ACTUAL>: {:name=>"Alice",
171
+ :email=>"alice@gmail.com",
172
+ :gender=>"F",
173
+ :department=>"Sales & Marketing"}
174
+ <EXPECTED>: {:name=>"Alice",
175
+ :email=>"alice@gmail.org",
176
+ :gender=>"F",
177
+ :department=>"Sales & Marketing"}
178
+ END
179
+ expected = expected.chomp
180
+ exc.message == expected or fail "Failed: #{desc}"
181
+ else
182
+ fail "TestFailed should be raised but not: #{desc}"
183
+ end
184
+ end
185
+ do_test "test_eq?() accepts 'msg:' kwarg." do
186
+ begin
187
+ test_eq? "ABC", "abc", msg: "NOT EQUAL"
188
+ rescue NanoTest::TestFailed => exc
189
+ expected = "NOT EQUAL\n"\
190
+ " <ACTUAL>: \"ABC\"\n"\
191
+ " <EXPECTED>: \"abc\""
192
+ exc.message == expected or fail "Failed: #{desc}"
193
+ else
194
+ fail "TestFailed should be raised but not: #{desc}"
195
+ end
196
+ end
197
+
198
+ ## test_match?()
199
+ do_test "test_match?() raises nothing if str matched to regexp." do |desc|
200
+ test_match? "123", /^\d+$/
201
+ end
202
+ do_test "test_match?() raises TestFailed if str not matched to regexp." do |desc|
203
+ begin
204
+ test_match? "ABC", /^\d+$/
205
+ rescue NanoTest::TestFailed => exc
206
+ expected = "<ACTUAL> =~ <EXPECTED> : failed.\n"\
207
+ " <ACTUAL>: \"ABC\"\n"\
208
+ " <EXPECTED>: /^\\d+$/"
209
+ exc.message == expected or fail "Failed: #{desc}"
210
+ else
211
+ fail "TestFailed should be raised but not: #{desc}"
212
+ end
213
+ end
214
+ do_test "test_match?() reports str and regexp values in pretty print format." do |desc|
215
+ begin
216
+ test_match? "ABC\nDEF\nGHI\n", /^\d+$/
217
+ rescue NanoTest::TestFailed => exc
218
+ expected = <<'END'
219
+ <ACTUAL> =~ <EXPECTED> : failed.
220
+ <ACTUAL>: "ABC\n" + "DEF\n" + "GHI\n"
221
+ <EXPECTED>: /^\d+$/
222
+ END
223
+ expected = expected.chomp
224
+ exc.message == expected or fail "Failed: #{desc}"
225
+ else
226
+ fail "TestFailed should be raised but not: #{desc}"
227
+ end
228
+ end
229
+ do_test "test_match?() accepts 'msg:' kwarg." do
230
+ begin
231
+ test_match? "ABC", /^\d+$/, msg: "NOT MATCHED"
232
+ rescue NanoTest::TestFailed => exc
233
+ expected = "NOT MATCHED\n"\
234
+ " <ACTUAL>: \"ABC\"\n"\
235
+ " <EXPECTED>: /^\\d+$/"
236
+ exc.message == expected or fail "Failed: #{desc}"
237
+ else
238
+ fail "TestFailed should be raised but not: #{desc}"
239
+ end
240
+ end
241
+
242
+ ## test_exception?()
243
+ do_test "test_exception?() raises nothing if expected exception raised in block." do
244
+ test_exception? ZeroDivisionError do
245
+ 1 / 0
246
+ end
247
+ end
248
+ do_test "test_exception?() raises TestFailed if expected exception not raised in block." do
249
+ begin
250
+ test_exception? ZeroDivisionError do
251
+ 1.0 / 0.0
252
+ end
253
+ rescue NanoTest::TestFailed => exc
254
+ expected = "Expected ZeroDivisionError to be raised, but not."
255
+ exc.message == expected or fail "Failed: #{desc}"
256
+ end
257
+ end
258
+
259
+ ## capture_output!()
260
+ do_test "capture_output!() captures stdout and stderro." do |desc|
261
+ sout, serr = capture_output!() do
262
+ print "ABC"
263
+ $stderr.print "DEF"
264
+ end
265
+ sout == "ABC" or fail "Failed (sout): #{desc}"
266
+ serr == "DEF" or fail "Failed (serr): #{desc}"
267
+ end
268
+ do_test "capture_output!() accepts stdin data." do |desc|
269
+ data = nil
270
+ capture_output!("abc\n") do
271
+ data = $stdin.read()
272
+ end
273
+ data == "abc\n" or fail "Failed: #{desc}"
274
+ end
275
+ do_test "capture_output!() restores original stdin, stdout and stderr." do |desc|
276
+ io = [$stdin, $stdout, $stderr]
277
+ capture_output!() do
278
+ $stdin != io[0] or fail "Failed (stdin): #{desc}"
279
+ $stdout != io[1] or fail "Failed (stdout): #{desc}"
280
+ $stderr != io[2] or fail "Failed (stderr): #{desc}"
281
+ end
282
+ $stdin == io[0] or fail "Failed (stdin): #{desc}"
283
+ $stdout == io[1] or fail "Failed (stdout): #{desc}"
284
+ $stderr == io[2] or fail "Failed (stderr): #{desc}"
285
+ end
286
+ do_test "capture_output!() makes io objects to pseudo tty." do |desc|
287
+ capture_output!(tty: true) do
288
+ $stdin.tty? == true or fail "Failed (stdin): #{desc}"
289
+ $stdout.tty? == true or fail "Failed (stdout): #{desc}"
290
+ $stderr.tty? == true or fail "Failed (stderr): #{desc}"
291
+ end
292
+ capture_output!(tty: false) do
293
+ $stdin.tty? == false or fail "Failed (stdin): #{desc}"
294
+ $stdout.tty? == false or fail "Failed (stdout): #{desc}"
295
+ $stderr.tty? == false or fail "Failed (stderr): #{desc}"
296
+ end
297
+ capture_output!() do
298
+ $stdin.tty? == false or fail "Failed (stdin): #{desc}"
299
+ $stdout.tty? == false or fail "Failed (stdout): #{desc}"
300
+ $stderr.tty? == false or fail "Failed (stderr): #{desc}"
301
+ end
302
+ end
303
+
304
+ $nantotest_subject_count = 0
305
+ puts ""
306
+
307
+ end