File: test_helper.rb

package info (click to toggle)
ruby-bindata 2.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 652 kB
  • sloc: ruby: 8,896; makefile: 4
file content (76 lines) | stat: -rw-r--r-- 1,934 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
#require 'rubygems'

require 'simplecov'
SimpleCov.start do
  enable_coverage :branch
end

require 'minitest/autorun'
require 'stringio'

$LOAD_PATH.unshift File.expand_path("../lib", File.dirname(__FILE__))
require 'bindata'

class StringIO
  # Returns the value that was written to the io
  def value
    rewind
    read
  end
end

module Kernel
  def expose_methods_for_testing
    cls = (Class === self) ? self : (class << self ; self; end)
    private_method_names = cls.private_instance_methods - Object.private_instance_methods
    cls.send(:public, *private_method_names)

    protected_method_names = cls.protected_instance_methods - Object.protected_instance_methods
    cls.send(:public, *protected_method_names)
  end

  def value_read_from_written
    self.class.read(self.to_binary_s)
  end
end

module Minitest::Assertions
  def assert_equals_binary(expected, actual)
    assert_equal expected.dup.force_encoding(Encoding::BINARY), actual
  end

  def assert_raises_on_line(exp, line, msg = nil, &block)
    ex = assert_raises(exp, &block)
    assert_equal(msg, ex.message) if msg

    line_num_regex = /(.*):(\d+)(:in.*)$/

    filename = line_num_regex.match(ex.backtrace[0])[1]

    filtered = ex.backtrace.grep(/^#{Regexp.escape(filename)}/)
    top = filtered.grep(Regexp.new(Regexp.escape("in <top (required)>")))

    err_line = line_num_regex.match(filtered[0])[2].to_i
    ref_line = line_num_regex.match(top[0])[2].to_i - 1

    assert_equal((err_line - ref_line), line)
  end

  def assert_warns(msg, &block)
    result = ""
    callable = proc { |str|
      result = str
    }
    Kernel.stub(:warn, callable) do
      block.call
    end

    assert_equal msg, result
  end
end

module Minitest::Expectations
  infect_an_assertion :assert_equals_binary, :must_equal_binary
  infect_an_assertion :assert_raises_on_line, :must_raise_on_line, :block
  infect_an_assertion :assert_warns, :must_warn, :block
end