assert 2.19.0 → 2.19.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +4 -2
  3. data/assert.gemspec +11 -6
  4. data/bin/assert +1 -0
  5. data/lib/assert.rb +20 -6
  6. data/lib/assert/actual_value.rb +11 -6
  7. data/lib/assert/assert_runner.rb +38 -17
  8. data/lib/assert/assertions.rb +85 -50
  9. data/lib/assert/cli.rb +32 -70
  10. data/lib/assert/clirb.rb +55 -0
  11. data/lib/assert/config.rb +22 -8
  12. data/lib/assert/config_helpers.rb +57 -22
  13. data/lib/assert/context.rb +16 -18
  14. data/lib/assert/context/let_dsl.rb +8 -2
  15. data/lib/assert/context/method_missing.rb +3 -0
  16. data/lib/assert/context/setup_dsl.rb +24 -16
  17. data/lib/assert/context/subject_dsl.rb +9 -7
  18. data/lib/assert/context/suite_dsl.rb +5 -1
  19. data/lib/assert/context/test_dsl.rb +58 -19
  20. data/lib/assert/context_info.rb +2 -0
  21. data/lib/assert/default_runner.rb +2 -0
  22. data/lib/assert/default_suite.rb +27 -15
  23. data/lib/assert/default_view.rb +49 -30
  24. data/lib/assert/factory.rb +2 -0
  25. data/lib/assert/file_line.rb +8 -6
  26. data/lib/assert/macro.rb +3 -1
  27. data/lib/assert/macros/methods.rb +73 -45
  28. data/lib/assert/result.rb +114 -62
  29. data/lib/assert/runner.rb +70 -51
  30. data/lib/assert/stub.rb +44 -3
  31. data/lib/assert/suite.rb +69 -28
  32. data/lib/assert/test.rb +43 -36
  33. data/lib/assert/utils.rb +22 -11
  34. data/lib/assert/version.rb +3 -1
  35. data/lib/assert/view.rb +46 -18
  36. data/lib/assert/view_helpers.rb +102 -92
  37. data/test/helper.rb +8 -4
  38. data/test/support/factory.rb +40 -21
  39. data/test/support/inherited_stuff.rb +2 -0
  40. data/test/system/stub_tests.rb +182 -144
  41. data/test/system/test_tests.rb +88 -60
  42. data/test/unit/actual_value_tests.rb +71 -50
  43. data/test/unit/assert_tests.rb +42 -23
  44. data/test/unit/assertions/assert_block_tests.rb +12 -10
  45. data/test/unit/assertions/assert_changes_tests.rb +27 -21
  46. data/test/unit/assertions/assert_empty_tests.rb +16 -12
  47. data/test/unit/assertions/assert_equal_tests.rb +28 -26
  48. data/test/unit/assertions/assert_file_exists_tests.rb +17 -13
  49. data/test/unit/assertions/assert_includes_tests.rb +12 -10
  50. data/test/unit/assertions/assert_instance_of_tests.rb +16 -14
  51. data/test/unit/assertions/assert_is_a_tests.rb +128 -0
  52. data/test/unit/assertions/assert_match_tests.rb +12 -10
  53. data/test/unit/assertions/assert_nil_tests.rb +18 -12
  54. data/test/unit/assertions/assert_raises_tests.rb +29 -20
  55. data/test/unit/assertions/assert_respond_to_tests.rb +12 -10
  56. data/test/unit/assertions/assert_same_tests.rb +26 -24
  57. data/test/unit/assertions/assert_true_false_tests.rb +34 -24
  58. data/test/unit/assertions_tests.rb +16 -9
  59. data/test/unit/config_helpers_tests.rb +17 -10
  60. data/test/unit/config_tests.rb +36 -9
  61. data/test/unit/context/let_dsl_tests.rb +2 -0
  62. data/test/unit/context/setup_dsl_tests.rb +26 -14
  63. data/test/unit/context/subject_dsl_tests.rb +5 -3
  64. data/test/unit/context/suite_dsl_tests.rb +6 -4
  65. data/test/unit/context/test_dsl_tests.rb +39 -17
  66. data/test/unit/context_info_tests.rb +6 -4
  67. data/test/unit/context_tests.rb +112 -54
  68. data/test/unit/default_runner_tests.rb +2 -0
  69. data/test/unit/default_suite_tests.rb +12 -6
  70. data/test/unit/factory_tests.rb +4 -2
  71. data/test/unit/file_line_tests.rb +9 -7
  72. data/test/unit/macro_tests.rb +13 -11
  73. data/test/unit/result_tests.rb +49 -41
  74. data/test/unit/runner_tests.rb +33 -18
  75. data/test/unit/suite_tests.rb +39 -15
  76. data/test/unit/test_tests.rb +65 -50
  77. data/test/unit/utils_tests.rb +52 -37
  78. data/test/unit/view_helpers_tests.rb +23 -14
  79. data/test/unit/view_tests.rb +7 -5
  80. metadata +26 -11
  81. data/test/unit/assertions/assert_kind_of_tests.rb +0 -66
