readme: add information section
[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 #alt2 import core
16
17 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."
18 var spaces = " "
19 var numstr = "1001"
20
21 var txt: Text
22 var trimable: Text
23 var num: Text
24
25 txt = str
26 trimable = spaces + str + spaces
27 num = numstr
28
29 #alt1 txt = new FlatBuffer.from(str)
30 #alt1 trimable = new FlatBuffer.from(spaces)
31 #alt1 trimable.append(str)
32 #alt1 trimable.append(spaces)
33 #alt1 num = new FlatBuffer.from(numstr)
34
35 #alt3 txt = new RopeBuffer.from(str)
36 #alt3 trimable = new RopeBuffer.from(spaces)
37 #alt3 trimable.append(str)
38 #alt3 trimable.append(spaces)
39 #alt3 num = new RopeBuffer.from(numstr)
40
41 # Test Text methods on all types of receivers
42
43 print txt.substring(0, 105)
44 print txt.substring_from(106)
45 print txt.length
46 assert not txt.is_empty
47 print txt.index_of('h')
48 print txt.index_of_from('h', 105)
49 print txt.index_of('Z')
50 print txt.last_index_of('L')
51 print txt.last_index_of_from('D', 105)
52 print txt.has_substring("Woe", 0)
53 print txt.has_substring("Let", 106)
54 assert trimable != txt
55 assert trimable.trim == txt
56 assert num.is_numeric
57 assert txt.has_prefix("Woe")
58 assert txt.has_suffix("Six.")
59 assert txt.hash == trimable.trim.hash
60
61 # Test Text.chars (SequenceRead[Char]) methods on all receivers
62
63 var chars = txt.chars
64
65 assert chars != txt.substring_from(106).chars
66 assert chars[0] == 'W'
67 assert chars.count('o') == 11
68 assert chars.first == chars[0]
69 assert chars.has('L')
70 assert spaces.chars.has_only(' ')
71 assert chars.index_of('D') == 37
72 assert not chars.is_empty
73 var count = 0
74 for i in chars do
75 assert i != '!'
76 printn i
77 count += 1
78 end
79 printn "\n"
80 assert count == txt.length
81 var iter_from = txt.substring_from(105).chars.iterator
82 var txt_iter_from = chars.iterator_from(105)
83 while iter_from.is_ok do
84 assert iter_from.item == txt_iter_from.item
85 iter_from.next
86 txt_iter_from.next
87 end
88 assert not iter_from.is_ok and not txt_iter_from.is_ok
89 var txt_reviter = chars.reverse_iterator_from(104)
90 var sub_reviter = txt.substring(0,105).chars.reverse_iterator
91 while txt_reviter.is_ok do
92 assert txt_reviter.item == sub_reviter.item
93 txt_reviter.next
94 sub_reviter.next
95 end
96 print chars.join(" ")