1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
11 # Various utilities to help testing SAXophoNit (and SAX parsers in general).
12 module saxophonit
::testing
14 import sax
::xml_reader
15 import sax
::input_source
16 import sax
::helpers
::xml_filter_impl
17 import sax
::ext
::decl_handler
18 import sax
::ext
::lexical_handler
22 # A filter that internally log events it recieves.
24 # Usually, when testing, 2 `SAXEventLogger` are used: one on which methods are
25 # manually called to simulate expected results, and another on which we attach
26 # the tested `XMLReader`. Then, we can compare logs using `diff`.
28 # Note: In order to test the `XMLReader` behaviour with ill-formed documents,
29 # fatal errors are not thrown by default.
39 # Each entry begins with the name of the event. Entries are sorted in the
40 # order they fired (the oldest first). Two event loggers have equivalent
41 # logs if and only if they received the same events in the same order and
42 # with equivalent arguments.
43 private var log
= new Array[Array[String]]
45 # http://xml.org/sax/properties/declaration-handler
46 private var decl_handler
: nullable DeclHandler = null
47 private var decl_handler_uri
= "http://xml.org/sax/properties/declaration-handler"
49 # http://xml.org/sax/properties/lexical-handler
50 private var lexical_handler
: nullable LexicalHandler = null
51 private var lexical_handler_uri
= "http://xml.org/sax/properties/declaration-handler"
54 # Constants for diff formatting.
56 # Treminal’s default formatting.
57 var term_default
: String = (new TermCharFormat).to_s
59 # Formatting for insertions.
60 var term_insertion
: String =
61 (new TermCharFormat).green_fg
.normal_weight
.to_s
63 # Formatting for emphased insertions.
64 var term_insertion_emphasis
: String =
65 (new TermCharFormat).green_fg
.bold
.to_s
67 # Formatting for deletions.
68 var term_deletion
: String =
69 (new TermCharFormat).red_fg
.normal_weight
.to_s
71 # Formatting for emphased deletions
72 var term_deletion_emphasis
: String =
73 (new TermCharFormat).red_fg
.bold
.to_s
76 # Clear the internal log.
81 # Show the differences between the internal logs of `self` and `expected`.
83 # If there is no differences, return an empty string. Else, return a string
84 # designed to be printed in the terminal. In this case, `=` means “in both”,
85 # `<` means “in `self`” and `>` means “in `expected`”.
86 fun diff
(expected
: SAXEventLogger): Text do
87 var buf
= new FlatBuffer
88 var sub_diff
: Array[Int]
94 if log
.length
< expected
.log
.length
then
97 max
= expected
.log
.length
98 else if expected
.log
.length
< log
.length
then
100 min
= expected
.log
.length
109 sub_diff
= diff_entry
(log
[i
], expected
.log
[i
])
110 if sub_diff
.length
> 0 then
112 diff_append_matches
(buf
, log
, [0..i
[)
115 diff_append_deletion
(buf
, log
, i
, sub_diff
)
116 diff_append_insertion
(buf
, expected
.log
, i
, sub_diff
)
117 else if not equal
then
118 diff_append_matches
(buf
, log
, [i
..i
])
122 if log
.length
< expected
.log
.length
then
124 diff_append_insertion
(buf
, expected
.log
, i
,
125 [0..(expected
.log
[i
].length
)[)
130 diff_append_deletion
(buf
, log
, i
, [0..(log
[i
].length
)[)
137 # Return the list of positions where `actual` and `expected` mismatch.
139 # Indexes are in ascending order.
140 private fun diff_entry
(actual
: Array[String], expected
: Array[String]):
142 var result
= new Array[Int]
147 if actual
.length
< expected
.length
then
149 max
= expected
.length
150 else if expected
.length
< actual
.length
then
151 min
= expected
.length
159 if expected
[i
] != actual
[i
] then
164 result
.insert_all
([i
..max
[, result
.length
)
168 # Append matches to the diff.
172 # * `buf`: buffer for the diff.
173 # * `log`: original log.
174 # * `range`: range to append to the diff.
175 private fun diff_append_matches
(buf
: Buffer, log
: Array[Array[String]],
176 range
: Range[Int]) do
178 buf
.append
("= {i}|{log[i].join("; ")}\n")
182 # Append a deletion to the diff.
186 # * `buf`: buffer for the diff.
187 # * `log`: log that contains the deleted entry.
188 # * `entry_index`: index of the deleted entry in `log`.
189 # * `sorted_mismatches`: sorted list of indexes of the items to emphasize
190 # in the specified entry.
191 private fun diff_append_deletion
(buf
: Buffer, log
: Array[Array[String]],
192 entry_index
: Int, sorted_mismatches
: Collection[Int]) do
193 buf
.append
(term_deletion
)
194 buf
.append
("< {entry_index}|")
195 diff_append_mismatch_entry
(buf
, log
[entry_index
], sorted_mismatches
,
196 term_deletion
, term_deletion_emphasis
)
197 buf
.append
(term_default
)
201 # Append a insertion to the diff.
205 # * `buf`: buffer for the diff.
206 # * `log`: log that contains the inserted entry.
207 # * `entry_index`: index of the inserted entry in `log`.
208 # * `sorted_mismatches`: sorted list of indexes of the items to emphasize
209 # in the specified entry.
210 private fun diff_append_insertion
(buf
: Buffer, log
: Array[Array[String]],
211 entry_index
: Int, sorted_mismatches
: Collection[Int]) do
212 buf
.append
(term_insertion
)
213 buf
.append
("> {entry_index}|")
214 diff_append_mismatch_entry
(buf
, log
[entry_index
], sorted_mismatches
,
215 term_insertion
, term_insertion_emphasis
)
216 buf
.append
(term_default
)
220 # Show an entry of a mismatch (without the margin).
222 # Append the string designed to be printed in the terminal to the
227 # * `buf`: output buffer.
228 # * `entry`: entry to format.
229 # * `sorted_mismatches`: sorted list of indexes of the items to emphasize.
230 # * `term_normal`: terminal control code to re-apply the formatting that was
231 # in force prior calling this method.
232 # * `term_emphasis`: terminal control code to apply to items listed in
233 # `sorted_mismatches`.
234 private fun diff_append_mismatch_entry
(buf
: Buffer, entry
: Array[String],
235 sorted_mismatches
: Collection[Int], term_normal
: String,
236 term_emphasis
: String) do
238 var j
= sorted_mismatches
.iterator
239 var length
= entry
.length
242 while j
.is_ok
and j
.item
< i
do
245 if j
.is_ok
and j
.item
== i
then
246 buf
.append
(term_emphasis
)
248 buf
.append
(term_normal
)
259 ############################################################################
262 redef fun property
(name
: String): nullable Object do
263 assert sax_recognized
: parent
!= null else
264 sys
.stderr
.write
("Property: {name}\n")
266 if decl_handler_uri
== name
then
267 assert property_readable
: property_readable
(name
) else
268 sys
.stderr
.write
("Property: {name}\n")
271 else if lexical_handler_uri
== name
then
272 assert property_readable
: property_readable
(name
) else
273 sys
.stderr
.write
("Property: {name}\n")
275 return lexical_handler
277 return parent
.property
(name
)
281 redef fun property
=(name
: String, value
: nullable Object) do
282 assert sax_recognized
: parent
!= null else
283 sys
.stderr
.write
("Property: {name}\n")
285 if decl_handler_uri
== name
then
286 assert property_readable
: property_writable
(name
) else
287 sys
.stderr
.write
("Property: {name}\n")
289 decl_handler
= value
.as(nullable DeclHandler)
290 else if lexical_handler_uri
== name
then
291 assert property_readable
: property_writable
(name
) else
292 sys
.stderr
.write
("Property: {name}\n")
294 lexical_handler
= value
.as(nullable LexicalHandler)
296 parent
.property
(name
) = value
300 redef fun parse
(input
: InputSource) do
301 assert parent_is_not_null
: parent
!= 0 else
302 sys
.stderr
.write
("No parent for filter.")
304 if parent
.feature_writable
(decl_handler_uri
) then
305 parent
.property
(decl_handler_uri
) = self
307 if parent
.feature_writable
(lexical_handler_uri
) then
308 parent
.property
(lexical_handler_uri
) = self
314 ############################################################################
317 redef fun resolve_entity
(public_id
: nullable String,
318 system_id
: nullable String):
319 nullable InputSource do
320 log
.push
(["resolve_entity",
321 public_id
or else "^NULL",
322 system_id
or else "^NULL"])
327 ############################################################################
330 redef fun notation_decl
(name
: String, public_id
: String,
331 system_id
: String) do
332 log
.push
(["notation_decl", name
, public_id
, system_id
])
336 redef fun unparsed_entity_decl
(name
: String, public_id
: String,
337 system_id
: String) do
338 log
.push
(["unparsed_entity_decl", name
, public_id
, system_id
])
343 ############################################################################
346 redef fun document_locator
=(locator
: SAXLocator) do
347 log
.push
(["document_locator=",
348 locator
.public_id
or else "^NULL",
349 locator
.system_id
or else "^NULL",
350 locator
.line_number
.to_s
,
351 locator
.column_number
.to_s
])
355 redef fun start_document
do
356 log
.push
(["start_document"])
360 redef fun end_document
do
361 log
.push
(["end_document"])
365 redef fun start_prefix_mapping
(prefix
: String, uri
: String) do
366 log
.push
(["start_prefix_mapping", prefix
, uri
])
370 redef fun end_prefix_mapping
(prefix
: String) do
371 log
.push
(["end_prefix_mapping", prefix
])
375 redef fun start_element
(uri
: String, local_name
: String, qname
: String,
377 var entry
= new Array[String]
379 var length
= atts
.length
381 entry
.push
("start_element")
383 entry
.push
(local_name
)
386 entry
.push
(atts
.uri
(i
) or else "^NULL")
387 entry
.push
(atts
.local_name
(i
) or else "^NULL")
388 entry
.push
(atts
.qname
(i
) or else "^NULL")
389 entry
.push
(atts
.type_of
(i
) or else "^NULL")
390 entry
.push
(atts
.value_of
(i
) or else "^NULL")
397 redef fun end_element
(uri
: String, local_name
: String, qname
: String) do
398 log
.push
(["end_element", uri
, local_name
, qname
])
402 redef fun characters
(str
: String) do
403 log
.push
(["characters", str
])
407 redef fun ignorable_whitespace
(str
: String) do
408 log
.push
(["ignorable_witespace", str
])
412 redef fun processing_instruction
(target
: String, data
: nullable String) do
413 log
.push
(["processing_instruction", target
, data
or else "^NULL"])
417 redef fun skipped_entity
(name
: String) do
418 log
.push
(["skipped_entity", name
])
423 ############################################################################
426 redef fun warning
(exception
: SAXParseException) do
427 log
.push
(["warning", exception
.full_message
])
431 redef fun error
(exception
: SAXParseException) do
432 log
.push
(["error", exception
.full_message
])
436 redef fun fatal_error
(exception
: SAXParseException) do
437 log
.push
(["fatal_error", exception
.full_message
])
438 if error_handler
!= null then
439 error_handler
.fatal_error
(exception
)
444 ############################################################################
447 redef fun element_decl
(name
: String, model
: String) do
448 log
.push
(["element_decl", name
, model
])
449 if decl_handler
!= null then
450 decl_handler
.element_decl
(name
, model
)
454 redef fun attribute_decl
(element_name
: String,
455 attribute_name
: String,
456 attribute_type
: String,
457 mode
: nullable String,
458 value
: nullable String) do
459 log
.push
(["attribute_decl",
463 mode
or else "^NULL",
464 value
or else "^NULL"])
465 if decl_handler
!= null then
466 decl_handler
.attribute_decl
(element_name
, attribute_name
,
467 attribute_type
, mode
, value
)
471 redef fun internal_entity_decl
(name
: String, value
: String) do
472 log
.push
(["internal_entity_decl", name
, value
])
473 if decl_handler
!= null then
474 decl_handler
.internal_entity_decl
(name
, value
)
478 redef fun external_entity_decl
(name
: String, value
: String) do
479 log
.push
(["external_entity_decl", name
, value
])
480 if decl_handler
!= null then
481 decl_handler
.external_entity_decl
(name
, value
)
486 ############################################################################
489 redef fun start_dtd
(name
: String, public_id
: nullable String,
490 system_id
: nullable String) do
491 log
.push
(["start_dtd", name
,
492 public_id
or else "^NULL",
493 system_id
or else "^NULL"])
494 if lexical_handler
!= null then
495 lexical_handler
.start_dtd
(name
, public_id
, system_id
)
500 log
.push
(["end_dtd"])
501 if lexical_handler
!= null then
502 lexical_handler
.end_dtd
506 redef fun start_entity
(name
: String) do
507 log
.push
(["start_entity", name
])
508 if lexical_handler
!= null then
509 lexical_handler
.start_entity
(name
)
513 redef fun end_entity
(name
: String) do
514 log
.push
(["end_entity", name
])
515 if lexical_handler
!= null then
516 lexical_handler
.end_entity
(name
)
520 redef fun start_cdata
do
521 log
.push
(["start_cdata"])
522 if lexical_handler
!= null then
523 lexical_handler
.start_cdata
527 redef fun end_cdata
do
528 log
.push
(["end_cdata"])
529 if lexical_handler
!= null then
530 lexical_handler
.end_cdata
534 redef fun comment
(str
: String) do
535 log
.push
(["comment", str
])
536 if lexical_handler
!= null then
537 lexical_handler
.comment
(str
)
543 # Base class for test suites on a SAX reader.
544 abstract class SAXTestSuite
547 # Logger of the expected event sequence.
548 var expected
= new SAXEventLogger
550 # Logger of the actual event sequence.
551 var actual
= new SAXEventLogger
553 # The tested SAX reader.
554 var reader
: XMLReader is noinit
556 private var init_done
: Bool = false
558 redef fun before_test
do
560 if not init_done
then
561 reader
= create_reader
562 actual
.parent
= reader
565 reader
.feature
("http://xml.org/sax/features/namespaces") = true
566 reader
.feature
("http://xml.org/sax/features/namespace-prefixes") = false
571 # Create a new SAX reader.
573 # This method is called at initialization to set `reader`.
574 fun create_reader
: XMLReader is abstract
576 # Assert logs are equal.
578 var diff
= actual
.diff
(expected
)
580 assert equals
: diff
.length
<= 0 else
581 sys
.stderr
.write
("\n")
582 sys
.stderr
.write
("# {actual.term_deletion}< Actual{actual.term_default}\n")
583 sys
.stderr
.write
("# {actual.term_insertion}> Expected{actual.term_default}\n")
584 sys
.stderr
.write
(diff
)
585 sys
.stderr
.write
("\n")
589 # Make the reader parse the specified string
590 fun parse_string
(str
: String) do
591 actual
.parse
(new InputSource.with_stream
(new StringReader(str
)))