assert 2.15.0 → 2.15.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -7
  2. data/Gemfile +0 -1
  3. data/{LICENSE.txt → LICENSE} +0 -0
  4. data/README.md +106 -35
  5. data/assert.gemspec +2 -2
  6. data/lib/assert/assert_runner.rb +19 -12
  7. data/lib/assert/assertions.rb +1 -0
  8. data/lib/assert/cli.rb +3 -0
  9. data/lib/assert/config.rb +24 -6
  10. data/lib/assert/config_helpers.rb +15 -28
  11. data/lib/assert/context.rb +4 -3
  12. data/lib/assert/context/test_dsl.rb +3 -2
  13. data/lib/assert/context_info.rb +19 -0
  14. data/lib/assert/default_runner.rb +12 -0
  15. data/lib/assert/default_suite.rb +64 -0
  16. data/lib/assert/default_view.rb +17 -15
  17. data/lib/assert/file_line.rb +3 -2
  18. data/lib/assert/result.rb +6 -0
  19. data/lib/assert/runner.rb +58 -21
  20. data/lib/assert/suite.rb +61 -100
  21. data/lib/assert/test.rb +3 -3
  22. data/lib/assert/version.rb +1 -1
  23. data/lib/assert/view.rb +58 -74
  24. data/lib/assert/view_helpers.rb +10 -48
  25. data/test/helper.rb +9 -0
  26. data/test/support/factory.rb +5 -5
  27. data/test/unit/assertions/assert_raises_tests.rb +20 -0
  28. data/test/unit/config_helpers_tests.rb +29 -29
  29. data/test/unit/config_tests.rb +43 -10
  30. data/test/unit/context/suite_dsl_tests.rb +1 -1
  31. data/test/unit/context_info_tests.rb +55 -0
  32. data/test/unit/default_runner_tests.rb +18 -0
  33. data/test/unit/default_suite_tests.rb +74 -0
  34. data/test/unit/file_line_tests.rb +6 -2
  35. data/test/unit/result_tests.rb +15 -4
  36. data/test/unit/runner_tests.rb +128 -6
  37. data/test/unit/suite_tests.rb +73 -182
  38. data/test/unit/view_helpers_tests.rb +44 -38
  39. data/test/unit/view_tests.rb +15 -39
  40. metadata +42 -28
  41. data/Rakefile +0 -1
@@ -99,7 +99,7 @@ module Assert
99
99
  scope = self.context_class.new(self, self.config, result_callback)
100
100
  start_time = Time.now
101
101
  capture_output do
102
- self.context_class.send('run_arounds', scope) do # TODO: why `send`?
102
+ self.context_class.run_arounds(scope) do
103
103
  run_test(scope, result_callback)
104
104
  end
105
105
  end
@@ -129,7 +129,7 @@ module Assert
129
129
  def run_test(scope, result_callback)
130
130
  begin
131
131
  # run any assert style 'setup do' setups
132
- self.context_class.send('run_setups', scope) # TODO: why `send`?
132
+ self.context_class.run_setups(scope)
133
133
  # run any test/unit style 'def setup' setups
134
134
  scope.setup if scope.respond_to?(:setup)
135
135
  # run the code block
@@ -145,7 +145,7 @@ module Assert
145
145
  ensure
146
146
  begin
147
147
  # run any assert style 'teardown do' teardowns
148
- self.context_class.send('run_teardowns', scope) # TODO: why `send`?
148
+ self.context_class.run_teardowns(scope)
149
149
  # run any test/unit style 'def teardown' teardowns
150
150
  scope.teardown if scope.respond_to?(:teardown)
151
151
  rescue Result::TestFailure => err
@@ -1,3 +1,3 @@
1
1
  module Assert
2
- VERSION = "2.15.0"
2
+ VERSION = "2.15.1"
3
3
  end
@@ -1,10 +1,13 @@
1
1
  require 'assert/config'
2
+ require 'assert/config_helpers'
2
3
  require 'assert/suite'
