soft-assert 1.0.0

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 (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/soft_assert.rb +198 -0
  3. metadata +73 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d8b100ae72eaf55529aeb0c85f5e841ab20b4e16bccfabc3ddec439a08a330f5
4
+ data.tar.gz: ee7cbf7bde40e163b3db7b518f7f6c7148dcf9973f2c3d2453bf0c63b65d4644
5
+ SHA512:
6
+ metadata.gz: 59668bf4be1d6daf2d7936f7289e55afc8b4d4a92e37d815c1e1a466a576a09a7f53d44944936611a829c065d2a669759d499e068d0e5baaa80cd2f2b9199d01
7
+ data.tar.gz: 6fea768c1ae7b387f90a23557be88c22f966b084a88f9f2847dafb0012f89727028ddde2277a079e62411076975cb4007b4497f16416ca75b5b130c868adbca2
@@ -0,0 +1,198 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'minitest/assertions'
4
+ require 'test/unit/assertions'
5
+
6
+ # SoftAssert is a module that provides a set of assertion methods that can be used in tests.
7
+ # These methods allow you to check that your code is behaving as expected.
8
+ # Unlike hard assertions, soft assertions do not stop the execution of the test when they fail.
9
+ # Instead, they record the failure and allow the test to continue.
10
+ # At the end of the test, you can call the assert_all method to raise an exception if any soft assertions failed.
11
+ module SoftAssert
12
+ extend Minitest::Assertions
13
+
14
+ class << self
15
+ attr_accessor :assertions
16
+ end
17
+
18
+ @assertions = 0
19
+ @soft_errors = []
20
+
21
+ # Asserts that a boolean value is true.
22
+ # If the assertion fails, the error is recorded and the test continues.
23
+ def self.assert_true(bool, message = nil)
24
+ Test::Unit::Assertions.assert_true(bool, message)
25
+ rescue Test::Unit::AssertionFailedError => e
26
+ @soft_errors << e
27
+ end
28
+
29
+ # Asserts that a boolean value is false.
30
+ # If the assertion fails, the error is recorded and the test continues.
31
+ def self.assert_false(bool, message = nil)
32
+ Test::Unit::Assertions.assert_false(bool, message)
33
+ rescue Test::Unit::AssertionFailedError => e
34
+ @soft_errors << e
35
+ end
36
+
37
+ # Asserts that two values are equal.
38
+ # If the assertion fails, the error is recorded and the test continues.
39
+ def self.assert_equal(expected, actual, message = nil)
40
+ Minitest::Assertions.assert_equal(expected, actual, message)
41
+ rescue Test::Unit::AssertionFailedError => e
42
+ @soft_errors << e
43
+ end
44
+
45
+ # Asserts that two values are not equal.
46
+ # If the assertion fails, the error is recorded and the test continues.
47
+ def self.assert_not_equal(expected, actual, message = nil)
48
+ refute_equal(expected, actual, message)
49
+ rescue Minitest::Assertion => e
50
+ @soft_errors << e
51
+ end
52
+
53
+ # Asserts that a collection includes a certain value.
54
+ # If the assertion fails, the error is recorded and the test continues.
55
+ def self.assert_contains(actual, expected, message = nil)
56
+ assert_includes(actual, expected, message)
57
+ rescue Minitest::Assertion => e
58
+ @soft_errors << e
59
+ end
60
+
61
+ # Asserts that a collection does not include a certain value.
62
+ # If the assertion fails, the error is recorded and the test continues.
63
+ def self.assert_not_contains(actual, expected, message = nil)
64
+ refute_includes(actual, expected, message)
65
+ rescue Minitest::Assertion => e
66
+ @soft_errors << e
67
+ end
68
+
69
+ # Asserts that a collection is empty.
70
+ # If the assertion fails, the error is recorded and the test continues.
71
+ def self.assert_empty(actual, message = nil)
72
+ Minitest::Assertions.assert_empty(actual, message)
73
+ rescue Test::Unit::AssertionFailedError => e
74
+ @soft_errors << e
75
+ end
76
+
77
+ # Asserts that a collection is not empty.
78
+ # If the assertion fails, the error is recorded and the test continues.
79
+ def self.assert_not_empty(actual, message = nil)
80
+ refute_empty(actual, message)
81
+ rescue Minitest::Assertion => e
82
+ @soft_errors << e
83
+ end
84
+
85
+ # Asserts that a value is nil.
86
+ # If the assertion fails, the error is recorded and the test continues.
87
+ def self.assert_nil(actual, message = nil)
88
+ Minitest::Assertions.assert_nil(actual, message)
89
+ rescue Test::Unit::AssertionFailedError => e
90
+ @soft_errors << e
91
+ end
92
+
93
+ # Asserts that a value is not nil.
94
+ # If the assertion fails, the error is recorded and the test continues.
95
+ def self.assert_not_nil(actual, message = nil)
96
+ refute_nil(actual, message)
97
+ rescue Minitest::Assertion => e
98
+ @soft_errors << e
99
+ end
100
+
101
+ # Asserts that a string matches a regular expression.
102
+ # If the assertion fails, the error is recorded and the test continues.
103
+ def self.assert_match(regex, actual, message = nil)
104
+ Minitest::Assertions.assert_match(regex, actual, message)
105
+ rescue Test::Unit::AssertionFailedError => e
106
+ @soft_errors << e
107
+ end
108
+
109
+ # Asserts that a string does not match a regular expression.
110
+ # If the assertion fails, the error is recorded and the test continues.
111
+ def self.assert_not_match(regex, actual, message = nil)
112
+ refute_match(regex, actual, message)
113
+ rescue Minitest::Assertion => e
114
+ @soft_errors << e
115
+ end
116
+
117
+ # Asserts that two arrays are equal, after sorting them.
118
+ # If the assertion fails, the error is recorded and the test continues.
119
+ def self.assert_array_equals(expected, actual, message = nil)
120
+ # Sort the arrays based on the string representation of each object
121
+ expected_sorted = expected.sort_by(&:to_s)
122
+ actual_sorted = actual.sort_by(&:to_s)
123
+
124
+ SoftAssert.assert_equal(expected_sorted, actual_sorted, message)
125
+ rescue Minitest::Assertion => e
126
+ @soft_errors << e
127
+ end
128
+
129
+ # Asserts that two arrays are equal, after sorting them.
130
+ # If the assertion fails, the error is recorded and the test continues.
131
+ def self.assert_array_not_equals(expected, actual, message = nil)
132
+ # Sort the arrays based on the string representation of each object
133
+ expected_sorted = expected.sort_by(&:to_s)
134
+ actual_sorted = actual.sort_by(&:to_s)
135
+
136
+ refute_equal(expected_sorted, actual_sorted, message)
137
+ rescue Minitest::Assertion => e
138
+ @soft_errors << e
139
+ end
140
+
141
+ # Asserts that two hashes are equal, after sorting them.
142
+ # If the assertion fails, the error is recorded and the test continues.
143
+ def self.assert_hash_equals(expected, actual, message = nil)
144
+ # Sort the hashes based on the string representation of each key-value pair
145
+ expected_sorted = expected.sort_by { |k, v| [k, v.to_s] }.to_h
146
+ actual_sorted = actual.sort_by { |k, v| [k, v.to_s] }.to_h
147
+
148
+ SoftAssert.assert_equal(expected_sorted, actual_sorted, message)
149
+ rescue Minitest::Assertion => e
150
+ @soft_errors << e
151
+ end
152
+
153
+ # Asserts that two hashes are not equal, after sorting them.
154
+ # If the assertion fails, the error is recorded and the test continues.
155
+ def self.assert_hash_not_equals(expected, actual, message = nil)
156
+ # Sort the hashes based on the string representation of each key-value pair
157
+ expected_sorted = expected.sort_by { |k, v| [k, v.to_s] }.to_h
158
+ actual_sorted = actual.sort_by { |k, v| [k, v.to_s] }.to_h
159
+
160
+ refute_equal(expected_sorted, actual_sorted, message)
161
+ rescue Minitest::Assertion => e
162
+ @soft_errors << e
163
+ end
164
+
165
+ # Asserts that two maps are equal, after sorting them.
166
+ # If the assertion fails, the error is recorded and the test continues.
167
+ def self.assert_map_equals(expected, actual, message = nil)
168
+ # Sort the hashes based on the string representation of each key-value pair
169
+ expected_sorted = expected.sort_by { |k, v| [k, v.to_s] }.to_h
170
+ actual_sorted = actual.sort_by { |k, v| [k, v.to_s] }.to_h
171
+
172
+ SoftAssert.assert_equal(expected_sorted, actual_sorted, message)
173
+ rescue Minitest::Assertion => e
174
+ @soft_errors << e
175
+ end
176
+
177
+ # Asserts that two maps are not equal, after sorting them.
178
+ # If the assertion fails, the error is recorded and the test continues.
179
+ def self.assert_map_not_equals(expected, actual, message = nil)
180
+ # Sort the hashes based on the string representation of each key-value pair
181
+ expected_sorted = expected.sort_by { |k, v| [k, v.to_s] }.to_h
182
+ actual_sorted = actual.sort_by { |k, v| [k, v.to_s] }.to_h
183
+
184
+ refute_equal(expected_sorted, actual_sorted, message)
185
+ rescue Minitest::Assertion => e
186
+ @soft_errors << e
187
+ end
188
+
189
+ # Raises an exception if any soft assertions have failed.
190
+ # This method should be called at the end of each test.
191
+ def self.assert_all
192
+ return if @soft_errors.empty?
193
+
194
+ errors = @soft_errors
195
+ @soft_errors = []
196
+ raise "Soft Assertion\n#{errors.join("\n\n")}\n"
197
+ end
198
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: soft-assert
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ashitha Rao
8
+ - Sumit Ghosh
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2024-09-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 5.20.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 5.20.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: test-unit
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: 3.6.1
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: 3.6.1
42
+ description: A set of assertion methods that do not stop the execution of the test
43
+ when they fail.
44
+ email:
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - lib/soft_assert.rb
50
+ homepage: https://github.com/AshithaRao/soft-assert-ruby/blob/main/README.md
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 2.7.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.4.6
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Soft assertions for Ruby
73
+ test_files: []