Merge: Added contributing guidelines and link from readme
[nit.git] / lib / core / text / test_abstract_text.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 is 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 is a part of
9 # another product.
10
11 module test_abstract_text is test_suite
12
13 import test_suite
14 import text
15 intrude import ropes
16
17 class TestText
18 super TestSuite
19
20 private var factories: Collection[TextFactory] = [
21 new ConcatFactory,
22 new RopeBufferFactory,
23 new FlatBufferFactory
24 : TextFactory]
25
26 fun test_escape_to_c do
27 for f in factories do
28 assert f.create("abAB12<>&").escape_to_c == "abAB12<>&"
29 assert f.create("\n\"'\\").escape_to_c == "\\n\\\"\\'\\\\"
30 assert f.create("allo???!").escape_to_c == "allo??\\?!"
31 assert f.create("??=??/??'??(??)").escape_to_c == "?\\?=?\\?/??\\'?\\?(?\\?)"
32 assert f.create("??!??<??>??-").escape_to_c == "?\\?!?\\?<?\\?>?\\?-"
33 end
34 end
35 end
36
37 # A factory that creates instances of a particular implementation of `Text`
38 interface TextFactory
39
40 # Create a `Text` instance from the specified string
41 fun create(s: String): Text is abstract
42 end
43
44
45 class ConcatFactory
46 super TextFactory
47
48 redef fun create(s) do return new Concat("", s)
49 end
50
51 class RopeBufferFactory
52 super TextFactory
53
54 redef fun create(s) do return new RopeBuffer.from(s)
55 end
56
57 class FlatBufferFactory
58 super TextFactory
59
60 redef fun create(s) do return new FlatBuffer.from(s)
61 end