35 lines
1.3 KiB
Plaintext
35 lines
1.3 KiB
Plaintext
# Tests of text: what a literal is where nothing asks for a type, and what
|
|||
|
|
# trimming gives of a view and of a string.
|
||
|
|
import std::testing;
|
||
|
|
|
||
|
|
|
||
|
|
# Whether the type is `strview`, which a literal is where nothing asks otherwise.
|
||
|
|
def is_view<T>(value: &T): bool {
|
||
|
|
return name_of::<T>() == "strview";
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def text_literals_are_views(t: &mut tester): void {
|
||
|
|
val plain := "abc";
|
||
|
|
val built := $"abc{1}";
|
||
|
|
t.expects(is_view(&plain) && !is_view(&built));
|
||
|
|
t.expects(is_view(&"abc".trim()) && "abc".length == 3);
|
||
|
|
|
||
|
|
# Each trimmed literal views its own bytes, which last as long as the program.
|
||
|
|
val first := " x ".trim();
|
||
|
|
val second := " y ".trim();
|
||
|
|
t.expects(first == "x" && second == "y");
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def text_trims_to_what_it_was_given(t: &mut tester): void {
|
||
|
|
val view: strview = " \t both \n";
|
||
|
|
t.expects(view.trim() == "both" && view.ltrim() == "both \n" && view.rtrim() == " \t both");
|
||
|
|
t.expects(is_view(&view.trim()));
|
||
|
|
|
||
|
|
# A string's trims are strings of their own, which leave it as it was.
|
||
|
|
val owned := string::from(" held ", nothing {});
|
||
|
|
var trimmed := owned.trim();
|
||
|
|
trimmed.append("!");
|
||
|
|
t.expects(trimmed.span == "held!" && owned.span == " held ");
|
||
|
|
t.expects(owned.ltrim().span == "held " && owned.rtrim().span == " held");
|
||
|
|
t.expects(!is_view(&owned.trim()));
|
||
|
|
}
|