@@ -1,10 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "benchmark"
2
4
  require "set"
3
5
  require "assert/assert_runner"
6
+ require "assert/clirb"
4
7
  require "assert/version"
5
8
 
6
9
  module Assert
7
10
  class CLI
11
+ SUCCESS_EXIT_STATUS = 0
12
+ ERROR_EXIT_STATUS = 1
13
+
8
14
  def self.debug?(args)
9
15
  args.include?("-d") || args.include?("--debug")
10
16
  end
@@ -22,13 +28,17 @@ module Assert
22
28
  end
23
29
 
24
30
  def self.bench(start_msg, &block)
25
- if !Assert.config.debug
26
- block.call; return
31
+ unless Assert.config.debug
32
+ block.call
33
+ return
27
34
  end
35
+
28
36
  print debug_start_msg(start_msg)
29
- RoundedMillisecondTime.new(Benchmark.measure(&block).real).tap do |time_in_ms|
30
- puts debug_finish_msg(time_in_ms)
31
- end
37
+ RoundedMillisecondTime
38
+ .new(Benchmark.measure(&block).real)
39
+ .tap do |time_in_ms|
40
+ puts debug_finish_msg(time_in_ms)
41
+ end
32
42
  end
33
43
 
34
44
  def initialize(*args)
@@ -61,25 +71,31 @@ module Assert
61
71
  end
62
72
 
63
73
  def run
74
+ fails_plus_errors_count = 0
64
75
  begin
65
76
  @cli.parse!(@args)
66
77
  catch(:halt) do
67
- Assert::AssertRunner.new(Assert.config, @cli.args, @cli.opts).run
78
+ fails_plus_errors_count =
79
+ Assert::AssertRunner.new(Assert.config, @cli.args, @cli.opts).run
68
80
  end
69
81
  rescue CLIRB::HelpExit
70
82
  puts help
83
+ exit(SUCCESS_EXIT_STATUS)
71
84
  rescue CLIRB::VersionExit
72
85
  puts Assert::VERSION
73
- rescue CLIRB::Error => exception
74
- puts "#{exception.message}\n\n"
75
- puts Assert.config.debug ? exception.backtrace.join("\n") : help
76
- exit(1)
77
- rescue StandardError => exception
78
- puts "#{exception.class}: #{exception.message}"
79
- puts exception.backtrace.join("\n")
80
- exit(1)
86
+ exit(SUCCESS_EXIT_STATUS)
87
+ rescue CLIRB::Error => ex
88
+ puts "#{ex.message}\n\n"
89
+ puts Assert.config.debug ? ex.backtrace.join("\n") : help
90
+ exit(ERROR_EXIT_STATUS)
91
+ rescue => ex
92
+ puts "#{ex.class}: #{ex.message}"
93
+ puts ex.backtrace.join("\n")
94
+ exit(ERROR_EXIT_STATUS)
81
95
  end
