easytest 0.1.2 → 0.2.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 +4 -0
- data/README.md +1 -1
- data/lib/easytest/case.rb +16 -3
- data/lib/easytest/errors.rb +3 -1
- data/lib/easytest/expectation.rb +36 -13
- data/lib/easytest/matcher/base.rb +17 -7
- data/lib/easytest/matcher/be.rb +1 -8
- data/lib/easytest/matcher/be_a.rb +13 -0
- data/lib/easytest/matcher/equal.rb +1 -14
- data/lib/easytest/matcher/false.rb +17 -0
- data/lib/easytest/matcher/instance_of.rb +13 -0
- data/lib/easytest/matcher/kind_of.rb +13 -0
- data/lib/easytest/matcher/{be_nil.rb → nil.rb} +6 -6
- data/lib/easytest/matcher/not_raise.rb +27 -0
- data/lib/easytest/matcher/raise.rb +38 -0
- data/lib/easytest/matcher/true.rb +17 -0
- data/lib/easytest/reporter.rb +33 -6
- data/lib/easytest/version.rb +1 -1
- data/lib/easytest.rb +13 -4
- metadata +9 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea89da5a0f2aba7252ea69be567591284bfcc1f53aa089e4824eb54773881b0b
|
4
|
+
data.tar.gz: b9bb01188ce657b918cdffe80cb962fd4e3c011fa9a4556b832d49eeed8fef5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b1539eeef25d6343a411a05afbfcff6143d9f33798fb5f3d13dbf8f3d742ea79247ced5c5da1a994bc0d77b41787ec8c7477ed06b26664ef30987007291ad26
|
7
|
+
data.tar.gz: c5e5ebe81739839785eab40d24a3d4e529d7c33f0d2ac5fd5cc20bf2984b65849d6b99f18b46848f6d17d362dfdc95fb16ae7d0f3403b77913a6b182b0f1fc9a
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
data/lib/easytest/case.rb
CHANGED
@@ -15,10 +15,23 @@ module Easytest
|
|
15
15
|
def run
|
16
16
|
block.call
|
17
17
|
true
|
18
|
-
rescue
|
19
|
-
loc = error
|
20
|
-
|
18
|
+
rescue MatchError, FatalError => error
|
19
|
+
loc = find_location(error) or raise
|
20
|
+
|
21
|
+
self.report = Reporter.new(
|
22
|
+
name: name,
|
23
|
+
error: error,
|
24
|
+
file: loc.absolute_path,
|
25
|
+
location: loc.to_s,
|
26
|
+
).report or raise
|
27
|
+
|
21
28
|
false
|
22
29
|
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def find_location(error)
|
34
|
+
error.backtrace_locations.find { |loc| loc.path.end_with?("_test.rb") }
|
35
|
+
end
|
23
36
|
end
|
24
37
|
end
|
data/lib/easytest/errors.rb
CHANGED
data/lib/easytest/expectation.rb
CHANGED
@@ -2,40 +2,63 @@ module Easytest
|
|
2
2
|
class Expectation
|
3
3
|
attr_reader :actual
|
4
4
|
attr_reader :block
|
5
|
+
attr_reader :negate
|
6
|
+
alias negate? negate
|
5
7
|
|
6
|
-
def initialize(actual, &block)
|
8
|
+
def initialize(actual, negate: false, &block)
|
7
9
|
@actual = actual
|
8
10
|
@block = block
|
11
|
+
@negate = negate
|
12
|
+
end
|
13
|
+
|
14
|
+
def not
|
15
|
+
self.class.new(actual, negate: true, &block)
|
9
16
|
end
|
10
17
|
|
11
18
|
def to_eq(expected)
|
12
|
-
Matcher::Equal.new(actual:
|
19
|
+
Matcher::Equal.new(actual: actual, expected: expected, negate: negate).match!
|
13
20
|
end
|
21
|
+
alias to_equal to_eq
|
14
22
|
|
15
23
|
def to_be(expected)
|
16
|
-
Matcher::Be.new(actual:
|
24
|
+
Matcher::Be.new(actual: actual, expected: expected, negate: negate).match!
|
17
25
|
end
|
18
26
|
|
19
27
|
def to_be_nil
|
20
|
-
Matcher::
|
28
|
+
Matcher::Nil.new(actual: actual, negate: negate).match!
|
21
29
|
end
|
22
30
|
|
23
|
-
def
|
31
|
+
def to_be_true
|
32
|
+
Matcher::True.new(actual: actual, negate: negate).match!
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_be_false
|
36
|
+
Matcher::False.new(actual: actual, negate: negate).match!
|
37
|
+
end
|
24
38
|
|
39
|
+
def to_be_a(expected)
|
40
|
+
Matcher::BeA.new(actual: actual, expected: expected, negate: negate).match!
|
25
41
|
end
|
26
42
|
|
27
|
-
def
|
43
|
+
def to_be_kind_of(expected)
|
44
|
+
Matcher::KindOf.new(actual: actual, expected: expected, negate: negate).match!
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_be_instance_of(expected)
|
48
|
+
Matcher::InstanceOf.new(actual: actual, expected: expected, negate: negate).match!
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_raise(exception_class)
|
52
|
+
raise FatalError, "`to_raise` requires a block like `expect { ... }.to_raise`" unless block
|
53
|
+
raise FatalError, "`not.to_raise` can cause a false positive, so use `to_not_raise` instead" if negate?
|
28
54
|
|
55
|
+
Matcher::Raise.new(actual: block, expected: exception_class, negate: negate).match!
|
29
56
|
end
|
30
57
|
|
31
|
-
|
58
|
+
def to_not_raise
|
59
|
+
raise FatalError, "`to_not_raise` requires a block like `expect { ... }.to_not_raise`" unless block
|
32
60
|
|
33
|
-
|
34
|
-
if block
|
35
|
-
block.call
|
36
|
-
else
|
37
|
-
actual
|
38
|
-
end
|
61
|
+
Matcher::NotRaise.new(actual: block).match!
|
39
62
|
end
|
40
63
|
end
|
41
64
|
end
|
@@ -2,9 +2,14 @@ module Easytest
|
|
2
2
|
module Matcher
|
3
3
|
class Base
|
4
4
|
attr_reader :actual
|
5
|
+
attr_reader :expected
|
6
|
+
attr_reader :negate
|
7
|
+
alias negate? negate
|
5
8
|
|
6
|
-
def initialize(actual:)
|
9
|
+
def initialize(actual:, expected:, negate: false)
|
7
10
|
@actual = actual
|
11
|
+
@expected = expected
|
12
|
+
@negate = negate
|
8
13
|
end
|
9
14
|
|
10
15
|
def match?
|
@@ -12,17 +17,22 @@ module Easytest
|
|
12
17
|
end
|
13
18
|
|
14
19
|
def match!
|
15
|
-
|
16
|
-
|
17
|
-
|
20
|
+
matched = match?
|
21
|
+
matched = !matched if negate?
|
22
|
+
raise_match_error unless matched
|
18
23
|
end
|
19
24
|
|
20
|
-
def
|
25
|
+
def message
|
21
26
|
raise NotImplementedError
|
22
27
|
end
|
23
28
|
|
24
|
-
def
|
25
|
-
|
29
|
+
def build_error_message
|
30
|
+
prefix = negate? ? "not " : ""
|
31
|
+
"#{prefix}#{message}"
|
32
|
+
end
|
33
|
+
|
34
|
+
def raise_match_error
|
35
|
+
raise MatchError.new(message: build_error_message, actual: actual, expected: expected)
|
26
36
|
end
|
27
37
|
end
|
28
38
|
end
|
data/lib/easytest/matcher/be.rb
CHANGED
@@ -1,19 +1,12 @@
|
|
1
1
|
module Easytest
|
2
2
|
module Matcher
|
3
3
|
class Be < Base
|
4
|
-
attr_reader :expected
|
5
|
-
|
6
|
-
def initialize(actual:, expected:)
|
7
|
-
super(actual: actual)
|
8
|
-
@expected = expected
|
9
|
-
end
|
10
|
-
|
11
4
|
def match?
|
12
5
|
actual.equal? expected
|
13
6
|
end
|
14
7
|
|
15
8
|
def message
|
16
|
-
"
|
9
|
+
"same"
|
17
10
|
end
|
18
11
|
end
|
19
12
|
end
|
@@ -1,26 +1,13 @@
|
|
1
1
|
module Easytest
|
2
2
|
module Matcher
|
3
3
|
class Equal < Base
|
4
|
-
attr_reader :expected
|
5
|
-
|
6
|
-
def initialize(actual:, expected:)
|
7
|
-
super(actual: actual)
|
8
|
-
@expected = expected
|
9
|
-
end
|
10
|
-
|
11
4
|
def match?
|
12
5
|
actual == expected
|
13
6
|
end
|
14
7
|
|
15
8
|
def message
|
16
|
-
"
|
9
|
+
"equal"
|
17
10
|
end
|
18
|
-
|
19
|
-
# def match!
|
20
|
-
# unless match?
|
21
|
-
# raise UnmatchedError.new(message: "should equal", actual: actual, expected: expected)
|
22
|
-
# end
|
23
|
-
# end
|
24
11
|
end
|
25
12
|
end
|
26
13
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Easytest
|
2
|
+
module Matcher
|
3
|
+
class False < Base
|
4
|
+
def initialize(actual:, negate:)
|
5
|
+
super(actual: actual, expected: false, negate: negate)
|
6
|
+
end
|
7
|
+
|
8
|
+
def match?
|
9
|
+
actual == expected
|
10
|
+
end
|
11
|
+
|
12
|
+
def message
|
13
|
+
"false"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -1,16 +1,16 @@
|
|
1
1
|
module Easytest
|
2
2
|
module Matcher
|
3
|
-
class
|
4
|
-
def
|
5
|
-
actual
|
3
|
+
class Nil < Base
|
4
|
+
def initialize(actual:, negate:)
|
5
|
+
super(actual: actual, expected: nil, negate: negate)
|
6
6
|
end
|
7
7
|
|
8
|
-
def
|
9
|
-
nil
|
8
|
+
def match?
|
9
|
+
actual.nil?
|
10
10
|
end
|
11
11
|
|
12
12
|
def message
|
13
|
-
"
|
13
|
+
"nil"
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Easytest
|
2
|
+
module Matcher
|
3
|
+
class NotRaise < Base
|
4
|
+
def initialize(actual:)
|
5
|
+
super(actual: actual, expected: nil)
|
6
|
+
end
|
7
|
+
|
8
|
+
def match?
|
9
|
+
begin
|
10
|
+
actual.call
|
11
|
+
true
|
12
|
+
rescue => error
|
13
|
+
@raised_error = error
|
14
|
+
false
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def raise_match_error
|
19
|
+
raise MatchError.new(message: build_error_message, actual: @raised_error, expected: expected)
|
20
|
+
end
|
21
|
+
|
22
|
+
def message
|
23
|
+
"not raise"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Easytest
|
2
|
+
module Matcher
|
3
|
+
class Raise < Base
|
4
|
+
def match?
|
5
|
+
begin
|
6
|
+
actual.call
|
7
|
+
false
|
8
|
+
rescue => error
|
9
|
+
@raised_error = error
|
10
|
+
match_error?(actual: error, expected: expected)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def raise_match_error
|
15
|
+
raise MatchError.new(message: build_error_message, actual: @raised_error, expected: expected)
|
16
|
+
end
|
17
|
+
|
18
|
+
def message
|
19
|
+
"raise"
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def match_error?(actual:, expected:)
|
25
|
+
case expected
|
26
|
+
when Class
|
27
|
+
actual.class == expected
|
28
|
+
when String
|
29
|
+
actual.message == expected
|
30
|
+
when Regexp
|
31
|
+
actual.message.match? expected
|
32
|
+
else
|
33
|
+
raise TypeError.new "Class, String, or Regexp is allowed: #{expected}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Easytest
|
2
|
+
module Matcher
|
3
|
+
class True < Base
|
4
|
+
def initialize(actual:, negate:)
|
5
|
+
super(actual: actual, expected: true, negate: negate)
|
6
|
+
end
|
7
|
+
|
8
|
+
def match?
|
9
|
+
actual == expected
|
10
|
+
end
|
11
|
+
|
12
|
+
def message
|
13
|
+
"true"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/easytest/reporter.rb
CHANGED
@@ -1,19 +1,46 @@
|
|
1
1
|
module Easytest
|
2
2
|
class Reporter
|
3
|
-
|
3
|
+
attr_reader :name
|
4
|
+
attr_reader :error
|
5
|
+
attr_reader :file
|
6
|
+
attr_reader :location
|
7
|
+
|
8
|
+
def initialize(name:, error:, file:, location:)
|
9
|
+
@name = name
|
4
10
|
@error = error
|
5
11
|
@file = file
|
6
12
|
@location = location
|
7
13
|
end
|
8
14
|
|
9
|
-
def report
|
15
|
+
def report
|
16
|
+
case error
|
17
|
+
when MatchError
|
18
|
+
report_match_error
|
19
|
+
when FatalError
|
20
|
+
report_fatal_error
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def report_match_error
|
27
|
+
<<~MSG
|
28
|
+
#{Rainbow("● #{name}").red.bright} #{Rainbow("(#{error.message})").dimgray}
|
29
|
+
|
30
|
+
#{Rainbow("Expected: #{error.expected.inspect}").green}
|
31
|
+
#{Rainbow("Received: #{error.actual.inspect}").red}
|
32
|
+
|
33
|
+
#{Rainbow("# #{location}").dimgray}
|
34
|
+
MSG
|
35
|
+
end
|
36
|
+
|
37
|
+
def report_fatal_error
|
10
38
|
<<~MSG
|
11
|
-
#{Rainbow("● #{
|
39
|
+
#{Rainbow("● #{name}").red.bright}
|
12
40
|
|
13
|
-
#{Rainbow(
|
14
|
-
#{Rainbow("Received: #{@error.actual.inspect}").red}
|
41
|
+
#{Rainbow(error.message).red}
|
15
42
|
|
16
|
-
#{Rainbow("
|
43
|
+
#{Rainbow("# #{location}").dimgray}
|
17
44
|
MSG
|
18
45
|
end
|
19
46
|
end
|
data/lib/easytest/version.rb
CHANGED
data/lib/easytest.rb
CHANGED
@@ -8,14 +8,23 @@ require_relative "easytest/cli"
|
|
8
8
|
require_relative "easytest/dsl"
|
9
9
|
require_relative "easytest/errors"
|
10
10
|
require_relative "easytest/expectation"
|
11
|
-
require_relative "easytest/matcher/base"
|
12
|
-
require_relative "easytest/matcher/be"
|
13
|
-
require_relative "easytest/matcher/be_nil"
|
14
|
-
require_relative "easytest/matcher/equal"
|
15
11
|
require_relative "easytest/reporter"
|
16
12
|
require_relative "easytest/runner"
|
17
13
|
require_relative "easytest/utils"
|
18
14
|
|
15
|
+
# Matcher
|
16
|
+
require_relative "easytest/matcher/base"
|
17
|
+
require_relative "easytest/matcher/be"
|
18
|
+
require_relative "easytest/matcher/be_a"
|
19
|
+
require_relative "easytest/matcher/equal"
|
20
|
+
require_relative "easytest/matcher/false"
|
21
|
+
require_relative "easytest/matcher/instance_of"
|
22
|
+
require_relative "easytest/matcher/kind_of"
|
23
|
+
require_relative "easytest/matcher/nil"
|
24
|
+
require_relative "easytest/matcher/not_raise"
|
25
|
+
require_relative "easytest/matcher/raise"
|
26
|
+
require_relative "easytest/matcher/true"
|
27
|
+
|
19
28
|
module Easytest
|
20
29
|
def self.start
|
21
30
|
@runner = Runner.new
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easytest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Masafumi Koba
|
@@ -44,8 +44,15 @@ files:
|
|
44
44
|
- lib/easytest/expectation.rb
|
45
45
|
- lib/easytest/matcher/base.rb
|
46
46
|
- lib/easytest/matcher/be.rb
|
47
|
-
- lib/easytest/matcher/
|
47
|
+
- lib/easytest/matcher/be_a.rb
|
48
48
|
- lib/easytest/matcher/equal.rb
|
49
|
+
- lib/easytest/matcher/false.rb
|
50
|
+
- lib/easytest/matcher/instance_of.rb
|
51
|
+
- lib/easytest/matcher/kind_of.rb
|
52
|
+
- lib/easytest/matcher/nil.rb
|
53
|
+
- lib/easytest/matcher/not_raise.rb
|
54
|
+
- lib/easytest/matcher/raise.rb
|
55
|
+
- lib/easytest/matcher/true.rb
|
49
56
|
- lib/easytest/reporter.rb
|
50
57
|
- lib/easytest/runner.rb
|
51
58
|
- lib/easytest/utils.rb
|