Class: Matchi::Match
- Inherits:
-
Object
- Object
- Matchi::Match
- Defined in:
- lib/matchi/match.rb
Overview
Regular expression matcher: passes when the actual value is a String or a Symbol that the expected Regexp matches. Any other actual value fails, as does a string with an invalid or incompatible encoding.
Only a Regexp is accepted as pattern: with a String pattern,
String#match? would turn the actual value into the regular expression,
handing the comparison to the code under test. Use Satisfy for
other predicates.
Instance Method Summary collapse
-
#initialize(expected) ⇒ Match
constructor
A new instance of Match.
- #match? ⇒ Boolean
- #to_s ⇒ String
Constructor Details
#initialize(expected) ⇒ Match
Returns a new instance of Match.
19 20 21 22 23 |
# File 'lib/matchi/match.rb', line 19 def initialize(expected) raise ::ArgumentError, "expected must be a Regexp (got: #{expected.inspect})" unless ::Regexp === expected @expected = expected end |
Instance Method Details
#match? ⇒ Boolean
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/matchi/match.rb', line 28 def match? raise ::ArgumentError, "a block must be provided" unless block_given? actual = yield return false unless ::String === actual || ::Symbol === actual # A plain copy: a subclass of String has no say in the comparison. string = ::String.new(::Symbol === actual ? actual.name : actual) return false unless string.valid_encoding? begin @expected.match?(string) rescue ::Encoding::CompatibilityError false end end |
#to_s ⇒ String
46 47 48 |
# File 'lib/matchi/match.rb', line 46 def to_s "match #{@expected.inspect}" end |