Module: SoftAssert
- Extended by:
- Minitest::Assertions
- Defined in:
- lib/soft_assert.rb
Overview
SoftAssert is a module that provides a set of assertion methods that can be used in tests. These methods allow you to check that your code is behaving as expected. Unlike hard assertions, soft assertions do not stop the execution of the test when they fail. Instead, they record the failure and allow the test to continue. At the end of the test, you can call the assert_all method to raise an exception if any soft assertions failed.
Class Attribute Summary collapse
-
.assertions ⇒ Object
Returns the value of attribute assertions.
Class Method Summary collapse
-
.assert_all ⇒ Object
Raises an exception if any soft assertions have failed.
-
.assert_array_equals(expected, actual, message = nil) ⇒ Object
Asserts that two arrays are equal, after sorting them.
-
.assert_array_not_equals(expected, actual, message = nil) ⇒ Object
Asserts that two arrays are equal, after sorting them.
-
.assert_contains(actual, expected, message = nil) ⇒ Object
Asserts that a collection includes a certain value.
-
.assert_empty(actual, message = nil) ⇒ Object
Asserts that a collection is empty.
-
.assert_equal(expected, actual, message = nil) ⇒ Object
Asserts that two values are equal.
-
.assert_false(bool, message = nil) ⇒ Object
Asserts that a boolean value is false.
-
.assert_hash_equals(expected, actual, message = nil) ⇒ Object
Asserts that two hashes are equal, after sorting them.
-
.assert_hash_not_equals(expected, actual, message = nil) ⇒ Object
Asserts that two hashes are not equal, after sorting them.
-
.assert_map_equals(expected, actual, message = nil) ⇒ Object
Asserts that two maps are equal, after sorting them.
-
.assert_map_not_equals(expected, actual, message = nil) ⇒ Object
Asserts that two maps are not equal, after sorting them.
-
.assert_match(regex, actual, message = nil) ⇒ Object
Asserts that a string matches a regular expression.
-
.assert_nil(actual, message = nil) ⇒ Object
Asserts that a value is nil.
-
.assert_not_contains(actual, expected, message = nil) ⇒ Object
Asserts that a collection does not include a certain value.
-
.assert_not_empty(actual, message = nil) ⇒ Object
Asserts that a collection is not empty.
-
.assert_not_equal(expected, actual, message = nil) ⇒ Object
Asserts that two values are not equal.
-
.assert_not_match(regex, actual, message = nil) ⇒ Object
Asserts that a string does not match a regular expression.
-
.assert_not_nil(actual, message = nil) ⇒ Object
Asserts that a value is not nil.
-
.assert_true(bool, message = nil) ⇒ Object
Asserts that a boolean value is true.
Class Attribute Details
.assertions ⇒ Object
Returns the value of attribute assertions.
15 16 17 |
# File 'lib/soft_assert.rb', line 15 def assertions @assertions end |
Class Method Details
.assert_all ⇒ Object
Raises an exception if any soft assertions have failed. This method should be called at the end of each test.
191 192 193 194 195 196 197 |
# File 'lib/soft_assert.rb', line 191 def self.assert_all return if @soft_errors.empty? errors = @soft_errors @soft_errors = [] raise "Soft Assertion\n#{errors.join("\n\n")}\n" end |
.assert_array_equals(expected, actual, message = nil) ⇒ Object
Asserts that two arrays are equal, after sorting them. If the assertion fails, the error is recorded and the test continues.
119 120 121 122 123 124 125 126 127 |
# File 'lib/soft_assert.rb', line 119 def self.assert_array_equals(expected, actual, = nil) # Sort the arrays based on the string representation of each object expected_sorted = expected.sort_by(&:to_s) actual_sorted = actual.sort_by(&:to_s) SoftAssert.assert_equal(expected_sorted, actual_sorted, ) rescue Minitest::Assertion => e @soft_errors << e end |
.assert_array_not_equals(expected, actual, message = nil) ⇒ Object
Asserts that two arrays are equal, after sorting them. If the assertion fails, the error is recorded and the test continues.
131 132 133 134 135 136 137 138 139 |
# File 'lib/soft_assert.rb', line 131 def self.assert_array_not_equals(expected, actual, = nil) # Sort the arrays based on the string representation of each object expected_sorted = expected.sort_by(&:to_s) actual_sorted = actual.sort_by(&:to_s) refute_equal(expected_sorted, actual_sorted, ) rescue Minitest::Assertion => e @soft_errors << e end |
.assert_contains(actual, expected, message = nil) ⇒ Object
Asserts that a collection includes a certain value. If the assertion fails, the error is recorded and the test continues.
55 56 57 58 59 |
# File 'lib/soft_assert.rb', line 55 def self.assert_contains(actual, expected, = nil) assert_includes(actual, expected, ) rescue Minitest::Assertion => e @soft_errors << e end |
.assert_empty(actual, message = nil) ⇒ Object
Asserts that a collection is empty. If the assertion fails, the error is recorded and the test continues.
71 72 73 74 75 |
# File 'lib/soft_assert.rb', line 71 def self.assert_empty(actual, = nil) Minitest::Assertions.assert_empty(actual, ) rescue Test::Unit::AssertionFailedError => e @soft_errors << e end |
.assert_equal(expected, actual, message = nil) ⇒ Object
Asserts that two values are equal. If the assertion fails, the error is recorded and the test continues.
39 40 41 42 43 |
# File 'lib/soft_assert.rb', line 39 def self.assert_equal(expected, actual, = nil) Minitest::Assertions.assert_equal(expected, actual, ) rescue Test::Unit::AssertionFailedError => e @soft_errors << e end |
.assert_false(bool, message = nil) ⇒ Object
Asserts that a boolean value is false. If the assertion fails, the error is recorded and the test continues.
31 32 33 34 35 |
# File 'lib/soft_assert.rb', line 31 def self.assert_false(bool, = nil) Test::Unit::Assertions.assert_false(bool, ) rescue Test::Unit::AssertionFailedError => e @soft_errors << e end |
.assert_hash_equals(expected, actual, message = nil) ⇒ Object
Asserts that two hashes are equal, after sorting them. If the assertion fails, the error is recorded and the test continues.
143 144 145 146 147 148 149 150 151 |
# File 'lib/soft_assert.rb', line 143 def self.assert_hash_equals(expected, actual, = nil) # Sort the hashes based on the string representation of each key-value pair expected_sorted = expected.sort_by { |k, v| [k, v.to_s] }.to_h actual_sorted = actual.sort_by { |k, v| [k, v.to_s] }.to_h SoftAssert.assert_equal(expected_sorted, actual_sorted, ) rescue Minitest::Assertion => e @soft_errors << e end |
.assert_hash_not_equals(expected, actual, message = nil) ⇒ Object
Asserts that two hashes are not equal, after sorting them. If the assertion fails, the error is recorded and the test continues.
155 156 157 158 159 160 161 162 163 |
# File 'lib/soft_assert.rb', line 155 def self.assert_hash_not_equals(expected, actual, = nil) # Sort the hashes based on the string representation of each key-value pair expected_sorted = expected.sort_by { |k, v| [k, v.to_s] }.to_h actual_sorted = actual.sort_by { |k, v| [k, v.to_s] }.to_h refute_equal(expected_sorted, actual_sorted, ) rescue Minitest::Assertion => e @soft_errors << e end |
.assert_map_equals(expected, actual, message = nil) ⇒ Object
Asserts that two maps are equal, after sorting them. If the assertion fails, the error is recorded and the test continues.
167 168 169 170 171 172 173 174 175 |
# File 'lib/soft_assert.rb', line 167 def self.assert_map_equals(expected, actual, = nil) # Sort the hashes based on the string representation of each key-value pair expected_sorted = expected.sort_by { |k, v| [k, v.to_s] }.to_h actual_sorted = actual.sort_by { |k, v| [k, v.to_s] }.to_h SoftAssert.assert_equal(expected_sorted, actual_sorted, ) rescue Minitest::Assertion => e @soft_errors << e end |
.assert_map_not_equals(expected, actual, message = nil) ⇒ Object
Asserts that two maps are not equal, after sorting them. If the assertion fails, the error is recorded and the test continues.
179 180 181 182 183 184 185 186 187 |
# File 'lib/soft_assert.rb', line 179 def self.assert_map_not_equals(expected, actual, = nil) # Sort the hashes based on the string representation of each key-value pair expected_sorted = expected.sort_by { |k, v| [k, v.to_s] }.to_h actual_sorted = actual.sort_by { |k, v| [k, v.to_s] }.to_h refute_equal(expected_sorted, actual_sorted, ) rescue Minitest::Assertion => e @soft_errors << e end |
.assert_match(regex, actual, message = nil) ⇒ Object
Asserts that a string matches a regular expression. If the assertion fails, the error is recorded and the test continues.
103 104 105 106 107 |
# File 'lib/soft_assert.rb', line 103 def self.assert_match(regex, actual, = nil) Minitest::Assertions.assert_match(regex, actual, ) rescue Test::Unit::AssertionFailedError => e @soft_errors << e end |
.assert_nil(actual, message = nil) ⇒ Object
Asserts that a value is nil. If the assertion fails, the error is recorded and the test continues.
87 88 89 90 91 |
# File 'lib/soft_assert.rb', line 87 def self.assert_nil(actual, = nil) Minitest::Assertions.assert_nil(actual, ) rescue Test::Unit::AssertionFailedError => e @soft_errors << e end |
.assert_not_contains(actual, expected, message = nil) ⇒ Object
Asserts that a collection does not include a certain value. If the assertion fails, the error is recorded and the test continues.
63 64 65 66 67 |
# File 'lib/soft_assert.rb', line 63 def self.assert_not_contains(actual, expected, = nil) refute_includes(actual, expected, ) rescue Minitest::Assertion => e @soft_errors << e end |
.assert_not_empty(actual, message = nil) ⇒ Object
Asserts that a collection is not empty. If the assertion fails, the error is recorded and the test continues.
79 80 81 82 83 |
# File 'lib/soft_assert.rb', line 79 def self.assert_not_empty(actual, = nil) refute_empty(actual, ) rescue Minitest::Assertion => e @soft_errors << e end |
.assert_not_equal(expected, actual, message = nil) ⇒ Object
Asserts that two values are not equal. If the assertion fails, the error is recorded and the test continues.
47 48 49 50 51 |
# File 'lib/soft_assert.rb', line 47 def self.assert_not_equal(expected, actual, = nil) refute_equal(expected, actual, ) rescue Minitest::Assertion => e @soft_errors << e end |
.assert_not_match(regex, actual, message = nil) ⇒ Object
Asserts that a string does not match a regular expression. If the assertion fails, the error is recorded and the test continues.
111 112 113 114 115 |
# File 'lib/soft_assert.rb', line 111 def self.assert_not_match(regex, actual, = nil) refute_match(regex, actual, ) rescue Minitest::Assertion => e @soft_errors << e end |
.assert_not_nil(actual, message = nil) ⇒ Object
Asserts that a value is not nil. If the assertion fails, the error is recorded and the test continues.
95 96 97 98 99 |
# File 'lib/soft_assert.rb', line 95 def self.assert_not_nil(actual, = nil) refute_nil(actual, ) rescue Minitest::Assertion => e @soft_errors << e end |
.assert_true(bool, message = nil) ⇒ Object
Asserts that a boolean value is true. If the assertion fails, the error is recorded and the test continues.
23 24 25 26 27 |
# File 'lib/soft_assert.rb', line 23 def self.assert_true(bool, = nil) Test::Unit::Assertions.assert_true(bool, ) rescue Test::Unit::AssertionFailedError => e @soft_errors << e end |