diff --git a/src/fmt/fmt_test.v b/src/fmt/fmt_test.v index f9869f1f..6c602208 100644 --- a/src/fmt/fmt_test.v +++ b/src/fmt/fmt_test.v @@ -1,5 +1,8 @@ module main +import os +import time + fn print_lines(lines []string) { println(' ') println('-------------') @@ -8,9 +11,28 @@ fn print_lines(lines []string) { } } +fn temp_file_name() string { + dir := os.temp_dir() + file := '${dir}/t${time.ticks()}' + return file +} + +fn to_tmp_file(data []string) string { + file := temp_file_name() + os.write_file(file, data.join_lines()) or { panic('error') } + return file +} + fn test_basic_wrap() { println(@METHOD) - output := run_fmt(['fmt', '-w', '30', 'testdata/simple.txt']) + input := [ + 'Now is the time for all good men to come to the aid of their country.', + '', + 'Now is the time for all good men to come to the aid of their country.', + ] + tmp := to_tmp_file(input) + output := run_fmt(['fmt', '-w', '30', tmp]) + os.rm(tmp)! // print_lines(output) expected := [ 'Now is the time for all good', @@ -26,7 +48,26 @@ fn test_basic_wrap() { fn test_narrow_to_formatted() { println(@METHOD) - output := run_fmt(['fmt', 'testdata/narrow.txt']) + input := [ + 'Hello World', + '', + 'Hi there!', + 'How are you?', + '', + 'Just do-it.', + 'Believe it.', + '', + 'banana,', + 'papaya,', + 'mango', + '', + 'Much ado about nothing.', + 'He he he.', + 'Adios amigo.', + ] + tmp := to_tmp_file(input) + output := run_fmt(['fmt', tmp]) + os.rm(tmp)! // print_lines(output) expected := [ 'Hello World', @@ -44,7 +85,16 @@ fn test_narrow_to_formatted() { fn test_line_indents_denote_new_paragraph() { println(@METHOD) - output := run_fmt(['fmt', '-w', '35', 'testdata/basic_indent.txt']) + input := [ + 'This is a single line paragraph', + ' because this line has a different indent', + '', + 'Otherwise these other lines comprise a simple', + 'multline paragraph.', + ] + tmp := to_tmp_file(input) + output := run_fmt(['fmt', '-w', '35', tmp]) + os.rm(tmp)! expected := [ 'This is a single line paragraph', ' because this line has a', @@ -59,7 +109,17 @@ fn test_line_indents_denote_new_paragraph() { fn test_numbered_list_no_options() { println(@METHOD) - output := run_fmt(['fmt', 'testdata/list.txt']) + input := [ + 'A list of items', + '', + ' 1. Now is the time for all good men to come to the aid of their country.', + ' 2. Now is the time for all good men to come to the aid of their country.', + ' 3. Now is the time for all good men to come to the aid of their country.', + ' 4. Now is the time for all good men to come to the aid of their country.', + ] + tmp := to_tmp_file(input) + output := run_fmt(['fmt', tmp]) + os.rm(tmp)! // print_lines(output) expected := [ 'A list of items', @@ -75,7 +135,17 @@ fn test_numbered_list_no_options() { fn test_numbered_list_w_40() { println(@METHOD) - output := run_fmt(['fmt', '-w', '40', 'testdata/list.txt']) + input := [ + 'A list of items', + '', + ' 1. Now is the time for all good men to come to the aid of their country.', + ' 2. Now is the time for all good men to come to the aid of their country.', + ' 3. Now is the time for all good men to come to the aid of their country.', + ' 4. Now is the time for all good men to come to the aid of their country.', + ] + tmp := to_tmp_file(input) + output := run_fmt(['fmt', '-w', '40', tmp]) + os.rm(tmp)! // print_lines(output) expected := [ 'A list of items', @@ -94,7 +164,24 @@ fn test_numbered_list_w_40() { fn test_split_only() { println(@METHOD) - output := run_fmt(['fmt', '-s', 'testdata/lorum_ipsum.txt']) + input := [ + 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur dignissim', + 'venenatis pede. Quisque dui dui, ultricies ut, facilisis non, pulvinar non. Duis quis arcu a purus volutpat iaculis. Morbi id dui in diam ornare', + 'dictum. Praesent consectetuer vehicula ipsum. Praesent tortor massa, congue et,', + 'ornare in, posuere eget, pede.', + '', + 'Vivamus rhoncus. Quisque lacus. In hac habitasse platea dictumst. Nullam mauris', + 'tellus, sollicitudin non, semper eget, sodales non, pede. Phasellus varius', + 'ullamcorper libero. Fusce ipsum lorem, iaculis nec, vulputate vitae, suscipit', + 'vel, tortor. Cras varius.', + '', + 'Nullam fringilla pellentesque orci. Nulla eu ante pulvinar velit rhoncus', + 'lacinia. Morbi fringilla lacus quis arcu. Vestibulum sem quam, dapibus in,', + 'fringilla ut, venenatis ut, neque.', + ] + tmp := to_tmp_file(input) + output := run_fmt(['fmt', '-s', tmp]) + os.rm(tmp)! expected := [ 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur', 'dignissim', @@ -120,7 +207,18 @@ fn test_split_only() { fn test_indents_no_blank_lines() { println(@METHOD) - output := run_fmt(['fmt', 'testdata/poem.txt']) + input := [ + 'Love is patient, love is kind. It does not envy,', + ' it does not boast, it is not proud. It is not rude,', + ' it is not self-seeking, it is not easily angered, ', + 'it keeps no record of wrongs. Love does not delight ', + 'in evil but rejoices with the truth. It always protects,', + ' always trusts, always hopes, always perseveres. ', + 'Love never fails.', + ] + tmp := to_tmp_file(input) + output := run_fmt(['fmt', tmp]) + os.rm(tmp)! expected := [ 'Love is patient, love is kind. It does not envy,', ' it does not boast, it is not proud. It is not rude, it is not', @@ -135,7 +233,18 @@ fn test_indents_no_blank_lines() { fn test_prefix_str_option() { println(@METHOD) - output := run_fmt(['fmt', '-p', '> ', 'testdata/prefix.txt']) + input := [ + 'Prefix lines test', + '', + '> Effects present letters inquiry no an removed or friends. Desire behind latter me though in. ', + '> Supposing shameless am he engrossed up additions. My possible peculiar together to. ', + '', + '> Desire so better am cannot he up before points. Remember mistaken opinions it pleasure of debating. ', + '> Court front maids forty if aware their at. Chicken use are pressed removed.', + ] + tmp := to_tmp_file(input) + output := run_fmt(['fmt', '-p', '> ', tmp]) + os.rm(tmp)! expected := [ 'Prefix lines test', '', @@ -153,8 +262,12 @@ fn test_prefix_str_option() { fn test_uniform_spacing_option() { println(@METHOD) + input := [ + 'venenatis pede. Quisque dui dui, ultricies ut, facilisis non, pulvinar non. Duis quis arcu a purus volutpat iaculis. Morbi id dui in diam ornare', + ] + tmp := to_tmp_file(input) // non-uniform case - output1 := run_fmt(['fmt', 'testdata/not_uniform_spacing.txt']) + output1 := run_fmt(['fmt', tmp]) expected1 := [ 'venenatis pede. Quisque dui dui, ultricies ut, facilisis non,', 'pulvinar non. Duis quis arcu a purus volutpat iaculis. Morbi id dui', @@ -163,7 +276,8 @@ fn test_uniform_spacing_option() { assert output1 == expected1 // uniform spacing case - output2 := run_fmt(['fmt', '-u', 'testdata/not_uniform_spacing.txt']) + output2 := run_fmt(['fmt', '-u', tmp]) + os.rm(tmp)! expected2 := [ 'venenatis pede. Quisque dui dui, ultricies ut, facilisis non, pulvinar non.', 'Duis quis arcu a purus volutpat iaculis. Morbi id dui in diam ornare', @@ -173,7 +287,18 @@ fn test_uniform_spacing_option() { fn test_uniform_spacing_with_prefix_and_width() { println(@METHOD) - output := run_fmt(['fmt', '-u', '-p', '> ', '-w', '30', 'testdata/prefix.txt']) + input := [ + 'Prefix lines test', + '', + '> Effects present letters inquiry no an removed or friends. Desire behind latter me though in. ', + '> Supposing shameless am he engrossed up additions. My possible peculiar together to. ', + '', + '> Desire so better am cannot he up before points. Remember mistaken opinions it pleasure of debating. ', + '> Court front maids forty if aware their at. Chicken use are pressed removed.', + ] + tmp := to_tmp_file(input) + output := run_fmt(['fmt', '-u', '-p', '> ', '-w', '30', tmp]) + os.rm(tmp)! expected := [ '> Prefix lines test', '', @@ -199,7 +324,14 @@ fn test_uniform_spacing_with_prefix_and_width() { fn test_crown_and_uniform_options() { println(@METHOD) - output := run_fmt(['fmt', '-c', '-u', 'testdata/crown.txt']) + input := [ + 'By default, blank lines, spaces between words, and indentation are preserved in the output;', + ' successive input lines with different indentation are not joined; tabs are expanded on input and', + 'introduced on output.', + ] + tmp := to_tmp_file(input) + output := run_fmt(['fmt', '-c', '-u', tmp]) + os.rm(tmp)! expected := [ 'By default, blank lines, spaces between words, and indentation are', ' preserved in the output; successive input lines with different', @@ -211,7 +343,14 @@ fn test_crown_and_uniform_options() { fn test_tagged_and_width_options() { println(@METHOD) - output := run_fmt(['fmt', '-t', '-w', '40', 'testdata/simple.txt']) + input := [ + 'Now is the time for all good men to come to the aid of their country.', + '', + 'Now is the time for all good men to come to the aid of their country.', + ] + tmp := to_tmp_file(input) + output := run_fmt(['fmt', '-t', '-w', '40', tmp]) + os.rm(tmp)! expected := [ 'Now is the time for all good men to come', ' to the aid of their country.', @@ -224,7 +363,12 @@ fn test_tagged_and_width_options() { fn test_unicode_handling() { println(@METHOD) - output := run_fmt(['fmt', '-w', '40', 'testdata/unicode.txt']) + input := [ + "I can do without ⑰ lobsters, you know. Come on!' So they ⼘≺↩⌝⚙⠃ couldn't get them out again. The Mock Turtle went on again:-- 'I didn't mean it!' Ⓡpleaded.", + ] + tmp := to_tmp_file(input) + output := run_fmt(['fmt', '-w', '40', tmp]) + os.rm(tmp)! expected := [ 'I can do without ⑰ lobsters, you know.', "Come on!' So they ⼘≺↩⌝⚙⠃ couldn't get", @@ -233,3 +377,21 @@ fn test_unicode_handling() { ] assert output == expected } + +fn test_unicode__tab_handling() { + println(@METHOD) + input := [ + "I can do without ⑰ lobsters, \tyou know. Come on!' So they ⼘≺↩⌝⚙⠃ couldn't get them out again. The Mock Turtle went on again:-- 'I didn't mean it!' Ⓡpleaded.", + ] + tmp := to_tmp_file(input) + output := run_fmt(['fmt', '-w', '40', tmp]) + os.rm(tmp)! + expected := [ + 'I can do without ⑰ lobsters, you', + "know. Come on!' So they ⼘≺↩⌝⚙⠃ couldn't", + 'get them out again. The Mock Turtle went', + "on again:-- 'I didn't mean it!'", + 'Ⓡpleaded.', + ] + assert output == expected +} diff --git a/src/fmt/testdata/basic_indent.txt b/src/fmt/testdata/basic_indent.txt deleted file mode 100644 index a2ba5744..00000000 --- a/src/fmt/testdata/basic_indent.txt +++ /dev/null @@ -1,5 +0,0 @@ -This is a single line paragraph - because this line has a different indent - -Otherwise these other lines comprise a simple -multline paragraph. \ No newline at end of file diff --git a/src/fmt/testdata/crown.txt b/src/fmt/testdata/crown.txt deleted file mode 100644 index 196e483e..00000000 --- a/src/fmt/testdata/crown.txt +++ /dev/null @@ -1,3 +0,0 @@ -By default, blank lines, spaces between words, and indentation are preserved in the output; - successive input lines with different indentation are not joined; tabs are expanded on input and -introduced on output. \ No newline at end of file diff --git a/src/fmt/testdata/list.txt b/src/fmt/testdata/list.txt deleted file mode 100644 index cc55cee5..00000000 --- a/src/fmt/testdata/list.txt +++ /dev/null @@ -1,6 +0,0 @@ -A list of items - - 1. Now is the time for all good men to come to the aid of their country. - 2. Now is the time for all good men to come to the aid of their country. - 3. Now is the time for all good men to come to the aid of their country. - 4. Now is the time for all good men to come to the aid of their country. diff --git a/src/fmt/testdata/lorum_ipsum.txt b/src/fmt/testdata/lorum_ipsum.txt deleted file mode 100644 index c6822282..00000000 --- a/src/fmt/testdata/lorum_ipsum.txt +++ /dev/null @@ -1,13 +0,0 @@ -Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur dignissim -venenatis pede. Quisque dui dui, ultricies ut, facilisis non, pulvinar non. Duis quis arcu a purus volutpat iaculis. Morbi id dui in diam ornare -dictum. Praesent consectetuer vehicula ipsum. Praesent tortor massa, congue et, -ornare in, posuere eget, pede. - -Vivamus rhoncus. Quisque lacus. In hac habitasse platea dictumst. Nullam mauris -tellus, sollicitudin non, semper eget, sodales non, pede. Phasellus varius -ullamcorper libero. Fusce ipsum lorem, iaculis nec, vulputate vitae, suscipit -vel, tortor. Cras varius. - -Nullam fringilla pellentesque orci. Nulla eu ante pulvinar velit rhoncus -lacinia. Morbi fringilla lacus quis arcu. Vestibulum sem quam, dapibus in, -fringilla ut, venenatis ut, neque. \ No newline at end of file diff --git a/src/fmt/testdata/narrow.txt b/src/fmt/testdata/narrow.txt deleted file mode 100644 index c12a17fe..00000000 --- a/src/fmt/testdata/narrow.txt +++ /dev/null @@ -1,15 +0,0 @@ -Hello World - -Hi there! -How are you? - -Just do-it. -Believe it. - -banana, -papaya, -mango - -Much ado about nothing. -He he he. -Adios amigo. \ No newline at end of file diff --git a/src/fmt/testdata/not_uniform_spacing.txt b/src/fmt/testdata/not_uniform_spacing.txt deleted file mode 100644 index 5a500b81..00000000 --- a/src/fmt/testdata/not_uniform_spacing.txt +++ /dev/null @@ -1 +0,0 @@ -venenatis pede. Quisque dui dui, ultricies ut, facilisis non, pulvinar non. Duis quis arcu a purus volutpat iaculis. Morbi id dui in diam ornare \ No newline at end of file diff --git a/src/fmt/testdata/poem.txt b/src/fmt/testdata/poem.txt deleted file mode 100644 index 4982bdbf..00000000 --- a/src/fmt/testdata/poem.txt +++ /dev/null @@ -1,7 +0,0 @@ -Love is patient, love is kind. It does not envy, - it does not boast, it is not proud. It is not rude, - it is not self-seeking, it is not easily angered, -it keeps no record of wrongs. Love does not delight -in evil but rejoices with the truth. It always protects, - always trusts, always hopes, always perseveres. -Love never fails. \ No newline at end of file diff --git a/src/fmt/testdata/prefix.txt b/src/fmt/testdata/prefix.txt deleted file mode 100644 index 90dbc8a6..00000000 --- a/src/fmt/testdata/prefix.txt +++ /dev/null @@ -1,7 +0,0 @@ -Prefix lines test - -> Effects present letters inquiry no an removed or friends. Desire behind latter me though in. -> Supposing shameless am he engrossed up additions. My possible peculiar together to. - -> Desire so better am cannot he up before points. Remember mistaken opinions it pleasure of debating. -> Court front maids forty if aware their at. Chicken use are pressed removed. diff --git a/src/fmt/testdata/simple.txt b/src/fmt/testdata/simple.txt deleted file mode 100644 index 2cb5a012..00000000 --- a/src/fmt/testdata/simple.txt +++ /dev/null @@ -1,3 +0,0 @@ -Now is the time for all good men to come to the aid of their country. - -Now is the time for all good men to come to the aid of their country. \ No newline at end of file diff --git a/src/fmt/testdata/unicode.txt b/src/fmt/testdata/unicode.txt deleted file mode 100644 index ea2cf692..00000000 --- a/src/fmt/testdata/unicode.txt +++ /dev/null @@ -1 +0,0 @@ -I can do without ⑰ lobsters, you know. Come on!' So they ⼘≺↩⌝⚙⠃ couldn't get them out again. The Mock Turtle went on again:-- 'I didn't mean it!' Ⓡpleaded. \ No newline at end of file