summaryrefslogtreecommitdiff
path: root/test/integration/integration_test.rb
blob: de8537d3239b4c4ec5429b356faac0e994c5dc3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
require 'minitest'
require 'minitest/autorun'
require 'ttytest'

class FzyTest < Minitest::Test
  FZY_PATH = File.expand_path('../../../fzy', __FILE__)

  def setup
    # fzy is fast.
    # This is never hit in a (passing) test suite, but helps speed up development
    TTYtest.default_max_wait_time = 0.2
  end

  def test_empty_list
    @tty = TTYtest.driver.new_terminal(%{echo placeholder;echo -n "" | #{FZY_PATH}})
    @tty.assert_row(0, 'placeholder')
    @tty.assert_row(1, '>')
    @tty.assert_row(2, '')
    @tty.assert_cursor_position(y: 1, x: 2)

    @tty.send_keys('t')
    @tty.assert_row(0, 'placeholder')
    @tty.assert_row(1, '> t')
    @tty.assert_row(2, '')
    @tty.assert_cursor_position(y: 1, x: 3)

    @tty.send_keys('z')
    @tty.assert_row(0, 'placeholder')
    @tty.assert_row(1, '> tz')
    @tty.assert_row(2, '')
    @tty.assert_cursor_position(y: 1, x: 4)

    @tty.send_keys("\r")
    @tty.assert_row(0, 'placeholder')
    @tty.assert_row(1, 'tz')
    @tty.assert_cursor_position(y: 2, x: 0)
  end

  def test_one_item
    @tty = TTYtest.driver.new_terminal(%{echo placeholder;echo -n "test" | #{FZY_PATH}})
    @tty.assert_row(0, 'placeholder')
    @tty.assert_row(1, '>')
    @tty.assert_row(2, 'test')
    @tty.assert_row(3, '')
    @tty.assert_cursor_position(y: 1, x: 2)

    @tty.send_keys('t')
    @tty.assert_row(0, 'placeholder')
    @tty.assert_row(1, '> t')
    @tty.assert_row(2, 'test')
    @tty.assert_row(3, '')
    @tty.assert_cursor_position(y: 1, x: 3)

    @tty.send_keys('z')
    @tty.assert_row(0, 'placeholder')
    @tty.assert_row(1, '> tz')
    @tty.assert_row(2, '')
    @tty.assert_row(3, '')
    @tty.assert_cursor_position(y: 1, x: 4)

    @tty.send_keys("\r")
    @tty.assert_row(0, 'placeholder')
    @tty.assert_row(1, 'tz')
    @tty.assert_cursor_position(y: 2, x: 0)
  end

  def test_two_items
    @tty = TTYtest.driver.new_terminal(%{echo placeholder;echo -n "test\nfoo" | #{FZY_PATH}})
    @tty.assert_row(0, 'placeholder')
    @tty.assert_row(1, '>')
    @tty.assert_row(2, 'test')
    @tty.assert_row(3, 'foo')
    @tty.assert_row(4, '')
    @tty.assert_cursor_position(y: 1, x: 2)

    @tty.send_keys('t')
    @tty.assert_row(0, 'placeholder')
    @tty.assert_row(1, '> t')
    @tty.assert_row(2, 'test')
    @tty.assert_row(3, '')
    @tty.assert_cursor_position(y: 1, x: 3)

    @tty.send_keys('z')
    @tty.assert_row(0, 'placeholder')
    @tty.assert_row(1, '> tz')
    @tty.assert_row(2, '')
    @tty.assert_row(3, '')
    @tty.assert_cursor_position(y: 1, x: 4)

    @tty.send_keys("\r")
    @tty.assert_row(0, 'placeholder')
    @tty.assert_row(1, 'tz')
    @tty.assert_cursor_position(y: 2, x: 0)
  end

  def ctrl(key)
    ((key.upcase.ord) - ('A'.ord) + 1).chr
  end

  def test_editing
    @tty = TTYtest.driver.new_terminal(%{echo placeholder;echo -n "test\nfoo" | #{FZY_PATH}})
    @tty.assert_row(0, 'placeholder')
    @tty.assert_row(1, '>')
    @tty.assert_cursor_position(y: 1, x: 2)

    @tty.send_keys("foo bar baz")
    @tty.assert_row(0, 'placeholder')
    @tty.assert_row(1, '> foo bar baz')
    @tty.assert_cursor_position(y: 1, x: 13)

    @tty.send_keys(ctrl('H'))
    @tty.assert_row(0, 'placeholder')
    @tty.assert_row(1, '> foo bar ba')
    @tty.assert_cursor_position(y: 1, x: 12)

    @tty.send_keys(ctrl('W'))
    @tty.assert_row(0, 'placeholder')
    @tty.assert_row(1, '> foo bar')
    @tty.assert_cursor_position(y: 1, x: 10)

    @tty.send_keys(ctrl('U'))
    @tty.assert_row(0, 'placeholder')
    @tty.assert_row(1, '>')
    @tty.assert_cursor_position(y: 1, x: 2)
  end

  def test_ctrl_d
    @tty = TTYtest.driver.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH}})
    @tty.assert_row(0, '>')

    @tty.send_keys('foo')
    @tty.assert_row(0, '> foo')

    @tty.send_keys(ctrl('D'))
    @tty.assert_row(0, '')
    @tty.assert_row(1, '')
    @tty.assert_row(2, '')
    @tty.assert_cursor_position(y: 0, x: 0)
  end

  def test_ctrl_c
    @tty = TTYtest.driver.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH}})
    @tty.assert_row(0, '>')

    @tty.send_keys('foo')
    @tty.assert_row(0, '> foo')

    @tty.send_keys(ctrl('C'))
    @tty.assert_row(0, '')
    @tty.assert_row(1, '')
    @tty.assert_row(2, '')
    @tty.assert_cursor_position(y: 0, x: 0)
  end
end