82
- exit(0)
96
+ exit(
97
+ fails_plus_errors_count == 0 ? SUCCESS_EXIT_STATUS : ERROR_EXIT_STATUS,
98
+ )
83
99
  end
84
100
 
85
101
  def help
@@ -91,63 +107,9 @@ module Assert
91
107
 
92
108
  module RoundedMillisecondTime
93
109
  ROUND_PRECISION = 3
94
- ROUND_MODIFIER = 10 ** ROUND_PRECISION
110
+ ROUND_MODIFIER = 10**ROUND_PRECISION
95
111
  def self.new(time_in_seconds)
96
112
  (time_in_seconds * 1000 * ROUND_MODIFIER).to_i / ROUND_MODIFIER.to_f
97
113
  end
98
114
  end
99
-
100
- class CLIRB # Version 1.1.0, https://github.com/redding/cli.rb
101
- Error = Class.new(RuntimeError);
102
- HelpExit = Class.new(RuntimeError); VersionExit = Class.new(RuntimeError)
103
- attr_reader :argv, :args, :opts, :data
104
-
105
- def initialize(&block)
106
- @options = []; instance_eval(&block) if block
107
- require "optparse"
108
- @data, @args, @opts = [], [], {}; @parser = OptionParser.new do |p|
109
- p.banner = ""; @options.each do |o|
110
- @opts[o.name] = o.value; p.on(*o.parser_args){ |v| @opts[o.name] = v }
111
- end
112
- p.on_tail("--version", ""){ |v| raise VersionExit, v.to_s }
113
- p.on_tail("--help", ""){ |v| raise HelpExit, v.to_s }
114
- end
115
- end
116
-
117
- def option(*args); @options << Option.new(*args); end
118
- def parse!(argv)
119
- @args = (argv || []).dup.tap do |args_list|
120
- begin; @parser.parse!(args_list)
121
- rescue OptionParser::ParseError => err; raise Error, err.message; end
122
- end; @data = @args + [@opts]
123
- end
124
- def to_s; @parser.to_s; end
125
- def inspect
126
- "#<#{self.class}:#{"0x0%x" % (object_id << 1)} @data=#{@data.inspect}>"
127
- end
128
-
129
- class Option
130
- attr_reader :name, :opt_name, :desc, :abbrev, :value, :klass, :parser_args
131
-
132
- def initialize(name, desc = nil, abbrev: nil, value: nil)
133
- @name, @desc = name, desc || ""
134
- @opt_name, @abbrev = parse_name_values(name, abbrev)
135
- @value, @klass = gvalinfo(value)
136
- @parser_args = if [TrueClass, FalseClass, NilClass].include?(@klass)
137
- ["-#{@abbrev}", "--[no-]#{@opt_name}", @desc]
138
- else
139
- ["-#{@abbrev}", "--#{@opt_name} VALUE", @klass, @desc]
140
- end
141
- end
142
-
143
- private
144
-
145
- def parse_name_values(name, custom_abbrev)
146
- [ (processed_name = name.to_s.strip.downcase).gsub("_", "-"),
147
- custom_abbrev || processed_name.gsub(/[^a-z]/, "").chars.first || "a"
148
- ]
149
- end
150
- def gvalinfo(v); v.kind_of?(Class) ? [nil,v] : [v,v.class]; end
151
- end
152
- end
153
115
  end
