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