@@ -24,8 +24,39 @@ return {
2424 assert .are_equal (type (doclayout .space ), ' userdata' )
2525 end ),
2626
27+ test (' chomp' , function ()
28+ local doc = ' a' .. doclayout .blanklines (2 )
29+ assert .are_equal (doclayout .chomp (doc ), doclayout .literal ' a' )
30+ end ),
31+ test (' nestle' , function ()
32+ local doc = doclayout .blanklines (3 ) .. ' a'
33+ assert .are_equal (doclayout .nestle (doc ), doclayout .literal ' a' )
34+ end ),
35+ group ' enclosing' {
36+ test (' quotes' , function ()
37+ local doc = doclayout .literal ' single'
38+ assert .are_equal (
39+ doclayout .quotes (doc ),
40+ " '" .. doclayout .literal ' single' .. " '"
41+ )
42+ end ),
43+ test (' double quotes' , function ()
44+ local doc = doclayout .literal ' double'
45+ assert .are_equal (
46+ doclayout .double_quotes (doc ),
47+ ' "' .. doclayout .literal ' double' .. ' "'
48+ )
49+ end ),
50+ test (' inside' , function ()
51+ local doc = doclayout .literal ' Hello,' + ' World!'
52+ assert .are_equal (
53+ doclayout .inside (' Yo! ' , ' Wassup?' , doc ),
54+ ' Yo! ' .. doc .. ' Wassup?'
55+ )
56+ end ),
57+ },
2758 group ' concat' {
28- test (' without sep' , function ()
59+ test (' with sep' , function ()
2960 local list = {
3061 doclayout .literal ' one' ,
3162 doclayout .literal ' two' ,
@@ -44,13 +75,122 @@ return {
4475 }
4576 assert .are_equal (doclayout .concat (list ), list [1 ] .. list [2 ] .. list [3 ])
4677 end ),
47- }
78+ },
4879 },
4980
5081 group ' render' {
5182 test (' empty doc' , function ()
5283 assert .are_equal (doclayout .render (doclayout .empty ), ' ' )
53- end )
84+ end ),
85+ test (' reflow' , function ()
86+ local greeting = doclayout .literal ' Hi!' .. doclayout .space
87+ .. doclayout .literal ' How' .. doclayout .space
88+ .. doclayout .literal ' are' .. doclayout .space
89+ .. doclayout .literal ' you?'
90+ assert .are_equal (doclayout .render (greeting , 7 ), ' Hi! How\n are\n you?' )
91+ end ),
92+ test (' after_break' , function ()
93+ local doc = doclayout .literal ' hi'
94+ + doclayout .after_break ' !'
95+ .. doclayout .after_break ' ?'
96+ .. doclayout .literal ' x'
97+ .. doclayout .after_break ' ?'
98+ assert .are_equal (doclayout .render (doc , 2 ), ' hi\n !x' )
99+ end ),
100+ test (' before_non_blank' , function ()
101+ local doc = doclayout .before_non_blank ' !!' .. ' ab'
102+ / doclayout .before_non_blank ' !!' .. ' a b'
103+ assert .are_equal (doclayout .render (doc ), ' ab\n !!a b' )
104+ end ),
105+ test (' blanks at beginning' , function ()
106+ local doc = doclayout .blanklines (2 ) .. ' aa'
107+ -- only one newline, as the top of doc is treated as implicit blank
108+ assert .are_equal (doclayout .render (doc ), ' \n aa' )
109+ end ),
110+ test (' blanklines' , function ()
111+ local doc = ' aa' .. doclayout .blanklines (2 ) .. ' bb'
112+ assert .are_equal (doclayout .render (doc ), ' aa\n\n\n bb' )
113+ end ),
114+ test (' braces' , function ()
115+ local doc = doclayout .braces ' maybe'
116+ assert .are_equal (doclayout .render (doc ), ' {maybe}' )
117+ end ),
118+ test (' brackets' , function ()
119+ local doc = doclayout .brackets ' 1'
120+ assert .are_equal (doclayout .render (doc ), ' [1]' )
121+ end ),
122+ test (' flush' , function ()
123+ local doc = doclayout .flush (doclayout .nest (2 , ' hi' ))
124+ assert .are_equal (doclayout .render (doc ), ' hi' )
125+ end ),
126+ test (' hang' , function ()
127+ local doc = doclayout .hang (4 , ' - ' , ' aa\n bb\n cc' )
128+ assert .are_equal (
129+ doclayout .render (doc ),
130+ table.concat {
131+ ' - aa\n ' ,
132+ ' bb\n ' ,
133+ ' cc' ,
134+ }
135+ )
136+ end ),
137+ test (' nest' , function ()
138+ local doc = doclayout .nest (2 , ' aa\n\n bb\n cc' )
139+ assert .are_equal (
140+ doclayout .render (doc ),
141+ table.concat {
142+ ' aa\n ' ,
143+ ' \n ' ,
144+ ' bb\n ' ,
145+ ' cc'
146+ }
147+ )
148+ end ),
149+ test (' nowrap' , function ()
150+ local doc = doclayout .nowrap (doclayout .literal ' first' + ' second' )
151+ assert .are_equal (doclayout .render (doc , 8 ), ' first second' )
152+ end ),
153+ test (' parens' , function ()
154+ local doc = doclayout .parens ' lisp'
155+ assert .are_equal (doclayout .render (doc ), ' (lisp)' )
156+ end ),
157+ test (' prefixed' , function ()
158+ local doc = doclayout .prefixed (' # ' , doclayout .literal ' aa' // ' bb' )
159+ assert .are_equal (doclayout .render (doc ), ' # aa\n #\n # bb' )
160+ end ),
161+ group ' table helpers' {
162+ test (' cblock' , function ()
163+ local doc = doclayout .cblock (2 , ' | ' )
164+ .. doclayout .cblock (4 , ' aa' )
165+ .. doclayout .cblock (2 , ' |' )
166+ assert .are_equal (doclayout .render (doc ), ' | aa |' )
167+ end ),
168+ test (' lblock' , function ()
169+ local doc = doclayout .lblock (2 , ' | ' )
170+ .. doclayout .lblock (4 , ' aa' )
171+ .. doclayout .lblock (2 , ' |' )
172+ assert .are_equal (doclayout .render (doc ), ' | aa |' )
173+ end ),
174+ test (' rblock' , function ()
175+ local doc = doclayout .rblock (2 , ' | ' )
176+ .. doclayout .rblock (4 , ' aa' )
177+ .. doclayout .rblock (2 , ' |' )
178+ assert .are_equal (doclayout .render (doc ), ' | aa |' )
179+ end ),
180+ test (' vfill' , function ()
181+ local doc = doclayout .vfill ' | '
182+ .. doclayout .lblock (4 , doclayout .literal ' aa' // ' bbb' )
183+ .. doclayout .vfill (' |' )
184+ assert .are_equal (
185+ doclayout .render (doc ),
186+ table.concat {
187+ ' | aa |\n ' ,
188+ ' | |\n ' ,
189+ ' | bbb |'
190+ }
191+ )
192+ end )
193+ }
54194 },
55195
56196 group ' document querying' {
@@ -86,6 +226,13 @@ return {
86226 assert .are_equal (doclayout .real_length (' ❄' ), 1 )
87227 assert .are_equal (doclayout .real_length (' シ' ), 2 )
88228 end ),
229+
230+ test (' update_column' , function ()
231+ local doc = ' long' .. doclayout .cr .. ' longer'
232+ assert .are_equal (doclayout .update_column (doc , 0 ), 6 )
233+ assert .are_equal (doclayout .update_column (doclayout .empty , 42 ), 0 )
234+ assert .are_equal (doclayout .update_column (' four' , 4 ), 8 )
235+ end )
89236 },
90237
91238 group ' Doc type' {
0 commit comments