petitest 0.1.0 → 0.1.1

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
  SHA1:
3
- metadata.gz: ad7132e7dbbfaf374485b3198b4e0d0950272f10
4
- data.tar.gz: be7660bd9bca8b83d12b0bac75bc7c7f4a870a2b
3
+ metadata.gz: 352ae5dae0ba8eb8e866f277ae8252b94c15f4bd
4
+ data.tar.gz: c5feb6113073d80f36e406e6cfd4f4929cd2160a
5
5
  SHA512:
6
- metadata.gz: c9290292fe37e1f0f44729e798900b17c2f151e4bd218da5878de2293549b3ea04532612c482096af2f78c9794a6ef51687cb50cfb2118f40a73eaeaea3fda64
7
- data.tar.gz: 9cbf07c7adb70c3b8e73e1febcd7b537642c6b317f82145f2455647d90b065045aa494368e07c687a8ea311616671b28b8ac2d48b521393f3076585c6e9cd644
6
+ metadata.gz: dd63a6b94dd92d850e56e96ca98e0eabfc80feaa1312b112f8c229f73b149a4c32acd0940a45ef0918acc75956bd5f43c949672fd6e2d2ebf5c64d9a74de87d4
7
+ data.tar.gz: 712f51bada7fd39c605d340629a072c9930a91563d27c9eb18ada7903ecde018dc949adfa4f0aef3ca0d6c878f63b629cc4c11e20eb1fad884bd36df01f436b1
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## v0.1.1
2
+
3
+ - Support block on `#assert` method for Power Assert
4
+ - Improve backtrace for plug-ins
5
+ - Change failure messages API
6
+
1
7
  ## v0.1.0
2
8
 
