1 extern crate array_tool;
2
3 #[test]
it_squeezes_characters()4 fn it_squeezes_characters() {
5 use array_tool::string::Squeeze;
6
7 assert_eq!("yellow moon".squeeze(""), "yelow mon");
8 assert_eq!(" now is the".squeeze(" "), " now is the");
9 assert_eq!("ééé".squeeze(""), "é");
10 assert_eq!(" /// aa".squeeze("/"), " / aa");
11
12 let string: String = format!("yellow moon");
13 assert_eq!(string.squeeze(""), "yelow mon");
14
15 assert_eq!("".squeeze(""), "");
16 }
17
18 #[test]
it_iterates_over_every_grapheme_character()19 fn it_iterates_over_every_grapheme_character() {
20 use array_tool::string::ToGraphemeBytesIter;
21
22 let temp = "a s—d féZ";
23 let mut giter = temp.grapheme_bytes_iter();
24
25 assert_eq!(giter.next().unwrap(), "a".as_bytes());
26 assert_eq!(giter.next().unwrap(), " ".as_bytes());
27 assert_eq!(giter.next().unwrap(), "s".as_bytes());
28 assert_eq!(giter.next().unwrap(), "—".as_bytes());
29 assert_eq!(giter.next().unwrap(), "d".as_bytes());
30 assert_eq!(giter.next().unwrap(), " ".as_bytes());
31 assert_eq!(giter.next().unwrap(), "f".as_bytes());
32 assert_eq!(giter.next().unwrap(), "é".as_bytes());
33 assert_eq!(giter.next().unwrap(), "Z".as_bytes());
34 assert_eq!(giter.next(), None);
35
36 let somestring2 = format!("{}", "a s—d féZ");
37 let mut giter2 = somestring2.grapheme_bytes_iter();
38
39 assert_eq!(giter2.next().unwrap(), "a".as_bytes());
40 assert_eq!(giter2.next().unwrap(), " ".as_bytes());
41 assert_eq!(giter2.next().unwrap(), "s".as_bytes());
42 assert_eq!(giter2.next().unwrap(), "—".as_bytes());
43 assert_eq!(giter2.next().unwrap(), "d".as_bytes());
44 assert_eq!(giter2.next().unwrap(), " ".as_bytes());
45 assert_eq!(giter2.next().unwrap(), "f".as_bytes());
46 assert_eq!(giter2.next().unwrap(), "é".as_bytes());
47 assert_eq!(giter2.next().unwrap(), "Z".as_bytes());
48 assert_eq!(giter2.next(), None);
49
50 assert_eq!("".grapheme_bytes_iter().next(), None);
51 }
52
53 #[test]
it_justifies_one_line_in_for_string()54 fn it_justifies_one_line_in_for_string() {
55 use array_tool::string::Justify;
56
57 assert_eq!("asd asdf asd".justify_line(14), "asd asdf asd");
58 assert_eq!("asd asdf asd".justify_line(16), "asd asdf asd");
59 assert_eq!("asd as df asd".justify_line(16), "asd as df asd");
60 assert_eq!("asd as df asd".justify_line(18), "asd as df asd");
61 assert_eq!(" asd as df asd ".justify_line(16), "asd as df asd");
62 assert_eq!("asdasdfasd".justify_line(16), "asdasdfasd");
63 assert_eq!("asdasdfasd".justify_line(6), "asdasdfasd");
64 assert_eq!("é é".justify_line(5), "é é");
65 assert_eq!("a s—d féZ".justify_line(12), "a s—d féZ");
66
67 assert_eq!("".justify_line(14), "");
68 }
69
70 #[test]
it_substitutes_character_at_each_indexed_point()71 fn it_substitutes_character_at_each_indexed_point() {
72 use array_tool::string::SubstMarks;
73
74 assert_eq!("asdf asdf asdf".subst_marks(vec![0,5,8], "Z"), "Zsdf ZsdZ asdf");
75 assert_eq!("asdf asdf asdf".subst_marks(vec![8,5,0], "Z"), "Zsdf ZsdZ asdf");
76 assert_eq!("asdf asdf asdf".subst_marks(vec![0,5,8], "\n"), "\nsdf \nsd\n asdf");
77 assert_eq!("ééé".subst_marks(vec![1], "Z"), "éZé");
78 assert_eq!("ééé".subst_marks(vec![1,2], "Z"), "éZZ");
79 assert_eq!("ZZZ".subst_marks(vec![0,2], "é" ), "éZé");
80 assert_eq!("ééé".subst_marks(vec![0], "Z"), "Zéé");
81
82 assert_eq!("".subst_marks(vec![0], "Z"), "");
83 }
84
85 #[test]
it_seeks_end_of_whitespace_after_offset()86 fn it_seeks_end_of_whitespace_after_offset() {
87 use array_tool::string::AfterWhitespace;
88
89 assert_eq!("asdf asdf asdf".seek_end_of_whitespace(6), Some(9));
90 assert_eq!("asdf".seek_end_of_whitespace(3), Some(0));
91 assert_eq!("asdf ".seek_end_of_whitespace(6), None);
92 assert_eq!("asdf".seek_end_of_whitespace(6), None);
93
94 assert_eq!("".seek_end_of_whitespace(6), None);
95 }
96
97 #[test]
it_word_wraps_for_string()98 fn it_word_wraps_for_string() {
99 use array_tool::string::WordWrap;
100
101 assert_eq!(
102 "01234 67 9 BC EFG IJ".word_wrap(6),
103 "01234\n67 9\nBC EFG\nIJ"
104 );
105
106 assert_eq!(
107 "0123456789ABC EFG IJ".word_wrap(6),
108 "0123456789ABC\nEFG IJ"
109 );
110 assert_eq!(
111 "1234\n 1234 6789 1234".word_wrap(10),
112 "1234\n 1234 6789\n1234"
113 );
114 assert_eq!(
115 "1234\n 1234 67 90 1234".word_wrap(10),
116 "1234\n 1234 67\n90 1234"
117 );
118 assert_eq!(
119 "1234\n 1234 67 90A 1234".word_wrap(10),
120 "1234\n 1234 67\n90A 1234"
121 );
122 assert_eq!("1 \n34 ".word_wrap(3), "1 \n34 ");
123 assert_eq!("1 34".word_wrap(3), "1 \n34");
124 assert_eq!("\n \n \n \n".word_wrap(1), "\n \n \n \n" );
125
126 // White space to new line shouldn't add new lines. Use seek ahead.
127 assert_eq!("\nAA\nA \nA \n".word_wrap(1), "\nAA\nA \nA \n" );
128 assert_eq!("\n \n \n \n ".word_wrap(1), "\n \n \n \n " );
129 }
130
131