2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2020-03-26 21:17:14 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
fn test_strip_margins_no_tabs() {
|
2021-03-24 21:39:59 +03:00
|
|
|
no_tabs := ['Hello there', 'This is a string', 'With multiple lines'].join('\n')
|
2020-03-26 21:17:14 +03:00
|
|
|
no_tabs_stripped := 'Hello there
|
|
|
|
|This is a string
|
|
|
|
|With multiple lines'.strip_margin()
|
|
|
|
assert no_tabs == no_tabs_stripped
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_strip_margins_text_before() {
|
2021-03-24 21:39:59 +03:00
|
|
|
text_before := ['There is text', 'before the delimiter', 'that should be removed as well'].join('\n')
|
2020-03-26 21:17:14 +03:00
|
|
|
text_before_stripped := 'There is text
|
|
|
|
f lasj asldfj j lksjdf |before the delimiter
|
|
|
|
Which is removed hello |that should be removed as well'.strip_margin()
|
|
|
|
assert text_before_stripped == text_before
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_strip_margins_white_space_after_delim() {
|
2021-03-24 21:39:59 +03:00
|
|
|
tabs := [' Tab', ' spaces', ' another tab'].join('\n')
|
2020-03-26 21:17:14 +03:00
|
|
|
tabs_stripped := ' Tab
|
|
|
|
| spaces
|
|
|
|
| another tab'.strip_margin()
|
|
|
|
assert tabs == tabs_stripped
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_strip_margins_alternate_delim() {
|
2021-03-24 21:39:59 +03:00
|
|
|
alternate_delimiter := ['This has a different delim,', 'but that is ok',
|
2021-09-21 16:20:09 +03:00
|
|
|
'because everything works'].join('\n')
|
2020-03-26 21:17:14 +03:00
|
|
|
alternate_delimiter_stripped := 'This has a different delim,
|
|
|
|
#but that is ok
|
2020-04-11 11:06:03 +03:00
|
|
|
#because everything works'.strip_margin_custom(`#`)
|
2020-03-26 21:17:14 +03:00
|
|
|
assert alternate_delimiter_stripped == alternate_delimiter
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_strip_margins_multiple_delims_after_first() {
|
|
|
|
delim_after_first_instance := ['The delimiter used',
|
2021-03-24 21:39:59 +03:00
|
|
|
'only matters the |||| First time it is seen', 'not any | other | times'].join('\n')
|
2020-03-26 21:17:14 +03:00
|
|
|
delim_after_first_instance_stripped := 'The delimiter used
|
|
|
|
|only matters the |||| First time it is seen
|
|
|
|
|not any | other | times'.strip_margin()
|
|
|
|
assert delim_after_first_instance_stripped == delim_after_first_instance
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_strip_margins_uneven_delims() {
|
2021-03-24 21:39:59 +03:00
|
|
|
uneven_delims := ["It doesn't matter if the delims are uneven,",
|
|
|
|
'The text will still be delimited correctly.', 'Maybe not everything needs 3 lines?',
|
2021-09-21 16:20:09 +03:00
|
|
|
'Let us go for 4 then'].join('\n')
|
2021-03-24 21:39:59 +03:00
|
|
|
uneven_delims_stripped := "It doesn't matter if the delims are uneven,
|
2020-03-26 21:17:14 +03:00
|
|
|
|The text will still be delimited correctly.
|
|
|
|
|Maybe not everything needs 3 lines?
|
2021-03-24 21:39:59 +03:00
|
|
|
|Let us go for 4 then".strip_margin()
|
2020-03-26 21:17:14 +03:00
|
|
|
assert uneven_delims_stripped == uneven_delims
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_strip_margins_multiple_blank_lines() {
|
|
|
|
multi_blank_lines := ['Multiple blank lines will be removed.',
|
2021-09-21 16:20:09 +03:00
|
|
|
' I actually consider this a feature.'].join('\n')
|
2020-03-26 21:17:14 +03:00
|
|
|
multi_blank_lines_stripped := 'Multiple blank lines will be removed.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| I actually consider this a feature.'.strip_margin()
|
|
|
|
assert multi_blank_lines == multi_blank_lines_stripped
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_strip_margins_end_newline() {
|
2021-03-24 21:39:59 +03:00
|
|
|
end_with_newline := ['This line will end with a newline', 'Something cool or something.', ''].join('\n')
|
2020-03-26 21:17:14 +03:00
|
|
|
end_with_newline_stripped := 'This line will end with a newline
|
|
|
|
|Something cool or something.
|
|
|
|
|
|
|
|
'.strip_margin()
|
|
|
|
assert end_with_newline_stripped == end_with_newline
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_strip_margins_space_delimiter() {
|
2021-03-24 21:39:59 +03:00
|
|
|
space_delimiter := ['Using a white-space char will', 'revert back to default behavior.'].join('\n')
|
2020-03-26 21:17:14 +03:00
|
|
|
space_delimiter_stripped := 'Using a white-space char will
|
2020-04-11 11:06:03 +03:00
|
|
|
|revert back to default behavior.'.strip_margin_custom(`\n`)
|
2020-03-26 21:17:14 +03:00
|
|
|
assert space_delimiter == space_delimiter_stripped
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_strip_margins_crlf() {
|
2021-03-24 21:39:59 +03:00
|
|
|
crlf := ["This string's line endings have CR as well as LFs.", 'This should pass', 'Definitely'].join('\r\n')
|
|
|
|
crlf_stripped := "This string's line endings have CR as well as LFs.\r
|
2020-03-26 21:17:14 +03:00
|
|
|
|This should pass\r
|
2021-03-24 21:39:59 +03:00
|
|
|
|Definitely".strip_margin()
|
2020-03-26 21:17:14 +03:00
|
|
|
|
|
|
|
assert crlf == crlf_stripped
|
|
|
|
}
|