3
4
  require 'assert/view_helpers'
4
5
 
5
6
  module Assert
6
7
 
7
- module View
8
+ class View
9
+ include Assert::ConfigHelpers
10
+ include Assert::ViewHelpers
8
11
 
9
12
  # this method is used to bring in custom user-specific views
10
13
  # require views by passing either a full path to the view ruby file
@@ -26,87 +29,68 @@ module Assert
26
29
  end
27
30
  end
28
31
 
29
- class Base
30
- include Assert::ViewHelpers
32
+ # setup options and their default values
31
33
 
32
- # setup options and their default values
34
+ option 'styled', false
35
+ option 'pass_styles' # none
36
+ option 'fail_styles' # none
37
+ option 'error_styles' # none
38
+ option 'skip_styles' # none
39
+ option 'ignore_styles' # none
33
40
 
34
- option 'styled', false
35
- option 'pass_styles' # none
36
- option 'fail_styles' # none
37
- option 'error_styles' # none
38
- option 'skip_styles' # none
39
- option 'ignore_styles' # none
41
+ option 'pass_abbrev', '.'
42
+ option 'fail_abbrev', 'F'
43
+ option 'ignore_abbrev', 'I'
44
+ option 'skip_abbrev', 'S'
45
+ option 'error_abbrev', 'E'
40
46
 
41
- option 'pass_abbrev', '.'
42
- option 'fail_abbrev', 'F'
43
- option 'ignore_abbrev', 'I'
44
- option 'skip_abbrev', 'S'
45
- option 'error_abbrev', 'E'
47
+ attr_reader :config
46
48
 
47
- attr_reader :config
48
-
49
- def initialize(config, output_io)
50
- @config , @output_io, = config, output_io
51
- @output_io.sync = true if @output_io.respond_to?(:sync=)
52
- end
53
-
54
- def view
55
- self
56
- end
57
-
58
- def is_tty?
59
- !!@output_io.isatty
60
- end
61
-
62
- def ansi_styled_msg(msg, result_or_sym)
63
- return msg if !self.is_tty? || !self.styled
64
- code = Assert::ViewHelpers::Ansi.code_for(*self.send("#{result_or_sym.to_sym}_styles"))
65
- return msg if code.empty?
66
- code + msg + Assert::ViewHelpers::Ansi.code_for(:reset)
67
- end
68
-
69
- # Callbacks
70
-
71
- # define callback handlers to output information. handlers are
72
- # instance_eval'd in the scope of the view instance. any stdout is captured
73
- # and sent to the io stream.
74
-
75
- def fire(callback, *args)
76
- self.send(callback, *args)
77
- end
49
+ def initialize(config, output_io)
50
+ @config , @output_io, = config, output_io
51
+ @output_io.sync = true if @output_io.respond_to?(:sync=)
52
+ end
78
53
 
79
- # available callbacks from the runner:
80
- # * `before_load`: called at the beginning, before the suite is loaded
81
- # * `after_load`: called after the suite is loaded, just before `on_start`
82
- # functionally equivalent to `on_start`
83
- # * `on_start`: called when a loaded test suite starts running
84
- # * `before_test`: called before a test starts running
85
- # the test is passed as an arg
86
- # * `on_result`: called when a running tests generates a result
87
- # the result is passed as an arg
88
- # * `after_test`: called after a test finishes running
89
- # the test is passed as an arg
90
- # * `on_finish`: called when the test suite is finished running
91
- # * `on_interrupt`: called when the test suite is interrupted while running
92
- # the interrupt exception is passed as an arg
93
-
94
- def before_load(test_files); end
95
- def after_load; end
96
- def on_start; end
97
- def before_test(test); end
98
- def on_result(result); end
99
- def after_test(test); end
100
- def on_finish; end
101
- def on_interrupt(err); end
102
-
103
- # IO capture
104
-
105
- def puts(*args); @output_io.puts(*args); end
106
- def print(*args); @output_io.print(*args); end
54
+ def view; self; end
107
55
 