@@ -0,0 +1,55 @@
1
+ module Assert
2
+ class CLIRB # Version 1.1.0, https://github.com/redding/cli.rb
3
+ Error = Class.new(RuntimeError);
4
+ HelpExit = Class.new(RuntimeError); VersionExit = Class.new(RuntimeError)
5
+ attr_reader :argv, :args, :opts, :data
6
+
7
+ def initialize(&block)
8
+ @options = []; instance_eval(&block) if block
9
+ require "optparse"
10
+ @data, @args, @opts = [], [], {}; @parser = OptionParser.new do |p|
11
+ p.banner = ""; @options.each do |o|
12
+ @opts[o.name] = o.value; p.on(*o.parser_args){ |v| @opts[o.name] = v }
13
+ end
14
+ p.on_tail("--version", ""){ |v| raise VersionExit, v.to_s }
15
+ p.on_tail("--help", ""){ |v| raise HelpExit, v.to_s }
16
+ end
17
+ end
18
+
19
+ def option(*args); @options << Option.new(*args); end
20
+ def parse!(argv)
21
+ @args = (argv || []).dup.tap do |args_list|
22
+ begin; @parser.parse!(args_list)
23
+ rescue OptionParser::ParseError => err; raise Error, err.message; end
24
+ end; @data = @args + [@opts]
25
+ end
26
+ def to_s; @parser.to_s; end
27
+ def inspect
28
+ "#<#{self.class}:#{"0x0%x" % (object_id << 1)} @data=#{@data.inspect}>"
29
+ end
30
+
31
+ class Option
32
+ attr_reader :name, :opt_name, :desc, :abbrev, :value, :klass, :parser_args
33
+
34
+ def initialize(name, desc = nil, abbrev: nil, value: nil)
35
+ @name, @desc = name, desc || ""
36
+ @opt_name, @abbrev = parse_name_values(name, abbrev)
37
+ @value, @klass = gvalinfo(value)
38
+ @parser_args = if [TrueClass, FalseClass, NilClass].include?(@klass)
39
+ ["-#{@abbrev}", "--[no-]#{@opt_name}", @desc]
40
+ else
41
+ ["-#{@abbrev}", "--#{@opt_name} VALUE", @klass, @desc]
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def parse_name_values(name, custom_abbrev)
48
+ [ (processed_name = name.to_s.strip.downcase).gsub("_", "-"),
49
+ custom_abbrev || processed_name.gsub(/[^a-z]/, "").chars.first || "a"
50
+ ]
51
+ end
52
+ def gvalinfo(v); v.kind_of?(Class) ? [nil,v] : [v,v.class]; end
53
+ end
54
+ end
55
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "assert/default_runner"
2
4
  require "assert/default_suite"
3
5
  require "assert/default_view"
@@ -9,7 +11,7 @@ module Assert
9
11
  def self.settings(*items)
10
12
  items.each do |item|
11
13
  define_method(item) do |*args|
12
- if !(value = args.size > 1 ? args : args.first).nil?
14
+ unless (value = args.size > 1 ? args : args.first).nil?
13
15
  instance_variable_set("@#{item}", value)
14
16
  end
15
17
  instance_variable_get("@#{item}")
@@ -39,7 +41,7 @@ module Assert
39
41
  @run_diff_proc = Assert::U.syscmd_diff_proc
40
42
 
41
43
  # option settings
42
- @runner_seed = begin; srand; srand % 0xFFFF; end.to_i
44
+ @runner_seed = (env_runner_seed || random_runner_seed).to_i
43
45
  @changed_only = false
44
46
  @changed_ref = ""
45
47
  @single_test = ""
@@ -51,30 +53,42 @@ module Assert
51
53
  @list = false
52
54
  @debug = false
53
55
 
54
- self.apply(settings || {})
56
+ apply(settings || {})
55
57
  end
56
58
 
57
59
  def apply(settings)
58
60
  settings.keys.each do |name|
59
- if !settings[name].nil? && self.respond_to?(name.to_s)
60
- self.send(name.to_s, settings[name])
61
+ if !settings[name].nil? && respond_to?(name.to_s)
62
+ send(name.to_s, settings[name])
61
63
  end
62
64
  end
63
65
  @single_test_file_line = nil
64
66
  end
65
67
 
66
68
  def single_test?
67
- !self.single_test.empty?
69
+ !single_test.empty?
68
70
  end
69
71
 
70
72
  def single_test_file_line
71
73
  @single_test_file_line ||= Assert::FileLine.parse(
72
- File.expand_path(self.single_test, Dir.pwd)
74
+ File.expand_path(single_test, Dir.pwd),
73
75
  )
74
76
  end
