v2 / vlib / builtin / string_strip_margin_test.v
96 lines · 81 sloc · 3.71 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4
5fn test_strip_margins_no_tabs() {
6 no_tabs := ['Hello there', 'This is a string', 'With multiple lines'].join('\n')
7 no_tabs_stripped := 'Hello there
8 |This is a string
9 |With multiple lines'.strip_margin()
10 assert no_tabs == no_tabs_stripped
11}
12
13fn test_strip_margins_text_before() {
14 text_before :=
15 ['There is text', 'before the delimiter', 'that should be removed as well'].join('\n')
16 text_before_stripped := 'There is text
17 f lasj asldfj j lksjdf |before the delimiter
18 Which is removed hello |that should be removed as well'.strip_margin()
19 assert text_before_stripped == text_before
20}
21
22fn test_strip_margins_white_space_after_delim() {
23 tabs := [' Tab', ' spaces', ' another tab'].join('\n')
24 tabs_stripped := ' Tab
25 | spaces
26 | another tab'.strip_margin()
27 assert tabs == tabs_stripped
28}
29
30fn test_strip_margins_alternate_delim() {
31 alternate_delimiter := ['This has a different delim,', 'but that is ok',
32 'because everything works'].join('\n')
33 alternate_delimiter_stripped := 'This has a different delim,
34 #but that is ok
35 #because everything works'.strip_margin_custom(`#`)
36 assert alternate_delimiter_stripped == alternate_delimiter
37}
38
39fn test_strip_margins_multiple_delims_after_first() {
40 delim_after_first_instance := ['The delimiter used',
41 'only matters the |||| First time it is seen', 'not any | other | times'].join('\n')
42 delim_after_first_instance_stripped := 'The delimiter used
43 |only matters the |||| First time it is seen
44 |not any | other | times'.strip_margin()
45 assert delim_after_first_instance_stripped == delim_after_first_instance
46}
47
48fn test_strip_margins_uneven_delims() {
49 uneven_delims := ["It doesn't matter if the delims are uneven,",
50 'The text will still be delimited correctly.', 'Maybe not everything needs 3 lines?',
51 'Let us go for 4 then'].join('\n')
52 uneven_delims_stripped := "It doesn't matter if the delims are uneven,
53 |The text will still be delimited correctly.
54 |Maybe not everything needs 3 lines?
55 |Let us go for 4 then".strip_margin()
56 assert uneven_delims_stripped == uneven_delims
57}
58
59fn test_strip_margins_multiple_blank_lines() {
60 multi_blank_lines := ['Multiple blank lines will be removed.',
61 ' I actually consider this a feature.'].join('\n')
62 multi_blank_lines_stripped := 'Multiple blank lines will be removed.
63
64
65
66 | I actually consider this a feature.'.strip_margin()
67 assert multi_blank_lines == multi_blank_lines_stripped
68}
69
70fn test_strip_margins_end_newline() {
71 end_with_newline :=
72 ['This line will end with a newline', 'Something cool or something.', ''].join('\n')
73 end_with_newline_stripped := 'This line will end with a newline
74 |Something cool or something.
75
76 '.strip_margin()
77 assert end_with_newline_stripped == end_with_newline
78}
79
80fn test_strip_margins_space_delimiter() {
81 space_delimiter :=
82 ['Using a white-space char will', 'revert back to default behavior.'].join('\n')
83 space_delimiter_stripped := 'Using a white-space char will
84 |revert back to default behavior.'.strip_margin_custom(`\n`)
85 assert space_delimiter == space_delimiter_stripped
86}
87
88fn test_strip_margins_crlf() {
89 crlf :=
90 ["This string's line endings have CR as well as LFs.", 'This should pass', 'Definitely'].join('\r\n')
91 crlf_stripped := "This string's line endings have CR as well as LFs.\r
92 |This should pass\r
93 |Definitely".strip_margin()
94
95 assert crlf == crlf_stripped
96}
97