Module: TTYtest::ColumnAssertions

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

Overview

Column Assertions for ttytest2.

Instance Method Summary collapse

Instance Method Details

#assert_column(col_number, expected) ⇒ Object

Asserts the contents of a single column match the value expected

Parameters:

  • col_number (Integer)

    the column (starting from 0) to test against

  • expected (String)

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

Raises:

  • (MatchError)

    if the column doesn’t match exactly



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

def assert_column(col_number, expected)
  validate(col_number)
  expected = expected.rstrip

  rows.each_with_index do |row, i|
    break if row.nil?

    next if row[col_number] == expected[i]

    raise MatchError,
          "expected column #{col_number} to be #{expected.inspect}\n
        Entire screen:\n#{self}"
  end
end

#assert_column_at(col_number, row_start, row_end, expected) ⇒ Object

Asserts the contents of a column contain the expected string at the specified position

Parameters:

  • col_number (Integer)

    the column (starting from 0) to test against

  • row_start (Integer)

    the row position to start comparing expected against

  • row_end (Integer)

    the row position to end comparing expected against (inclusive)

  • expected (String)

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

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ttytest/assertions/column_assertions.rb', line 47

def assert_column_at(col_number, row_start, row_end, expected)
  validate(col_number)
  expected = expected.rstrip
  actual = []

  rows.each_with_index do |row, i|
    next if i < row_start
    break if i > row_end || row.nil?

    actual[i - row_start] = row[col_number]
    next if row[col_number] == expected[i - row_start]

    raise MatchError,
          "expected column #{col_number} to be #{expected.inspect}\n
        Entire screen:\n#{self}"
  end

  return if !actual.nil? && actual.join.eql?(expected)

  inspection = get_inspection(actual.join)

  raise MatchError,
        "expected column #{col_number} to contain #{expected} at #{row_start}-#{row_end} and got #{inspection}\nEntire screen:\n#{self}"
end

#assert_column_ends_with(col_number, expected) ⇒ Object

Asserts the contents of a column ends with expected

Parameters:

  • col_number (Integer)

    the column (starting from 0) to test against

  • expected (String)

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

Raises:



107
108
109
110
111
112
113
114
115
116
# File 'lib/ttytest/assertions/column_assertions.rb', line 107

def assert_column_ends_with(col_number, expected)
  validate(col_number)
  expected = expected.rstrip
  actual = get_column(col_number).join

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

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

#assert_column_is_empty(col_number) ⇒ Object

Asserts the specified column is empty

Parameters:

  • col_number (Integer)

    the column (starting from 0) to test against

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ttytest/assertions/column_assertions.rb', line 28

def assert_column_is_empty(col_number)
  validate(col_number)

  rows.each do |row|
    break if row.nil?

    next if row == '' || row.length < col_number + 1 || row[col_number] == ' '

    raise MatchError,
          "expected column #{col_number} to be empty\nEntire screen:\n#{self}"
  end
end

#assert_column_like(col_number, expected) ⇒ Object Also known as: assert_column_contains

Asserts the contents of a single column contains the value expected

Parameters:

  • col_number (Integer)

    the column number (starting from 0) to test against

  • expected (String)

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

Raises:



76
77
78
79
80
81
82
83
84
85
# File 'lib/ttytest/assertions/column_assertions.rb', line 76

def assert_column_like(col_number, expected)
  validate(col_number)
  expected = expected.rstrip
  actual = get_column(col_number).join

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

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

#assert_column_regexp(col_number, regexp_str) ⇒ Object

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

Parameters:

  • col_number (Integer)

    the column (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 column doesn’t match against the regular expression



122
123
124
125
126
127
128
129
130
131
# File 'lib/ttytest/assertions/column_assertions.rb', line 122

def assert_column_regexp(col_number, regexp_str)
  validate(col_number)
  regexp = Regexp.new(regexp_str)
  actual = get_column(col_number).join

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

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

#assert_column_starts_with(col_number, expected) ⇒ Object

Asserts the contents of a single column starts with expected string

Parameters:

  • col_number (Integer)

    the column (starting from 0) to test against

  • expected (String)

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

Raises:



92
93
94
95
96
97
98
99
100
101
# File 'lib/ttytest/assertions/column_assertions.rb', line 92

def assert_column_starts_with(col_number, expected)
  validate(col_number)
  expected = expected.rstrip
  actual = get_column(col_number).join

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

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

#assert_columns_each_match_regexp(col_start, col_end, regexp_str) ⇒ Object

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

Parameters:

  • col_start (Integer)

    the column (starting from 0) to test against

  • col_end (Integer)

    the last column to test against

  • regexp_str (String)

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

Raises:

  • (MatchError)

    if the column doesn’t match against the regular expression



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/ttytest/assertions/column_assertions.rb', line 138

def assert_columns_each_match_regexp(col_start, col_end, regexp_str)
  validate(col_end)
  regexp = Regexp.new(regexp_str)
  col_cur = col_start
  col_end += 1 if col_end.zero?

  while col_cur < col_end
    actual = get_column(col_cur).join
    col_cur += 1
    next if !actual.nil? && actual.match?(regexp)

    raise MatchError,
          "expected column #{col_cur - 1} to match regexp #{regexp_str} but it did not. Column value #{get_inspection(actual)}\nEntire screen:\n#{self}"
  end
end

#assert_columns_match_regexp(col_start, col_end, regexp_str, remove_newlines: false) ⇒ Object

Asserts the contents of multiple columns match the passed in regular expression.

Parameters:

  • col_start (Integer)

    the column (starting from 0) to test against

  • col_end (Integer)

    the last column to test against

  • regexp_str (String)

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

  • remove_newlines (bool) (defaults to: false)

    an optional paramter to specify if line separators should be removed, defaults to false

Raises:

  • (MatchError)

    if the columns don’t match against the regular expression



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/ttytest/assertions/column_assertions.rb', line 160

def assert_columns_match_regexp(col_start, col_end, regexp_str, remove_newlines: false)
  validate(col_end)
  regexp = Regexp.new(regexp_str)
  col_cur = col_start
  col_end += 1 if col_end.zero?
  actual = ''

  while col_cur < col_end
    col = get_column(col_cur).join
    col = col.rstrip if remove_newlines
    actual += col
    col_cur += 1
  end

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

  raise MatchError,
        "expected columns #{col_start}-#{col_end} to match regexp #{regexp_str} but they did not. Columns value #{get_inspection(actual)}\nEntire screen:\n#{self}"
end