Module: HexaPDF::TestUtils

Defined in:
lib/hexapdf/test_utils.rb

Overview

Contains various helper methods for testing HexaPDF

Defined Under Namespace

Classes: OperatorRecorder

Instance Method Summary collapse

Instance Method Details

#assert_method_invoked(object, name, *expected_values, check_block: false) ⇒ Object

Asserts that the method name of object gets invoked with the expected_values when executing the block. expected_values should contain arrays of arguments, one array for each invocation of the method.



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/hexapdf/test_utils.rb', line 79

def assert_method_invoked(object, name, *expected_values, check_block: false)
  args = []
  block = []
  object.define_singleton_method(name) {|*la, &lb| args << la; block << lb }
  yield
  assert_equal(expected_values, args, "Incorrect arguments for #{object.class}##{name}")
  block.each do |block_arg|
    assert_kind_of(Proc, block_arg, "Missing block for #{object.class}##{name}") if check_block
  end
ensure
  object.singleton_class.send(:remove_method, name)
end

#assert_operators(content, operators, only_names: false, range: 0..-1)) ⇒ Object

Asserts that the content string contains the operators.



68
69
70
71
72
73
74
# File 'lib/hexapdf/test_utils.rb', line 68

def assert_operators(content, operators, only_names: false, range: 0..-1)
  processor = OperatorRecorder.new
  HexaPDF::Content::Parser.new.parse(content, processor)
  result = processor.recorded_ops[range]
  result.map!(&:first) if only_names
  assert_equal(operators, result)
end

#collector(source) ⇒ Object

Collects the result from the HexaPDF::Filter source into a binary string.



102
103
104
105
106
107
108
# File 'lib/hexapdf/test_utils.rb', line 102

def collector(source)
  str = ''.b
  while source.alive? && (data = source.resume)
    str << data
  end
  str
end

#feeder(string, len = string.length) ⇒ Object

Creates a fiber that yields the given string in len length parts.



93
94
95
96
97
98
99
# File 'lib/hexapdf/test_utils.rb', line 93

def feeder(string, len = string.length)
  Fiber.new do
    until string.empty?
      Fiber.yield(string.slice!(0, len).force_encoding('BINARY'))
    end
  end
end