v2 / vlib / builtin / string_trim_indent_test.v
126 lines · 112 sloc · 1.94 KB · 763f94388b1ceff59c3e68ebda2c9f57197f32a4
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_empty_string() {
6 assert ''.trim_indent() == ''
7}
8
9fn test_blank_string() {
10 assert ' \t'.trim_indent() == ''
11}
12
13fn test_multiline_blank_string() {
14 assert '
15 \t
16'.trim_indent() == ''
17}
18
19fn test_zero_indentation() {
20 assert 'abc
21def'.trim_indent() == 'abc\ndef'
22}
23
24fn test_zero_indentation_and_blank_first_and_last_lines() {
25 assert '
26abc
27def
28'.trim_indent() == 'abc\ndef'
29}
30
31fn test_common_case_tabbed() {
32 assert '
33 abc
34 def
35 '.trim_indent() == 'abc\ndef'
36}
37
38fn test_common_case_spaced() {
39 assert '
40 abc
41 def
42 '.trim_indent() == 'abc\ndef'
43}
44
45fn test_common_case_tabbed_with_middle_blank_like() {
46 assert '
47 abc
48
49 def
50 '.trim_indent() == 'abc\n\ndef'
51}
52
53fn test_common_case_tabbed_with_blank_first_line() {
54 assert ' \t
55 abc
56 def
57 '.trim_indent() == 'abc\ndef'
58}
59
60fn 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
67fn 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
91fn 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
115fn 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,
123this is a string,
124all the leading indents are removed
125and also the first and the last lines if they are blank'
126}
127