| 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 | |
| 5 | fn test_empty_string() { |
| 6 | assert ''.trim_indent() == '' |
| 7 | } |
| 8 | |
| 9 | fn test_blank_string() { |
| 10 | assert ' \t'.trim_indent() == '' |
| 11 | } |
| 12 | |
| 13 | fn test_multiline_blank_string() { |
| 14 | assert ' |
| 15 | \t |
| 16 | '.trim_indent() == '' |
| 17 | } |
| 18 | |
| 19 | fn test_zero_indentation() { |
| 20 | assert 'abc |
| 21 | def'.trim_indent() == 'abc\ndef' |
| 22 | } |
| 23 | |
| 24 | fn test_zero_indentation_and_blank_first_and_last_lines() { |
| 25 | assert ' |
| 26 | abc |
| 27 | def |
| 28 | '.trim_indent() == 'abc\ndef' |
| 29 | } |
| 30 | |
| 31 | fn test_common_case_tabbed() { |
| 32 | assert ' |
| 33 | abc |
| 34 | def |
| 35 | '.trim_indent() == 'abc\ndef' |
| 36 | } |
| 37 | |
| 38 | fn test_common_case_spaced() { |
| 39 | assert ' |
| 40 | abc |
| 41 | def |
| 42 | '.trim_indent() == 'abc\ndef' |
| 43 | } |
| 44 | |
| 45 | fn test_common_case_tabbed_with_middle_blank_like() { |
| 46 | assert ' |
| 47 | abc |
| 48 | |
| 49 | def |
| 50 | '.trim_indent() == 'abc\n\ndef' |
| 51 | } |
| 52 | |
| 53 | fn test_common_case_tabbed_with_blank_first_line() { |
| 54 | assert ' \t |
| 55 | abc |
| 56 | def |
| 57 | '.trim_indent() == 'abc\ndef' |
| 58 | } |
| 59 | |
| 60 | fn test_common_case_tabbed_with_blank_first_and_last_line() { |
| 61 | assert ' \t |
| 62 | abc |
| 63 | def |
| 64 | \t '.trim_indent() == 'abc\ndef' |
| 65 | } |
| 66 | |
| 67 | fn test_html() { |
| 68 | assert ' |
| 69 | <!doctype html> |
| 70 | <html lang="en"> |
| 71 | <head> |
| 72 | </head> |
| 73 | <body> |
| 74 | <p> |
| 75 | Hello, World! |
| 76 | </p> |
| 77 | </body> |
| 78 | </html> |
| 79 | '.trim_indent() == '<!doctype html> |
| 80 | <html lang="en"> |
| 81 | <head> |
| 82 | </head> |
| 83 | <body> |
| 84 | <p> |
| 85 | Hello, World! |
| 86 | </p> |
| 87 | </body> |
| 88 | </html>' |
| 89 | } |
| 90 | |
| 91 | fn test_broken_html() { |
| 92 | assert ' |
| 93 | <!doctype html> |
| 94 | <html lang="en"> |
| 95 | <head> |
| 96 | </head> |
| 97 | <body> |
| 98 | <p> |
| 99 | Hello, World! |
| 100 | </p> |
| 101 | </body> |
| 102 | </html> |
| 103 | '.trim_indent() == ' <!doctype html> |
| 104 | <html lang="en"> |
| 105 | <head> |
| 106 | </head> |
| 107 | <body> |
| 108 | <p> |
| 109 | Hello, World! |
| 110 | </p> |
| 111 | </body> |
| 112 | </html>' |
| 113 | } |
| 114 | |
| 115 | fn test_doc_example() { |
| 116 | st := ' |
| 117 | Hello there, |
| 118 | this is a string, |
| 119 | all the leading indents are removed |
| 120 | and also the first and the last lines if they are blank |
| 121 | '.trim_indent() |
| 122 | assert st == 'Hello there, |
| 123 | this is a string, |
| 124 | all the leading indents are removed |
| 125 | and also the first and the last lines if they are blank' |
| 126 | } |
| 127 | |