@@ -60,3 +60,138 @@ describe("str.unescaped_pairs", function()
60
60
{ { 1 , 3 }, { 5 , 8 }, { 9 , 11 } }
61
61
)
62
62
end )
63
+
64
+ describe (" str.dedent" , function ()
65
+ -- apparently clear() needs to run before anything else...
66
+ ls_helpers .clear ()
67
+ exec (" set rtp+=" .. os.getenv (" LUASNIP_SOURCE" ))
68
+ local function get_dedent_result (input_string )
69
+ local result = exec_lua (
70
+ string.format (
71
+ [[ return require("luasnip.util.str").dedent("%s")]] ,
72
+ input_string
73
+ )
74
+ )
75
+ return result
76
+ end
77
+
78
+ it (" spaces at beginnig" , function ()
79
+ local input_table = {
80
+ " line1" ,
81
+ " " ,
82
+ " line3" ,
83
+ " line4" ,
84
+ }
85
+ local input_string = table.concat (input_table , [[ \n]] )
86
+ local expect_table = {
87
+ " line1" ,
88
+ " " ,
89
+ " line3" ,
90
+ " line4" ,
91
+ }
92
+ local expected = table.concat (expect_table , " \n " )
93
+
94
+ local result = get_dedent_result (input_string )
95
+
96
+ assert .are .same (expected , result )
97
+ end )
98
+ it (" tabs at beginnig" , function ()
99
+ local input_table = {
100
+ [[ \t\tline1]] ,
101
+ [[ \t\t]] ,
102
+ [[ \t\t\tline3]] ,
103
+ [[ \t\t\t\tline4]] ,
104
+ }
105
+ local input_string = table.concat (input_table , [[ \n]] )
106
+ local expect_table = {
107
+ " line1" ,
108
+ " " ,
109
+ " \t line3" ,
110
+ " \t\t line4" ,
111
+ }
112
+ local expected = table.concat (expect_table , " \n " )
113
+
114
+ local result = get_dedent_result (input_string )
115
+
116
+ assert .are .same (expected , result )
117
+ end )
118
+ it (" tabs & spaces at beginnig" , function ()
119
+ local input_table = {
120
+ [[ \t\t line1]] ,
121
+ [[ \t\t ]] ,
122
+ [[ \t\t \t line3]] ,
123
+ [[ \t\t \t\t line4]] ,
124
+ }
125
+ local input_string = table.concat (input_table , [[ \n]] )
126
+ local expect_table = {
127
+ " line1" ,
128
+ " " ,
129
+ " \t line3" ,
130
+ " \t\t line4" ,
131
+ }
132
+ local expected = table.concat (expect_table , " \n " )
133
+
134
+ local result = get_dedent_result (input_string )
135
+
136
+ assert .are .same (expected , result )
137
+ end )
138
+ end )
139
+
140
+ describe (" str.convert_indent" , function ()
141
+ -- apparently clear() needs to run before anything else...
142
+ ls_helpers .clear ()
143
+ exec (" set rtp+=" .. os.getenv (" LUASNIP_SOURCE" ))
144
+ local function get_convert_indent_result (input_string , indent_string )
145
+ local result = exec_lua (
146
+ string.format (
147
+ [[ return require("luasnip.util.str").convert_indent("%s", "%s")]] ,
148
+ input_string ,
149
+ indent_string
150
+ )
151
+ )
152
+ return result
153
+ end
154
+
155
+ it (" two spaces to tab" , function ()
156
+ local input_table = {
157
+ " line1: no indent" ,
158
+ " " ,
159
+ " line3: 1 indent" ,
160
+ " line4: 2 indent" ,
161
+ }
162
+ local input_string = table.concat (input_table , [[ \n]] )
163
+ local indent_string = " "
164
+ local expect_table = {
165
+ " line1: no indent" ,
166
+ " " ,
167
+ " \t line3: 1 indent" ,
168
+ " \t\t line4: 2 indent" ,
169
+ }
170
+ local expected = table.concat (expect_table , " \n " )
171
+
172
+ local result = get_convert_indent_result (input_string , indent_string )
173
+
174
+ assert .are .same (expected , result )
175
+ end )
176
+ it ([[ literal \t to tab]] , function ()
177
+ local input_table = {
178
+ " line1: no indent" ,
179
+ " " ,
180
+ [[ \\tline3: 1 indent]] ,
181
+ [[ \\t\\tline4: 2 indent]] ,
182
+ }
183
+ local input_string = table.concat (input_table , [[ \n]] )
184
+ local indent_string = [[ \\t]]
185
+ local expect_table = {
186
+ " line1: no indent" ,
187
+ " " ,
188
+ " \t line3: 1 indent" ,
189
+ " \t\t line4: 2 indent" ,
190
+ }
191
+ local expected = table.concat (expect_table , " \n " )
192
+
193
+ local result = get_convert_indent_result (input_string , indent_string )
194
+
195
+ assert .are .same (expected , result )
196
+ end )
197
+ end )
0 commit comments