Module: XMP2Assert::XMP2Rexp

Included in:
Assertions
Defined in:
lib/xmp2assert/xmp2rexp.rb

Overview

An XMP comment normally looks like this:

Object.new   # => #<Object:0x007f896c9b49c8>

Here, the hexadecimal number 0x007f896c9b49c8 is the raw pointer address of this evaluated object. When we check sanity of this XMP, that part would never match because pointer addresses are kind of arbitrary.

To reroute the problem, here we convert a XMP comment into a regular expression. The idea behind this is the diff process hook implemented in https://github.com/ruby/chkbuild

Class Method Summary collapse

Class Method Details

.xmp2rexp(xmp) ⇒ Regexp

Generates a regular expression that roughly matches the input.

Parameters:

  • xmp (String)

    example.

Returns:

  • (Regexp)

    converted regular expression.



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/xmp2assert/xmp2rexp.rb', line 47

def xmp2rexp xmp
  # :NOTE: we are  editing regular expressions using  regular expressions. In
  # order  to hack  this method  you must  be a  seasoned regular  expression
  # craftsperson who can count backslashes at ease.
  src = Regexp.escape xmp
  src.gsub!(/([^\\])(\\n|\\ )+/, '\\1\s+')
  src.gsub!(/(#<[\w:]+?:)0x[0-9a-f]+/, '\\10x[0-9a-f]+')
  src.gsub!(/\\\.rb:\d+/, '\\.rb:\d+')

  return Regexp.new "\\A#{src}\\z"
end