56
+ def is_tty?
57
+ !!@output_io.isatty
108
58
  end
109
59
 
60
+ # Callbacks
61
+
62
+ # define callback handlers to output information. These will be called
63
+ # by the test runner.
64
+
65
+ # available callbacks from the runner:
66
+ # * `before_load`: called at the beginning, before the suite is loaded
67
+ # * `after_load`: called after the suite is loaded, just before `on_start`
68
+ # functionally equivalent to `on_start`
69
+ # * `on_start`: called when a loaded test suite starts running
70
+ # * `before_test`: called before a test starts running
71
+ # the test is passed as an arg
72
+ # * `on_result`: called when a running tests generates a result
73
+ # the result is passed as an arg
74
+ # * `after_test`: called after a test finishes running
75
+ # the test is passed as an arg
76
+ # * `on_finish`: called when the test suite is finished running
77
+ # * `on_interrupt`: called when the test suite is interrupted while running
78
+ # the interrupt exception is passed as an arg
79
+
80
+ def before_load(test_files); end
81
+ def after_load; end
82
+ def on_start; end
83
+ def before_test(test); end
84
+ def on_result(result); end
85
+ def after_test(test); end
86
+ def on_finish; end
87
+ def on_interrupt(err); end
88
+
89
+ # IO capture
90
+
91
+ def puts(*args); @output_io.puts(*args); end
92
+ def print(*args); @output_io.print(*args); end
93
+
110
94
  end
111
95
 
112
96
  end
@@ -38,43 +38,6 @@ module Assert
38
38
  format % test.result_rate
39
39
  end
40
40
 
41
- # get all the result details for a set of tests
42
- def result_details_for(tests, result_order = :normal)
43
- test_index = 0
44
- tests.collect do |test|
45
- test_index += 1
46
-
47
- details = test.results.collect do |result|
48
- ResultDetails.new(result, test, test_index)
49
- end
50
- details.reverse! if result_order == :reversed
51
- details
52
- end.compact.flatten
53
- end
54
-
55
- # get all the result details for a set of tests matching a file or context
56
- def matched_result_details_for(match, tests, result_order = :normal)
57
- context_match = match.kind_of?(Class) && match.ancestors.include?(Assert::Context)
58
- file_match = match.kind_of?(String)
59
-
60
- matching_tests = if context_match
61
- tests.select {|test| test.context_info.klass == match}
62
- elsif file_match
63
- tests.select {|test| test.context_info.file == match}
64
- else
65
- tests
66
- end
67
-
68
- result_details_for(matching_tests, result_order)
69
- end
70
-
71
- # only show result details for failed or errored results
72
- # show result details if a skip or passed result was issues w/ a message
73
- def show_result_details?(result)
74
- [:fail, :error].include?(result.to_sym) ||
75
- !!([:skip, :ignore].include?(result.to_sym) && result.message)
76
- end
77
-
78
41
  # show any captured output
79
42
  def captured_output(output)
80
43
  "--- stdout ---\n"\
@@ -82,6 +45,11 @@ module Assert
82
45
  "--------------"
83
46
  end
84
47
 
48
+ # show any captured output
49
+ def re_run_test_cmd(test_id)
50
+ "assert -t #{test_id.gsub(Dir.pwd, '.')}"
51
+ end
52
+
85
53
  def test_count_statement
86
54
  "#{self.count(:tests)} test#{'s' if self.count(:tests) != 1}"
87
55
  end
@@ -218,17 +186,11 @@ module Assert
218
186
  style_names.map{ |n| "\e[#{CODES[n]}m" if CODES.key?(n) }.compact.join('')
219
187
  end
220
188
 
221
- end
222
-
223
- class ResultDetails
224
-
225
- attr_reader :result, :test_index, :test, :output
226
-
227
- def initialize(result, test, test_index)
228
- @result = result
229
- @test = test
230
- @test_index = test_index
231
- @output = test.output
189
+ def ansi_styled_msg(msg, result_or_sym)
190
+ return msg if !self.is_tty? || !self.styled
191
+ code = Assert::ViewHelpers::Ansi.code_for(*self.send("#{result_or_sym.to_sym}_styles"))
192
+ return msg if code.empty?
193
+ code + msg + Assert::ViewHelpers::Ansi.code_for(:reset)
232
194
  end
