summaryrefslogtreecommitdiffstats
path: root/up_test.go
blob: 87f527870fd451292b45ec315360c3e921fef5f4 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main

import "testing"

func Test_Editor_insert(t *testing.T) {
	tests := []struct {
		comment   string
		e         Editor
		insert    []rune
		wantValue []rune
	}{
		{
			comment: "prepend ASCII char",
			e: Editor{
				value:  []rune(`abc`),
				cursor: 0,
			},
			insert:    []rune{'X'},
			wantValue: []rune(`Xabc`),
		},
		{
			comment: "prepend UTF char",
			e: Editor{
				value:  []rune(`abc`),
				cursor: 0,
			},
			insert:    []rune{'☃'},
			wantValue: []rune(`☃abc`),
		},
		{
			comment: "insert ASCII char",
			e: Editor{
				value:  []rune(`abc`),
				cursor: 1,
			},
			insert:    []rune{'X'},
			wantValue: []rune(`aXbc`),
		},
		{
			comment: "insert UTF char",
			e: Editor{
				value:  []rune(`abc`),
				cursor: 1,
			},
			insert:    []rune{'☃'},
			wantValue: []rune(`a☃bc`),
		},
		{
			comment: "append ASCII char",
			e: Editor{
				value:  []rune(`abc`),
				cursor: 3,
			},
			insert:    []rune{'X'},
			wantValue: []rune(`abcX`),
		},
		{
			comment: "append UTF char",
			e: Editor{
				value:  []rune(`abc`),
				cursor: 3,
			},
			insert:    []rune{'☃'},
			wantValue: []rune(`abc☃`),
		},
		{
			comment: "insert 2 ASCII chars",
			e: Editor{
				value:  []rune(`abc`),
				cursor: 1,
			},
			insert:    []rune{'X', 'Y'},
			wantValue: []rune(`aXYbc`),
		},
	}

	for _, tt := range tests {
		tt.e.insert(tt.insert...)
		if string(tt.e.value) != string(tt.wantValue) {
			t.Errorf("%q: bad value\nwant: %q\nhave: %q", tt.comment, tt.wantValue, tt.e.value)
		}
	}
}

func Test_Editor_unix_word_rubout(t *testing.T) {
	tests := []struct {
		comment       string
		e             Editor
		wantValue     []rune
		wantKillspace []rune
	}{
		{
			comment: "unix-word-rubout at beginning of line",
			e: Editor{
				value:  []rune(`abc`),
				cursor: 0,
			},
			wantValue:     []rune(`abc`),
			wantKillspace: []rune(``),
		},
		{
			comment: "unix-word-rubout at soft beginning of line",
			e: Editor{
				value:  []rune(` abc`),
				cursor: 1,
			},
			wantValue:     []rune(`abc`),
			wantKillspace: []rune(` `),
		},
		{
			comment: "unix-word-rubout until soft beginning of line",
			e: Editor{
				value:  []rune(` abc`),
				cursor: 2,
			},
			wantValue:     []rune(` bc`),
			wantKillspace: []rune(`a`),
		},
		{
			comment: "unix-word-rubout until beginning of line",
			e: Editor{
				value:  []rune(`abc`),
				cursor: 2,
			},
			wantValue:     []rune(`c`),
			wantKillspace: []rune(`ab`),
		},
		{
			comment: "unix-word-rubout in middle of line",
			e: Editor{
				value:  []rune(`lorem ipsum dolor`),
				cursor: 11,
			},
			wantValue:     []rune(`lorem  dolor`),
			wantKillspace: []rune(`ipsum`),
		},
		{
			comment: "unix-word-rubout cursor at beginning of word",
			e: Editor{
				value:  []rune(`lorem ipsum dolor`),
				cursor: 12,
			},
			wantValue:     []rune(`lorem dolor`),
			wantKillspace: []rune(`ipsum `),
		},
		{
			comment: "unix-word-rubout cursor between multiple spaces",
			e: Editor{
				value:  []rune(`a b   c`),
				cursor: 5,
			},
			wantValue:     []rune(`a  c`),
			wantKillspace: []rune(`b  `),
		},
		{
			comment: "unix-word-rubout tab as space char (although is it a realistic case in the context of a command line instruction?)",
			e: Editor{
				value: []rune(`a b		c`),
				cursor: 5,
			},
			wantValue: []rune(`a c`),
			wantKillspace: []rune(`b		`),
		},
	}

	for _, tt := range tests {
		tt.e.unixWordRubout()
		if string(tt.e.value) != string(tt.wantValue) {
			t.Errorf("%q: bad value\nwant: %q\nhave: %q", tt.comment, tt.wantValue, tt.e.value)
		}
		if string(tt.e.killspace) != string(tt.wantKillspace) {
			t.Errorf("%q: bad value in killspace\nwant: %q\nhave: %q", tt.comment, tt.wantKillspace, tt.e.value)
		}
	}
}