3
9
  - 1st Release :tada:
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Petitest
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/petitest.svg)](https://rubygems.org/gems/petitest)
4
+ [![Documentation](http://img.shields.io/badge/docs-rdoc.info-blue.svg)](http://www.rubydoc.info/github/petitest/petitest)
5
+
3
6
  A minimal solid testing framework for Ruby.
4
7
 
5
8
  ## Installation
@@ -1,4 +1,40 @@
1
1
  module Petitest
2
2
  class AssertionFailureError < ::StandardError
3
+ # @return [String, nil]
4
+ attr_reader :additional_message
5
+
6
+ # @return [String]
7
+ attr_reader :template
8
+
9
+ # @return [Hash{Symbol => Object}]
10
+ attr_reader :template_variables
11
+
12
+ # @param additional_message [String, nil]
13
+ # @param template [String]
14
+ # @param template_variables [Hash, nil]
15
+ def initialize(
16
+ additional_message:,
17
+ template:,
18
+ template_variables:
19
+ )
20
+ @additional_message = additional_message
21
+ @template = template
22
+ @template_variables = template_variables
23
+ super(assertion_type_message)
24
+ end
25
+
26
+ # @return [String]
27
+ def assertion_type_message
28
+ template % inspected_template_variables
29
+ end
30
+
31
+ private
32
+
33
+ # @return [Hash{Symbol => String}]
34
+ def inspected_template_variables
35
+ template_variables.map do |key, value|
36
+ [key, value.inspect]
37
+ end.to_h
38
+ end
3
39
  end
4
40
  end
@@ -1,45 +1,65 @@
1
1
  module Petitest
2
2
  module Assertions
3
- # @param actual [Object]
4
- # @param user_specified_message [String, nil]
5
- def assert(actual, user_specified_message = nil)
6
- check(
7
- assertion_failure_message: ::Petitest::AssertionFailureMessage.new(
3
+ # @param actual_or_message [Object]
4
+ # @param message [String, nil]
5
+ def assert(actual_or_message = nil, message = nil, &block)
6
+ if block
7
+ assert_block(actual_or_message, &block)
8
+ else
9
+ actual = actual_or_message
10
+ check(
11
+ message: message,
8
12
  template: "%{actual} is not truthy",
9
13
  template_variables: {
10
14
  actual: actual,
11
15
  },
12
- user_specified_message: user_specified_message,
13
- ),
14
- result: actual,
16
+ ) do
17
+ actual
18
+ end
19
+ end
20
+ end
21
+
22
+ # @param message [String, nil]
23
+ def assert_block(message = nil, &block)
24
+ check(
25
+ message: message,
26
+ template: "Given block returned falsy",
27
+ &block
15
28
  )
16
29
  end
17
30
 
18
31
  # @param expected [Object]
19
32
  # @param actual [Object]
20
- # @param user_specified_message [String, nil]
21
- def assert_equal(expected, actual, user_specified_message = nil)
22
- result = expected == actual
33
+ # @param message [String, nil]
34
+ def assert_equal(expected, actual, message = nil)
23
35
  check(
24
- assertion_failure_message: ::Petitest::AssertionFailureMessage.new(
25
- template: "%{expected} expected but was %{actual}",
26
- template_variables: {
27
- actual: actual,
28
- expected: expected,
29
- },
30
- user_specified_message: user_specified_message,
31
- ),
32
- result: result,
33
- )
36
+ message: message,
37
+ template: "%{expected} expected but was %{actual}",
38
+ template_variables: {
39
+ actual: actual,
40
+ expected: expected,
41
+ },
42
+ ) do
43
+ expected == actual
44
+ end
34
45
  end
35
46
 
36
47
  private
37
48
 
38
- # @param assertion_failure_message [Petitest::AssertionFailureMessage]
39
- # @param result [Boolean]
40
- def check(assertion_failure_message:, result:)
41
- unless result
42
- raise ::Petitest::AssertionFailureError.new(assertion_failure_message)
49
+ # @param message [String, nil]
50
+ # @param template [String]
51
+ # @param template_variables [Hash, nil]
52
+ def check(
53
+ message:,
54
+ template:,
55
+ template_variables: {}
56
+ )
57
+ unless yield
58
+ raise ::Petitest::AssertionFailureError.new(
59
+ additional_message: message,
60
+ template: template,
61
+ template_variables: template_variables,
62
+ )
43
63
  end
44
64
  end
45
65
  end
@@ -16,7 +16,8 @@ module Petitest
16
16
  error_class_name: test_case.error_class_name,
17
17
  error_message: test_case.error_message,
18
18
  failed: test_case.failed?,
19
- failure_message: test_case.failure_message,
19
+ failure_additional_message: test_case.failure_additional_message,
20
+ failure_assertion_type_message: test_case.failure_assertion_type_message,
20
21
  finished_at: test_case.finished_at.iso8601(6),
21
22
  method_line_number: test_case.test_method.line_number,
22
23
  method_name: test_case.test_method.method_name,
@@ -61,9 +61,16 @@ module Petitest
61
61
  end
62
62
 
63
63
  # @return [String, nil]
64
- def failure_message
64
+ def failure_additional_message
65
65
  if failed?
66
- error.to_s
66
+ error.additional_message
67
+ end
68
+ end
69
+
70
+ # @return [String, nil]
71
+ def failure_assertion_type_message
72
+ if failed?
73
+ error.assertion_type_message
67
74
  end
68
75
  end
69
76
 
@@ -74,8 +81,15 @@ module Petitest
74
81
 
75
82
  # @return [Array<String>, nil]
76
83
  def filtered_backtrace
77
- @filtered_backtrace ||= backtrace.reject do |line|
78
- line.start_with?(self.class.prefix_to_filter_backtrace)
84
+ @filtered_backtrace ||= begin
85
+ backtrace.reverse_each.each_with_object([]) do |line, lines|
86
+ if line.start_with?(::File.expand_path("../assertions.rb", __FILE__))
87
+ break lines
88
+ end
89
+ unless line.start_with?(self.class.prefix_to_filter_backtrace)
90
+ lines << line
91
+ end
92
+ end.reverse
79
93
  end
80
94
  end
81
95
 
@@ -13,7 +13,13 @@ module Petitest
13
13
 
14
14
  # @note Override
15
15
  def to_s
16
- colorize(test_case.failure_message, :failure)
16
+ colorize(
17
+ [
18
+ test_case.failure_assertion_type_message,
19
+ test_case.failure_additional_message,
20
+ ].compact.join("\n"),
21
+ :failure,
22
+ )
17
23
  end
18
24
  end
19
25
  end
@@ -1,3 +1,3 @@
1
1
  module Petitest
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/petitest.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require "petitest/assertion_failure_error"
2
- require "petitest/assertion_failure_message"
3
2
  require "petitest/assertions"
4
3
  require "petitest/configuration"
5
4
  require "petitest/subscribers/base_subscriber"
data/petitest.gemspec CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ["Ryo Nakamura"]
9
9
  spec.email = ["r7kamura@gmail.com"]
10
10
  spec.summary = "A minimal solid testing framework for Ruby."
11
- spec.homepage = "https://github.com/r7kamura/petitest"
11
+ spec.homepage = "https://github.com/petitest/petitest"
12
12
  spec.license = "MIT"
13
13
 
14
14
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: petitest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
@@ -53,7 +53,6 @@ files:
53
53
  - Rakefile
54
54
  - lib/petitest.rb
55
55
  - lib/petitest/assertion_failure_error.rb
56
- - lib/petitest/assertion_failure_message.rb
57
56
  - lib/petitest/assertions.rb
58
57
  - lib/petitest/autorun.rb
59
58
  - lib/petitest/configuration.rb
@@ -81,7 +80,7 @@ files:
81
80
  - lib/petitest/texts/times_text.rb
82
81
  - lib/petitest/version.rb
83
82
  - petitest.gemspec
84
- homepage: https://github.com/r7kamura/petitest
83
+ homepage: https://github.com/petitest/petitest
85
84
  licenses:
86
85
  - MIT
87
86
  metadata: {}
@@ -1,35 +0,0 @@
1
- module Petitest
2
- class AssertionFailureMessage
3
- # @return [String]
4
- attr_reader :template
5
-
6
- # @return [Hash{Symbol => Object}]
7
- attr_reader :template_variables
8
-
9
- # @return [String, nil]
10
- attr_reader :user_specified_message
11
-
12
- # @param template [String]
13
- # @param template_variables [Hash{Symbol => Object}]
14
- # @param user_specified_message [String, nil]
15
- def initialize(template:, template_variables: {}, user_specified_message:)
16
- @template = template
17
- @template_variables = template_variables
18
- @user_specified_message = user_specified_message
19
- end
20
-
21
- # @note Override
22
- def to_s
23
- template % inspected_template_variables
24
- end
25
-
26
- private
27
-
28
- # @return [Hash{Symbol => String}]
29
- def inspected_template_variables
30
- template_variables.map do |key, value|
31
- [key, value.inspect]
32
- end.to_h
33
- end
34
- end
35
- end