Module: TTYtest::Matchers

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

Overview

Assertions for ttytest2.

Constant Summary collapse

METHODS =
public_instance_methods

Instance Method Summary collapse

Instance Method Details

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

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



214
215
216
217
218
219
220
221
# File 'lib/ttytest/matchers.rb', line 214

def assert_contents(expected)
  matched, diff = matched(expected, rows)

  return if matched

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

#assert_contents_at(row_start, row_end, expected) ⇒ Object Also known as: assert_matches_at, assert_rows

Asserts the contents of the terminal at specified rows

Parameters:

  • expected (String)

    the expected contents of the terminal at specified rows. Trailing whitespace on each line is ignored

Raises:

  • (MatchError)

    if the terminal doesn’t match the expected content



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/ttytest/matchers.rb', line 228

def assert_contents_at(row_start, row_end, expected)
  validate(row_end)
  row_end += 1 if row_end.zero?

  matched, diff = matched(expected, rows.slice(row_start, row_end))

  return if matched

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

#assert_cursor_hiddenObject

Raises:



187
188
189
190
191
# File 'lib/ttytest/matchers.rb', line 187

def assert_cursor_hidden
  return if cursor_hidden?

  raise MatchError, "expected cursor to be hidden was visible\nEntire screen:\n#{self}"
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



169
170
171
172
173
174
175
176
177
# File 'lib/ttytest/matchers.rb', line 169

def assert_cursor_position(x, y)
  expected = [x, y]
  actual = [cursor_x, cursor_y]

  return if actual == expected

  raise MatchError,
        "expected cursor to be at #{expected.inspect} but was at #{get_inspection(actual)}\nEntire screen:\n#{self}"
end

#assert_cursor_visibleObject

Raises:



180
181
182
183
184
# File 'lib/ttytest/matchers.rb', line 180

def assert_cursor_visible
  return if cursor_visible?

  raise MatchError, "expected cursor to be visible was hidden\nEntire screen:\n#{self}"
end

#assert_row(row_number, expected) ⇒ Object Also known as: assert_line

Asserts the contents of a single row match the value expected

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:

  • (MatchError)

    if the row doesn’t match exactly



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ttytest/matchers.rb', line 35

def assert_row(row_number, expected)
  validate(row_number)
  expected = expected.rstrip
  actual = row(row_number)

  return if !actual.nil? && actual == expected

  raise MatchError,
        "expected row #{row_number} to be #{expected.inspect} but got #{get_inspection(actual)}\n
        Entire screen:\n#{self}"
end

#assert_row_at(row_number, column_start, column_end, expected) ⇒ Object Also known as: assert_line_at

Asserts the contents of a single row contains the expected string at a specific position

Parameters:

  • row_number (Integer)

    the row (starting from 0) to test against

  • column_start (Integer)

    the column position to start comparing expected against

  • columns_end (Integer)

    the column position to end comparing expected against

  • expected (String)

    the expected value that the row starts with. Any trailing whitespace is ignored

Raises:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ttytest/matchers.rb', line 68

def assert_row_at(row_number, column_start, column_end, expected)
  validate(row_number)
  expected = expected.rstrip
  actual = row(row_number)
  column_end += 1

  return if !actual.nil? && actual[column_start, column_end].eql?(expected)

  inspection = get_inspection_bounded(actual, column_start, column_end)

  raise MatchError,
        "expected row #{row_number} to contain #{expected[column_start,
                                                          column_end]} at #{column_start}-#{column_end} and got #{inspection}\nEntire screen:\n#{self}"
end

#assert_row_ends_with(row_number, expected) ⇒ Object

Asserts the contents of a single row end with expected

Parameters:

  • row_number (Integer)

    the row (starting from 0) to test against

  • expected (String)

    the expected value that the row starts with. Any trailing whitespace is ignored

Raises:



121
122
123
124
125
126
127
128
129
130
# File 'lib/ttytest/matchers.rb', line 121

def assert_row_ends_with(row_number, expected)
  validate(row_number)
  expected = expected.rstrip
  actual = row(row_number)

  return if !actual.nil? && actual.end_with?(expected)

  raise MatchError,
        "expected row #{row_number} to end with #{expected.inspect} and got #{get_inspection(actual)}\nEntire screen:\n#{self}"
end

#assert_row_is_empty(row_number) ⇒ Object Also known as: assert_line_is_empty

Asserts the specified row is empty

Parameters:

  • row_number (Integer)

    the row (starting from 0) to test against

Raises:



51
52
53
54
55
56
57
58
59
# File 'lib/ttytest/matchers.rb', line 51

