Module: TTYtest::Assertions

Included in:
Capture
Defined in:
lib/ttytest/assertions.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



177
178
179
180
181
182
183
184
# File 'lib/ttytest/assertions.rb', line 177

def assert_contents(expected)
  matched, diff = get_diff(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



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/ttytest/assertions.rb', line 191

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

  matched, diff = get_diff(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_contents_emptyObject Also known as: assert_screen_empty

Asserts the contents of the screen are empty

Raises:



222
223
224
225
226
227
# File 'lib/ttytest/assertions.rb', line 222

def assert_contents_empty
  return if rows.all? { |s| s.to_s.empty? }

  raise MatchError,
        "Expected screen to be empty, but found content.\nEntire screen:\n#{self}"
end

#assert_contents_include(expected) ⇒ Object Also known as: assert_screen_includes

Asserts the contents of the screen include the passed in string

Parameters:

  • expected (String)

    the string value expected to be found in the screen contents

Raises:

  • (MatchError)

    if the screen does not contain the expected value



208
209
210
211
212
213
214
215
216
217
# File 'lib/ttytest/assertions.rb', line 208

def assert_contents_include(expected)
  found = false
  rows.each do |row|
    found = true if row.include?(expected)
  end
  return if found

  raise MatchError,
        "Expected screen contents to include #{expected}, but it was not found.\nEntire screen:\n#{self}"
end

#assert_contents_match_regexp(regexp_str) ⇒ Object Also known as: assert_screen_matches_regexp

Asserts the contents of the screen as a single string match the passed in regular expression

Parameters:

  • regexp_str (String)

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

Raises:

  • (MatchError)

    if the screen as a string doesn’t match against the regular expression



233
234
235
236
237
238
239
240
241
# File 'lib/ttytest/assertions.rb', line 233

def assert_contents_match_regexp(regexp_str)
  regexp = Regexp.new(regexp_str)
  screen = capture.to_s

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

  raise MatchError,
        "Expected screen contents to match regexp #{regexp_str} but they did not\nEntire screen:\n#{self}"
end

#assert_cursor_hiddenObject

Asserts the cursor is currently hidden

Raises:



168
169
170
171
172
# File 'lib/ttytest/assertions.rb', line 168

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



148
149
150
151
152
153
154
155
156
# File 'lib/ttytest/assertions.rb', line 148

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

Asserts the cursor is currently visible

Raises:



160
161
162
163
164
# File 'lib/ttytest/assertions.rb', line 160

def assert_cursor_visible
  return if cursor_visible?

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

#assert_file_contains(file_path, needle) ⇒ Object Also known as: assert_file_like

Asserts the specified file contains the passed in string value

Parameters:

  • file_path (String)

    the path to the file

  • needle (String)

    the value to search for in the file

Raises:

  • (MatchError)

    if the file does not contain value in variable needle



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/ttytest/assertions.rb', line 266

def assert_file_contains(file_path, needle)
  raise file_not_found_error(file_path) unless File.exist?(file_path)
  raise file_is_dir_error(file_path) unless File.file?(file_path)

  file_contains = false
  File.foreach(file_path) do |line|
    if line.include?(needle)
      file_contains = true
      break
    end
  end
  return if file_contains

  raise MatchError,
        "File with path #{file_path} did not contain #{needle}.\nEntire screen:\n#{self}"
end

#assert_file_doesnt_exist(file_path) ⇒ Object

Asserts the specified file does not exists

Parameters:

  • file_path (String)

    the path to the file

Raises:

  • (MatchError)

    if the file is found or is a directory/symlink



255
256
257
258
259
260
# File 'lib/ttytest/assertions.rb', line 255

def assert_file_doesnt_exist(file_path)
  return unless File.exist?(file_path) || File.file?(file_path)

  raise MatchError,
        "File with path #{file_path} was found or is a directory when it was asserted it did not exist.\nEntire screen:\n#{self}"
end

#assert_file_exists(file_path) ⇒ Object

Asserts the specified file exists

Parameters:

  • file_path (String)

    the path to the file

Raises:

  • (MatchError)

    if the file is not found or is a directory/symlink



247
248
249
250
# File 'lib/ttytest/assertions.rb', line 247

def assert_file_exists(file_path)
  raise file_not_found_error(file_path) unless File.exist?(file_path)
  raise file_is_dir_error(file_path) unless File.file?(file_path)
end

#assert_file_has_line_count(file_path, expected_count) ⇒ Object

Asserts the specified file has line count specified

Parameters:

  • file_path (String)

    the path to the file

  • expected_count (String)

    the expected line count of the file

Raises:

  • (MatchError)

    if the file has a different line count than specified



304
305
306
307
308
309
310
311
312
313
# File 'lib/ttytest/assertions.rb', line 304

def assert_file_has_line_count(file_path, expected_count)
  raise file_not_found_error(file_path) unless File.exist?(file_path)
  raise file_is_dir_error(file_path) unless File.file?(file_path)

  actual_count = File.foreach(file_path).count
  return if actual_count == expected_count

  raise MatchError,
        "File had #{actual_count} lines, not #{expected_count} lines as expected.\nEntire screen:\n#{self}"
end

#assert_file_has_permissions(file_path, permissions) ⇒ Object

Asserts the specified file has the permissions specified

Parameters:

  • file_path (String)

    the path to the file

  • permissions (String)

    the expected permissions of the file (in form ‘644’ or ‘775’)

Raises:

  • (MatchError)

    if the file has different permissions than specified



288
289
290
291
292
293
294
295
296
297
298
# File 'lib/ttytest/assertions.rb', line 288

def assert_file_has_permissions(file_path, permissions)
  raise file_not_found_error(file_path) unless File.exist?(file_path)
  raise file_is_dir_error(file_path) unless File.file?(file_path)

  file_mode = File.stat(file_path).mode
  perms_octal = format('%o', file_mode)[-3...]
  return if perms_octal == permissions

  raise MatchError,
        "File had permissions #{perms_octal}, not #{permissions} as expected.\n Entire 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



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ttytest/assertions.rb', line 10

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:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ttytest/assertions.rb', line 43

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 Also known as: assert_line_ends_with

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:



97
98
99
100
101
102
103
104
105
106
# File 'lib/ttytest/assertions.rb', line 97

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:



26
27
28
29
30
31
32
33
34
# File 'lib/ttytest/assertions.rb', line 26

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:



63
64
65
66
67
68
69
70
71
72
# File 'lib/ttytest/assertions.rb', line 63

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 Also known as: assert_line_regexp

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



113
114
115
116
117
118
119
120
121
122
# File 'lib/ttytest/assertions.rb', line 113

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 Also known as: assert_line_starts_with

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:



81
82
83
84
85
86
87
88
89
90
# File 'lib/ttytest/assertions.rb', line 81

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 Also known as: assert_lines_each_match_regexp

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



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ttytest/assertions.rb', line 130

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