Module: TLDR::Assertions
- Included in:
- TLDR
- Defined in:
- lib/tldr/assertions.rb,
lib/tldr/assertions/minitest_compatibility.rb
Defined Under Namespace
Modules: MinitestCompatibility
Class Method Summary collapse
- .capture_io ⇒ Object
- .diff(expected, actual) ⇒ Object
- .h(obj) ⇒ Object
- .msg(message = nil, &default) ⇒ Object
Instance Method Summary collapse
- #assert(bool, message = nil) ⇒ Object
- #assert_empty(obj, message = nil) ⇒ Object
- #assert_equal(expected, actual, message = nil) ⇒ Object
- #assert_in_delta(expected, actual, delta = 0.001, message = nil) ⇒ Object
- #assert_in_epsilon(expected, actual, epsilon = 0.001, message = nil) ⇒ Object
- #assert_include?(expected, actual, message = nil) ⇒ Boolean
- #assert_instance_of(expected, actual, message = nil) ⇒ Object
- #assert_kind_of(expected, actual, message = nil) ⇒ Object
- #assert_match(matcher, actual, message = nil) ⇒ Object
- #assert_nil(obj, message = nil) ⇒ Object
- #assert_operator(left_operand, operator, right_operand, message = nil) ⇒ Object
- #assert_output(expected_stdout, expected_stderr, message = nil, &block) ⇒ Object
- #assert_path_exists(path, message = nil) ⇒ Object
- #assert_pattern(message = nil) ⇒ Object
- #assert_predicate(obj, method, message = nil) ⇒ Object
- #assert_raises(*exp) ⇒ Object
- #assert_respond_to(obj, method, message = nil) ⇒ Object
- #assert_same(expected, actual, message = nil) ⇒ Object
- #assert_silent ⇒ Object
- #assert_throws(expected, message = nil) ⇒ Object
- #refute(test, message = nil) ⇒ Object
- #refute_empty(obj, message = nil) ⇒ Object
- #refute_equal(expected, actual, message = nil) ⇒ Object
- #refute_in_delta(expected, actual, delta = 0.001, message = nil) ⇒ Object
- #refute_in_epsilon(expected, actual, epsilon = 0.001, msg = nil) ⇒ Object
- #refute_include?(expected, actual, message = nil) ⇒ Boolean
- #refute_instance_of(expected, actual, message = nil) ⇒ Object
- #refute_kind_of(expected, actual, message = nil) ⇒ Object
- #refute_match(matcher, actual, message = nil) ⇒ Object
- #refute_nil(obj, message = nil) ⇒ Object
- #refute_operator(left_operand, operator, right_operand, message = nil) ⇒ Object
- #refute_path_exists(path, message = nil) ⇒ Object
- #refute_pattern(message = nil) ⇒ Object
- #refute_predicate(obj, method, message = nil) ⇒ Object
- #refute_respond_to(obj, method, message = nil) ⇒ Object
- #refute_same(expected, actual, message = nil) ⇒ Object
Class Method Details
.capture_io ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/tldr/assertions.rb', line 31 def self.capture_io captured_stdout, captured_stderr = StringIO.new, StringIO.new original_stdout, original_stderr = $stdout, $stderr $stdout, $stderr = captured_stdout, captured_stderr yield [captured_stdout.string, captured_stderr.string] ensure $stdout = original_stdout $stderr = original_stderr end |
.diff(expected, actual) ⇒ Object
27 28 29 |
# File 'lib/tldr/assertions.rb', line 27 def self.diff expected, actual SuperDiff::EqualityMatchers::Main.call(expected:, actual:) end |
.h(obj) ⇒ Object
16 17 18 |
# File 'lib/tldr/assertions.rb', line 16 def self.h obj obj.pretty_inspect.chomp end |
.msg(message = nil, &default) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/tldr/assertions.rb', line 20 def self.msg = nil, &default proc { = .call if Proc === [.to_s, default.call].reject(&:empty?).join("\n") } end |
Instance Method Details
#assert(bool, message = nil) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/tldr/assertions.rb', line 45 def assert bool, = nil ||= "Expected #{Assertions.h(bool)} to be truthy" if bool true else = .call if Proc === fail Failure, end end |
#assert_empty(obj, message = nil) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/tldr/assertions.rb', line 61 def assert_empty obj, = nil = Assertions.msg() { "Expected #{Assertions.h(obj)} to be empty" } assert_respond_to obj, :empty? assert obj.empty?, end |
#assert_equal(expected, actual, message = nil) ⇒ Object
76 77 78 79 |
# File 'lib/tldr/assertions.rb', line 76 def assert_equal expected, actual, = nil = Assertions.msg() { Assertions.diff(expected, actual) } assert expected == actual, end |
#assert_in_delta(expected, actual, delta = 0.001, message = nil) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/tldr/assertions.rb', line 88 def assert_in_delta expected, actual, delta = 0.001, = nil difference = (expected - actual).abs = Assertions.msg() { "Expected |#{expected} - #{actual}| (#{difference}) to be within #{delta}" } assert delta >= difference, end |
#assert_in_epsilon(expected, actual, epsilon = 0.001, message = nil) ⇒ Object
104 105 106 |
# File 'lib/tldr/assertions.rb', line 104 def assert_in_epsilon expected, actual, epsilon = 0.001, = nil assert_in_delta expected, actual, [expected.abs, actual.abs].min * epsilon, end |
#assert_include?(expected, actual, message = nil) ⇒ Boolean
112 113 114 115 116 117 118 |
# File 'lib/tldr/assertions.rb', line 112 def assert_include? expected, actual, = nil = Assertions.msg() { "Expected #{Assertions.h(actual)} to include #{Assertions.h(expected)}" } assert_respond_to actual, :include? assert actual.include?(expected), end |
#assert_instance_of(expected, actual, message = nil) ⇒ Object
128 129 130 131 132 133 |
# File 'lib/tldr/assertions.rb', line 128 def assert_instance_of expected, actual, = nil = Assertions.msg() { "Expected #{Assertions.h(actual)} to be an instance of #{expected}, not #{actual.class}" } assert actual.instance_of?(expected), end |
#assert_kind_of(expected, actual, message = nil) ⇒ Object
142 143 144 145 146 147 |
# File 'lib/tldr/assertions.rb', line 142 def assert_kind_of expected, actual, = nil = Assertions.msg() { "Expected #{Assertions.h(actual)} to be a kind of #{expected}, not #{actual.class}" } assert actual.kind_of?(expected), # standard:disable Style/ClassCheck end |
#assert_match(matcher, actual, message = nil) ⇒ Object
156 157 158 159 160 161 162 163 164 |
# File 'lib/tldr/assertions.rb', line 156 def assert_match matcher, actual, = nil = Assertions.msg() { "Expected #{Assertions.h(actual)} to match #{Assertions.h(matcher)}" } assert_respond_to matcher, :=~ matcher = Regexp.new(Regexp.escape(matcher)) if String === matcher assert matcher =~ actual, Regexp.last_match end |
#assert_nil(obj, message = nil) ⇒ Object
174 175 176 177 178 179 180 |
# File 'lib/tldr/assertions.rb', line 174 def assert_nil obj, = nil = Assertions.msg() { "Expected #{Assertions.h(obj)} to be nil" } assert obj.nil?, end |
#assert_operator(left_operand, operator, right_operand, message = nil) ⇒ Object
190 191 192 193 194 195 |
# File 'lib/tldr/assertions.rb', line 190 def assert_operator left_operand, operator, right_operand, = nil = Assertions.msg() { "Expected #{Assertions.h(left_operand)} to be #{operator} #{Assertions.h(right_operand)}" } assert left_operand.__send__(operator, right_operand), end |
#assert_output(expected_stdout, expected_stderr, message = nil, &block) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/tldr/assertions.rb', line 204 def assert_output expected_stdout, expected_stderr, = nil, &block assert_block "assert_output requires a block to capture output" unless block actual_stdout, actual_stderr = Assertions.capture_io(&block) if Regexp === expected_stderr assert_match expected_stderr, actual_stderr, "In stderr" elsif !expected_stderr.nil? assert_equal expected_stderr, actual_stderr, "In stderr" end if Regexp === expected_stdout assert_match expected_stdout, actual_stdout, "In stdout" elsif !expected_stdout.nil? assert_equal expected_stdout, actual_stdout, "In stdout" end end |
#assert_path_exists(path, message = nil) ⇒ Object
222 223 224 225 226 227 228 |
# File 'lib/tldr/assertions.rb', line 222 def assert_path_exists path, = nil = Assertions.msg() { "Expected #{Assertions.h(path)} to exist" } assert File.exist?(path), end |
#assert_pattern(message = nil) ⇒ Object
238 239 240 241 242 243 244 245 246 |
# File 'lib/tldr/assertions.rb', line 238 def assert_pattern = nil assert false, "assert_pattern requires a block to capture errors" unless block_given? begin yield rescue NoMatchingPatternError => e assert false, Assertions.msg() { "Expected pattern to match, but NoMatchingPatternError was raised: #{e.}" } end end |
#assert_predicate(obj, method, message = nil) ⇒ Object
258 259 260 261 262 263 264 |
# File 'lib/tldr/assertions.rb', line 258 def assert_predicate obj, method, = nil = Assertions.msg() { "Expected #{Assertions.h(obj)} to be #{method}" } assert obj.send(method), end |
#assert_raises(*exp) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/tldr/assertions.rb', line 274 def assert_raises *exp assert false, "assert_raises requires a block to capture errors" unless block_given? = exp.pop if String === exp.last exp << StandardError if exp.empty? begin yield rescue Failure, Skip raise rescue *exp => e return e rescue SignalException, SystemExit raise rescue Exception => e # standard:disable Lint/RescueException assert false, Assertions.msg() { [ "#{Assertions.h(exp)} exception expected, not", "Class: <#{e.class}>", "Message: <#{e..inspect}>", "---Backtrace---", TLDR.filter_backtrace(e.backtrace).join("\n"), "---------------" ].compact.join("\n") } end exp = exp.first if exp.size == 1 assert false, "#{}#{Assertions.h(exp)} expected but nothing was raised" end |
#assert_respond_to(obj, method, message = nil) ⇒ Object
306 307 308 309 310 311 312 |
# File 'lib/tldr/assertions.rb', line 306 def assert_respond_to obj, method, = nil = Assertions.msg() { "Expected #{Assertions.h(obj)} (#{obj.class}) to respond to #{Assertions.h(method)}" } assert obj.respond_to?(method), end |
#assert_same(expected, actual, message = nil) ⇒ Object
322 323 324 325 326 327 328 329 330 331 |
# File 'lib/tldr/assertions.rb', line 322 def assert_same expected, actual, = nil = Assertions.msg() { <<~MSG Expected objects to be the same, but weren't Expected: #{Assertions.h(expected)} (oid=#{expected.object_id}) Actual: #{Assertions.h(actual)} (oid=#{actual.object_id}) MSG } assert expected.equal?(actual), end |
#assert_silent ⇒ Object
340 341 342 343 344 |
# File 'lib/tldr/assertions.rb', line 340 def assert_silent assert_output "", "" do yield end end |
#assert_throws(expected, message = nil) ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/tldr/assertions.rb', line 346 def assert_throws expected, = nil punchline = nil caught = true value = catch(expected) do begin yield rescue ArgumentError => e raise e unless e..include?("uncaught throw") punchline = ", not #{e..split(" ").last}" end caught = false end assert caught, Assertions.msg() { "Expected #{Assertions.h(expected)} to have been thrown#{punchline}" } value end |
#refute(test, message = nil) ⇒ Object
56 57 58 59 |
# File 'lib/tldr/assertions.rb', line 56 def refute test, = nil ||= Assertions.msg() { "Expected #{Assertions.h(test)} to not be truthy" } assert !test, end |
#refute_empty(obj, message = nil) ⇒ Object
70 71 72 73 74 |
# File 'lib/tldr/assertions.rb', line 70 def refute_empty obj, = nil = Assertions.msg() { "Expected #{Assertions.h(obj)} to not be empty" } assert_respond_to obj, :empty? refute obj.empty?, end |
#refute_equal(expected, actual, message = nil) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/tldr/assertions.rb', line 81 def refute_equal expected, actual, = nil = Assertions.msg() { "Expected #{Assertions.h(actual)} to not be equal to #{Assertions.h(expected)}" } refute expected == actual, end |
#refute_in_delta(expected, actual, delta = 0.001, message = nil) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/tldr/assertions.rb', line 96 def refute_in_delta expected, actual, delta = 0.001, = nil difference = (expected - actual).abs = Assertions.msg() { "Expected |#{expected} - #{actual}| (#{difference}) to not be within #{delta}" } refute delta >= difference, end |
#refute_in_epsilon(expected, actual, epsilon = 0.001, msg = nil) ⇒ Object
108 109 110 |
# File 'lib/tldr/assertions.rb', line 108 def refute_in_epsilon expected, actual, epsilon = 0.001, msg = nil refute_in_delta expected, actual, expected * epsilon, msg end |
#refute_include?(expected, actual, message = nil) ⇒ Boolean
120 121 122 123 124 125 126 |
# File 'lib/tldr/assertions.rb', line 120 def refute_include? expected, actual, = nil = Assertions.msg() { "Expected #{Assertions.h(actual)} to not include #{Assertions.h(expected)}" } assert_respond_to actual, :include? refute actual.include?(expected), end |
#refute_instance_of(expected, actual, message = nil) ⇒ Object
135 136 137 138 139 140 |
# File 'lib/tldr/assertions.rb', line 135 def refute_instance_of expected, actual, = nil = Assertions.msg() { "Expected #{Assertions.h(actual)} to not be an instance of #{expected}" } refute actual.instance_of?(expected), end |
#refute_kind_of(expected, actual, message = nil) ⇒ Object
149 150 151 152 153 154 |
# File 'lib/tldr/assertions.rb', line 149 def refute_kind_of expected, actual, = nil = Assertions.msg() { "Expected #{Assertions.h(actual)} to not be a kind of #{expected}" } refute actual.kind_of?(expected), # standard:disable Style/ClassCheck end |
#refute_match(matcher, actual, message = nil) ⇒ Object
166 167 168 169 170 171 172 |
# File 'lib/tldr/assertions.rb', line 166 def refute_match matcher, actual, = nil = Assertions.msg() { "Expected #{Assertions.h(actual)} to not match #{Assertions.h(matcher)}" } assert_respond_to matcher, :=~ refute matcher =~ actual, end |
#refute_nil(obj, message = nil) ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/tldr/assertions.rb', line 182 def refute_nil obj, = nil = Assertions.msg() { "Expected #{Assertions.h(obj)} to not be nil" } refute obj.nil?, end |
#refute_operator(left_operand, operator, right_operand, message = nil) ⇒ Object
197 198 199 200 201 202 |
# File 'lib/tldr/assertions.rb', line 197 def refute_operator left_operand, operator, right_operand, = nil = Assertions.msg() { "Expected #{Assertions.h(left_operand)} to not be #{operator} #{Assertions.h(right_operand)}" } refute left_operand.__send__(operator, right_operand), end |
#refute_path_exists(path, message = nil) ⇒ Object
230 231 232 233 234 235 236 |
# File 'lib/tldr/assertions.rb', line 230 def refute_path_exists path, = nil = Assertions.msg() { "Expected #{Assertions.h(path)} to not exist" } refute File.exist?(path), end |
#refute_pattern(message = nil) ⇒ Object
248 249 250 251 252 253 254 255 256 |
# File 'lib/tldr/assertions.rb', line 248 def refute_pattern = nil assert false, "assert_pattern requires a block to capture errors" unless block_given? begin yield refute true, Assertions.msg() { "Expected pattern not to match, but NoMatchingPatternError was not raised" } rescue NoMatchingPatternError end end |
#refute_predicate(obj, method, message = nil) ⇒ Object
266 267 268 269 270 271 272 |
# File 'lib/tldr/assertions.rb', line 266 def refute_predicate obj, method, = nil = Assertions.msg() { "Expected #{Assertions.h(obj)} to not be #{method}" } refute obj.send(method), end |
#refute_respond_to(obj, method, message = nil) ⇒ Object
314 315 316 317 318 319 320 |
# File 'lib/tldr/assertions.rb', line 314 def refute_respond_to obj, method, = nil = Assertions.msg() { "Expected #{Assertions.h(obj)} (#{obj.class}) to not respond to #{Assertions.h(method)}" } refute obj.respond_to?(method), end |
#refute_same(expected, actual, message = nil) ⇒ Object
333 334 335 336 337 338 |
# File 'lib/tldr/assertions.rb', line 333 def refute_same expected, actual, = nil = Assertions.msg() { "Expected #{Assertions.h(expected)} (oid=#{expected.object_id}) to not be the same as #{Assertions.h(actual)} (oid=#{actual.object_id})" } refute expected.equal?(actual), end |