oktest 1.3.1 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7e79b6abcb74066a35308ed22341f6fff30b2c9a5bd485e2850c5428db9843f1
4
- data.tar.gz: a2fef95c6c2207b9732a31bd25b61d3b43389e84f55f55399e465511ce55ea4a
3
+ metadata.gz: 71a8ad713c8536b57d4ed59541ea7a734da36187f502566aefee6479b3858e5f
4
+ data.tar.gz: e55ae061182626cab3b75559a0353d13fdbe5042f119a035474e6c28be311798
5
5
  SHA512:
6
- metadata.gz: 60e20c0564717185d9291ea5beca7bf09a55e924c259847660a5519831124213db4ddb8945711bc21f7ea6bae69c8e24f089fc9e15e1aab6047dc50fcfe46bf8
7
- data.tar.gz: fef79a96010440ea673342ec1a76bc313d88b0ed1f8d400a7832ddfbb23157b08addd96aa84ae43b523cfd96a6fa9aadc4548435d34705b67dae507d9bd6e97c
6
+ metadata.gz: 579a24fb8c2847bbd53805367d36176cd09fbeacc2b5690212589108517ba19f809fea79a6bfb2b07e87ff83925ef8ea9aa18a0eb420a775683f1876bf60f3e8
7
+ data.tar.gz: 0a3f94d919a328d8d8bfdc19da5e512e2896252df6dba80d2775fc5fedaed245b1465553c56541c110eb79cd2b68dd3cb5aea0685fe89e7b555b02efa7a2fcde
data/README.md CHANGED
@@ -77,7 +77,12 @@ Oktest.rb requires Ruby 2.4 or later.
77
77
  * <a href="#fixture-keyword-argument"><code>fixture:</code> keyword argument</a>
78
78
  * <a href="#global-scope">Global Scope</a>
79
79
  * <a href="#helpers">Helpers</a>
80
+ * <a href="#capture_stdio"><code>capture_stdio()</code></a>
80
81
  * <a href="#capture_sio"><code>capture_sio()</code></a>
82
+ * <a href="#capture_stdout"><code>capture_stdout()</code></a>
83
+ * <a href="#capture_stderr"><code>capture_stderr()</code></a>
84
+ * <a href="#capture_command"><code>capture_command()</code></a>
85
+ * <a href="#capture_command-1"><code>capture_command!()</code></a>
81
86
  * <a href="#dummy_file"><code>dummy_file()</code></a>
82
87
  * <a href="#dummy_dir"><code>dummy_dir()</code></a>
83
88
  * <a href="#dummy_values"><code>dummy_values()</code></a>
@@ -91,6 +96,8 @@ Oktest.rb requires Ruby 2.4 or later.
91
96
  * <a href="#complex-example">Complex Example</a>
92
97
  * <a href="#helper-methods-for-json-matcher">Helper Methods for JSON Matcher</a>
93
98
  * <a href="#tips">Tips</a>
99
+ * <a href="#oktesttopic"><code>Oktest.topic()</code></a>
100
+ * <a href="#topic-target">Topic target</a>
94
101
  * <a href="#ok--in-minitest"><code>ok {}</code> in MiniTest</a>
95
102
  * <a href="#testing-rack-application">Testing Rack Application</a>
96
103
  * <a href="#environment-variale-oktest_rb">Environment Variale <code>$OKTEST_RB</code></a>
@@ -823,7 +830,7 @@ ok {exc.class} == NoMethodError
823
830
  ok {exc.message} == "undefined method `len' for \"abc\":String"
824
831
 
825
832
  ## assert that procedure does NOT raise any exception
826
- ok {pr}.raise_nothing? # (>= Oktest 1.3)
833
+ ok {pr}.raise_nothing? # (Oktest >= 1.4)
827
834
  ok {pr}.NOT.raise? # no exception class nor error message
828
835
  not_ok {pr}.raise? # same as above
829
836
 
