lib/core/stream: LineIterator use CachedIterator
[nit.git] / lib / saxophonit / test_testing.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 # Test suite for `testing`.
12 module test_testing is test
13
14 import saxophonit::testing
15
16 class TestSaxEventLogger
17 test
18
19 # Constants for diff formatting.
20
21 # Treminal’s default formatting.
22 private var default: String is noinit
23
24 # Formatting for insertions.
25 private var ins: String is noinit
26
27 # Formatting for emphased insertions.
28 private var ins_em: String is noinit
29
30 # Formatting for deletions.
31 private var del: String is noinit
32
33 # Formatting for emphased deletions
34 private var del_em: String is noinit
35
36
37 private var a = new SAXEventLogger
38 private var b = new SAXEventLogger
39
40 private var init_done: Bool = false
41
42 fun before_test is before do
43 if not init_done then
44 default = a.term_default
45 ins = a.term_insertion
46 ins_em = a.term_insertion_emphasis
47 del = a.term_deletion
48 del_em = a.term_deletion_emphasis
49 init_done = true
50 end
51 a.clear_log
52 b.clear_log
53 end
54
55 private fun assert_equals(id: Int, expected: String, actual: String) do
56 assert equals: expected == actual else
57 sys.stderr.write("\nAssert {id} failed. Expected:\n")
58 sys.stderr.write(expected)
59 sys.stderr.write("\nGot:\n")
60 sys.stderr.write(actual)
61 sys.stderr.write("\n")
62 end
63 end
64
65 fun test_diff_empty is test do
66 assert "" == a.diff(b).to_s
67 assert "" == b.diff(a).to_s
68 end
69
70 fun test_diff_equal1 is test do
71 b.start_document
72 a.start_document
73 assert "" == a.diff(b).to_s
74 assert "" == b.diff(a).to_s
75 end
76
77 fun test_diff_equal2 is test do
78 b.start_document
79 b.end_document
80 a.start_document
81 a.end_document
82 assert "" == a.diff(b).to_s
83 assert "" == b.diff(a).to_s
84 end
85
86 fun test_diff_insertion is test do
87 var exp: String
88 var test: String
89
90 b.start_document
91 b.start_dtd("html", "", "")
92 b.end_dtd
93 b.end_document
94
95 a.start_document
96 a.start_dtd("html", "", "")
97 a.end_document
98
99 exp = "= 0|start_document\n" +
100 "= 1|start_dtd; html; ; \n" +
101 "{del}< 2|{del_em}end_document{del}{default}\n" +
102 "{ins}> 2|{ins_em}end_dtd{ins}{default}\n" +
103 "{ins}> 3|{ins_em}end_document{ins}{default}\n"
104 test = a.diff(b).to_s
105 assert_equals(1, exp, test)
106
107 exp = "= 0|start_document\n" +
108 "= 1|start_dtd; html; ; \n" +
109 "{del}< 2|{del_em}end_dtd{del}{default}\n" +
110 "{ins}> 2|{ins_em}end_document{ins}{default}\n" +
111 "{del}< 3|{del_em}end_document{del}{default}\n"
112 test = b.diff(a).to_s
113 assert_equals(2, exp, test)
114 end
115
116 fun test_diff_edition is test do
117 var exp: String
118 var test: String
119
120 b.start_document
121 b.start_dtd("html", "", "")
122 b.end_dtd
123 b.end_document
124
125 a.start_document
126 a.start_dtd("html", "foo", "")
127 a.end_dtd
128 a.end_document
129
130 exp = "= 0|start_document\n" +
131 "{del}< 1|start_dtd; html; {del_em}foo{del}; {default}\n" +
132 "{ins}> 1|start_dtd; html; {ins_em}{ins}; {default}\n" +
133 "= 2|end_dtd\n" +
134 "= 3|end_document\n"
135 test = a.diff(b).to_s
136 assert_equals(1, exp, test)
137
138 exp = "= 0|start_document\n" +
139 "{del}< 1|start_dtd; html; {del_em}{del}; {default}\n" +
140 "{ins}> 1|start_dtd; html; {ins_em}foo{ins}; {default}\n" +
141 "= 2|end_dtd\n" +
142 "= 3|end_document\n"
143 test = b.diff(a).to_s
144 assert_equals(2, exp, test)
145 end
146 end