easytest 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +18 -0
- data/lib/easytest/case.rb +2 -2
- data/lib/easytest/dsl.rb +10 -8
- data/lib/easytest/expectation.rb +10 -2
- data/lib/easytest/hook.rb +27 -0
- data/lib/easytest/matcher/have_attributes.rb +15 -0
- data/lib/easytest/matcher/satisfy.rb +13 -0
- data/lib/easytest/runner.rb +19 -1
- data/lib/easytest/version.rb +1 -1
- data/lib/easytest.rb +8 -1
- data/sig/easytest.rbs +21 -3
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1a0b3705ffc7039bfb7ac442d22bce3abf4f6cbb4a100b2f88a637b658d462e3
|
4
|
+
data.tar.gz: f578262b1621be8e67811a048c3ce0b6c1fb592a82c482a0a945d7b97e221f0c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe5a6d95407ebccecd912cc2ef7624f449e304662004db4b361be58ccd0ec0a758ed380c8af5efc6632a1172e7acac8e6361274274fa33d6daa0a835e592affb
|
7
|
+
data.tar.gz: 406ab02728e1d3b58fa18bae5122f5af0691a72c8aaa5568b4d3d572d750419aaea8af9e2b18b15ab03aa24097b75370d1c16bd5bc220af5956820fb399ada3d
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -77,6 +77,24 @@ $ easytest
|
|
77
77
|
|
78
78
|
The test now passes! 🎉
|
79
79
|
|
80
|
+
### Hooks
|
81
|
+
|
82
|
+
You can add hooks called `before` and `after` to each test case:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
before do
|
86
|
+
# set up something...
|
87
|
+
end
|
88
|
+
|
89
|
+
after do
|
90
|
+
# clean up something...
|
91
|
+
end
|
92
|
+
|
93
|
+
test "something" do
|
94
|
+
# test something...
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
80
98
|
### Skip
|
81
99
|
|
82
100
|
If you want to skip any cases, you can change `test` to `skip`:
|
data/lib/easytest/case.rb
CHANGED
@@ -9,9 +9,9 @@ module Easytest
|
|
9
9
|
alias skipped? skipped
|
10
10
|
alias only? only
|
11
11
|
|
12
|
-
def initialize(name:,
|
12
|
+
def initialize(name:, skipped: false, only: false, &block)
|
13
13
|
@name = name
|
14
|
-
@file =
|
14
|
+
@file = caller_locations(3, 1).first.absolute_path
|
15
15
|
@block = block
|
16
16
|
@skipped = skipped
|
17
17
|
@only = only
|
data/lib/easytest/dsl.rb
CHANGED
@@ -3,23 +3,25 @@ module Easytest
|
|
3
3
|
refine Kernel do
|
4
4
|
def test(name, &block)
|
5
5
|
Utils.raise_if_no_test_name(name, method: "test")
|
6
|
+
Easytest.add_case Case.new(name: name, &block)
|
7
|
+
end
|
8
|
+
|
9
|
+
def before(&block)
|
10
|
+
Easytest.add_hook Hook.new(type: :before, &block)
|
11
|
+
end
|
6
12
|
|
7
|
-
|
8
|
-
Easytest.
|
13
|
+
def after(&block)
|
14
|
+
Easytest.add_hook Hook.new(type: :after, &block)
|
9
15
|
end
|
10
16
|
|
11
17
|
def skip(name, &block)
|
12
18
|
Utils.raise_if_no_test_name(name, method: "skip")
|
13
|
-
|
14
|
-
file = caller_locations(1, 1).first.absolute_path
|
15
|
-
Easytest.add_case Case.new(name: name, file: file, skipped: true, &block)
|
19
|
+
Easytest.add_case Case.new(name: name, skipped: true, &block)
|
16
20
|
end
|
17
21
|
|
18
22
|
def only(name, &block)
|
19
23
|
Utils.raise_if_no_test_name(name, method: "only")
|
20
|
-
|
21
|
-
file = caller_locations(1, 1).first.absolute_path
|
22
|
-
Easytest.add_case Case.new(name: name, file: file, only: true, &block)
|
24
|
+
Easytest.add_case Case.new(name: name, only: true, &block)
|
23
25
|
end
|
24
26
|
|
25
27
|
def expect(actual = nil, &block)
|
data/lib/easytest/expectation.rb
CHANGED
@@ -49,8 +49,16 @@ module Easytest
|
|
49
49
|
Matcher::Match.new(actual: actual, expected: expected, negate: negate).match!
|
50
50
|
end
|
51
51
|
|
52
|
-
def to_contain_exactly(*
|
53
|
-
Matcher::ContainExactly.new(actual: actual, expected:
|
52
|
+
def to_contain_exactly(*expected)
|
53
|
+
Matcher::ContainExactly.new(actual: actual, expected: expected, negate: negate).match!
|
54
|
+
end
|
55
|
+
|
56
|
+
def to_have_attributes(**expected)
|
57
|
+
Matcher::HaveAttributes.new(actual: actual, expected: expected, negate: negate).match!
|
58
|
+
end
|
59
|
+
|
60
|
+
def to_satisfy(&expected)
|
61
|
+
Matcher::Satisfy.new(actual: actual, expected: expected, negate: negate).match!
|
54
62
|
end
|
55
63
|
|
56
64
|
def to_raise(expected, with_message = nil)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Easytest
|
2
|
+
class Hook
|
3
|
+
attr_reader :file
|
4
|
+
attr_reader :type
|
5
|
+
attr_reader :block
|
6
|
+
|
7
|
+
def initialize(type:, &block)
|
8
|
+
raise ArgumentError, "" unless [:before, :after].include?(type)
|
9
|
+
|
10
|
+
@file = caller_locations(3, 1).first.absolute_path
|
11
|
+
@type = type
|
12
|
+
@block = block
|
13
|
+
end
|
14
|
+
|
15
|
+
def run(test_case)
|
16
|
+
block.call(test_case)
|
17
|
+
end
|
18
|
+
|
19
|
+
def before?
|
20
|
+
type == :before
|
21
|
+
end
|
22
|
+
|
23
|
+
def after?
|
24
|
+
type == :after
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/easytest/runner.rb
CHANGED
@@ -18,10 +18,15 @@ module Easytest
|
|
18
18
|
|
19
19
|
def run
|
20
20
|
include_only_case = cases.any?(&:only?)
|
21
|
+
hooks_by_file = hooks.group_by(&:file)
|
21
22
|
|
22
23
|
cases.group_by(&:file).each do |file, cases_per_file|
|
23
24
|
self.file_count += 1
|
24
25
|
|
26
|
+
hooks = hooks_by_file.fetch(file, [])
|
27
|
+
before_hooks = hooks.filter(&:before?)
|
28
|
+
after_hooks = hooks.filter(&:after?)
|
29
|
+
|
25
30
|
reports = []
|
26
31
|
|
27
32
|
cases_per_file.each do |c|
|
@@ -29,7 +34,12 @@ module Easytest
|
|
29
34
|
c.skip!
|
30
35
|
end
|
31
36
|
|
32
|
-
|
37
|
+
begin
|
38
|
+
before_hooks.each { |hook| hook.run(c) }
|
39
|
+
result, report = c.run
|
40
|
+
ensure
|
41
|
+
after_hooks.each { |hook| hook.run(c) }
|
42
|
+
end
|
33
43
|
|
34
44
|
case result
|
35
45
|
when :passed
|
@@ -82,6 +92,14 @@ module Easytest
|
|
82
92
|
cases << new_case
|
83
93
|
end
|
84
94
|
|
95
|
+
def hooks
|
96
|
+
@hooks ||= []
|
97
|
+
end
|
98
|
+
|
99
|
+
def add_hook(hook)
|
100
|
+
hooks << hook
|
101
|
+
end
|
102
|
+
|
85
103
|
private
|
86
104
|
|
87
105
|
def total_count
|
data/lib/easytest/version.rb
CHANGED
data/lib/easytest.rb
CHANGED
@@ -7,6 +7,7 @@ require_relative "easytest/case"
|
|
7
7
|
require_relative "easytest/dsl"
|
8
8
|
require_relative "easytest/errors"
|
9
9
|
require_relative "easytest/expectation"
|
10
|
+
require_relative "easytest/hook"
|
10
11
|
require_relative "easytest/reporter"
|
11
12
|
require_relative "easytest/runner"
|
12
13
|
require_relative "easytest/utils"
|
@@ -19,6 +20,7 @@ require_relative "easytest/matcher/contain_exactly"
|
|
19
20
|
require_relative "easytest/matcher/empty"
|
20
21
|
require_relative "easytest/matcher/equal"
|
21
22
|
require_relative "easytest/matcher/false"
|
23
|
+
require_relative "easytest/matcher/have_attributes"
|
22
24
|
require_relative "easytest/matcher/include"
|
23
25
|
require_relative "easytest/matcher/instance_of"
|
24
26
|
require_relative "easytest/matcher/kind_of"
|
@@ -26,6 +28,7 @@ require_relative "easytest/matcher/match"
|
|
26
28
|
require_relative "easytest/matcher/nil"
|
27
29
|
require_relative "easytest/matcher/raise"
|
28
30
|
require_relative "easytest/matcher/raise_nothing"
|
31
|
+
require_relative "easytest/matcher/satisfy"
|
29
32
|
require_relative "easytest/matcher/true"
|
30
33
|
|
31
34
|
module Easytest
|
@@ -34,7 +37,11 @@ module Easytest
|
|
34
37
|
end
|
35
38
|
|
36
39
|
def self.add_case(new_case)
|
37
|
-
@runner.
|
40
|
+
@runner.add_case(new_case)
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.add_hook(hook)
|
44
|
+
@runner.add_hook(hook)
|
38
45
|
end
|
39
46
|
|
40
47
|
def self.run
|
data/sig/easytest.rbs
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# An easy testing framework.
|
2
2
|
module Easytest
|
3
3
|
# The Easytest version.
|
4
4
|
VERSION: String
|
@@ -7,7 +7,7 @@ module Easytest
|
|
7
7
|
module DSL
|
8
8
|
end
|
9
9
|
|
10
|
-
#
|
10
|
+
# An expectation in test cases.
|
11
11
|
class Expectation
|
12
12
|
# Negate an expectation.
|
13
13
|
def not: () -> instance
|
@@ -47,7 +47,13 @@ module Easytest
|
|
47
47
|
def to_match: (untyped expected) -> void
|
48
48
|
|
49
49
|
# Expect to contain all the given items regardless of order.
|
50
|
-
def to_contain_exactly: (*untyped
|
50
|
+
def to_contain_exactly: (*untyped expected) -> void
|
51
|
+
|
52
|
+
# Expect to have the given attributes.
|
53
|
+
def to_have_attributes: (**untyped expected) -> void
|
54
|
+
|
55
|
+
# Expect to satisfy a condition that the given block returns `true`.
|
56
|
+
def to_satisfy: () { (untyped actual) -> bool } -> void
|
51
57
|
|
52
58
|
# Expect to raise the given exception or an exception with the given message.
|
53
59
|
def to_raise: (Class expected, ?(String | Regexp) with_message) -> void
|
@@ -56,12 +62,24 @@ module Easytest
|
|
56
62
|
# Expect to raise nothing.
|
57
63
|
def to_raise_nothing: () -> void
|
58
64
|
end
|
65
|
+
|
66
|
+
# A test case.
|
67
|
+
class Case
|
68
|
+
# Return a test case name.
|
69
|
+
def name: () -> String
|
70
|
+
end
|
59
71
|
end
|
60
72
|
|
61
73
|
module Kernel
|
62
74
|
# TODO: Can we avoid `RBS::DuplicatedMethodDefinitionError` "::Kernel#test has duplicated definitions"?
|
63
75
|
# def test: (String name) ?{ () -> void } -> void
|
64
76
|
|
77
|
+
# Run the given block before each test case.
|
78
|
+
def before: () { (Easytest::Case case) -> void } -> void
|
79
|
+
|
80
|
+
# Run the given block after each test case.
|
81
|
+
def after: () { (Easytest::Case case) -> void } -> void
|
82
|
+
|
65
83
|
# Skip the given test case.
|
66
84
|
def skip: (String name) { () -> void } -> void
|
67
85
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easytest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Masafumi Koba
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-10-
|
11
|
+
date: 2022-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rainbow
|
@@ -42,6 +42,7 @@ files:
|
|
42
42
|
- lib/easytest/dsl.rb
|
43
43
|
- lib/easytest/errors.rb
|
44
44
|
- lib/easytest/expectation.rb
|
45
|
+
- lib/easytest/hook.rb
|
45
46
|
- lib/easytest/matcher/base.rb
|
46
47
|
- lib/easytest/matcher/be.rb
|
47
48
|
- lib/easytest/matcher/be_a.rb
|
@@ -49,6 +50,7 @@ files:
|
|
49
50
|
- lib/easytest/matcher/empty.rb
|
50
51
|
- lib/easytest/matcher/equal.rb
|
51
52
|
- lib/easytest/matcher/false.rb
|
53
|
+
- lib/easytest/matcher/have_attributes.rb
|
52
54
|
- lib/easytest/matcher/include.rb
|
53
55
|
- lib/easytest/matcher/instance_of.rb
|
54
56
|
- lib/easytest/matcher/kind_of.rb
|
@@ -56,6 +58,7 @@ files:
|
|
56
58
|
- lib/easytest/matcher/nil.rb
|
57
59
|
- lib/easytest/matcher/raise.rb
|
58
60
|
- lib/easytest/matcher/raise_nothing.rb
|
61
|
+
- lib/easytest/matcher/satisfy.rb
|
59
62
|
- lib/easytest/matcher/true.rb
|
60
63
|
- lib/easytest/reporter.rb
|
61
64
|
- lib/easytest/runner.rb
|