@@ -1303,9 +1310,9 @@ end
1303
1310
  ## Helpers
1304
1311
 
1305
1312
 
1306
- ### `capture_sio()`
1313
+ ### `capture_stdio()`
1307
1314
 
1308
- `capture_sio()` captures standard I/O.
1315
+ `capture_stdio()` captures standard I/O.
1309
1316
 
1310
1317
  test/example31_test.rb:
1311
1318
 
@@ -1318,7 +1325,7 @@ Oktest.scope do
1318
1325
 
1319
1326
  spec "example spec" do
1320
1327
  data = nil
1321
- sout, serr = capture_sio("blabla") do # !!!!!
1328
+ sout, serr = capture_stdio("blabla") do # !!!!!
1322
1329
  data = $stdin.read() # read from stdin
1323
1330
  puts "fooo" # write into stdout
1324
1331
  $stderr.puts "baaa" # write into stderr
@@ -1333,17 +1340,113 @@ Oktest.scope do
1333
1340
  end
1334
1341
  ```
1335
1342
 
1336
- * The first argument of `capture_sio()` represents data from `$stdin`.
1337
- If it is not necessary, you can omit it like `caputre_sio() do ... end`.
1343
+ * The first argument of `capture_stdio()` represents data from `$stdin`.
1344
+ If it is not necessary, you can omit it like `caputre_stdio() do ... end`.
1338
1345
  * If you need `$stdin.tty? == true` and `$stdout.tty? == true`,
1339
- call `capture_sio(tty: true) do ... end`.
1346
+ call `capture_stdio(tty: true) do ... end`.
1347
+ * Available on Oktest >= 1.4.
1348
+
1349
+
1350
+ ### `capture_sio()`
1351
+
1352
+ `capture_sio()` is an alias of `capture_stdio()`.
1353
+ This is provided for backward compatibility.
1354
+
1355
+ (`capture_stdio()` is provided as `capture_sio()` on Oktest < 1.4,
1356
+ and `capture_sio()` is renamed to `capture_stdio()` since Oktest 1.4.)
1357
+
1358
+
1359
+ ### `capture_stdout()`
1360
+
1361
+ `capture_stdout()` captures output of $stdout.
1362
+ If output of $stderr is not empty, assertion error will be raised.
1363
+
1364
+ `capture_stdout()` is almost same as the following.
1365
+
1366
+ ```ruby
1367
+ def capture_stdout(*args, **kwargs, &b)
1368
+ sout, serr = capture_stdio(*args, **kwargs, &b)
1369
+ ok {serr} == ""
1370
+ return sout
1371
+ end
1372
+ ```
1373
+
1374
+ * Available on Oktest >= 1.4.
1375
+
1376
+
1377
+ ### `capture_stderr()`
1378
+
1379
+ `capture_stderr()` captures output of $stderr.
1380
+ If output of $stdout is not empty, assertion error will be raised.
1381
+
1382
+ `capture_stderr()` is almost same as the following.
1383
+
1384
+ ```ruby
1385
+ def capture_stderr(*args, **kwargs, &b)
1386
+ sout, serr = capture_stdio(*args, **kwargs, &b)
1387
+ ok {sout} == ""
1388
+ return serr
1389
+ end
1390
+ ```
1391
+
1392
+ * Available on Oktest >= 1.4.
1393
+
1394
+
1395
+ ### `capture_command()`
1396
+
1397
+ `capture_command()` executes a command and returns output of stdout and stderr.
1398
+
1399
+ test/example32_test.rb:
1400
+
1401
+ ```ruby
1402
+ require 'oktest'
1403
+
1404
+ Oktest.scope do
1405
+
1406
+ topic "Capturing" do
1407
+
1408
+ spec "example spec" do
1409
+ input = "AAA\nBBB\n"
1410
+ sout, serr = capture_command("cat -n", input) # !!!!!
1411
+ ok {sout} == " 1\tAAA\n 2\tBBB\n"
1412
+ ok {serr} == ""
1413
+ end
1414
+
1415
+ spec "skip if command not installed" do
1416
+ begin
1417
+ sout, serr = capture_command "foobar"
1418
+ rescue Errno::ENOENT
1419
+ skip_when true, "command `foobar` not installed."
1420
+ end
1421
+ end
1422
+
1423
+ end
1424
+
1425
+ end
1426
+ ```
1427
+
1428
+ * The second argument of `capture_command()` represents `$stdin` data.
1429
+ It is optional.
1430
+ * `capture_command()` raises RuntimeError if command failed.
1431
+ * `capture_command()` accepts error handler block. The block will be called only when command failed. If error handler block is specified, `capture_command()` doesn't raise RuntimeError even if command failed.
1432
+ * `capture_command()` raises `Errno::ENOENT` exception if command not found. This exception will not be handled by error handler block.
1433
+ * Keyword parameter `tty: true` is not available.
1434
+ * Available on Oktest >= 1.4.
1435
+
1436
+
1437
+ ### `capture_command!()`
1438
+
1439
+ `capture_command!()` is similar to `capture_command()` but not raise error
1440
+ when command failed.
1441
+
1442
+ * Available on Oktest >= 1.4.
1340
1443
 
1341
1444
 
1342
1445
  ### `dummy_file()`
1343
1446
 
1344
1447
  `dummy_file()` creates a dummy file temporarily.
1345
1448
 
1346
- test/example32_test.rb:
1449
+ test/example33_test.rb:
1347
1450
 
1348
1451
  ```ruby
