~sircmpwn/ctools

ref: f2d583864b4c95633f8897b3e1d44b2fa5352899 ctools/test/fold -rwxr-xr-x 3.5 KiB View raw
f2d58386 — Gabor Koszegi Implement fold 2 months ago
                                                                                
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
#!/bin/sh
tool="fold"
. "$HARNESS"

should_handle_one_file() (
	printf "abcdefghijklmnopqrstuvwxyz" >"$TMPDIR"/test-one-file
	exp="$(printf "abcdefgh\nijklmnop\nqrstuvwx\nyz")"
	res="$(fold -w 8 "$TMPDIR"/test-one-file)"
	[ "$res" = "$exp" ]
)

should_handle_two_files() (
	printf "abcdefghijklmno\n" >"$TMPDIR"/test-two-files-1
	printf "pqrstuvwxyz\n" >"$TMPDIR"/test-two-files-2
	exp="$(printf "pqrstuv\nwxyz\nabcdefg\nhijklmn\no\n")"
	res="$(fold -w 7 "$TMPDIR"/test-two-files-2 "$TMPDIR"/test-two-files-1)"
	[ "$res" = "$exp" ]
)

should_handle_stdin() (
	exp="$(printf "STDIN tes\nt line1\nstdin TES\nT line 2")"
	res1="$(printf "STDIN test line1\nstdin TEST line 2" | fold -w 9)"
	res2="$(printf "STDIN test line1\nstdin TEST line 2" | fold -w 9 -)"
	[ "$res1" = "$res2" ] && [ "$res1" = "$exp" ]
)

should_handle_utf8() (
	exp="$(printf "Ώ¥あ∀𝜔\n∊ℝ 𝜉(\n𝜔)≿⌨")"
	res="$(printf "Ώ¥あ∀𝜔∊ℝ 𝜉(𝜔)≿⌨" | fold -w 5)"
	[ "$res" = "$exp" ]
)

should_handle_tab() (
	printf "テスト\ttest\tтест\tδοκιμή" >"$TMPDIR"/test-tab
	exp="$(printf "テスト\ttest\tтес\nт\tδοκιμή")"
	res="$(fold -w 20 "$TMPDIR"/test-tab)"

	[ "$res" = "$exp" ]
)

should_handle_backspace() (
	str="abcdefghij\bklmnopqrst\buvwxyzyxwvut\b\tsr"
	exp="$(printf "abcdefghij\bk\nlmnopqrst\buv\nwxyzyxwvut\b\n\ts\nr")"
	res="$(printf "$str" | fold -w 10)"
	[ "$res" = "$exp" ]
)

should_handle_cr() (
	str="abcdef\rghijk\rlmnopqr\rstuv\nwxyzyxwvutsr"
	exp="$(printf "abcdef\rghijk\rlmnopq\nr\rstuv\nwxyzyx\nwvutsr")"
	res="$(printf "$str" | fold -w 6)"
	[ "$res" = "$exp" ]
)

should_handle_b_flag() (
	printf "テ\rスト\ttest\tтес\bт\tδοκιμή" >"$TMPDIR"/test-b-flag-src
	read -r bytes <<-END_BYTES
\343\203\206\015\343\012\
\202\271\343\203\210\012\
\011\164\145\163\164\012\
\011\321\202\320\265\012\
\321\201\010\321\202\012\
\011\316\264\316\277\012\
\316\272\316\271\316\012\
\274\316\256
	END_BYTES
	printf "$bytes" >"$TMPDIR"/test-b-flag-exp
	fold -b -w 5 "$TMPDIR"/test-b-flag-src >"$TMPDIR"/test-b-flag-dst
	cmp -s "$TMPDIR"/test-b-flag-dst "$TMPDIR"/test-b-flag-exp
	[ $? -eq 0 ]
)

should_handle_s_flag() (
	printf "ąbč đê fghi j\tķl mnopqrs" >"$TMPDIR"/test-s-flag

	exp="$(printf "ąbč đê \nfghi j\t\nķl mnopqrs")"
	res="$(fold -s -w 10 "$TMPDIR"/test-s-flag)"
	[ "$res" = "$exp" ] || return 1

	exp="$(printf "ąbč đê\n fghi j\t\nķl \nmnopqrs")"
	res="$(fold -b -s -w 10 "$TMPDIR"/test-s-flag)"
	[ "$res" = "$exp" ]
)

should_handle_w_flag() (
	read src <<-END_STR
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
	END_STR

	read -r exp_str_default <<-END_STR
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\
abcdefghijklmnopqrstuvwxyzab\ncdefghijklmnopqrstuvwxyz
	END_STR

	read -r exp_str_13 <<-END_STR
abcdefghijklm\nnopqrstuvwxyz\nabcdefghijklm\nnopqrstuvwxyz\n\
abcdefghijklm\nnopqrstuvwxyz\nabcdefghijklm\nnopqrstuvwxyz
	END_STR

	exp="$(printf "$exp_str_default")"
	res="$(printf "$src" | fold)"
	[ "$res" = "$exp" ] || return 1

	exp="$(printf "$exp_str_13")"
	res="$(printf "$src" | fold -w 13)"
	[ "$res" = "$exp" ]

	# for non-positive or non-number -w arguments fold should
	# behave unpredictably
)

should_handle_ddash fold /dev/null

runtests \
	should_handle_ddash \
	should_handle_w_flag \
	should_handle_one_file \
	should_handle_two_files \
	should_handle_stdin \
	should_handle_utf8 \
	should_handle_tab \
	should_handle_backspace \
	should_handle_cr \
	should_handle_b_flag \
	should_handle_s_flag