Stdlib: Added unit tests for Ropes
[nit.git] / tests / test_ropes.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it if you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or as a part of
9 # another product.
10
11 module test_ropes
12
13 var str_part_1 = "Je"
14
15 var str_part_2 = "suis"
16
17 var str_part_3 = "ici"
18
19 var str_part_4 = "."
20
21 var space = " "
22
23 var test = "Je suis ici."
24
25 var rope_uniq_test = new ImmutableRope.with_string("zzzzzzzzzzzzzzzzzzzzzzzzzzzz")
26
27 var not_rope_uniq_test = new ImmutableRope.with_string("zzzzzzzzzzzzzezzzzzzzzzzzzzz")
28
29 ################################
30 # Rope methods tests #
31 ################################
32
33 var buf = new Buffer
34
35 buf.append(str_part_1)
36 buf.append(space)
37 buf.append(str_part_2)
38 buf.append(space)
39 buf.append(str_part_3)
40 buf.append(str_part_4)
41
42 print buf
43
44 var buf_rope = new BufferRope
45
46 buf_rope.append_multi(str_part_1,space,str_part_2,space,str_part_3,str_part_4)
47
48 var buf_rope_with_str = new BufferRope.with_string(test)
49
50 print buf_rope*3
51
52 print buf_rope_with_str*5
53
54 print buf_rope + buf_rope_with_str
55
56 assert buf_rope.length == buf_rope_with_str.length
57
58 assert buf_rope == buf_rope_with_str
59
60 assert buf_rope.multi_concat(buf_rope, buf_rope) == buf_rope * 3
61
62 print buf_rope.subrope(0, 5)
63
64 assert buf_rope.subrope(0, 5) == "Je su"
65
66 buf_rope.append("Hi !")
67
68 assert buf_rope > buf_rope_with_str
69
70 assert buf_rope == buf_rope.chars.to_s
71
72 assert buf_rope.chars.to_s == buf_rope
73
74 assert buf == buf_rope_with_str
75
76 ######################################
77 # BufferRope methods tests #
78 ######################################
79
80 assert buf_rope + buf_rope == buf_rope.concat(buf_rope)
81
82 assert buf_rope == buf_rope.freeze
83
84 ######################################
85 # Rope.chars methods tests #
86 ######################################
87
88 assert buf_rope.chars[3] == 's'
89
90 assert buf_rope.chars.index_of('k') == -1
91
92 assert buf_rope.chars.index_of('s') == 3
93
94 assert buf_rope.chars.count('s') == 4
95
96 assert buf_rope.chars.last == '!'
97
98 assert rope_uniq_test.chars.has_only('z')
99
100 assert not not_rope_uniq_test.chars.has_only('z')
101
102 assert (new BufferRope).chars.has_only('l')
103
104 print buf_rope.to_lower
105
106 print buf_rope.to_upper
107
108