75
77
 
76
78
  def single_test_file_path
77
- self.single_test_file_line.file if self.single_test_file_line
79
+ single_test_file_line&.file
80
+ end
81
+
82
+ def env_runner_seed
83
+ ENV["SEED"]
84
+ end
85
+
86
+ def random_runner_seed
87
+ @random_runner_seed ||=
88
+ begin
89
+ srand
90
+ srand % 0xFFFF
91
+ end
78
92
  end
79
93
  end
80
94
  end
@@ -1,34 +1,69 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Assert
2
4
  module ConfigHelpers
3
- def runner; self.config.runner; end
4
- def suite; self.config.suite; end
5
- def view; self.config.view; end
5
+ def runner
6
+ config.runner
7
+ end
8
+
9
+ def suite
10
+ config.suite
11
+ end
12
+
13
+ def view
14
+ config.view
15
+ end
6
16
 
7
17
  def runner_seed
8
- self.config.runner_seed
18
+ config.runner_seed
9
19
  end
10
20
 
11
21
  def single_test?
12
- self.config.single_test?
22
+ config.single_test?
13
23
  end
14
24
 
15
25
  def single_test_file_line
16
- self.config.single_test_file_line
26
+ config.single_test_file_line
27
+ end
28
+
29
+ def tests_to_run?
30
+ config.suite.tests_to_run?
31
+ end
32
+
33
+ def tests_to_run_count
34
+ config.suite.tests_to_run_count
35
+ end
36
+
37
+ def test_count
38
+ config.suite.test_count
39
+ end
40
+
41
+ def result_count
42
+ config.suite.result_count
17
43
  end
18
44
 
19
- def tests_to_run?; self.config.suite.tests_to_run?; end
20
- def tests_to_run_count; self.config.suite.tests_to_run_count; end
45
+ def pass_result_count
46
+ config.suite.pass_result_count
47
+ end
48
+
49
+ def fail_result_count
50
+ config.suite.fail_result_count
51
+ end
21
52
 
22
- def test_count; self.config.suite.test_count; end
23
- def result_count; self.config.suite.result_count; end
24
- def pass_result_count; self.config.suite.pass_result_count; end
25
- def fail_result_count; self.config.suite.fail_result_count; end
26
- def error_result_count; self.config.suite.error_result_count; end
27
- def skip_result_count; self.config.suite.skip_result_count; end
28
- def ignore_result_count; self.config.suite.ignore_result_count; end
53
+ def error_result_count
54
+ config.suite.error_result_count
55
+ end
56
+
57
+ def skip_result_count
58
+ config.suite.skip_result_count
59
+ end
60
+
61
+ def ignore_result_count
62
+ config.suite.ignore_result_count
63
+ end
29
64
 
30
65
  def all_pass?
31
- self.pass_result_count == self.result_count
66
+ pass_result_count == result_count
32
67
  end
33
68
 
34
69
  def formatted_run_time(run_time, format = "%.6f")
@@ -44,29 +79,29 @@ module Assert
44
79
  end
45
80
 
46
81
  def formatted_suite_run_time(format = "%.6f")
47
- formatted_run_time(self.config.suite.run_time, format)
82
+ formatted_run_time(config.suite.run_time, format)
48
83
  end
49
84
 
50
85
  def formatted_suite_test_rate(format = "%.6f")
51
- formatted_test_rate(self.config.suite.test_rate, format)
86
+ formatted_test_rate(config.suite.test_rate, format)
52
87
  end
53
88
 
54
89
  def formatted_suite_result_rate(format = "%.6f")
55
- formatted_result_rate(self.config.suite.result_rate, format)
90
+ formatted_result_rate(config.suite.result_rate, format)
56
91
  end
57
92
 
58
93
  def show_test_profile_info?
59
- !!self.config.profile
94
+ !!config.profile
60
95
  end
61
96
 
62
97
  def show_test_verbose_info?
63
- !!self.config.verbose
98
+ !!config.verbose
64
99
  end
65
100
 
