Class: ResizeTerminalTest

Inherits:
Minitest::Test
  • Object
show all
Defined in:
lib/resize_terminal.rb

Instance Method Summary collapse

Instance Method Details

#setupObject



89
90
91
92
93
94
# File 'lib/resize_terminal.rb', line 89

def setup
  # Backup original ARGV and environment variables
  @original_argv = ARGV.dup
  @original_columns = ENV.fetch('COLUMNS', nil)
  @original_lines = ENV.fetch('LINES', nil)
end

#teardownObject



96
97
98
99
100
101
# File 'lib/resize_terminal.rb', line 96

def teardown
  # Restore original ARGV and environment variables
  ARGV.replace(@original_argv)
  ENV['COLUMNS'] = @original_columns
  ENV['LINES'] = @original_lines
end

#test_resize_terminal_invalid_responseObject



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/resize_terminal.rb', line 148

def test_resize_terminal_invalid_response
  # Simulate interactive terminal with invalid response
  $stdin.stub(:tty?, true) do
    ARGV.replace([])
    response = "\e[999;999H\e[6n\e[InvalidResponse".dup
    $stdin.stub(:getch, -> { response.slice!(0) || '' }) do
      assert_output(nil,
                    /Error: Failed to match terminal response pattern/) do
        assert_equal 1, resize_terminal(require_stdout: false)
      end
    end
  end
end

#test_resize_terminal_no_responseObject



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/resize_terminal.rb', line 134

def test_resize_terminal_no_response
  # Simulate interactive terminal with no response
  $stdin.stub(:tty?, true) do
    ARGV.replace([])
    $stdin.stub(:getch, -> { '' }) do
      # assert_output(nil, /Error: No response received from terminal/) do
      assert_output(nil,
                    "Error: Timeout while reading terminal response. Unsupported terminal emulator.\n") do
        assert_equal 1, resize_terminal(require_stdout: false)
      end
    end
  end
end

#test_resize_terminal_non_interactiveObject



174
175
176
177
178
179
180
181
# File 'lib/resize_terminal.rb', line 174

def test_resize_terminal_non_interactive
  # Simulate non-interactive terminal
  $stdin.stub(:tty?, false) do
    assert_output(nil, /Usage: resize_terminal/) do
      resize_terminal
    end
  end
end

#test_resize_terminal_successfulObject

def test_resize_terminal_successful

# Simulate interactive terminal
$stdin.stub(:tty?, true) do
  ARGV.replace([])
  ENV['COLUMNS'] = '80'
  ENV['LINES'] = '24'
  response = "\e[999;999H\e[6n\e[24;80R"
  $stdin.stub(:getch, -> { response.slice!(0) || '' }) do
    assert_output(nil, /24x80/) do
      resize_terminal
    end
  end
end

end



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/resize_terminal.rb', line 117

def test_resize_terminal_successful
  # Simulate interactive terminal
  $stdin.stub(:tty?, true) do
    ARGV.replace([])
    columns = 40 + (2 * rand(10))
    ENV['COLUMNS'] = columns.to_s
    ENV['LINES'] = '24'
    response = "\e[999;999H\e[6n\e[24;#{columns}R".dup
    $stdin.stub(:getch, -> { response.slice!(0) || '' }) do
      assert_output("\e7\e[r\e[999;999H\e[6n\e8xterm-256color #{columns}x24\n") do
        # assert_output('', '') do
        resize_terminal(require_stdout: false)
      end
    end
  end
end

#test_resize_terminal_timeoutObject



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/resize_terminal.rb', line 162

def test_resize_terminal_timeout
  # Simulate interactive terminal with timeout
  $stdin.stub(:tty?, true) do
    ARGV.replace([])
    Timeout.stub(:timeout, ->(_) { raise Timeout::Error }) do
      assert_output(nil, /Error: Timeout while reading terminal response/) do
        assert_equal 1, resize_terminal(require_stdout: false)
      end
    end
  end
end