1349
1452
  require 'oktest'
@@ -1383,7 +1486,7 @@ end
1383
1486
 
1384
1487
  `dummy_dir()` creates a dummy directory temporarily.
1385
1488
 
1386
- test/example33_test.rb:
1489
+ test/example34_test.rb:
1387
1490
 
1388
1491
  ```ruby
1389
1492
  require 'oktest'
@@ -1425,7 +1528,7 @@ end
1425
1528
 
1426
1529
  `dummy_values()` changes hash values temporarily.
1427
1530
 
1428
- test/example34_test.rb:
1531
+ test/example35_test.rb:
1429
1532
 
1430
1533
  ```ruby
1431
1534
  require 'oktest'
@@ -1477,7 +1580,7 @@ end
1477
1580
 
1478
1581
  `dummy_attrs()` changes object attribute values temporarily.
1479
1582
 
1480
- test/example35_test.rb:
1583
+ test/example36_test.rb:
1481
1584
 
1482
1585
  ```ruby
1483
1586
  require 'oktest'
@@ -1532,7 +1635,7 @@ end
1532
1635
 
1533
1636
  `dummy_ivars()` changes instance variables in object with dummy values temporarily.
1534
1637
 
1535
- test/example36_test.rb:
1638
+ test/example37_test.rb:
1536
1639
 
1537
1640
  ```ruby
1538
1641
  require 'oktest'
