Class: Whitestone::Assertion::Match

Inherits:
Base show all
Defined in:
lib/whitestone/assertion_classes.rb

Overview

class Assertion::Equality

Instance Method Summary collapse

Methods inherited from Base

#block

Methods included from Guards

#args_or_block_one_only, #block_required, #no_block_allowed, #one_argument, #two_arguments, #two_or_three_arguments, #type_check

Constructor Details

#initialize(mode, *args, &block) ⇒ Match

Returns a new instance of Match.



208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/whitestone/assertion_classes.rb', line 208

def initialize(mode, *args, &block)
  super
  no_block_allowed
  args = two_arguments(args)
  unless args.map { |a| a.class }.to_set == Set[Regexp, String]
    raise AssertionSpecificationError, "Expect a String and a Regexp (any order)"
  end
  @regexp, @string = args
  if String === @regexp
    @string, @regexp = @regexp, @string
  end
  @string = Col.uncolored(@string)
end

Instance Method Details

#messageObject



224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/whitestone/assertion_classes.rb', line 224

def message
  _not_ =
    case @mode
    when :assert then " "
    when :negate then " NOT "
    end
  String.new.tap { |str|
    string = Col.plain(@string).inspect.___truncate(200)
    regexp = @regexp.inspect
    str << Col["Match failure: string should#{_not_}match regex\n"].yb.to_s
    str << Col["  String: ", string].fmt('yb,rb') << "\n"
    str << Col["  Regexp: ", regexp].fmt('yb,gb')
  }
end

#runObject



221
222
223
# File 'lib/whitestone/assertion_classes.rb', line 221

def run
  @regexp =~ @string
end