def assert_row_is_empty(row_number)
  validate(row_number)
  actual = row(row_number)

  return if actual == ''

  raise MatchError,
        "expected row #{row_number} to be empty but got #{get_inspection(actual)}\nEntire screen:\n#{self}"
end

#assert_row_like(row_number, expected) ⇒ Object Also known as: assert_row_contains, assert_line_contains, assert_line_like

Asserts the contents of a single row contains the value expected

Parameters:

  • row_number (Integer)

    the row (starting from 0) to test against

  • expected (String)

    the expected value contained in the row. Any trailing whitespace is ignored

Raises:



88
89
90
91
92
93
94
95
96
97
# File 'lib/ttytest/matchers.rb', line 88

def assert_row_like(row_number, expected)
  validate(row_number)
  expected = expected.rstrip
  actual = row(row_number)

  return if !actual.nil? && actual.include?(expected)

  raise MatchError,
        "expected row #{row_number} to be like #{expected.inspect} but got #{get_inspection(actual)}\nEntire screen:\n#{self}"
end

#assert_row_regexp(row_number, regexp_str) ⇒ Object

Asserts the contents of a single row match against the passed in regular expression

Parameters:

  • row_number (Integer)

    the row (starting from 0) to test against

  • regexp_str (String)

    the regular expression as a string that will be used to match with.

Raises:

  • (MatchError)

    if the row doesn’t match against the regular expression



136
137
138
139
140
141
142
143
144
145
# File 'lib/ttytest/matchers.rb', line 136

def assert_row_regexp(row_number, regexp_str)
  validate(row_number)
  regexp = Regexp.new(regexp_str)
  actual = row(row_number)

  return if !actual.nil? && actual.match?(regexp)

  raise MatchError,
        "expected row #{row_number} to match regexp #{regexp_str} but it did not. Row value #{get_inspection(actual)}\nEntire screen:\n#{self}"
end

#assert_row_starts_with(row_number, expected) ⇒ Object

Asserts the contents of a single row starts with expected string

Parameters:

  • row_number (Integer)

    the row (starting from 0) to test against

  • expected (String)

    the expected value that the row starts with. Any trailing whitespace is ignored

Raises:



106
107
108
109
110
111
112
113
114
115
# File 'lib/ttytest/matchers.rb', line 106

def assert_row_starts_with(row_number, expected)
  validate(row_number)
  expected = expected.rstrip
  actual = row(row_number)

  return if !actual.nil? && actual.start_with?(expected)

  raise MatchError,
        "expected row #{row_number} to start with #{expected.inspect} and got #{get_inspection(actual)}\nEntire screen:\n#{self}"
end

#assert_rows_each_match_regexp(row_start, row_end, regexp_str) ⇒ Object

Asserts the contents of a multiple rows each match against the passed in regular expression

Parameters:

  • row_start (Integer)

    the row (starting from 0) to test against

  • row_end (Integer)

    the last row to test against

  • regexp_str (String)

    the regular expression as a string that will be used to match with.

Raises:

  • (MatchError)

    if the row doesn’t match against the regular expression



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/ttytest/matchers.rb', line 152

def assert_rows_each_match_regexp(row_start, row_end, regexp_str)
  validate(row_end)
  regexp = Regexp.new(regexp_str)
  row_end += 1 if row_end.zero?

  rows.slice(row_start, row_end).each_with_index do |actual_row, index|
    next if !actual_row.nil? && actual_row.match?(regexp)

    raise MatchError,
          "expected row #{index} to match regexp #{regexp_str} but it did not. Row value #{get_inspection(actual_row)}\nEntire screen:\n#{self}"
  end
end

#get_inspection(actual) ⇒ Object



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

def get_inspection(actual)
  if actual.nil?
    'nil'
  else
    actual.inspect
  end
end

#get_inspection_bounded(actual, column_start, column_end) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/ttytest/matchers.rb', line 14

def get_inspection_bounded(actual, column_start, column_end)
  if actual.nil?
    'nil'
  else
    actual[column_start, column_end]
  end
end

#matched(expected, actual) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/ttytest/matchers.rb', line 193

def matched(expected, actual)
  expected_rows = expected.split("\n")
  diff = []
  matched = true
  actual.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

  [matched, diff]
end

#validate(row) ⇒ Object

Raises:



22
23
24
25
26
27
28
29
# File 'lib/ttytest/matchers.rb', line 22

def validate(row)
  return if @height.nil?
  return unless row >= @height

  raise MatchError,
        "row is at #{row}, which is greater than set height #{height}, so assertions will fail. If intentional, set height larger or break apart tests.\n
        Entire screen:\n#{self}"
end