Module: XMP2Assert::Assertions

Includes:
Test::Unit::Assertions
Defined in:
lib/xmp2assert/assertions.rb

Overview

Helper module that implements assertions.

Instance Method Summary collapse

Instance Method Details

#assert_xmp(script, message = nil, stdin_data: '', **opts) ⇒ Object

Run a ruby script and assert for its comment. This is the main API.

assert_xmp "1 + 2 # => 3"


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/xmp2assert/assertions.rb', line 50

def assert_xmp script, message = nil, stdin_data: '', **opts
  qscript        = XMP2Assert::Quasifile.new script
  qf, qo, qe, qx = XMP2Assert::Converter.convert qscript
  XMP2Assert::Renderer.render qf, qx do |f|
    XMP2Assert::Spawn.new f, **opts do |_, i, o, e, r, t|
      i.write stdin_data
      i.close
      out = Thread.new { o.read.force_encoding(qscript.__ENCODING__) }
      err = Thread.new { e.read.force_encoding(qscript.__ENCODING__) }
      while n = t.gets do
        x = t.read n.to_i
        expected, actual, bt = *Marshal.load(x)
        begin
          assert_xmp_raw expected, actual, message
        rescue Test::Unit::AssertionFailedError => x
          r.close
          x.set_backtrace bt
          raise x
        else
          r.puts
        end
      end
      assert_xmp_raw qo, out.value, message unless qo.empty?
      assert_xmp_raw qe, err.value, message unless qe.empty?
      stderr_pass_through qe, err
    end
  end
end

#assert_xmp_raw(xmp, actual, message = nil) ⇒ Object

:TODO: tbw



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/xmp2assert/assertions.rb', line 80

def assert_xmp_raw xmp, actual, message = nil
  expected = XMP2Assert::XMP2Rexp.xmp2rexp xmp

  raise unless expected.match actual
rescue
  # Regexp#match can raise. That should also be a failure.
  ix  = Test::Unit::Assertions::AssertionMessage.convert xmp
  ia  = Test::Unit::Assertions::AssertionMessage.convert actual
  ex  = Test::Unit::AssertionFailedError.new(message,
                  expected: xmp,
                    actual: actual,
        inspected_expected: ix,
          inspected_actual: ia,
              user_message: message)
  raise ex
else
  return self # or...?
end