@@ -23,6 +23,16 @@ function qcheck(what, args, expected) {
2323 } ) ;
2424}
2525
26+ function qcheckgen ( gen , what , args , expected ) {
27+ it ( `${ what } : returns ${ expected } for ${ args } ` , ( ) => {
28+ console . log ( `BEGIN TEST ${ what } :` , args ) ;
29+ let actual = Array . from ( gen ( ...args ) ) ;
30+ console . log ( `ACTUAL: ` , actual ) ;
31+ console . log ( `EXPECTED:` , [ expected ] ) ;
32+ expect ( actual [ 0 ] ) . toBe ( expected ) ;
33+ } ) ;
34+ }
35+
2636describe ( 'MagicReflow' , ( ) => {
2737 describe ( 'vlen' , ( ) => {
2838 let vlen = MagicReflow . vlen ;
@@ -76,28 +86,30 @@ describe('MagicReflow', () => {
7686 describe ( 'indent_with_tabs' , ( ) => {
7787 let iwt = MagicReflow . indent_with_tabs ;
7888
79- describe ( 'empty strings' , ( ) => {
80- qcheck ( iwt , [ 0 , 0 , 4 ] , '' ) ;
81- qcheck ( iwt , [ 0 , 1 , 4 ] , '' ) ;
82- } ) ;
89+ qcheck ( iwt , [ 0 , 4 ] , '' ) ;
90+ qcheck ( iwt , [ 1 , 4 ] , ' ' ) ;
91+ qcheck ( iwt , [ 4 , 4 ] , '\t' ) ;
92+ qcheck ( iwt , [ 5 , 4 ] , '\t ' ) ;
93+ qcheck ( iwt , [ 8 , 4 ] , '\t\t' ) ;
94+ qcheck ( iwt , [ 10 , 4 ] , '\t\t ' ) ;
95+ } ) ;
8396
84- describe ( 'first tab' , ( ) => {
85- qcheck ( iwt , [ 4 , 0 , 4 ] , '\t' ) ;
86- qcheck ( iwt , [ 3 , 1 , 4 ] , '\t' ) ;
87- } ) ;
97+ describe ( 'leading_tabs_to_spaces' , ( ) => {
98+ let gen = MagicReflow . leading_tabs_to_spaces ;
8899
89- describe ( 'multiple tabs' , ( ) => {
90- qcheck ( iwt , [ 8 , 0 , 4 ] , '\t\t' ) ;
91- qcheck ( iwt , [ 6 , 2 , 4 ] , '\t\t' ) ;
92- } ) ;
100+ qcheckgen ( gen , 'tab' , [ [ '\tfoo' ] , 4 ] , ' foo' ) ;
101+ qcheckgen ( gen , 'tab+spc' , [ [ '\t foo' ] , 4 ] , ' foo' ) ;
102+ qcheckgen ( gen , 'spc+tab+spc' , [ [ ' \t foo' ] , 4 ] , ' foo' ) ;
103+ qcheckgen ( gen , 'spc+tab+spc+tab' , [ [ ' \t \tfoo' ] , 4 ] , ' foo' ) ;
104+ } ) ;
93105
94- describe ( 'trailing spaces ' , ( ) => {
95- qcheck ( iwt , [ 2 , 0 , 4 ] , ' ' ) ;
96- qcheck ( iwt , [ 2 , 2 , 4 ] , '\t' ) ;
97- qcheck ( iwt , [ 1 , 2 , 4 ] , ' ' ) ;
98- qcheck ( iwt , [ 5 , 2 , 4 ] , '\t ' ) ;
99- qcheck ( iwt , [ 5 , 6 , 4 ] , '\t ' ) ;
100- } ) ;
106+ describe ( 'leading_spaces_to_tabs ' , ( ) => {
107+ let gen = MagicReflow . leading_spaces_to_tabs ;
108+
109+ qcheckgen ( gen , 'tab' , [ [ ' foo' ] , 4 ] , '\tfoo ' ) ;
110+ qcheckgen ( gen , 'tab+spc' , [ [ ' foo' ] , 4 ] , '\t foo ' ) ;
111+ qcheckgen ( gen , 'spc+tab+spc' , [ [ ' foo' ] , 4 ] , '\t foo ' ) ;
112+ qcheckgen ( gen , 'spc+tab+spc+tab' , [ [ ' foo' ] , 4 ] , '\t\tfoo' ) ;
101113 } ) ;
102114
103115 describe ( 'when reflowing a single paragraph' , ( ) => {
@@ -194,6 +206,17 @@ describe('MagicReflow', () => {
194206 ' This is\n the first\n line. This\n is the\n second line.'
195207 ) ) ;
196208
209+ it ( 'converts spaces to tabs if appropriate' , ( ) => test (
210+ [ ' This is the first line.' , 24 , 4 , false ] ,
211+ '\tThis is the first line.'
212+ ) ) ;
213+
214+ it ( 'handles inconsistent use of tabs and spaces' , ( ) => test (
215+ [ '\tLeading tab.\n Second line.' , 40 , 8 , true ] ,
216+ ' Leading tab. Second line.'
217+ ) ) ;
218+
219+
197220 it ( 'fixes inconsistent indentation' , ( ) => test (
198221 [ `
199222 This is the first line.
@@ -214,22 +237,32 @@ describe('MagicReflow', () => {
214237
215238 describe ( 'when dealing with leading tab characters' , ( ) => {
216239 it ( 'preserves block style with tabs (one line)' , ( ) => test (
217- [ '\tLeading tab.\n\tSecond line.' , 40 ] ,
240+ [ '\tLeading tab.\n\tSecond line.' , 40 , 8 , false ] ,
218241 '\tLeading tab. Second line.'
219242 ) ) ;
220243 it ( 'preserves block style with tabs (multi-line)' , ( ) => test (
221- [ '\tLeading tab that is a long line.\n\tSecond line.' , 24 , 8 ] ,
244+ [ '\tLeading tab that is a long line.\n\tSecond line.' , 24 , 8 , false ] ,
222245 '\tLeading tab that\n\tis a long line.\n\tSecond line.'
223246 ) ) ;
224247 it ( 'uses the correct tab width for indentation' , ( ) => test (
225- [ '\tLeading tab that is a long line. Should be 24 cols.' , 24 , 4 ] ,
248+ [ '\tLeading tab that is a long line. Should be 24 cols.' , 24 , 4 , false ] ,
226249 '\tLeading tab that is\n\ta long line. Should\n\tbe 24 cols.' ,
227250 ) ) ;
228251 it ( 'handles tabs after leading sigils' , ( ) => test (
229252 [ '1.\tThis is a numbered list item that needs wrapping.' , 24 , 4 , false ] ,
230253 '1.\tThis is a numbered\n\tlist item that needs\n\twrapping.'
231254 ) ) ;
232255
256+ it ( 'converts tabs to spaces if appropriate' , ( ) => test (
257+ [ '\tLeading tab.\n\tSecond line.' , 40 , 8 , true ] ,
258+ ' Leading tab. Second line.'
259+ ) ) ;
260+
261+ it ( 'handles inconsistent use of tabs and spaces' , ( ) => test (
262+ [ '\tLeading tab.\n Second line.' , 40 , 8 , false ] ,
263+ '\tLeading tab. Second line.'
264+ ) ) ;
265+
233266 // BEGIN: Leading indents are not supported currently.
234267 xit ( 'preserves leading indents with tabs (one line)' , ( ) => test (
235268 [ '\tLeading tab.\nSecond line.' , 40 ] ,
0 commit comments