233
195
 
234
196
  end
@@ -8,3 +8,12 @@ $LOAD_PATH.unshift(ROOT_PATH)
8
8
  # require pry for debugging (`binding.pry`)
9
9
  require 'pry'
10
10
  require 'test/support/factory'
11
+
12
+ # 1.8.7 backfills
13
+
14
+ # Array#sample
15
+ if !(a = Array.new).respond_to?(:sample) && a.respond_to?(:choice)
16
+ class Array
17
+ alias_method :sample, :choice
18
+ end
19
+ end
@@ -1,18 +1,18 @@
1
1
  require 'assert/config'
2
+ require 'assert/default_suite'
2
3
  require 'assert/factory'
3
4
  require 'assert/result'
4
- require 'assert/suite'
5
5
  require 'assert/test'
6
6
 
7
7
  module Factory
8
8
  extend Assert::Factory
9
9
 
10
10
  def self.context_info_called_from
11
- "#{Factory.path}_tests.rb:#{Factory.integer}"
11
+ File.expand_path("#{Factory.path}_tests.rb:#{Factory.integer}", Dir.pwd)
12
12
  end
13
13
 
14
14
  def self.context_info(context_klass = nil)
15
- Assert::Suite::ContextInfo.new(context_klass || self.context_class, context_info_called_from)
15
+ Assert::ContextInfo.new(context_klass || self.context_class, context_info_called_from)
16
16
  end
17
17
 
18
18
  # Generate an anonymous `Context` inherited from `Assert::Context` by default.
@@ -34,7 +34,7 @@ module Factory
34
34
  def self.test(*args, &block)
35
35
  config, context_info, name = [
36
36
  args.last.kind_of?(Assert::Config) ? args.pop : self.modes_off_config,
37
- args.last.kind_of?(Assert::Suite::ContextInfo) ? args.pop : self.context_info,
37
+ args.last.kind_of?(Assert::ContextInfo) ? args.pop : self.context_info,
38
38
  args.last.kind_of?(::String) ? args.pop : 'a test'
39
39
  ]
40
40
  Assert::Test.for_block(name, context_info, config, &block)
@@ -75,7 +75,7 @@ module Factory
75
75
  end
76
76
 
77
77
  def self.modes_off_suite
78
- Assert::Suite.new(self.modes_off_config)
78
+ Assert::DefaultSuite.new(self.modes_off_config)
79
79
  end
80
80
 
81
81
  def self.modes_off_context_class(*args, &block)
