tests: add tests for tests.sh so we can test the test script
[nit.git] / tests / test_text.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 var str = "Woe to you, oh earth and sea for the Devil sends the beast with wrath because he knows the time is short. Let him who hath understanding reckon the number of the beast, for it is a human number, its number is Six Hundred and Sixty-Six."
16 var spaces = " "
17 var numstr = "1001"
18
19 var txt: Text
20 var trimable: Text
21 var num: Text
22
23 txt = str
24 trimable = spaces + str + spaces
25 num = numstr
26
27 #alt1 txt = new FlatBuffer.from(str)
28 #alt1 trimable = new FlatBuffer.from(spaces)
29 #alt1 trimable.append(str)
30 #alt1 trimable.append(spaces)
31 #alt1 num = new FlatBuffer.from(numstr)
32
33 #alt2 txt = new RopeString.from(str)
34 #alt2 trimable = new RopeString.from(spaces)
35 #alt2 trimable = trimable + str
36 #alt2 trimable = trimable + spaces
37 #alt2 num = new RopeString.from(numstr)
38
39 # Test Text methods on all types of receivers
40
41 print txt.to_upper
42 print txt.to_lower
43 assert txt * 2 == txt + txt
44 print txt.substring(0, 105)
45 print txt.substring_from(106)
46 print txt.reversed
47 print txt.length
48 assert not txt.is_empty
49 print txt.index_of('h')
50 print txt.index_of_from('h', 105)
51 print txt.index_of('Z')
52 print txt.last_index_of('L')
53 print txt.last_index_of_from('D', 105)
54 print txt.has_substring("Woe", 0)
55 print txt.has_substring("Let", 106)
56 assert trimable != txt
57 assert trimable.trim == txt
58 assert num.is_numeric
59 assert txt.has_prefix("Woe")
60 assert txt.has_suffix("Six.")
61 assert txt.hash == trimable.trim.hash
62
63 # Test Text.chars (SequenceRead[Char]) methods on all receivers
64
65 var chars = txt.chars
66
67 assert chars != txt.substring_from(106).chars
68 assert (txt.substring(0,105) + txt.substring_from(105)).chars == txt.chars
69 assert chars[0] == 'W'
70 assert chars.count('o') == 11
71 assert chars.first == chars[0]
72 assert chars.has('L')
73 assert spaces.chars.has_only(' ')
74 assert chars.index_of('D') == 37
75 assert not chars.is_empty
76 var count = 0
77 for i in chars do
78 assert i != '!'
79 printn i
80 count += 1
81 end
82 printn "\n"
83 assert count == txt.length
84 var iter_from = txt.substring_from(105).chars.iterator
85 var txt_iter_from = chars.iterator_from(105)
86 while iter_from.is_ok do
87 assert iter_from.item == txt_iter_from.item
88 iter_from.next
89 txt_iter_from.next
90 end
91 assert not iter_from.is_ok and not txt_iter_from.is_ok
92 var txt_reviter = chars.reverse_iterator_from(104)
93 var sub_reviter = txt.substring(0,105).chars.reverse_iterator
94 while txt_reviter.is_ok do
95 assert txt_reviter.item == sub_reviter.item
96 txt_reviter.next
97 sub_reviter.next
98 end
99 print chars.join(" ")