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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 42389cdde1ebd51de0f636f3c5ec0e67ffe842ff91029f71617795799841d47b
4
- data.tar.gz: 559dadc55a86bfc6da11234b05f1fd9a5477311379f94ea41565731516271405
3
+ metadata.gz: ea89da5a0f2aba7252ea69be567591284bfcc1f53aa089e4824eb54773881b0b
4
+ data.tar.gz: b9bb01188ce657b918cdffe80cb962fd4e3c011fa9a4556b832d49eeed8fef5d
5
5
  SHA512:
6
- metadata.gz: e4d9cc9b6bc5a76ecccf5f98d076e123eeddabe7a16c42a093050e154c3c32565a80c99301ed615ab0be95e2b7b7e0773088d3d5dc621ddc2fda3f69e28ed184
7
- data.tar.gz: 8a8612f4baf5bc8bc026ecb20fbbe906ad32044f22c9232137dc68b5bb32940d634aed10703d6455f339518bba18c8a433bee66d2ed30afbcff2c93cbe03ec57
6
+ metadata.gz: 6b1539eeef25d6343a411a05afbfcff6143d9f33798fb5f3d13dbf8f3d742ea79247ced5c5da1a994bc0d77b41787ec8c7477ed06b26664ef30987007291ad26
7
+ data.tar.gz: c5e5ebe81739839785eab40d24a3d4e529d7c33f0d2ac5fd5cc20bf2984b65849d6b99f18b46848f6d17d362dfdc95fb16ae7d0f3403b77913a6b182b0f1fc9a
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
+ ## 0.2.0
6
+
7
+ - Add many matchers.
8
+
5
9
  ## 0.1.2
6
10
 
7
11
  - Fix the `easytest` command exit code.
data/README.md CHANGED
@@ -46,7 +46,7 @@ $ bundle exec easytest
46
46
  Expected: 2
47
47
  Received: 3
48
48
 
49
- at test/addition_test.rb:6:in `block in <top (required)>'
49
+ # test/addition_test.rb:6:in `block in <top (required)>'
50
50
 
51
51
 
52
52
  Tests: 1 failed, 0 passed, 1 total (1 files)
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 UnmatchedError => error
19
- loc = error.backtrace_locations[2]
20
- self.report = Reporter.new(error: error, file: loc.absolute_path, location: loc.to_s).report(name)
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
@@ -1,7 +1,9 @@
1
1
  module Easytest
2
2
  class Error < StandardError; end
3
3
 
4
- class UnmatchedError < Error
4
+ class FatalError < Error; end
5
+
6
+ class MatchError < Error
5
7
  attr_reader :actual
6
8
  attr_reader :expected
7
9
 
@@ -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: eval_actual, expected: expected).match!
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: eval_actual, expected: expected).match!
24
+ Matcher::Be.new(actual: actual, expected: expected, negate: negate).match!
17
25
  end
18
26
 
19
27
  def to_be_nil
20
- Matcher::BeNil.new(actual: eval_actual).match!
28
+ Matcher::Nil.new(actual: actual, negate: negate).match!
21
29
  end
22
30
 
23
- def to_raise(exception_class)
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 to_not_raise(exception_class)
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
- private
58
+ def to_not_raise
59
+ raise FatalError, "`to_not_raise` requires a block like `expect { ... }.to_not_raise`" unless block
32
60
 
33
- def eval_actual
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
- unless match?
16
- raise UnmatchedError.new(message: message, actual: actual, expected: expected)
17
- end
20
+ matched = match?
21
+ matched = !matched if negate?
22
+ raise_match_error unless matched
18
23
  end
19
24
 
20
- def expected
25
+ def message
21
26
  raise NotImplementedError
22
27
  end
23
28
 
24
- def message
25
- raise NotImplementedError
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
@@ -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
- "should be same"
9
+ "same"
17
10
  end
18
11
  end
19
12
  end
@@ -0,0 +1,13 @@
1
+ module Easytest
2
+ module Matcher
3
+ class BeA < Base
4
+ def match?
5
+ actual.is_a? expected
6
+ end
7
+
8
+ def message
9
+ "be a"
10
+ end
11
+ end
12
+ end
13
+ 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
- "should equal"
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
@@ -0,0 +1,13 @@
1
+ module Easytest
2
+ module Matcher
3
+ class InstanceOf < Base
4
+ def match?
5
+ actual.instance_of? expected
6
+ end
7
+
8
+ def message
9
+ "instance of"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Easytest
2
+ module Matcher
3
+ class KindOf < Base
4
+ def match?
5
+ actual.kind_of? expected
6
+ end
7
+
8
+ def message
9
+ "kind of"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,16 +1,16 @@
1
1
  module Easytest
2
2
  module Matcher
3
- class BeNil < Base
4
- def match?
5
- actual.nil?
3
+ class Nil < Base
4
+ def initialize(actual:, negate:)
5
+ super(actual: actual, expected: nil, negate: negate)
6
6
  end
7
7
 
8
- def expected
9
- nil
8
+ def match?
9
+ actual.nil?
10
10
  end
11
11
 
12
12
  def message
13
- "should be nil"
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
@@ -1,19 +1,46 @@
1
1
  module Easytest
2
2
  class Reporter
3
- def initialize(error:, file:, location:)
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(test_name)
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("● #{test_name}").red.bright} #{Rainbow("(#{@error.message})").dimgray}
39
+ #{Rainbow("● #{name}").red.bright}
12
40
 
13
- #{Rainbow("Expected: #{@error.expected.inspect}").green}
14
- #{Rainbow("Received: #{@error.actual.inspect}").red}
41
+ #{Rainbow(error.message).red}
15
42
 
16
- #{Rainbow("at #{@location}").dimgray}
43
+ #{Rainbow("# #{location}").dimgray}
17
44
  MSG
18
45
  end
19
46
  end
@@ -1,3 +1,3 @@
1
1
  module Easytest
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.2
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/be_nil.rb
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