petitest 0.1.3 → 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
  SHA1:
3
- metadata.gz: 2c22fb01aaa90a168cd116fc8c99313dab896a85
4
- data.tar.gz: f30f8cc4416912d7765cb27cdd650dd853c385ab
3
+ metadata.gz: 94d661afca2ae26812fcfd79ffd3c54e1fbd4085
4
+ data.tar.gz: 26ce49148e23ff5e854b4ae1898ab7c11250a81b
5
5
  SHA512:
6
- metadata.gz: 38043bfacf0205fc857e1d86fa266d4a02aa08864361b5d302ad8860da9365aa5ac473e028440a98e08f432334f23854308a08c834dfc781850cf31539e1cafe
7
- data.tar.gz: 7ac2359c9a3fc6b3679b8e3d461f5b47a32782e29ab7fd26bfa7545956b7b64e92ee9bf75b4a286e2edcae2ab40a9af2f8c42e5f73ea99ff3185cb6e41a1c108
6
+ metadata.gz: 4e6243251265c19413ffff8923419cccf9c436e9c12ae058abb22d9545464270ac294ed87bec5d3f57ddb350fd9910e96013d947c820e3295e1cebe6bb7a5ce5
7
+ data.tar.gz: b68470322a44f4155d482e0d154758f2c09188a3f670452b0542a03d8087f950506e899ba10c0aa1c3fa9bca82c6e2dccef4c6c15e62d984ed9a04c02e3f935a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## v0.2.0
2
+
3
+ - Merge errors into failures
4
+ - Change test message interface
5
+ - Remove utility assertions except for `#assert`
6
+ - Add Petitest.configuration.backtrace_filter
7
+ - Stop tests if any error occured before running tests
8
+ - Improve report about empty errors/failures
9
+
1
10
  ## v0.1.3
2
11
 
3
12
  - Fix OutputConcern bug
data/README.md CHANGED
@@ -110,3 +110,4 @@ Times:
110
110
  ## Plug-ins
111
111
 
112
112
  - https://github.com/petitest/petitest-power_assert
113
+ - https://github.com/petitest/petitest-tap
data/lib/petitest.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require "petitest/assertion_failure_error"
2
- require "petitest/assertions"
3
2
  require "petitest/configuration"
4
3
  require "petitest/subscriber_concerns/output_concern"
5
4
  require "petitest/subscriber_concerns/time_concern"
@@ -12,9 +11,6 @@ require "petitest/test_group"
12
11
  require "petitest/test_method"
13
12
  require "petitest/texts/base_text"
14
13
  require "petitest/texts/error_message_text"
15
- require "petitest/texts/errors_element_text"
16
- require "petitest/texts/errors_text"
17
- require "petitest/texts/failure_message_text"
18
14
  require "petitest/texts/failures_element_text"
19
15
  require "petitest/texts/failures_text"
20
16
  require "petitest/texts/filtered_backtrace_text"
@@ -1,40 +1,4 @@
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
39
3
  end
40
4
  end
@@ -1,6 +1,9 @@
1
1
  require "petitest"
2
2
 
3
3
  at_exit do
4
+ if $! && !($!.is_a?(::SystemExit) && $!.success?)
5
+ next
6
+ end
4
7
  test_cases = Petitest::TestGroup.test_cases
5
8
  result = Petitest::TestCasesRunner.new(test_cases).run
6
9
  exit_code = result ? 0 : 1
@@ -1,31 +1,48 @@
1
1
  module Petitest
2
2
  class Configuration
3
- DEFAULT_COLOR_SCHEME = {
4
- detail: :cyan,
5
- error: :red,
6
- failure: :red,
7
- pass: :green,
8
- skip: :yellow,
9
- }
3
+ attr_writer :backtrace_filters
10
4
 
11
- # @return [Hash{Symbol => Symbol}]
12
- attr_accessor :color_scheme
5
+ attr_writer :color
6
+
7
+ attr_writer :color_scheme
8
+
9
+ attr_writer :output
10
+
11
+ attr_writer :subscribers
12
+
13
+ # @return [Array<String>]
14
+ def backtrace_filters
15
+ @backtrace_filters ||= begin
16
+ path = ::File.expand_path("../..", __FILE__)
17
+ [-> (line) { line.start_with?(path) }]
18
+ end
19
+ end
13
20
 
