/
indent.t
142 lines (104 loc) · 3.78 KB
/
indent.t
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
use v6;
use Test;
# L<S32::Str/Str/"=item indent">
plan 62;
# TODO: Rakudo doesn't have full support for constants, so we have to assume a
# hardcoded 8 instead of $?TABSTOP for now.
my $tab = 8;
# Basic indentation
for 1..4 -> $i {
is 'quack'.indent($i),
' ' x $i ~ 'quack',
"Simple .indent($i)";
}
for 1..4 -> $i {
is "\x[2001] !".indent($i).ords.perl,
("\x[2001] " ~ (' ' x $i) ~ '!').ords.perl,
"New indent goes after existing - .indent($i)";
}
# Same-whitespace-character indent
for 1..4 -> $i {
for (' ', "\x[2000]") -> $prefix {
is ($prefix ~ 'quack').indent($i).perl,
($prefix x ($i + 1) ~ 'quack').perl,
"Same space - .indent($i) prefix={$prefix.ord.fmt('"\\x[%x]"')}";
}
}
is "\tquack".indent($tab),
"\t\tquack",
'Samespace indent should work for a full $?TABSTOP with \\t';
for 1..$tab -> $i {
for (' ', "\t", "\x[2000]") -> $prefix {
is ($prefix ~ ' ' ~ 'quack').indent($i).perl,
($prefix ~ ' ' ~ (' ' x $i) ~ 'quack').perl,
"Mixed space - .indent($i) prefix={$prefix.ord.fmt('"\\x[%x]"')}";
}
}
# Simple outdentation
is ' quack'.indent(-2),
' quack',
'Simple outdent';
is "\t quack".indent(-1),
' ' x $tab ~ "quack",
'Simple outdent with tab (explodes because we delete from left)';
is ' quack'.indent(-4),
'quack',
'Excess outdent test for correct output';
# TODO: need a better way of detecting warn() calls, also need a test that it
# should only warn once per .indent call
given 'Excess outdent test for warning' -> $test {
' quack'.indent(-4);
flunk $test;
CONTROL { default { pass $test; } }
}
# Whatever-star
is ''.indent(*), '', 'indent(*) on empty string';
is " quack\n meow\n helicopter fish".indent(*).perl,
" quack\nmeow\n helicopter fish".perl,
'Whatever* outdent with at least 1 common indent';
is " quack\nmeow\n helicopter fish".indent(*).perl,
" quack\nmeow\n helicopter fish".perl,
'Whatever* outdent with one line flush left already';
is " quack\n\n meow\n".indent(*),
"quack\n\n meow\n",
".indent(*) ignores empty lines";
# Tab expansion
is "\t!".indent(-1),
' ' x ($tab - 1) ~ '!',
'Tab explosion on outdent';
is "\t\t!".indent(-1),
"\t" ~ ' ' x ($tab - 1) ~ '!',
'Test that tabs explode from the right';
ok ([eq] ((' ' Xx 0..$tab - 1) X~ "\t")».indent(-4)),
'Check that varying amounts of space before a tabstop explode in a visually consistent way';
is " \t!".indent(-1),
' ' x ($tab - 1) ~ '!',
'Spaces before a hard tab should be coalesced into the tabstop when exploding';
is " \t\t!".indent(-1),
"\t" ~ ' ' x ($tab - 1) ~ '!',
'Test that space-tab-tab outdent works as intended';
is " \t \t quack".indent(-2),
" \t" ~ (' ' x $tab - 1) ~ 'quack',
'Check that mixed spaces and tabs outdent correctly';
is "\tquack\n\t meow".indent($tab),
"\t\tquack\n\t {' ' x $tab}meow",
'Multiline indent test with tab-space indent';
is "\ta\n b".indent(1).perl,
"\ta\n b".lines».indent(1).join("\n").perl,
'Multiline indent test with mixed line beginnings';
is "\tquack\nmeow".indent($tab),
"\t\tquack\n{' ' x $tab}meow",
'Multiline $?TABSTOP-width indent with an unindented line and a tab-indented line';
# Misc
is "\ta\n b".indent(0),
"\ta\n b",
'.indent(0) should be a no-op';
is "a\n\nb\n".indent(2).perl,
" a\n\n b\n".perl,
".indent ignores empty lines";
is "\ta\n b".indent(1).indent(16).indent(0).indent(*).perl,
"\ta\n b".indent(True).indent('0x10').indent('0e0').indent(*).perl,
'.indent accepts weird scalar input and coerces it to Int when necessary';
is " \t a\n \t b\n".indent(1).perl,
" \t a\n \t b\n".perl,
'Indentation should not be appended after a trailing \n';