oktest 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +197 -22
- data/Rakefile.rb +5 -2
- data/benchmark/Rakefile.rb +4 -0
- data/lib/oktest.rb +170 -58
- data/oktest.gemspec +3 -3
- data/test/assertion_test.rb +1 -1
- data/test/filter_test.rb +1 -1
- data/test/fixture_test.rb +14 -1
- data/test/generator_test.rb +1 -1
- data/test/helper_test.rb +3 -3
- data/test/initialize.rb +1 -1
- data/test/mainapp_test.rb +59 -43
- data/test/matcher_test.rb +1 -1
- data/test/misc_test.rb +5 -5
- data/test/node_test.rb +27 -4
- data/test/reporter_test.rb +30 -31
- data/test/runner_test.rb +8 -17
- data/test/tc.rb +12 -0
- data/test/util_test.rb +71 -1
- data/test/utilhelper_test.rb +84 -0
- data/test/visitor_test.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 829638b6c9703b023e61adfdef426e7ddb206f2ba93775143b8ae39c86b0f381
|
4
|
+
data.tar.gz: f4d70f1272398f74b0abaad9bd0ea027ca557a8860c44d6f11f056a60d89fc4c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5669eea2d32e6d0ecdfdd263a50e9a740fd6e2da081bbc0e35d433796d242507341bccc53c2c6ca7cd5a26d0b55a7c8222da5fb15eca27945a1207222c4ee32d
|
7
|
+
data.tar.gz: 8cdce5238153c774972ac26bf6b389786b920a41c8d4d2069df03d17e80ebdd299be83a63d44f7840088482b3e05e35019fdf0804a57c1273e93cde14a9a547e
|
data/README.md
CHANGED
@@ -43,7 +43,7 @@ Oktest.scope do #
|
|
43
43
|
end #
|
44
44
|
```
|
45
45
|
|
46
|
-
Oktest.rb requires Ruby 2.
|
46
|
+
Oktest.rb requires Ruby 2.0 or later.
|
47
47
|
|
48
48
|
|
49
49
|
|
@@ -74,6 +74,7 @@ Oktest.rb requires Ruby 2.3 or later.
|
|
74
74
|
* <a href="#at_end-crean-up-handler"><code>at_end()</code>: Crean-up Handler</a>
|
75
75
|
* <a href="#named-fixtures">Named Fixtures</a>
|
76
76
|
* <a href="#fixture-injection">Fixture Injection</a>
|
77
|
+
* <a href="#fixture-keyword-argument"><code>fixture:</code> keyword argument</a>
|
77
78
|
* <a href="#global-scope">Global Scope</a>
|
78
79
|
* <a href="#helpers">Helpers</a>
|
79
80
|
* <a href="#capture_sio"><code>capture_sio()</code></a>
|
@@ -83,6 +84,7 @@ Oktest.rb requires Ruby 2.3 or later.
|
|
83
84
|
* <a href="#dummy_attrs"><code>dummy_attrs()</code></a>
|
84
85
|
* <a href="#dummy_ivars"><code>dummy_ivars()</code></a>
|
85
86
|
* <a href="#recorder"><code>recorder()</code></a>
|
87
|
+
* <a href="#partial_regexp"><code>partial_regexp()</code></a>
|
86
88
|
* <a href="#json-matcher">JSON Matcher</a>
|
87
89
|
* <a href="#simple-example">Simple Example</a>
|
88
90
|
* <a href="#nested-example">Nested Example</a>
|
@@ -91,6 +93,7 @@ Oktest.rb requires Ruby 2.3 or later.
|
|
91
93
|
* <a href="#tips">Tips</a>
|
92
94
|
* <a href="#ok--in-minitest"><code>ok {}</code> in MiniTest</a>
|
93
95
|
* <a href="#testing-rack-application">Testing Rack Application</a>
|
96
|
+
* <a href="#environment-variale-oktest_rb">Environment Variale <code>$OKTEST_RB</code></a>
|
94
97
|
* <a href="#traverser-class">Traverser Class</a>
|
95
98
|
* <a href="#benchmarks">Benchmarks</a>
|
96
99
|
* <a href="#--faster-option"><code>--faster</code> Option</a>
|
@@ -115,7 +118,7 @@ $ oktest --help
|
|
115
118
|
$ mkdir test
|
116
119
|
|
117
120
|
### create test script
|
118
|
-
$ oktest --
|
121
|
+
$ oktest --skeleton > test/example_test.rb
|
119
122
|
$ less test/example_test.rb
|
120
123
|
|
121
124
|
### run test script
|
@@ -1232,12 +1235,42 @@ end
|
|
1232
1235
|
-->
|
1233
1236
|
|
1234
1237
|
|
1238
|
+
### `fixture:` keyword argument
|
1239
|
+
|
1240
|
+
`scope()` takes `fixture:` keyword argument which overwrites fixture value.
|
1241
|
+
|
1242
|
+
test/example26_test.rb:
|
1243
|
+
|
1244
|
+
```ruby
|
1245
|
+
require 'oktest'
|
1246
|
+
|
1247
|
+
Oktest.scope do
|
1248
|
+
|
1249
|
+
fixture :user do |uname, uid: 101| # `uid` is keyword param
|
1250
|
+
{name: uname, id: uid}
|
1251
|
+
end
|
1252
|
+
|
1253
|
+
fixture :uname do
|
1254
|
+
"Alice"
|
1255
|
+
end
|
1256
|
+
|
1257
|
+
## keyword argument `fixture:` overwrites fixture values
|
1258
|
+
spec "example", fixture: {uname: "Bob", uid: 201} do # !!!!!
|
1259
|
+
|user|
|
1260
|
+
ok {user[:name]} == "Bob" # != "Alice"
|
1261
|
+
ok {user[:id]} == 201 # != 101
|
1262
|
+
end
|
1263
|
+
|
1264
|
+
end
|
1265
|
+
```
|
1266
|
+
|
1267
|
+
|
1235
1268
|
### Global Scope
|
1236
1269
|
|
1237
1270
|
It is a good idea to separate common fixtures into dedicated file.
|
1238
1271
|
In this case, use `Oktest.global_scope()` instead of `Oktest.scope()`.
|
1239
1272
|
|
1240
|
-
test/
|
1273
|
+
test/example27_test.rb:
|
1241
1274
|
|
1242
1275
|
```ruby
|
1243
1276
|
require 'oktest'
|
@@ -1611,6 +1644,124 @@ end
|
|
1611
1644
|
```
|
1612
1645
|
|
1613
1646
|
|
1647
|
+
### `partial_regexp()`
|
1648
|
+
|
1649
|
+
`partial_regexp()` can embed regexp pattern into string, and compile it into Regexp object. This is very useful to validate multiline string with regexp.
|
1650
|
+
|
1651
|
+
Assume that you are testing the following function `f1()`. It generates multiline string containing date and random string.
|
1652
|
+
|
1653
|
+
```ruby
|
1654
|
+
def f1()
|
1655
|
+
today = Date.today.to_s # ex: '2021-12-31'
|
1656
|
+
secret = Random.bytes(8).unpack('H*')[0] # ex: "cd0b260ac728eda5"
|
1657
|
+
return <<END
|
1658
|
+
* [config.date] #{today}
|
1659
|
+
* [config.secret] #{secret}
|
1660
|
+
END
|
1661
|
+
end
|
1662
|
+
```ruby
|
1663
|
+
|
1664
|
+
To test `f1()`, you may write the following test code.
|
1665
|
+
As you can see, expected regexp literal is complicated.
|
1666
|
+
|
1667
|
+
```ruby
|
1668
|
+
topic 'f1()' do
|
1669
|
+
spec "generates multiline string." do
|
1670
|
+
expected = /\A\* \[config\.date\] \d\d\d\d-\d\d-\d\d\n\* \[config\.secret\] [0-9a-f]+\n/
|
1671
|
+
ok {f1()} =~ expected
|
1672
|
+
end
|
1673
|
+
end
|
1674
|
+
```
|
1675
|
+
|
1676
|
+
[`x` option](https://ruby-doc.org/core-2.7.0/Regexp.html#class-Regexp-label-Free-Spacing+Mode+and+Comments) of regexp (such as `/.../x`) allows you to write regexp literal in multiline format.
|
1677
|
+
But you have to escape metachars (`*`, `.`, `[]`, and white space).
|
1678
|
+
|
1679
|
+
```ruby
|
1680
|
+
topic 'f1()' do
|
1681
|
+
spec "generates multiline string." do
|
1682
|
+
expected = /\A
|
1683
|
+
\*\ \[config\.date\]\ \ \ \d\d\d\d-\d\d-\d\d\n
|
1684
|
+
\*\ \[config\.secret\]\ [0-9a-f]+\n
|
1685
|
+
\z/x # !!!!!
|
1686
|
+
ok {f1()} =~ expected
|
1687
|
+
end
|
1688
|
+
end
|
1689
|
+
```
|
1690
|
+
|
1691
|
+
In such case, `partial_regexp()` is very useful. It compiles string into Regexp object.
|
1692
|
+
Using `partial_regexp()`, you can write expected regexp very easily.
|
1693
|
+
|
1694
|
+
```ruby
|
1695
|
+
topic 'f1()' do
|
1696
|
+
spec "generates multiline string." do
|
1697
|
+
# - Regexp can be in `{== ==}`.
|
1698
|
+
# - Other text part is escaped by `Regexp.escape()`.
|
1699
|
+
expected = partial_regexp <<'END' # !!!!!
|
1700
|
+
* [config.date] {== \d\d\d\d-\d\d-\d\d ==}
|
1701
|
+
* [config.secret] {== [0-9a-f]+ ==}
|
1702
|
+
END
|
1703
|
+
ok {f1()} =~ expected
|
1704
|
+
## above is equivarent to:
|
1705
|
+
#expected = /\A
|
1706
|
+
#\*\ \[config\.date\]\ \ \ \d\d\d\d-\d\d-\d\d\n
|
1707
|
+
#\*\ \[config\.secret\]\ [0-9a-f]+\n
|
1708
|
+
#\z/x # !!!!!
|
1709
|
+
#ok {f1()} =~ expected
|
1710
|
+
end
|
1711
|
+
end
|
1712
|
+
```
|
1713
|
+
|
1714
|
+
`partial_regexp()` takes 4 arguments.
|
1715
|
+
|
1716
|
+
```ruby
|
1717
|
+
def partial_regexp(pattern, begin_='\A', end_='\z', mark='{== ==}')
|
1718
|
+
```
|
1719
|
+
|
1720
|
+
`partial_regexp()` adds `\A` and `\z` automatically.
|
1721
|
+
If you want not to add them, pass empty string or nil as 2nd and 3rd argument, like this:
|
1722
|
+
|
1723
|
+
```ruby
|
1724
|
+
partial_regexp <<-'END', '', '' # !!!!!
|
1725
|
+
...
|
1726
|
+
END
|
1727
|
+
```
|
1728
|
+
|
1729
|
+
If you want to change embed mark, specify 4th argument, like this:
|
1730
|
+
|
1731
|
+
```ruby
|
1732
|
+
partial_regexp <<-'END', '\A', '\z', '%%(.*?)%%' # !!!!!
|
1733
|
+
* [config.date] %% \d\d\d\d-\d\d-\d\d %%
|
1734
|
+
* [config.secret] %% [0-9a-f]+ %%
|
1735
|
+
END
|
1736
|
+
```
|
1737
|
+
|
1738
|
+
Oktest.rb provides `partial_regexp!()`, too.
|
1739
|
+
Difference between `partial_regexp()` and `partial_regexp!()` is the result of `#inspect()`.
|
1740
|
+
This is imortant only when assertion failed and error message reported.
|
1741
|
+
You can use whichever you like.
|
1742
|
+
|
1743
|
+
```ruby
|
1744
|
+
r1 = partial_regexp <<-'END'
|
1745
|
+
* [config.date] {== \d\d\d\d-\d\d-\d\d ==}
|
1746
|
+
* [config.secret] {== [0-9a-f]+ ==}
|
1747
|
+
END
|
1748
|
+
p r1
|
1749
|
+
#=> /\A
|
1750
|
+
# \*\ \[config\.date\]\ \ \ \d\d\d\d-\d\d-\d\d\n
|
1751
|
+
# \*\ \[config\.secret\]\ [0-9a-f]+\n
|
1752
|
+
# \z/x
|
1753
|
+
|
1754
|
+
r2 = partial_regexp! <<-'END' # !!!!!
|
1755
|
+
* [config.date] {== \d\d\d\d-\d\d-\d\d ==}
|
1756
|
+
* [config.secret] {== [0-9a-f]+ ==}
|
1757
|
+
END
|
1758
|
+
p r2
|
1759
|
+
#=> partial_regexp(<<PREXP, '\A', '\z')
|
1760
|
+
# * [config.date] {== \d\d\d\d-\d\d-\d\d ==}
|
1761
|
+
# * [config.secret] {== [0-9a-f]+ ==}
|
1762
|
+
# PREXP
|
1763
|
+
```
|
1764
|
+
|
1614
1765
|
|
1615
1766
|
## JSON Matcher
|
1616
1767
|
|
@@ -1922,6 +2073,20 @@ end
|
|
1922
2073
|
```
|
1923
2074
|
|
1924
2075
|
|
2076
|
+
### Environment Variale `$OKTEST_RB`
|
2077
|
+
|
2078
|
+
You can set default command-line option to environment variale `$OKTEST_RB`.
|
2079
|
+
For examle, you can specify default reporting style with `$OKTEST_RB`.
|
2080
|
+
|
2081
|
+
```terminal
|
2082
|
+
### change default reporting style to plain-style.
|
2083
|
+
$ export OKTEST_RB="-s plain" # !!!!!
|
2084
|
+
|
2085
|
+
### run test script in plain-style reporting without '-s' option.
|
2086
|
+
$ ruby test/foo_test.rb
|
2087
|
+
```
|
2088
|
+
|
2089
|
+
|
1925
2090
|
### Traverser Class
|
1926
2091
|
|
1927
2092
|
Oktest.rb provides `Traverser` class which implements Visitor pattern.
|
@@ -2001,12 +2166,12 @@ Oktest.rb gem file contains benchmark script.
|
|
2001
2166
|
It shows that Oktest.rb runs more than three times faster than RSpec.
|
2002
2167
|
|
2003
2168
|
```terminal
|
2004
|
-
$ gem install oktest # ver 1.
|
2169
|
+
$ gem install oktest # ver 1.2.0
|
2005
2170
|
$ gem install rspec # ver 3.10.0
|
2006
2171
|
$ gem install minitest # ver 5.14.4
|
2007
2172
|
$ gem install test-unit # ver 3.4.4
|
2008
2173
|
|
2009
|
-
$ cp -pr $GEM_HOME/gems/oktest-1.
|
2174
|
+
$ cp -pr $GEM_HOME/gems/oktest-1.2.0/benchmark .
|
2010
2175
|
$ cd benchmark/
|
2011
2176
|
$ rake -T
|
2012
2177
|
$ ruby --version
|
@@ -2018,47 +2183,57 @@ $ rake benchmark:all
|
|
2018
2183
|
Example result:
|
2019
2184
|
|
2020
2185
|
```
|
2021
|
-
==================== oktest ====================
|
2186
|
+
==================== {{*oktest*}} ====================
|
2022
2187
|
oktest -sq run_all.rb
|
2023
2188
|
|
2024
|
-
## total:100000 (pass:100000, fail:0, error:0, skip:0, todo:0) in
|
2189
|
+
## total:100000 (pass:100000, fail:0, error:0, skip:0, todo:0) in 2.36s
|
2025
2190
|
|
2026
|
-
|
2191
|
+
{{*6.815 real*}} 6.511 user 0.257 sys
|
2027
2192
|
|
2028
|
-
==================== oktest:faster ====================
|
2193
|
+
==================== {{*oktest:faster*}} ====================
|
2029
2194
|
oktest -sq --faster run_all.rb
|
2030
2195
|
|
2031
|
-
## total:100000 (pass:100000, fail:0, error:0, skip:0, todo:0) in
|
2196
|
+
## total:100000 (pass:100000, fail:0, error:0, skip:0, todo:0) in 2.01s
|
2032
2197
|
|
2033
|
-
|
2198
|
+
{{*6.401 real*}} 6.123 user 0.240 sys
|
2034
2199
|
|
2035
|
-
==================== rspec ====================
|
2200
|
+
==================== {{*rspec*}} ====================
|
2036
2201
|
rspec run_all.rb | tail -4
|
2037
2202
|
|
2038
|
-
Finished in
|
2203
|
+
Finished in 15.27 seconds (files took 16.08 seconds to load)
|
2039
2204
|
100000 examples, 0 failures
|
2040
2205
|
|
2041
2206
|
|
2042
|
-
|
2207
|
+
{{*32.062 real*}} 27.778 user 4.383 sys
|
2043
2208
|
|
2044
|
-
==================== minitest ====================
|
2209
|
+
==================== {{*minitest*}} ====================
|
2045
2210
|
ruby run_all.rb | tail -4
|
2046
2211
|
|
2047
|
-
Finished in 5.
|
2212
|
+
Finished in 5.281425s, 18934.2838 runs/s, 37868.5677 assertions/s.
|
2048
2213
|
|
2049
|
-
100000 runs,
|
2214
|
+
100000 runs, 200000 assertions, 0 failures, 0 errors, 0 skips
|
2050
2215
|
|
2051
|
-
|
2216
|
+
{{*9.140 real*}} 8.657 user 0.705 sys
|
2052
2217
|
|
2053
|
-
==================== testunit ====================
|
2218
|
+
==================== {{*testunit*}} ====================
|
2054
2219
|
ruby run_all.rb | tail -5
|
2055
2220
|
-------------------------------------------------------------------------------
|
2056
|
-
100000 tests,
|
2221
|
+
100000 tests, 200000 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
|
2057
2222
|
100% passed
|
2058
2223
|
-------------------------------------------------------------------------------
|
2059
|
-
|
2224
|
+
7775.59 tests/s, 15551.18 assertions/s
|
2060
2225
|
|
2061
|
-
|
2226
|
+
{{*19.580 real*}} 19.020 user 0.885 sys
|
2227
|
+
```
|
2228
|
+
|
2229
|
+
Summary:
|
2230
|
+
|
2231
|
+
```
|
2232
|
+
Oktest: 6.815 real 6.511 user 0.257 sys
|
2233
|
+
Oktest (--fast): 6.401 real 6.123 user 0.240 sys
|
2234
|
+
RSpec: 32.062 real 27.778 user 4.383 sys
|
2235
|
+
MiniTest: 9.140 real 8.657 user 0.705 sys
|
2236
|
+
Test::Unit: 19.580 real 19.020 user 0.885 sys
|
2062
2237
|
```
|
2063
2238
|
|
2064
2239
|
|
data/Rakefile.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
|
3
3
|
###
|
4
|
-
### $Release: 1.
|
4
|
+
### $Release: 1.2.0 $
|
5
5
|
### $Copyright: copyright(c) 2011-2021 kuwata-lab.com all rights reserved $
|
6
6
|
### $License: MIT License $
|
7
7
|
###
|
@@ -37,6 +37,8 @@ How to release:
|
|
37
37
|
$ git add -p CHANGES.md
|
38
38
|
$ git commit -m "ruby: update 'CHANGES.md'"
|
39
39
|
$ git log -1
|
40
|
+
$ cid=$(git log -1 | awk 'NR==1{print $2}')
|
41
|
+
$ echo $cid
|
40
42
|
$ rake package RELEASE=#{rel}
|
41
43
|
$ rake package:extract # confirm files in gem file
|
42
44
|
$ pushd #{proj}-#{rel}/data; find . -type f; popd
|
@@ -48,7 +50,8 @@ How to release:
|
|
48
50
|
$ git push --tags
|
49
51
|
$ rake clean
|
50
52
|
$ git checkout ruby
|
51
|
-
$ git
|
53
|
+
$ git log -1 $cid
|
54
|
+
$ git cherry-pick $cid # cherry-pick update of CHANGES.md
|
52
55
|
END
|
53
56
|
end
|
54
57
|
|
data/benchmark/Rakefile.rb
CHANGED
@@ -29,6 +29,7 @@ Oktest.scope do
|
|
29
29
|
<% for k in 1..nspecs %>
|
30
30
|
spec "#<%= k %>: 1+1 should be 2" do
|
31
31
|
ok {1+1} == 2
|
32
|
+
ok {1+1} == 2
|
32
33
|
end
|
33
34
|
<% end %>
|
34
35
|
|
@@ -54,6 +55,7 @@ RSpec.describe "Example #<%= i %>" do
|
|
54
55
|
<% for k in 1..nspecs %>
|
55
56
|
it "#<%= k %>: 1+1 should be 2" do
|
56
57
|
expect(1+1).to eq 2
|
58
|
+
expect(1+1).to eq 2
|
57
59
|
end
|
58
60
|
<% end %>
|
59
61
|
|
@@ -80,6 +82,7 @@ describe "Example #<%= i %>" do
|
|
80
82
|
<% for k in 1..nspecs %>
|
81
83
|
it "#<%= k %>: 1+1 should be 2" do
|
82
84
|
assert_equal 2, 1+1
|
85
|
+
assert_equal 2, 1+1
|
83
86
|
end
|
84
87
|
<% end %>
|
85
88
|
|
@@ -107,6 +110,7 @@ class Example_<%= n %>_<%= i %>_TC < Test::Unit::TestCase
|
|
107
110
|
def test_<%= n %>_<%= i %>_<%= j %>_<%= k %>()
|
108
111
|
#assert 1+1 == 2
|
109
112
|
assert_equal 2, 1+1
|
113
|
+
assert_equal 2, 1+1
|
110
114
|
end
|
111
115
|
<% end %>
|
112
116
|
|