|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +begin |
| 4 | + require "minitest/assertions" |
| 5 | +rescue LoadError |
| 6 | + raise LoadError.new("You need to add `minitest` to your Gemfile") |
| 7 | +end |
| 8 | + |
| 9 | +module Quickdraw |
| 10 | + module Minitest |
| 11 | + class Test < Quickdraw::BasicTest |
| 12 | + def self.method_added(name) |
| 13 | + name = name.to_s |
| 14 | + |
| 15 | + return unless name.start_with?("test_") |
| 16 | + |
| 17 | + location = instance_method(name).source_location |
| 18 | + |
| 19 | + class_eval(<<~RUBY, *location) |
| 20 | + test("#{name[5..].tr('_', ' ')}") do |
| 21 | + #{name} |
| 22 | + end |
| 23 | + RUBY |
| 24 | + end |
| 25 | + |
| 26 | + def assert(value, message = nil) |
| 27 | + case message |
| 28 | + when nil |
| 29 | + super(value) { yield } |
| 30 | + when Proc |
| 31 | + super(value, &message) |
| 32 | + else |
| 33 | + super(value) { message } |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + def refute(value, message = nil) |
| 38 | + super(value) { message || yield } |
| 39 | + end |
| 40 | + |
| 41 | + # Fails unless obj is empty. |
| 42 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#assert_empty-instance_method) |
| 43 | + def assert_empty(obj, message = nil) = nil |
| 44 | + define_method :assert_empty, ::Minitest::Assertions.instance_method(:assert_empty) |
| 45 | + |
| 46 | + # Fails unless exp == act printing the difference between the two, if possible. |
| 47 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#assert_equal-instance_method) |
| 48 | + def assert_equal(exp, act, msg = nil) = nil |
| 49 | + define_method :assert_equal, ::Minitest::Assertions.instance_method(:assert_equal) |
| 50 | + |
| 51 | + # For comparing Floats. Fails unless exp and act are within delta of each other. |
| 52 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#assert_in_delta-instance_method) |
| 53 | + def assert_in_delta(exp, act, delta = 0.001, msg = nil) = nil |
| 54 | + define_method :assert_in_delta, ::Minitest::Assertions.instance_method(:assert_in_delta) |
| 55 | + |
| 56 | + # For comparing Floats. Fails unless exp and act have a relative error less than epsilon. |
| 57 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#assert_in_epsilon-instance_method) |
| 58 | + def assert_in_epsilon(exp, act, epsilon = 0.001, msg = nil) = nil |
| 59 | + define_method :assert_in_epsilon, ::Minitest::Assertions.instance_method(:assert_in_epsilon) |
| 60 | + |
| 61 | + # Fails unless collection includes obj. |
| 62 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#assert_includes-instance_method) |
| 63 | + def assert_includes(collection, obj, msg = nil) = nil |
| 64 | + define_method :assert_includes, ::Minitest::Assertions.instance_method(:assert_includes) |
| 65 | + |
| 66 | + # Fails unless obj is an instance of cls. |
| 67 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#assert_instance_of-instance_method) |
| 68 | + def assert_instance_of(cls, obj, msg = nil) = nil |
| 69 | + define_method :assert_instance_of, ::Minitest::Assertions.instance_method(:assert_instance_of) |
| 70 | + |
| 71 | + # Fails unless obj is a kind of cls. |
| 72 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#assert_kind_of-instance_method) |
| 73 | + def assert_kind_of(cls, obj, msg = nil) = nil |
| 74 | + define_method :assert_kind_of, ::Minitest::Assertions.instance_method(:assert_kind_of) |
| 75 | + |
| 76 | + # Fails unless matcher =~ obj. |
| 77 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#assert_match-instance_method) |
| 78 | + def assert_match(matcher, obj, msg = nil) = nil |
| 79 | + define_method :assert_match, ::Minitest::Assertions.instance_method(:assert_match) |
| 80 | + |
| 81 | + # Fails unless obj is nil. |
| 82 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#assert_nil-instance_method) |
| 83 | + def assert_nil(obj, msg = nil) = nil |
| 84 | + define_method :assert_nil, ::Minitest::Assertions.instance_method(:assert_nil) |
| 85 | + |
| 86 | + # For testing with binary operators. |
| 87 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#assert_operator-instance_method) |
| 88 | + def assert_operator(o1, op, o2 = nil, msg = nil) = nil |
| 89 | + define_method :assert_operator, ::Minitest::Assertions.instance_method(:assert_operator) |
| 90 | + |
| 91 | + # Fails unless path exists. |
| 92 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#assert_path_exists-instance_method) |
| 93 | + def assert_path_exists(path, msg = nil) = nil |
| 94 | + define_method :assert_path_exists, ::Minitest::Assertions.instance_method(:assert_path_exists) |
| 95 | + |
| 96 | + # For testing with pattern matching (only supported with Ruby 3.0 and later). |
| 97 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#assert_pattern-instance_method) |
| 98 | + def assert_pattern(&block) = nil |
| 99 | + define_method :assert_pattern, ::Minitest::Assertions.instance_method(:assert_pattern) |
| 100 | + |
| 101 | + # For testing with predicates. |
| 102 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#assert_predicate-instance_method) |
| 103 | + def assert_predicate(o1, op, msg = nil) = nil |
| 104 | + define_method :assert_predicate, ::Minitest::Assertions.instance_method(:assert_predicate) |
| 105 | + |
| 106 | + # Fails unless the block raises one of exp. Returns the exception matched. |
| 107 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#assert_raises-instance_method) |
| 108 | + def assert_raises(*exp, &block) = nil |
| 109 | + define_method :assert_raises, ::Minitest::Assertions.instance_method(:assert_raises) |
| 110 | + |
| 111 | + # Fails unless obj responds to meth. |
| 112 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#assert_respond_to-instance_method) |
| 113 | + def assert_respond_to(obj, meth, msg = nil) = nil |
| 114 | + define_method :assert_respond_to, ::Minitest::Assertions.instance_method(:assert_respond_to) |
| 115 | + |
| 116 | + # Fails unless exp and act are #equal? |
| 117 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#assert_same-instance_method) |
| 118 | + def assert_same(exp, act, msg = nil) = nil |
| 119 | + define_method :assert_same, ::Minitest::Assertions.instance_method(:assert_same) |
| 120 | + |
| 121 | + # Fails unless the block throws sym. |
| 122 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#assert_throws-instance_method) |
| 123 | + def assert_throws(sym, msg = nil, &block) = nil |
| 124 | + define_method :assert_throws, ::Minitest::Assertions.instance_method(:assert_throws) |
| 125 | + |
| 126 | + # Fails if obj is empty. |
| 127 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#refute_empty-instance_method) |
| 128 | + def refute_empty(obj, msg = nil) = nil |
| 129 | + define_method :refute_empty, ::Minitest::Assertions.instance_method(:refute_empty) |
| 130 | + |
| 131 | + # Fails if exp == act. |
| 132 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#refute_equal-instance_method) |
| 133 | + def refute_equal(exp, act, msg = nil) = nil |
| 134 | + define_method :refute_equal, ::Minitest::Assertions.instance_method(:refute_equal) |
| 135 | + |
| 136 | + # For comparing Floats. Fails if exp is within delta of act. |
| 137 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#refute_in_delta-instance_method) |
| 138 | + def refute_in_delta(exp, act, delta = 0.001, msg = nil) = nil |
| 139 | + define_method :refute_in_delta, ::Minitest::Assertions.instance_method(:refute_in_delta) |
| 140 | + |
| 141 | + # For comparing Floats. Fails if exp and act have a relative error less than epsilon. |
| 142 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#refute_in_epsilon-instance_method) |
| 143 | + def refute_in_epsilon(a, b, epsilon = 0.001, msg = nil) = nil |
| 144 | + define_method :refute_in_epsilon, ::Minitest::Assertions.instance_method(:refute_in_epsilon) |
| 145 | + |
| 146 | + # Fails if collection includes obj. |
| 147 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#refute_includes-instance_method) |
| 148 | + def refute_includes(collection, obj, msg = nil) = nil |
| 149 | + define_method :refute_includes, ::Minitest::Assertions.instance_method(:refute_includes) |
| 150 | + |
| 151 | + # Fails if obj is an instance of cls. |
| 152 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#refute_instance_of-instance_method) |
| 153 | + def refute_instance_of(cls, obj, msg = nil) = nil |
| 154 | + define_method :refute_instance_of, ::Minitest::Assertions.instance_method(:refute_instance_of) |
| 155 | + |
| 156 | + # Fails if obj is a kind of cls. |
| 157 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#refute_kind_of-instance_method) |
| 158 | + def refute_kind_of(cls, obj, msg = nil) = nil |
| 159 | + define_method :refute_kind_of, ::Minitest::Assertions.instance_method(:refute_kind_of) |
| 160 | + |
| 161 | + # Fails if matcher =~ obj. |
| 162 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#refute_match-instance_method) |
| 163 | + def refute_match(matcher, obj, msg = nil) = nil |
| 164 | + define_method :refute_match, ::Minitest::Assertions.instance_method(:refute_match) |
| 165 | + |
| 166 | + # Fails if obj is nil. |
| 167 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#refute_nil-instance_method) |
| 168 | + def refute_nil(obj, msg = nil) = nil |
| 169 | + define_method :refute_nil, ::Minitest::Assertions.instance_method(:refute_nil) |
| 170 | + |
| 171 | + # Fails if o1 is not op o2. |
| 172 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#refute_operator-instance_method) |
| 173 | + def refute_operator(o1, op, o2 = nil, msg = nil) = nil |
| 174 | + define_method :refute_operator, ::Minitest::Assertions.instance_method(:refute_operator) |
| 175 | + |
| 176 | + # Fails if path exists. |
| 177 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#refute_path_exists-instance_method) |
| 178 | + def refute_path_exists(path, msg = nil) = nil |
| 179 | + define_method :refute_path_exists, ::Minitest::Assertions.instance_method(:refute_path_exists) |
| 180 | + |
| 181 | + # For testing with pattern matching (only supported with Ruby 3.0 and later). |
| 182 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#refute_pattern-instance_method) |
| 183 | + def refute_pattern(&block) = nil |
| 184 | + define_method :refute_pattern, ::Minitest::Assertions.instance_method(:refute_pattern) |
| 185 | + |
| 186 | + # For testing with predicates. |
| 187 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#refute_predicate-instance_method) |
| 188 | + def refute_predicate(o1, op, msg = nil) = nil |
| 189 | + define_method :refute_predicate, ::Minitest::Assertions.instance_method(:refute_predicate) |
| 190 | + |
| 191 | + # Fails if obj responds to the message meth. |
| 192 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#refute_respond_to-instance_method) |
| 193 | + def refute_respond_to(obj, meth, msg = nil) = nil |
| 194 | + define_method :refute_respond_to, ::Minitest::Assertions.instance_method(:refute_respond_to) |
| 195 | + |
| 196 | + # Fails if exp is the same (by object identity) as act. |
| 197 | + # [Docs](https://www.rubydoc.info/gems/minitest/5.25.4/Minitest/Assertions#refute_same-instance_method) |
| 198 | + def refute_same(exp, act, msg = nil) = nil |
| 199 | + define_method :refute_same, ::Minitest::Assertions.instance_method(:refute_same) |
| 200 | + |
| 201 | + private define_method :message, ::Minitest::Assertions.instance_method(:message) |
| 202 | + private define_method :diff, ::Minitest::Assertions.instance_method(:diff) |
| 203 | + private define_method :things_to_diff, ::Minitest::Assertions.instance_method(:things_to_diff) |
| 204 | + private define_method :mu_pp_for_diff, ::Minitest::Assertions.instance_method(:mu_pp_for_diff) |
| 205 | + private define_method :mu_pp, ::Minitest::Assertions.instance_method(:mu_pp) |
| 206 | + |
| 207 | + def flunk(message) |
| 208 | + failure! { message } |
| 209 | + end |
| 210 | + |
| 211 | + def pass |
| 212 | + success! |
| 213 | + end |
| 214 | + end |
| 215 | + end |
| 216 | +end |
0 commit comments