File: test_helper.rb

package info (click to toggle)
ruby-bindata 2.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 568 kB
  • ctags: 947
  • sloc: ruby: 8,357; makefile: 3
file content (61 lines) | stat: -rw-r--r-- 1,479 bytes parent folder | download | duplicates (3)
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
#require 'rubygems'

#require 'coveralls'
#Coveralls.wear!

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

  def must_equal_binary(expected)
    must_equal expected.dup.force_encoding(Encoding::BINARY)
  end

  def must_raise_on_line(exp, line, msg = nil)
    ex = self.must_raise exp
    ex.message.must_equal msg if msg

    idx = ex.backtrace.find_index { |bt| /:in `must_raise_on_line'$/ =~ bt }

    line_num_regex = /.*:(\d )(:.*|$)/
    err_line = line_num_regex.match(ex.backtrace[0])[1].to_i
    ref_line = line_num_regex.match(ex.backtrace[idx   1])[1].to_i

    (err_line - ref_line).must_equal line
  end

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