petitest 0.2.0 → 0.2.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: 94d661afca2ae26812fcfd79ffd3c54e1fbd4085
4
- data.tar.gz: 26ce49148e23ff5e854b4ae1898ab7c11250a81b
3
+ metadata.gz: bea44c83eede7392c848c866de9ad3f02e8bf6cd
4
+ data.tar.gz: 6de656fb3130c545bd98bb7702f16e281b9e8d04
5
5
  SHA512:
6
- metadata.gz: 4e6243251265c19413ffff8923419cccf9c436e9c12ae058abb22d9545464270ac294ed87bec5d3f57ddb350fd9910e96013d947c820e3295e1cebe6bb7a5ce5
7
- data.tar.gz: b68470322a44f4155d482e0d154758f2c09188a3f670452b0542a03d8087f950506e899ba10c0aa1c3fa9bca82c6e2dccef4c6c15e62d984ed9a04c02e3f935a
6
+ metadata.gz: 2f46a03a8de9f1cbfa1a689a06a8883671f7ddc36429adebf2e1beef431498735f87a0d1739a03dcee74af70b87d8b2e42e61b9fd64ccf7ca0e1dc2c73739068
7
+ data.tar.gz: '0550090e2cf0f0b70d950d25785446af0030d73cf862d2f4160d62461ffe3b0d6e09d1bf2c2da64c67e8473150be9725393b40ff3bec4288ec4aab215351dd98'
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## v0.2.1
2
+
3
+ - Add `after_running_test_group` and `before_running_test_group` events
4
+ - Add `Petitest::TestGroup#nest_level`
5
+ - Add `Petitest::TestGroup#sub_test_group`
6
+ - Add document style report
7
+ - Improve error report format
8
+ - Improve default `#assert` message
9
+
1
10
  ## v0.2.0
2
11
 
3
12
  - Merge errors into failures
data/README.md CHANGED
@@ -32,7 +32,6 @@ gem install petitest
32
32
  Define a child class of `Petitest::TestGroup` with `#test_xxx` methods.
33
33
 
34
34
  ```ruby
35
- # test/some_test.rb
36
35
  require "petitest/autorun"
37
36
 
38
37
  class Sometest < Petitest::TestGroup
@@ -71,43 +70,42 @@ ruby test/sometest_test.rb
71
70
  ```
72
71
 
73
72
  ```
74
- .FFE..
73
+ .FFF..
75
74
 
76
75
  Failures:
77
76
 
78
- 1) Sometest#test_false
77
+ 1) PetitestTest#test_false
79
78
  assert(false)
80
- false is not truthy
81
- # test/sometest_test.rb:9:in `test_false'
79
+ Expected false to be truthy
80
+ # test/petitest_test.rb:9:in `test_false'
82
81
 
83
- 2) Sometest#test_nil
82
+ 2) PetitestTest#test_nil
84
83
  assert(nil)
85
- nil is not truthy
86
- # test/sometest_test.rb:13:in `test_nil'
84
+ Expected nil to be truthy
85
+ # test/petitest_test.rb:13:in `test_nil'
87
86
 
88
- Errors:
89
-
90
- 1) Sometest#test_raise
87
+ 3) PetitestTest#test_raise
91
88
  raise
92
- RuntimeError:
93
- # test/sometest_test.rb:17:in `test_raise'
89
+ RuntimeError
90
+
91
+ # test/petitest_test.rb:17:in `test_raise'
94
92
 
95
93
  Counts:
96
94
 
97
95
  6 tests
98
96
  3 passes
99
- 2 failures
100
- 1 errors
97
+ 3 failures
101
98
  0 skips
102
99
 
103
100
  Times:
104
101
 