66
101
  # return a list of result type symbols that have actually occurred
67
102
  def ocurring_result_types
68
103
  @result_types ||= [:pass, :fail, :ignore, :skip, :error].select do |sym|
69
- self.send("#{sym}_result_count") > 0
104
+ send("#{sym}_result_count") > 0
70
105
  end
71
106
  end
72
107
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "assert/actual_value"
2
4
  require "assert/assertions"
3
5
  require "assert/context/let_dsl"
@@ -36,7 +38,7 @@ module Assert
36
38
  @__assert_pending__ = 0
37
39
 
38
40
  @__assert_result_callback__ = proc do |result|
39
- if !@__assert_with_bt__.empty?
41
+ unless @__assert_with_bt__.empty?
40
42
  result.set_with_bt(@__assert_with_bt__.dup)
41
43
  end
42
44
  result_callback.call(result)
@@ -61,7 +63,8 @@ module Assert
61
63
  end
62
64
 
63
65
  # The opposite of assert. Check if the result is false. If so, create a new
64
- # pass result. Otherwise create a new fail result with the desc and fail msg.
66
+ # pass result. Otherwise create a new fail result with the desc and
67
+ # fail msg.
65
68
  def assert_not(assertion, fail_desc = nil)
66
69
  assert(!assertion, fail_desc) do
67
70
  "Failed assert_not: assertion was "\
@@ -72,8 +75,7 @@ module Assert
72
75
 
73
76
  def assert_that(
74
77
  actual_value = Assert::ActualValue.not_given,
75
- &actual_value_block
76
- )
78
+ &actual_value_block)
77
79
  Assert::ActualValue.new(actual_value, context: self, &actual_value_block)
78
80
  end
79
81
 
@@ -85,7 +87,7 @@ module Assert
85
87
  else
86
88
  capture_result(
87
89
  Assert::Result::Fail,
88
- "Pending pass (make it not pending)"
90
+ "Pending pass (make it not pending)",
89
91
  )
90
92
  end
91
93
  end
@@ -105,12 +107,10 @@ module Assert
105
107
  else
106
108
  capture_result(Assert::Result::Fail, message || "")
107
109
  end
110
+ elsif halt_on_fail?
111
+ raise Result::TestSkipped, "Pending fail: #{message || ""}"
108
112
  else
109
- if halt_on_fail?
110
- raise Result::TestSkipped, "Pending fail: #{message || ""}"
111
- else
112
- capture_result(Assert::Result::Skip, "Pending fail: #{message || ""}")
113
- end
113
+ capture_result(Assert::Result::Skip, "Pending fail: #{message || ""}")
114
114
  end
115
115
  end
116
116
  alias_method :flunk, :fail
@@ -137,11 +137,11 @@ module Assert
137
137
  begin
138
138
  @__assert_with_bt__.push(bt.first)
139
139
  instance_eval(&block)
140
- rescue Result::TestSkipped, Result::TestFailure => e
141
- if e.assert_with_bt.nil? && !@__assert_with_bt__.empty?
142
- e.assert_with_bt = @__assert_with_bt__.dup
140
+ rescue Result::TestSkipped, Result::TestFailure => ex
141
+ if ex.assert_with_bt.nil? && !@__assert_with_bt__.empty?
142
+ ex.assert_with_bt = @__assert_with_bt__.dup
143
143
  end
144
- raise(e)
144
+ raise(ex)
145
145
  ensure
146
146
  @__assert_with_bt__.pop
147
147
  end
@@ -176,12 +176,10 @@ module Assert
176
176
 
177
177
  def capture_result(result_klass, msg)
178
178
  @__assert_result_callback__.call(
179
- result_klass.for_test(@__assert_running_test__, msg, caller_locations)
179
+ result_klass.for_test(@__assert_running_test__, msg, caller_locations),
180
180
  )
181
181
  end
182
182
 
183
- def __assert_config__
184
- @__assert_config__
185
- end
183
+ attr_reader :__assert_config__
186
184
  end
187
185
  end