@@ -35,6 +35,26 @@ module Assert::Assertions
35
35
  messages.each_with_index{ |msg, n| assert_match /^#{exp[n]}/, msg }
36
36
  end
37
37
 
38
+ should "return any raised exception instance" do
39
+ error = nil
40
+ error_msg = Factory.string
41
+ test = Factory.test do
42
+ error = assert_raises(RuntimeError){ raise(RuntimeError, error_msg) }
43
+ end
44
+ test.run
45
+
46
+ assert_not_nil error
47
+ assert_kind_of RuntimeError, error
48
+ assert_equal error_msg, error.message
49
+
50
+ test = Factory.test do
51
+ error = assert_raises(RuntimeError){ }
52
+ end
53
+ test.run
54
+
55
+ assert_nil error
56
+ end
57
+
38
58
  end
39
59
 
40
60
  class AssertNothingRaisedTests < Assert::Context
@@ -14,26 +14,46 @@ module Assert::ConfigHelpers
14
14
  def config
15
15
  # use the assert config since it has tests, contexts, etc
16
16
  # also maybe use a fresh config that is empty
17
- @config ||= [Assert.config, Assert::Config.new].choice
17
+ @config ||= [Assert.config, Assert::Config.new].sample
18
18
  end
19
19
  end
20
20
  @helpers = @helpers_class.new
21
21
  end
22
22
  subject{ @helpers }
23
23
 
24
- should have_imeths :runner_seed, :count, :tests?, :all_pass?
25
- should have_imeths :run_time, :test_rate, :result_rate
26
- should have_imeths :suite_contexts, :ordered_suite_contexts
27
- should have_imeths :suite_files, :ordered_suite_files
24
+ should have_imeths :runner, :suite, :view
25
+ should have_imeths :runner_seed, :single_test?, :single_test_file_line
26
+ should have_imeths :count, :tests?, :all_pass?
27
+ should have_imeths :formatted_run_time
28
+ should have_imeths :formatted_test_rate, :formatted_result_rate
28
29
  should have_imeths :show_test_profile_info?, :show_test_verbose_info?
29
30
  should have_imeths :ocurring_result_types
30
31
 
32
+ should "know the config's runner, suite and view" do
33
+ assert_equal subject.config.runner, subject.runner
34
+ assert_equal subject.config.suite, subject.suite
35
+ assert_equal subject.config.view, subject.view
36
+ end
37
+
31
38
  should "know its runner seed" do
32
39
  assert_equal subject.config.runner_seed, subject.runner_seed
33
40
  end
34
41
 
42
+ should "know if it is in single test mode" do
43
+ Assert.stub(subject.config, :single_test?){ true }
44
+ assert_true subject.single_test?
45
+
46
+ Assert.stub(subject.config, :single_test?){ false }
47
+ assert_false subject.single_test?
48
+ end
49
+
50
+ should "know its single test file line" do
51
+ exp = subject.config.single_test_file_line
52
+ assert_equal exp, subject.single_test_file_line
53
+ end
54
+
35
55
  should "know how to count things on the suite" do
36
- thing = [:pass, :fail, :results, :tests].choice
56
+ thing = [:pass, :fail, :results, :tests].sample
37
57
  assert_equal subject.config.suite.count(thing), subject.count(thing)
38
58
  end
39
59
 
@@ -46,33 +66,13 @@ module Assert::ConfigHelpers
46
66
  format = '%.6f'
47
67
 
48
68
  exp = format % subject.config.suite.run_time
49
- assert_equal exp, subject.run_time(format)
69
+ assert_equal exp, subject.formatted_run_time(format)
50
70
 
51
71
  exp = format % subject.config.suite.test_rate
52
- assert_equal exp, subject.test_rate(format)
72
+ assert_equal exp, subject.formatted_test_rate(format)
53
73
 
54
74
  exp = format % subject.config.suite.result_rate
55
- assert_equal exp, subject.result_rate(format)
56
- end
57
-
58
- should "know its suite contexts and ordered suite contexts" do
59
- exp = subject.config.suite.tests.inject([]) do |contexts, test|
60
- contexts << test.context_info.klass
61
- end.uniq
62
- assert_equal exp, subject.suite_contexts
63
-
64
- exp = subject.suite_contexts.sort{ |a,b| a.to_s <=> b.to_s }
65
- assert_equal exp, subject.ordered_suite_contexts
66
- end
67
-
68
- should "know its suite files and ordered suite files" do
69
- exp = subject.config.suite.tests.inject([]) do |files, test|
70
- files << test.context_info.file
71
- end.uniq
72
- assert_equal exp, subject.suite_files
73
-
74
- exp = subject.suite_files.sort{ |a,b| a.to_s <=> b.to_s }
75
- assert_equal exp, subject.ordered_suite_files
75
+ assert_equal exp, subject.formatted_result_rate(format)
76
76
  end
77
77
 
78
78
  should "know whether to show test profile info" do
@@ -1,9 +1,11 @@
1
1
  require 'assert'
2
2
  require 'assert/config'
3
3
 
4
+ require 'assert/default_runner'
5
+ require 'assert/default_suite'
4
6
  require 'assert/default_view'
7
+ require 'assert/file_line'
5
8
  require 'assert/runner'
6
- require 'assert/suite'
7
9
 
8
10
  class Assert::Config
9
11
 
@@ -14,22 +16,24 @@ class Assert::Config
14
16
  end
15
17
  subject{ @config }
16
18
 
17
- should have_imeths :suite, :view, :runner
19
+ should have_imeths :view, :suite, :runner
18
20
  should have_imeths :test_dir, :test_helper, :test_file_suffixes
19
21
  should have_imeths :changed_proc, :pp_proc, :use_diff_proc, :run_diff_proc
20
- should have_imeths :runner_seed, :changed_only, :changed_ref, :pp_objects
21
- should have_imeths :capture_output, :halt_on_fail, :profile, :verbose, :list
22
- should have_imeths :debug, :apply
22
+ should have_imeths :runner_seed, :changed_only, :changed_ref, :single_test
23
+ should have_imeths :pp_objects, :capture_output, :halt_on_fail, :profile
24
+ should have_imeths :verbose, :list, :debug
25
+ should have_imeths :apply, :single_test?
26
+ should have_imeths :single_test_file_line, :single_test_file_path
23
27
 
24
28
  should "default the view, suite, and runner" do
25
- assert_kind_of Assert::DefaultView, subject.view
26
- assert_kind_of Assert::Suite, subject.suite
27
- assert_kind_of Assert::Runner, subject.runner
29
+ assert_kind_of Assert::DefaultView, subject.view
30
+ assert_kind_of Assert::DefaultSuite, subject.suite
31
+ assert_kind_of Assert::DefaultRunner, subject.runner
28
32
  end
29
33
 
30
34
  should "default the test dir/helper/suffixes" do
31
- assert_equal 'test', subject.test_dir
32
- assert_equal 'helper.rb', subject.test_helper
35
+ assert_equal 'test', subject.test_dir
36
+ assert_equal 'helper.rb', subject.test_helper
33
37
  assert_equal ['_tests.rb', "_test.rb"], subject.test_file_suffixes
34
38
  end
35
39
 
@@ -44,6 +48,7 @@ class Assert::Config
44
48
  assert_not_nil subject.runner_seed
45
49
  assert_not subject.changed_only
46
50
  assert_empty subject.changed_ref
51
+ assert_empty subject.single_test
47
52
  assert_not subject.pp_objects
48
53
  assert_not subject.capture_output
49
54
  assert subject.halt_on_fail
@@ -62,6 +67,34 @@ class Assert::Config
62
67
  assert_not Assert::Config.new(:halt_on_fail => false).halt_on_fail
63
68
  end
64
69
 
70
+ should "know if it is in single test mode" do
71
+ assert_false subject.single_test?
72
+
73
+ subject.apply(:single_test => Factory.string)
74
+ assert_true subject.single_test?
75
+ end
76
+
77
+ should "know its single test file line" do
78
+ exp = Assert::FileLine.parse(File.expand_path('', Dir.pwd))
79
+ assert_equal exp, subject.single_test_file_line
80
+
81
+ file_line_path = "#{Factory.path}_tests.rb:#{Factory.integer}"
82
+ subject.apply(:single_test => file_line_path)
83
+
84
+ exp = Assert::FileLine.parse(File.expand_path(file_line_path, Dir.pwd))
85
+ assert_equal exp, subject.single_test_file_line
86
+ end
87
+
88
+ should "know its single test file path" do
89
+ exp = Assert::FileLine.parse(File.expand_path('', Dir.pwd)).file
90
+ assert_equal exp, subject.single_test_file_path
91
+
92
+ path = "#{Factory.path}_tests.rb"
93
+ file_line_path = "#{path}:#{Factory.integer}"
94
+ subject.apply(:single_test => file_line_path)
95
+ assert_equal File.expand_path(path, Dir.pwd), subject.single_test_file_path
96
+ end
97
+
65
98
  end
66
99
 
67
100
  end