105
- Started: 2017-03-22T05:40:32.846640+09:00
106
- Finished: 2017-03-22T05:40:32.846784+09:00
107
- Total: 0.000144s
102
+ Started: 2017-03-24T03:09:17.776418+09:00
103
+ Finished: 2017-03-24T03:09:17.776527+09:00
104
+ Total: 0.000109s
108
105
  ```
109
106
 
110
107
  ## Plug-ins
111
108
 
109
+ - https://github.com/petitest/petitest-assertions
112
110
  - https://github.com/petitest/petitest-power_assert
113
111
  - https://github.com/petitest/petitest-tap
data/lib/petitest.rb CHANGED
@@ -3,11 +3,13 @@ require "petitest/configuration"
3
3
  require "petitest/subscriber_concerns/output_concern"
4
4
  require "petitest/subscriber_concerns/time_concern"
5
5
  require "petitest/subscribers/base_subscriber"
6
+ require "petitest/subscribers/document_report_subscriber"
6
7
  require "petitest/subscribers/json_report_subscriber"
7
8
  require "petitest/subscribers/progress_report_subscriber"
8
9
  require "petitest/test_case"
9
10
  require "petitest/test_cases_runner"
10
11
  require "petitest/test_group"
12
+ require "petitest/test_groups"
11
13
  require "petitest/test_method"
12
14
  require "petitest/texts/base_text"
13
15
  require "petitest/texts/error_message_text"
@@ -16,6 +18,7 @@ require "petitest/texts/failures_text"
16
18
  require "petitest/texts/filtered_backtrace_text"
17
19
  require "petitest/texts/raised_code_text"
18
20
  require "petitest/texts/test_case_result_character_text"
21
+ require "petitest/texts/test_case_result_line_text"
19
22
  require "petitest/texts/test_cases_result_margin_top_text"
20
23
  require "petitest/texts/test_cases_result_text"
21
24
  require "petitest/texts/test_counts_text"
@@ -4,8 +4,7 @@ at_exit do
4
4
  if $! && !($!.is_a?(::SystemExit) && $!.success?)
5
5
  next
6
6
  end
7
- test_cases = Petitest::TestGroup.test_cases
8
- result = Petitest::TestCasesRunner.new(test_cases).run
7
+ result = Petitest::TestCasesRunner.new(Petitest::TestGroup.children).run
9
8
  exit_code = result ? 0 : 1
10
9
  exit(exit_code)
11
10
  end
@@ -42,7 +42,7 @@ module Petitest
42
42
 
43
43
  # @return [Array<Petitest::Subscribers::BaseSubscriber>]
44
44
  def subscribers
45
- @subscribers ||= [::Petitest::Subscribers::ProgressReportSubscriber.new]
45
+ @subscribers ||= [::Petitest::Subscribers::DocumentReportSubscriber.new]
46
46
  end
47
47
  end
48
48
  end
@@ -9,6 +9,10 @@ module Petitest
9
9
  def after_running_test_cases(test_cases)
10
10
  end
11
11
 
12
+ # @param test_group [Class]
13
+ def after_running_test_group(test_group)
14
+ end
15
+
12
16
  # @param test_case [Petit::TestCase]
13
17
  def before_running_test_case(test_case)
14
18
  end
@@ -16,6 +20,10 @@ module Petitest
16
20
  # @param test_cases [Array<Petit::TestCase>]
17
21
  def before_running_test_cases(test_cases)
18
22
  end
23
+
24
+ # @param test_group [Class]
25
+ def before_running_test_group(test_group)
26
+ end
19
27
  end
20
28
  end
21
29
  end
@@ -0,0 +1,37 @@
1
+ require "petitest/subscribers/base_subscriber"
2
+ require "petitest/subscriber_concerns/output_concern"
3
+ require "petitest/subscriber_concerns/time_concern"
4
+
5
+ module Petitest
6
+ module Subscribers
7
+ class DocumentReportSubscriber < ::Petitest::Subscribers::BaseSubscriber
8
+ include ::Petitest::SubscriberConcerns::OutputConcern
9
+ include ::Petitest::SubscriberConcerns::TimeConcern
10
+
11
+ # @note Override
12
+ def after_running_test_case(test_case)
13
+ super
14
+ string = ::Petitest::Texts::TestCaseResultLineText.new(test_case: test_case).to_s
15
+ output.puts(string)
16
+ end
17
+
18
+ # @note Override
19
+ def after_running_test_cases(test_cases)
20
+ super
21
+ string = ::Petitest::Texts::TestCasesResultText.new(
22
+ finished_at: finished_at,
23
+ started_at: started_at,
24
+ test_cases: test_cases,
25
+ ).to_s
26
+ output.puts(string)
27
+ end
28
+
29
+ # @note Override
30
+ def before_running_test_group(test_group_class)
31
+ super
32
+ string = "#{' ' * test_group_class.nest_level}#{test_group_class.description}"
33
+ output.puts(string)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -34,6 +34,19 @@ module Petitest
34
34
  end
35
35
  end
36
36
 
37
+ # @return [String]
38
+ def description
39
+ @description ||= "##{method_name}"
40
+ end
41
+
42
+ # @return [String]
43
+ def full_description
44
+ [
45
+ test_group_class.full_description,
46
+ description,
47
+ ].join(" ")
48
+ end
49
+
37
50
  # @return [String, nil]
38
51
  def error_class_name
39
52
  if error
@@ -1,20 +1,19 @@
1
1
  module Petitest
2
2
  class TestCasesRunner
3
- # @return [Array<Petitest::TestCase>]
4
- attr_reader :test_cases
3
+ # @return [Array<Class>]
4
+ attr_reader :test_group_classes
5
5
 
6
- # @param test_cases [Array<Petitest::TestCase>]
7
- def initialize(test_cases)
8
- @test_cases = test_cases
6
+ # @param test_group_classes [Array<Class>]
7
+ def initialize(test_group_classes)
8
+ @test_group_classes = test_group_classes
9
9
  end
10
10
 
11
11
  # @return [Boolean]
12
12
  def run
13
+ test_cases = test_group_classes.flat_map(&:test_cases_and_children_test_cases)
13
14
  before_running_test_cases(test_cases)
14
- test_cases.each do |test_case|
15
- before_running_test_case(test_case)
16
- test_case.run
17
- after_running_test_case(test_case)
15
+ test_group_classes.each do |test_group_class|
16
+ run_test_group(test_group_class)
18
17
  end
19
18
  after_running_test_cases(test_cases)
20
19
  test_cases.all?(&:passed?)
@@ -36,6 +35,13 @@ module Petitest
36
35
  end
37
36
  end
38
37
 
38
+ # @param test_group_class [Class]
39
+ def after_running_test_group(test_group_class)
40
+ subscribers.each do |subscriber|
41
+ subscriber.after_running_test_group(test_group_class)
42
+ end
43
+ end
44
+
39
45
  # @param test_case [Petitest::TestCase]
40
46
  def before_running_test_case(test_case)
41
47
  subscribers.each do |subscriber|
@@ -50,6 +56,32 @@ module Petitest
50
56
  end
51
57
  end
52
58
 
59
+ # @param test_group_class [Class]
60
+ def before_running_test_group(test_group_class)
61
+ subscribers.each do |subscriber|
62
+ subscriber.before_running_test_group(test_group_class)
63
+ end
64
+ end
65
+
66
+ # @param test_case [Petitest::TestCase]
67
+ def run_test_case(test_case)
68
+ before_running_test_case(test_case)
69
+ test_case.run
70
+ after_running_test_case(test_case)
71
+ end
72
+
73
+ # @param test_group_class [Class]
74
+ def run_test_group(test_group_class)
75
+ before_running_test_group(test_group_class)
76
+ test_group_class.test_cases.each do |test_case|
77
+ run_test_case(test_case)
78
+ end
79
+ test_group_class.children.each do |child_test_group_class|
80
+ run_test_group(child_test_group_class)
81
+ end
82
+ after_running_test_group(test_group_class)
83
+ end
84
+
53
85
  # @return [Array<Petitest::Subscribers::BaseSubscriber>]
54
86
  def subscribers
55
87
  ::Petitest.configuration.subscribers
@@ -3,29 +3,78 @@ module Petitest
3
3
  TEST_METHOD_NAME_PREFIX = "test_"
4
4
 
5
5
  class << self
6
+ attr_writer :description
7
+
8
+ attr_writer :metadata
9
+
10
+ attr_writer :nest_level
11
+
12
+ # @return [Array<Class>]
13
+ def children
14
+ @children ||= []
15
+ end
16
+
6
17
  # @return [Array<Class>]
7
18
  def descendants
8
- @@descendants ||= []
19
+ children.flat_map(&:children)
20
+ end
21
+
22
+ # @return [String]
23
+ def description
24
+ @description ||= name
25
+ end
26
+
27
+ # @return [String, nil]
28
+ def full_description
29
+ descriptions = concrete_test_group_ancestors.reverse.map(&:description)
30
+ unless descriptions.empty?
31
+ descriptions.join(" ")
32
+ end
9
33
  end
10
34
 
11
35
  # @note Override
12
- def inherited(sub_class)
36
+ def inherited(child)
13
37
  super
14
- descendants << sub_class
38
+ children << child
39
+ end
40
+
41
+ # @return [Hash{Symbol => Object}]
42
+ def metadata
43
+ @metadata ||= {}
44
+ end
45
+
46
+ # @return [Integer]
47
+ def nest_level
48
+ @nest_level ||= 0
49
+ end
50
+
51
+ # @param description [String]
52
+ # @param metadata [Hash{Symbol => Object}]
53
+ def sub_test_group(description, metadata = {}, &block)
54
+ child = ::Class.new(self)
55
+ child.nest_level = nest_level + 1
56
+ child.description = description
57
+ child.metadata = self.metadata.merge(metadata)
58
+ child.undefine_test_methods
59
+ child.class_eval(&block)
60
+ child
15
61
  end
16
62
 
17
63
  # @return [Array<Petit::TestCase>]
18
64
  def test_cases
19
- descendants.flat_map do |test_group_class|
20
- test_group_class.test_methods.map do |test_method|
21
- ::Petitest::TestCase.new(
22
- test_group_class: test_group_class,
23
- test_method: test_method,
24
- )
25
- end
65
+ @test_cases ||= test_methods.map do |test_method|
66
+ ::Petitest::TestCase.new(
67
+ test_group_class: self,
68
+ test_method: test_method,
69
+ )
26
70
  end
27
71
  end
28
72
 
73
+ # @return [Array<Petit::TestCase>]
74
+ def test_cases_and_children_test_cases
75
+ test_cases + children.flat_map(&:test_cases_and_children_test_cases)
76
+ end
77
+
29
78
  # @return [Array<String>]
30
79
  def test_method_names
31
80
  public_instance_methods.map(&:to_s).select do |method_name|
@@ -44,6 +93,26 @@ module Petitest
44
93
  )
45
94
  end
46
95
  end
96
+
97
+ def undefine_test_methods
98
+ test_method_names.each do |method_name|
99
+ undef_method(method_name)
100
+ end
101
+ end
102
+
103
+ private
104
+
105
+ # @return [Array<Class>]
106
+ def concrete_test_group_ancestors
107
+ ancestors.each_with_object([]) do |klass, classes|
108
+ if klass == ::Petitest::TestGroup
109
+ break classes
110
+ end
111
+ if klass.is_a?(::Class)
112
+ classes << klass
113
+ end
114
+ end
115
+ end
47
116
  end
48
117
 
49
118
  # @param actual_or_message [Object]
@@ -51,10 +120,10 @@ module Petitest
51
120
  def assert(actual_or_message = nil, message = nil, &block)
52
121
  if block
53
122
  message = actual_or_message
54
- check(message || "Given block returned falsy", &block)
123
+ check(message || "Expected given block to return truthy", &block)
55
124
  else
56
125
  actual = actual_or_message
57
- check(message || "#{actual.inspect} is not truthy") do
126
+ check(message || "Expected #{actual.inspect} to be truthy") do
58
127
  actual
59
128
  end
60
129
  end
@@ -0,0 +1,4 @@
1
+ module Petitest
2
+ module TestGroups
3
+ end
4
+ end
@@ -13,7 +13,11 @@ module Petitest
13
13
 
14
14
  # @note Override
15
15
  def to_s
16
- colorize("#{test_case.error_class_name}: #{test_case.error_message}", :error)
16
+ elements = []
17
+ elements << test_case.error_class_name unless test_case.error.is_a?(::Petitest::AssertionFailureError)
18
+ elements << test_case.error_message unless test_case.error_message.empty?
19
+ string = elements.join("\n")
20
+ colorize(string, :error)
17
21
  end
18
22
  end
19
23
  end
@@ -0,0 +1,37 @@
1
+ require "petitest/texts/base_text"
2
+
3
+ module Petitest
4
+ module Texts
5
+ class TestCaseResultLineText < ::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
+ indent(
17
+ colorize("##{test_case.test_method.method_name}", color_type),
18
+ 2 * (test_case.test_group_class.nest_level + 1),
19
+ )
20
+ end
21
+
22
+ private
23
+
24
+ # @return [Symbol]
25
+ def color_type
26
+ case
27
+ when test_case.failed?
28
+ :error
29
+ when test_case.skipped?
30
+ :skip
31
+ else
32
+ :pass
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,3 +1,3 @@
1
1
  module Petitest
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/petitest.gemspec CHANGED
@@ -20,5 +20,6 @@ Gem::Specification.new do |spec|
20
20
  spec.required_ruby_version = ">= 2.1.0"
21
21
 
22
22
  spec.add_development_dependency "bundler", "~> 1.14"
23
+ spec.add_development_dependency "petitest-power_assert"
23
24
  spec.add_development_dependency "rake", "~> 10.0"
24
25
  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.2.0
4
+ version: 0.2.1
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-23 00:00:00.000000000 Z
11
+ date: 2017-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: petitest-power_assert
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -58,11 +72,13 @@ files:
58
72
  - lib/petitest/subscriber_concerns/output_concern.rb
59
73
  - lib/petitest/subscriber_concerns/time_concern.rb
60
74
  - lib/petitest/subscribers/base_subscriber.rb
75
+ - lib/petitest/subscribers/document_report_subscriber.rb
61
76
  - lib/petitest/subscribers/json_report_subscriber.rb
62
77
  - lib/petitest/subscribers/progress_report_subscriber.rb
63
78
  - lib/petitest/test_case.rb
64
79
  - lib/petitest/test_cases_runner.rb
65
80
  - lib/petitest/test_group.rb
81
+ - lib/petitest/test_groups.rb
66
82
  - lib/petitest/test_method.rb
67
83
  - lib/petitest/texts/base_text.rb
68
84
  - lib/petitest/texts/error_message_text.rb
@@ -71,6 +87,7 @@ files:
71
87
  - lib/petitest/texts/filtered_backtrace_text.rb
72
88
  - lib/petitest/texts/raised_code_text.rb
73
89
  - lib/petitest/texts/test_case_result_character_text.rb
90
+ - lib/petitest/texts/test_case_result_line_text.rb
74
91
  - lib/petitest/texts/test_cases_result_margin_top_text.rb
75
92
  - lib/petitest/texts/test_cases_result_text.rb
76
93
  - lib/petitest/texts/test_counts_text.rb