Module: TTYtest::Matchers

Included in:
Capture
Defined in:
lib/ttytest/matchers.rb

Constant Summary collapse

METHODS =
public_instance_methods

Instance Method Summary collapse

Instance Method Details

#assert_contents(expected) ⇒ Object Also known as: assert_matches

Asserts the full contents of the terminal

Parameters:

  • expected (String)

    the full expected contents of the terminal. Trailing whitespace on each line is ignored

Raises:

  • (MatchError)

    if the terminal doesn’t match the expected content



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ttytest/matchers.rb', line 44

def assert_contents(expected)
  expected_rows = expected.split("\n")
  diff = []
  matched = true
  rows.each_with_index do |actual_row, index|
    expected_row = (expected_rows[index] || "").rstrip
    if actual_row != expected_row
      diff << "-#{expected_row}"
      diff << "+#{actual_row}"
      matched = false
    else
      diff << " #{actual_row}".rstrip
    end
  end

  if !matched
    raise MatchError, "screen did not match expected content:\n--- expected\n+++ actual\n#{diff.join("\n")}"
  end
end

#assert_cursor_hiddenObject

Raises:



35
36
37
38
39
# File 'lib/ttytest/matchers.rb', line 35

def assert_cursor_hidden
  if !cursor_hidden?
    raise MatchError, "expected cursor to be hidden was visible\nEntire screen:\n#{to_s}"
  end
end

#assert_cursor_position(x:, y:) ⇒ Object

Asserts that the cursor is in the expected position

Parameters:

  • x (Integer)

    cursor x (row) position, starting from 0

  • y (Integer)

    cursor y (column) position, starting from 0

Raises:

  • (MatchError)

    if the cursor position doesn’t match



19
20
21
22
23
24
25
# File 'lib/ttytest/matchers.rb', line 19

def assert_cursor_position(x:, y:)
  expected = [x, y]
  actual = [cursor_x, cursor_y]
  if actual != expected
    raise MatchError, "expected cursor to be at #{expected.inspect} but was at #{actual.inspect}\nEntire screen:\n#{to_s}"
  end
end

#assert_cursor_visibleObject

Raises:



28
29
30
31
32
# File 'lib/ttytest/matchers.rb', line 28

def assert_cursor_visible
  if !cursor_visible?
    raise MatchError, "expected cursor to be visible was hidden\nEntire screen:\n#{to_s}"
  end
end

#assert_row(row_number, expected) ⇒ Object

Asserts the contents of a single row

Parameters:

  • row_number (Integer)

    the row (starting from 0) to test against

  • expected (String)

    the expected value of the row. Any trailing whitespace is ignored

Raises:



7
8
9
10
11
12
13
# File 'lib/ttytest/matchers.rb', line 7

def assert_row(row_number, expected)
  expected = expected.rstrip
  actual = row(row_number)
  if actual != expected
    raise MatchError, "expected row #{row_number} to be #{expected.inspect} but got #{actual.inspect}\nEntire screen:\n#{to_s}"
  end
end