14
21
  # @return [Boolean]
15
- attr_accessor :color
22
+ def color
23
+ @color ||= true
24
+ end
25
+
26
+ # @return [Hash{Symbol => Symbol}]
27
+ def color_scheme
28
+ @color_scheme ||= {
29
+ detail: :cyan,
30
+ error: :red,
31
+ pass: :green,
32
+ skip: :yellow,
33
+ }
34
+ end
16
35
 
17
36
  # @return [IO]
18
- attr_accessor :output
37
+ def output
38
+ @output ||= ::STDOUT.tap do |io|
39
+ io.sync = true
40
+ end
41
+ end
19
42
 
20
43
  # @return [Array<Petitest::Subscribers::BaseSubscriber>]
21
- attr_accessor :subscribers
22
-
23
- def initialize
24
- @color_scheme = DEFAULT_COLOR_SCHEME.dup
25
- @color = true
26
- @output = ::STDOUT
27
- @output.sync = true
28
- @subscribers = [::Petitest::Subscribers::ProgressReportSubscriber.new]
44
+ def subscribers
45
+ @subscribers ||= [::Petitest::Subscribers::ProgressReportSubscriber.new]
29
46
  end
30
47
  end
31
48
  end
@@ -15,14 +15,11 @@ module Petitest
15
15
  data = {
16
16
  test_cases: test_cases.map do |test_case|
17
17
  {
18
- aborted: test_case.aborted?,
19
18
  backtrace: test_case.backtrace,
20
19
  class_name: test_case.test_group_class.to_s,
21
20
  error_class_name: test_case.error_class_name,
22
21
  error_message: test_case.error_message,
23
22
  failed: test_case.failed?,
24
- failure_additional_message: test_case.failure_additional_message,
25
- failure_assertion_type_message: test_case.failure_assertion_type_message,
26
23
  finished_at: test_case.finished_at.iso8601(6),
27
24
  method_line_number: test_case.test_method.line_number,
28
25
  method_name: test_case.test_method.method_name,
@@ -15,13 +15,6 @@ module Petitest
15
15
  # @return [Petitest::TestMethod]
16
16
  attr_reader :test_method
17
17
 
18
- class << self
19
- # @return [String]
20
- def prefix_to_filter_backtrace
21
- @prefix_to_filter_backtrace ||= ::File.expand_path("../..", __FILE__)
22
- end
23
- end
24
-
25
18
  # @param test_group_class [Class]
26
19
  # @param test_method [Petitest::TestMethod]
27
20
  def initialize(
@@ -34,75 +27,47 @@ module Petitest
34
27
  @test_method = test_method
35
28
  end
36
29
 
37
- # @return [Boolean]
38
- def aborted?
39
- processed? && has_error? && !has_assertion_error?
40
- end
41
-
42
30
  # @return [Array<String>, nil]
43
31
  def backtrace
44
- if has_error?
32
+ if error
45
33
  error.backtrace
46
34
  end
47
35
  end
48
36
 
49
37
  # @return [String, nil]
50
38
  def error_class_name
51
- if aborted?
39
+ if error
52
40
  error.class.to_s
53
41
  end
54
42
  end
55
43
 
56
44
  # @return [String, nil]
57
45
  def error_message
58
- if aborted?
46
+ if error
59
47
  error.to_s
60
48
  end
61
49
  end
62
50
 
63
- # @return [String, nil]
64
- def failure_additional_message
65
- if failed?
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
74
- end
75
- end
76
-
77
51
  # @return [Boolean]
78
52
  def failed?
79
- processed? && has_assertion_error?
53
+ processed? && !error.nil?
80
54
  end
81
55
 
82
56
  # @return [Array<String>, nil]
83
57
  def filtered_backtrace
84
58
  @filtered_backtrace ||= begin
59
+ path = ::File.expand_path("../test_group.rb", __FILE__)
85
60
  backtrace.reverse_each.each_with_object([]) do |line, lines|
86
- if line.start_with?(::File.expand_path("../assertions.rb", __FILE__))
61
+ if line.start_with?(path)
87
62
  break lines
88
63
  end
89
- unless line.start_with?(self.class.prefix_to_filter_backtrace)
64
+ unless ::Petitest.configuration.backtrace_filters.any? { |filter| filter === line }
90
65
  lines << line
91
66
  end
92
67
  end.reverse
93
68
  end
94
69
  end
95
70
 
96
- # @return [Boolean]
97
- def has_assertion_error?
98
- error.is_a?(::Petitest::AssertionFailureError)
99
- end
100
-
101
- # @return [Boolean]
102
- def has_error?
103
- !error.nil?
104
- end
105
-
106
71
  # @return [Boolean]
107
72
  def passed?
108
73
  processed? && error.nil?
@@ -1,7 +1,5 @@
1
1
  module Petitest
2
2
  class TestGroup
3
- include ::Petitest::Assertions
4
-
5
3
  TEST_METHOD_NAME_PREFIX = "test_"
6
4
 
7
5
  class << self
@@ -16,7 +14,7 @@ module Petitest
16
14
  descendants << sub_class
17
15
  end
18
16
 
19
- # @return [Array<Petit::TestCase]
17
+ # @return [Array<Petit::TestCase>]
20
18
  def test_cases
21
19
  descendants.flat_map do |test_group_class|
22
20
  test_group_class.test_methods.map do |test_method|
@@ -47,5 +45,28 @@ module Petitest
47
45
  end
48
46
  end
49
47
  end
48
+
49
+ # @param actual_or_message [Object]
50
+ # @param message [String, nil]
51
+ def assert(actual_or_message = nil, message = nil, &block)
52
+ if block
53
+ message = actual_or_message
54
+ check(message || "Given block returned falsy", &block)
55
+ else
56
+ actual = actual_or_message
57
+ check(message || "#{actual.inspect} is not truthy") do
58
+ actual
59
+ end
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ # @param message [String, nil]
66
+ def check(message, &block)
67
+ unless block.call
68
+ raise ::Petitest::AssertionFailureError.new(message)
69
+ end
70
+ end
50
71
  end
51
72
  end
@@ -30,7 +30,7 @@ module Petitest
30
30
  def body
31
31
  [
32
32
  ::Petitest::Texts::RaisedCodeText.new(test_case: test_case),
33
- ::Petitest::Texts::FailureMessageText.new(test_case: test_case),
33
+ ::Petitest::Texts::ErrorMessageText.new(test_case: test_case),
34
34
  ::Petitest::Texts::FilteredBacktraceText.new(test_case: test_case),
35
35
  ].join("\n")
36
36
  end
@@ -24,24 +24,32 @@ module Petitest
24
24
 
25
25
  # @return [String, nil]
26
26
  def caller_file_content
27
- if ::FileTest.file?(caller_path)
27
+ if caller_path && ::FileTest.file?(caller_path)
28
28
  ::File.read(caller_path)
29
29
  end
30
30
  end
31
31
 
32
- # @return [String]
32
+ # @return [String, nil]
33
33
  def caller_line_number
34
- caller_segments[1].to_i
34
+ if caller_segments[1]
35
+ caller_segments[1].to_i
36
+ end
35
37
  end
36
38
 
37
- # @return [String]
39
+ # @return [String, nil]
38
40
  def caller_path
39
41
  caller_segments[0]
40
42
  end
41
43
 
42
44
  # @return [Array<String>]
43
45
  def caller_segments
44
- @caller_segments ||= test_case.filtered_backtrace[0].split(":", 3)
46
+ @caller_segments ||= begin
47
+ if test_case.filtered_backtrace[0]
48
+ test_case.filtered_backtrace[0].split(":", 3)
49
+ else
50
+ []
51
+ end
52
+ end
45
53
  end
46
54
  end
47
55
  end
@@ -14,10 +14,8 @@ module Petitest
14
14
  # @note Override
15
15
  def to_s
16
16
  case
17
- when test_case.aborted?
18
- colorize("E", :error)
19
17
  when test_case.failed?
20
- colorize("F", :failure)
18
+ colorize("F", :error)
21
19
  when test_case.skipped?
22
20
  colorize("*", :skip)
23
21
  else
@@ -28,18 +28,34 @@ module Petitest
28
28
  # @note Override
29
29
  def to_s
30
30
  [
31
- ::Petitest::Texts::TestCasesResultMarginTopText.new(test_cases: test_cases),
32
- [
33
- ::Petitest::Texts::FailuresText.new(test_cases: test_cases.select(&:failed?)),
34
- ::Petitest::Texts::ErrorsText.new(test_cases: test_cases.select(&:aborted?)),
35
- ::Petitest::Texts::TestCountsText.new(test_cases: test_cases),
36
- ::Petitest::Texts::TimesText.new(
37
- finished_at: finished_at,
38
- started_at: started_at,
39
- ),
40
- ].join("\n\n"),
31
+ header,
32
+ body,
41
33
  ].join
42
34
  end
35
+
36
+ private
37
+
38
+ # @return [String]
39
+ def body
40
+ texts = []
41
+ texts << ::Petitest::Texts::FailuresText.new(test_cases: test_cases_failed) unless test_cases_failed.empty?
42
+ texts << ::Petitest::Texts::TestCountsText.new(test_cases: test_cases)
43
+ texts << ::Petitest::Texts::TimesText.new(
44
+ finished_at: finished_at,
45
+ started_at: started_at,
46
+ )
47
+ texts.join("\n\n")
48
+ end
49
+
50
+ # @return [String]
51
+ def header
52
+ ::Petitest::Texts::TestCasesResultMarginTopText.new(test_cases: test_cases).to_s
53
+ end
54
+
55
+ # @return [Array<Petitest::TestCase>]
56
+ def test_cases_failed
57
+ @test_cases_failed ||= test_cases.select(&:failed?)
58
+ end
43
59
  end
44
60
  end
45
61
  end
@@ -27,16 +27,10 @@ module Petitest
27
27
  text_of_count_of_test_cases,
28
28
  text_of_count_of_passed_test_cases,
29
29
  text_of_count_of_failed_test_cases,
30
- text_of_count_of_aborted_test_cases,
31
30
  text_of_count_of_skipped_test_cases,
32
31
  ].join("\n")
33
32
  end
34
33
 
35
- # @return [Integer]
36
- def count_of_aborted_test_cases
37
- test_cases.select(&:aborted?).length
38
- end
39
-
40
34
  # @return [Integer]
41
35
  def count_of_failed_test_cases
42
36
  test_cases.select(&:failed?).length
@@ -67,14 +61,9 @@ module Petitest
67
61
  @max_digits_length ||= count_of_test_cases.to_s.length
68
62
  end
69
63
 
70
- # @return [String]
71
- def text_of_count_of_aborted_test_cases
72
- colorize("%#{max_digits_length}d errors" % count_of_aborted_test_cases, :error)
73
- end
74
-
75
64
  # @return [String]
76
65
  def text_of_count_of_failed_test_cases
77
- colorize("%#{max_digits_length}d failures" % count_of_failed_test_cases, :failure)
66
+ colorize("%#{max_digits_length}d failures" % count_of_failed_test_cases, :error)
78
67
  end
79
68
 
80
69
  # @return [String]
@@ -1,3 +1,3 @@
1
1
  module Petitest
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: petitest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-22 00:00:00.000000000 Z
11
+ date: 2017-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -53,7 +53,6 @@ files:
53
53
  - Rakefile
54
54
  - lib/petitest.rb
55
55
  - lib/petitest/assertion_failure_error.rb
56
- - lib/petitest/assertions.rb
57
56
  - lib/petitest/autorun.rb
58
57
  - lib/petitest/configuration.rb
59
58
  - lib/petitest/subscriber_concerns/output_concern.rb
@@ -67,9 +66,6 @@ files:
67
66
  - lib/petitest/test_method.rb
68
67
  - lib/petitest/texts/base_text.rb
69
68
  - lib/petitest/texts/error_message_text.rb
70
- - lib/petitest/texts/errors_element_text.rb
71
- - lib/petitest/texts/errors_text.rb
72
- - lib/petitest/texts/failure_message_text.rb
73
69
  - lib/petitest/texts/failures_element_text.rb
74
70
  - lib/petitest/texts/failures_text.rb
75
71
  - lib/petitest/texts/filtered_backtrace_text.rb
@@ -1,66 +0,0 @@
1
- module Petitest
2
- module Assertions
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,
12
- template: "%{actual} is not truthy",
13
- template_variables: {
14
- actual: actual,
15
- },
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
28
- )
29
- end
30
-
31
- # @param expected [Object]
32
- # @param actual [Object]
33
- # @param message [String, nil]
34
- def assert_equal(expected, actual, message = nil)
35
- check(
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
45
- end
46
-
47
- private
48
-
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
- )
63
- end
64
- end
65
- end
66
- end
@@ -1,54 +0,0 @@
1
- require "petitest/texts/base_text"
2
-
3
- module Petitest
4
- module Texts
5
- class ErrorsElementText < ::Petitest::Texts::BaseText
6
- # @return [Integer]
7
- attr_reader :index
8
-
9
- # @return [Petitest::TestCase]
10
- attr_reader :test_case
11
-
12
- # @param index [Integer]
13
- # @param test_case [Petitest::TestCase]
14
- def initialize(index:, test_case:)
15
- @index = index
16
- @test_case = test_case
17
- end
18
-
19
- # @note Override
20
- def to_s
21
- [
22
- heading,
23
- indent(body, 2)
24
- ].join("\n")
25
- end
26
-
27
- private
28
-
29
- # @return [String]
30
- def body
31
- [
32
- ::Petitest::Texts::RaisedCodeText.new(test_case: test_case),
33
- ::Petitest::Texts::ErrorMessageText.new(test_case: test_case),
34
- ::Petitest::Texts::FilteredBacktraceText.new(test_case: test_case),
35
- ].join("\n")
36
- end
37
-
38
- # @return [String]
39
- def heading
40
- "#{ordinal_number}) #{test_signature}"
41
- end
42
-
43
- # @return [Integer]
44
- def ordinal_number
45
- index + 1
46
- end
47
-
48
- # @return [String]
49
- def test_signature
50
- test_case.test_signature
51
- end
52
- end
53
- end
54
- end
@@ -1,45 +0,0 @@
1
- require "petitest/texts/base_text"
2
-
3
- module Petitest
4
- module Texts
5
- class ErrorsText < ::Petitest::Texts::BaseText
6
- # @return [Array<Petitest::TestCase>]
7
- attr_reader :test_cases
8
-
9
- # @param test_cases [Array<Petitest::TestCase>]
10
- def initialize(test_cases:)
11
- @test_cases = test_cases
12
- end
13
-
14
- # @note Override
15
- def to_s
16
- [
17
- heading,
18
- indent(body, 2),
19
- ].join("\n\n")
20
- end
21
-
22
- private
23
-
24
- # @return [String]
25
- def body
26
- errors_element_texts.join("\n\n")
27
- end
28
-
29
- # @return [Array<Petitest::Textx::FailuresElementText>]
30
- def errors_element_texts
31
- test_cases.map.with_index do |test_case, index|
32
- ::Petitest::Texts::ErrorsElementText.new(
33
- index: index,
34
- test_case: test_case,
35
- )
36
- end
37
- end
38
-
39
- # @return [String]
40
- def heading
41
- "Errors:"
42
- end
43
- end
44
- end
45
- end
@@ -1,26 +0,0 @@
1
- require "petitest/texts/base_text"
2
-
3
- module Petitest
4
- module Texts
5
- class FailureMessageText < ::Petitest::Texts::BaseText
6
- # @return [Petitest::TestCase]
7
- attr_reader :test_case
8
-
9
- # @param test_case [Petitest::TestCase]
10
- def initialize(test_case:)
11
- @test_case = test_case
12
- end
13
-
14
- # @note Override
15
- def to_s
16
- colorize(
17
- [
18
- test_case.failure_assertion_type_message,
19
- test_case.failure_additional_message,
20
- ].compact.join("\n"),
21
- :failure,
22
- )
23
- end
24
- end
25
- end
26
- end