@@ -1589,7 +1692,7 @@ end
1589
1692
  See [Benry::Recorder README](https://github.com/kwatch/benry-ruby/blob/ruby/benry-recorder/README.md)
1590
1693
  for detals.
1591
1694
 
1592
- test/example37_test.rb:
1695
+ test/example38_test.rb:
1593
1696
 
1594
1697
  ```ruby
1595
1698
  require 'oktest'
@@ -1803,8 +1906,8 @@ Oktest.scope do
1803
1906
  "id": 1000..9999, # range object
1804
1907
  "age": Integer, # class object
1805
1908
  "email": /^\w+@example\.com$/, # regexp
1806
- "gender": Set.new(["M", "F"]), # Set object ("M" or "F")
1807
- "deleted": Set.new([true, false]), # boolean (true or false)
1909
+ "gender": Set["M", "F"], # Set object ("M" or "F")
1910
+ "deleted": Set[true, false], # boolean (true or false)
1808
1911
  "tags": [/^\w+$/].each, # Enumerator object (!= Array obj)
1809
1912
  "twitter?": /^@\w+$/, # key 'xxx?' means optional value
1810
1913
  }
@@ -1879,7 +1982,7 @@ Oktest.scope do
1879
1982
  {
1880
1983
  "team": String,
1881
1984
  "members": [
1882
- {"id": 1000..9999, "name": String, "gender": Set.new(["M", "F"])}
1985
+ {"id": 1000..9999, "name": String, "gender": Set["M", "F"]}
1883
1986
  ].each, # Enumerator object (!= Array obj)
1884
1987
  "leader?": String, # key 'xxx?' means optional value
1885
1988
  }
@@ -1960,7 +2063,7 @@ ok {JSON({"val": 99 })} === {"val": 1..100} # implies Integer value
1960
2063
 
1961
2064
  Oktest.rb provides some helper methods and objects:
1962
2065
 
1963
- * `Enum(x, y, z)` is almost same as `Set.new([x, y, z])`.
2066
+ * `Enum(x, y, z)` is almost same as `Set[x, y, z]`.
1964
2067
  * `Bool()` is same as `Enum(true, false)`.
1965
2068
  * `Length(3)` matches to length 3, and `Length(1..3)` matches to length 1..3.
1966
2069
 
@@ -1970,7 +2073,7 @@ test/example44_test.rb:
1970
2073
  ```ruby
1971
2074
  actual = {"gender": "M", "deleted": false, "code": "ABCD1234"}
1972
2075
  ok {JSON(actual)} == {
1973
- "gender": Enum("M", "F"), # same as Set.new(["M", "F"])
2076
+ "gender": Enum("M", "F"), # same as Set["M", "F"]
1974
2077
  "deleted": Bool(), # same as Enum(true, false)
1975
2078
  "code": Length(6..10), # code length should be 6..10
1976
2079
  }
@@ -1981,6 +2084,41 @@ test/example44_test.rb:
1981
2084
  ## Tips
1982
2085
 
1983
2086
 
2087
+ ### `Oktest.topic()`
2088
+
2089
+ If you want to reduce nested block depth, `Oktest.topic()` will help you.
2090
+
2091
+ ```ruby
2092
+ ## This...
2093
+ Oktest.topic HelloClass do
2094
+ ...
2095
+ end
2096
+
2097
+ ## ...is equivarent to...
2098
+ Oktest.scope do
2099
+ Oktest.toic HelloClass do
2100
+ ...
2101
+ end
2102
+ end
2103
+ ```
2104
+
2105
+ (Since Oktest >= 1.5)
2106
+
2107
+
2108
+ ### Topic target
2109
+
2110
+ `topic()` passes an target argument to the block.
2111
+
2112
+ ```ruby
2113
+ topic '/api/orders/{id}' do |urlpath|
2114
+ p urlpath #=> "/api/orders/{id}"
2115
+ ...
2116
+ end
2117
+ ```
2118
+
2119
+ (Since Oktest >= 1.5)
2120
+
2121
+
1984
2122
  ### `ok {}` in MiniTest
1985
2123
 
1986
2124
  If you want to use `ok {actual} == expected` style assertion in MiniTest,
data/Rakefile.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  ###
4
- ### $Release: 1.3.1 $
4
+ ### $Release: 1.5.0 $
5
5
  ### $Copyright: copyright(c) 2011-2024 kuwata-lab.com all rights reserved $
6
6
  ### $License: MIT License $
7
7
  ###
data/benchmark/run_all.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # coding: utf-8
2
+ # frozen_string_literal: true
2
3
 
3
4
  Dir.glob(File.join(File.dirname(__FILE__), '*_test.rb')).each do |x|
4
5
  #load x
data/bin/oktest CHANGED
@@ -1,3 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+ # frozen_string_literal: true
4
+
2
5
  require 'oktest'
3
6
  Oktest.main()