Module: TTYtest::ScreenAssertions

Included in:
Assertions
Defined in:
lib/ttytest/assertions/screen_assertions.rb

Overview

Screen Assertions for ttytest2.

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



9
10
11
12
13
14
15
16
# File 'lib/ttytest/assertions/screen_assertions.rb', line 9

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:

  • row_start (Integer)

    the row (starting from 0) to test against

  • row_end (Integer)

    the last row to test against

  • 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



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

def assert_contents_at(row_start, row_end, expected)
  validate(row_end)

  matched, diff = get_diff(expected, rows[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:



55
56
57
58
59
60
# File 'lib/ttytest/assertions/screen_assertions.rb', line 55

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



41
42
43
44
45
46
47
48
49
50
# File 'lib/ttytest/assertions/screen_assertions.rb', line 41

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



66
67
68
69
70
71
72
73
74
# File 'lib/ttytest/assertions/screen_assertions.rb', line 66

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