note
	description: "HTML report generator."
	author: "Software Engineering Lab, York University"

deferred class 
	ES_HTML_GEN

feature -- Initialization

	make_gen (name: STRING_8; file_name: STRING_8; show_err: BOOLEAN)
			-- Create HTML file.
		do
			output_file_name := file_name
			test_name := name
			show_error := show_err
		end
	
feature {NONE} -- Initialization

	default_create
			-- Process instances of classes with no creation clause.
			-- (Default: do nothing.)
			-- (from ANY)
		do
		end
	
feature -- Access

	generating_type: TYPE [detachable ES_HTML_GEN]
			-- Type of current object
			-- (type of which it is a direct instance)
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			generating_type_not_void: Result /= Void
		end

	generator: STRING_8
			-- Name of current object's generating class
			-- (base class of the type of which it is a direct instance)
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			generator_not_void: Result /= Void
			generator_not_empty: not Result.is_empty
		end
	
feature -- Comparison

	frozen deep_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void
			-- or attached to isomorphic object structures?
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_deep_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			shallow_implies_deep: standard_equal (a, b) implies Result
			both_or_none_void: (a = Void) implies (Result = (b = Void))
			same_type: (Result and (a /= Void)) implies (b /= Void and then a.same_type (b))
			symmetric: Result implies deep_equal (b, a)
		end

	frozen equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached
			-- to objects considered equal?
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.is_equal (b))
		end

	frozen is_deep_equal (other: ES_HTML_GEN): BOOLEAN
			-- Are Current and other attached to isomorphic object structures?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			shallow_implies_deep: standard_is_equal (other) implies Result
			same_type: Result implies same_type (other)
			symmetric: Result implies other.is_deep_equal (Current)
		end

	is_equal (other: ES_HTML_GEN): BOOLEAN
			-- Is other attached to an object considered
			-- equal to current object?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			symmetric: Result implies other ~ Current
			consistent: standard_is_equal (other) implies Result
		end

	frozen standard_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached to
			-- field-by-field identical objects of the same type?
			-- Always uses default object comparison criterion.
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.standard_is_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.standard_is_equal (b))
		end

	frozen standard_is_equal (other: ES_HTML_GEN): BOOLEAN
			-- Is other attached to an object of the same type
			-- as current object, and field-by-field identical to it?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			same_type: Result implies same_type (other)
			symmetric: Result implies other.standard_is_equal (Current)
		end
	
feature -- Status report

	conforms_to (other: ANY): BOOLEAN
			-- Does type of current object conform to type
			-- of other (as per Eiffel: The Language, chapter 13)?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		end

	same_type (other: ANY): BOOLEAN
			-- Is type of current object identical to type of other?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			definition: Result = (conforms_to (other) and other.conforms_to (Current))
		end
	
feature -- Duplication

	frozen clone (other: detachable ANY): like other
		obsolete "Use `twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- equal to other
			--
			-- For non-void other, clone calls copy;
			-- to change copying/cloning semantics, redefine copy.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.twin
			end
		ensure -- from ANY
			instance_free: class
			equal: Result ~ other
		end

	copy (other: ES_HTML_GEN)
			-- Update current object using fields of object attached
			-- to other, so as to yield equal objects.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		external
			"built_in"
		ensure -- from ANY
			is_equal: Current ~ other
		end

	frozen deep_clone (other: detachable ANY): like other
		obsolete "Use `deep_twin' instead. [2017-05-31]"
			-- Void if other is void: otherwise, new object structure
			-- recursively duplicated from the one attached to other
			-- (from ANY)
		do
			if other /= Void then
				Result := other.deep_twin
			end
		ensure -- from ANY
			instance_free: class
			deep_equal: deep_equal (other, Result)
		end

	frozen deep_copy (other: ES_HTML_GEN)
			-- Effect equivalent to that of:
			--		copy (other . deep_twin)
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		do
			copy (other.deep_twin)
		ensure -- from ANY
			deep_equal: deep_equal (Current, other)
		end

	frozen deep_twin: ES_HTML_GEN
			-- New object structure recursively duplicated from Current.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			deep_twin_not_void: Result /= Void
			deep_equal: deep_equal (Current, Result)
		end

	frozen standard_clone (other: detachable ANY): like other
		obsolete "Use `standard_twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.standard_twin
			end
		ensure -- from ANY
			instance_free: class
			equal: standard_equal (Result, other)
		end

	frozen standard_copy (other: ES_HTML_GEN)
			-- Copy every field of other onto corresponding field
			-- of current object.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		external
			"built_in"
		ensure -- from ANY
			is_standard_equal: standard_is_equal (other)
		end

	frozen standard_twin: ES_HTML_GEN
			-- New object field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			standard_twin_not_void: Result /= Void
			equal: standard_equal (Result, Current)
		end

	frozen twin: ES_HTML_GEN
			-- New object equal to Current
			-- twin calls copy; to change copying/twinning semantics, redefine copy.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			twin_not_void: Result /= Void
			is_equal: Result ~ Current
		end
	
feature -- Basic operations

	frozen as_attached: attached ES_HTML_GEN
		obsolete "Remove calls to this feature. [2017-05-31]"
			-- Attached version of Current.
			-- (Can be used during transitional period to convert
			-- non-void-safe classes to void-safe ones.)
			-- (from ANY)
		do
			Result := Current
		end

	frozen default: detachable ES_HTML_GEN
			-- Default value of object's type
			-- (from ANY)
		do
		end

	frozen default_pointer: POINTER
			-- Default value of type POINTER
			-- (Avoid the need to write p.default for
			-- some p of type POINTER.)
			-- (from ANY)
		do
		ensure -- from ANY
			instance_free: class
		end

	default_rescue
			-- Process exception for routines with no Rescue clause.
			-- (Default: do nothing.)
			-- (from ANY)
		do
		end

	frozen do_nothing
			-- Execute a null action.
			-- (from ANY)
		do
		ensure -- from ANY
			instance_free: class
		end
	
feature {NONE} -- Implementation

	get_statistics
		deferred
		end

	output_file: attached PLAIN_TEXT_FILE
		do
			if attached output_file1 as output_file2 then
				Result := output_file2
			else
				create Result.make_with_name ("default_output")
			end
		end

	show_error: BOOLEAN

	wrap_html_comments (comment: STRING_8): attached STRING_8
			-- Wraps the comments in the HTML code.
		local
			ls: LIST [STRING_8]
			is_first: BOOLEAN
		do
			if comment.count > Wrap_length then
				ls := comment.split ('%N'.to_character_32)
				create Result.make (comment.count)
				is_first := True
				across
					ls as line
				loop
					if not is_first then
						Result.append ("<br>")
					end
					Result.append (wrap_line (line.item))
					is_first := False
				end
			else
				Result := comment
			end
		end

	Wrap_length: INTEGER_32 = 100

	wrap_line (line: STRING_8): STRING_8
		local
			ln, ws: INTEGER_32
			ls: LIST [STRING_8]
		do
			ls := line.split (' '.to_character_32)
			create Result.make (line.count + ls.count)
			from
				ln := 0
				ws := 0
				ls.start
			variant
					2 * (ls.count - ls.index + 1) + ws
			until
				ls.after
			loop
				if ln + ls.item.count + 1 <= Wrap_length or ws = 0 then
					Result.append (ls.item)
					Result.extend (' ')
					ln := ln + ls.item.count + 1
					ws := ws + 1
					ls.forth
				else
					Result.append ("<br>")
					ln := 0
					ws := 0
				end
			end
		end

	write_failed_case (one_case: ES_TEST_CASE)
			-- Output test result for one failed case.
		require
			one_case_valid: one_case /= Void
		local
			tag: STRING_8
		do
			output_file.putstring ("<td><font color=%"red%"><b>FAILED</b></font></td>%N")
			output_file.putstring ("<td align=%"center%">")
			if one_case.contract_violated then
				if show_error then
					check
							attached one_case.meaning (one_case.violation_type) as m
					then
						output_file.putstring ("<font color=%"blue%">" + m + "</font>")
					end
					if attached one_case.violation_tag as t then
						tag := t.twin
						tag.replace_substring_all ("%N", "<br>")
						output_file.putstring ("<p align=%"left%">" + "<font color=%"blue%" face=%"Courier%">" + "<font size=%"1%">" + "%N" + tag + "%N" + "</font>")
					end
				else
					check
							attached one_case.meaning (one_case.violation_type) as m
					then
						output_file.putstring (m)
					end
				end
			else
				output_file.putstring ("NONE")
			end
			output_file.putstring ("</td>%N")
			output_file.putstring ("<td>")
			if attached {ES_VIOLATION_CASE} one_case as l then
				output_file.putstring ("*")
			end
			output_file.putstring (wrap_html_comments (one_case.case_name))
			output_file.putstring ("</td>%N")
		end

	write_one_case_details (one_case: ES_TEST_CASE)
			-- Output test result for one case.
		require
			one_case_valid: one_case /= Void
		do
			output_file.putstring ("<tr>")
			if one_case.passed then
				write_passed_case (one_case)
			else
				write_failed_case (one_case)
			end
			output_file.putstring ("</tr>%N")
		end

	write_passed_case (one_case: ES_TEST_CASE)
			-- Output test result for one passed case.
		require
			one_case_valid: one_case /= Void
		do
			output_file.putstring ("<td><font color=%"green%"><b>PASSED</b></font></td>%N")
			output_file.putstring ("<td align=%"center%">")
			if one_case.contract_violated then
				output_file.putstring ("<font color=%"blue%">CAUGHT</font>")
			else
				output_file.putstring ("NONE")
			end
			output_file.putstring ("</td>%N")
			output_file.putstring ("<td>")
			if attached {ES_VIOLATION_CASE} one_case as l then
				output_file.putstring ("*")
			end
			output_file.putstring (wrap_html_comments (one_case.case_name))
			output_file.putstring ("</td>%N")
		end

	write_statistics (case_type: STRING_8; passed: INTEGER_32; total: INTEGER_32)
			-- Write statistic information.
		require
			case_type_valid: case_type /= Void
			passed_valid: passed >= 0
			total_valid: total >= 0
		do
			output_file.putstring ("<tr>")
			output_file.putstring ("<td align=%"center%"><font color=%"blue%"><b>")
			output_file.putstring (case_type)
			output_file.putstring ("</b></font></td>%N")
			output_file.putstring ("<td align=%"center%">")
			output_file.putint (passed)
			output_file.putstring ("</td>")
			output_file.putstring ("<td align=%"center%">")
			output_file.putint (total)
			output_file.putstring ("</td>")
			output_file.putstring ("</tr>%N")
		end

	write_statistics_big (case_type: STRING_8; passed: INTEGER_32; total: INTEGER_32)
			-- Write statistic information.
		require
			case_type_valid: case_type /= Void
			passed_valid: passed >= 0
			total_valid: total >= 0
		do
			output_file.putstring ("<tr>")
			output_file.putstring ("<td align=%"center%"><font size=%"16%" color=%"blue%"><b>")
			output_file.putstring (case_type)
			output_file.putstring ("</b></font></td>%N")
			output_file.putstring ("<td align=%"center%">")
			output_file.putint (passed)
			output_file.putstring ("</td>")
			output_file.putstring ("<td align=%"center%">")
			output_file.putint (total)
			output_file.putstring ("</td>")
			output_file.putstring ("</tr>%N")
		end

	write_summary (file_name: STRING_8)
			-- Write the summary of the test in HTML format.
		require
			file_name_valid: file_name /= Void
		do
			get_statistics
			create output_file1.make_with_name (file_name)
			output_file.open_write
			write_summary_start
			write_summary_details
			write_summary_end
			output_file.close
		end

	write_summary_big (file_name: STRING_8)
			-- Write the summary of the test in HTML format.
		require
			file_name_valid: file_name /= Void
		do
			get_statistics
			create output_file1.make_with_name (file_name)
			output_file.open_write
			write_summary_start_big
			write_summary_details
			write_summary_end_big
			output_file.close
		end

	write_summary_details
		deferred
		end

	write_summary_end
			-- Finish writing the HTML file.
		do
			output_file.putstring ("</table>")
			output_file.putstring ("</body>%N")
			output_file.putstring ("</html>%N")
		end

	write_summary_end_big
			-- Finish writing the HTML file.
		do
			output_file.putstring ("</table>")
			output_file.putstring ("</font>")
			output_file.putstring ("</body>%N")
			output_file.putstring ("</html>%N")
		end

	write_summary_start
			-- Start to write the HTML file.
		local
			date: DATE
			time: TIME
		do
			output_file.putstring ("<html>%N<head>%N<base target=%"main%">%N</head>%N")
			output_file.putstring ("<body>%N")
			create date.make_now
			create time.make_now
			output_file.put_string ("<p align=%"center%"><b>Test Run:")
			output_file.putstring (date.out)
			output_file.put_string ("   ")
			output_file.put_string (time.out)
			output_file.put_string ("</b></p>")
			output_file.putstring ("<font color=%"blue%">")
			output_file.putstring ("<H2  align=%"center%">")
			check
					attached test_name as tn
			then
				output_file.putstring (tn)
			end
			output_file.putstring ("</H2>")
			output_file.putstring ("</font>")
			output_file.putstring ("<p align=%"center%">Note: * indicates a violation test case</p>")
			output_file.putstring ("%N")
			output_file.putstring ("<table border=1 align=%"center%">")
			output_file.putstring ("%N")
			if number_of_tests > 0 then
				if number_passed_tests = number_of_tests then
					output_file.putstring ("<tr><td bgcolor=%"#00FF00%" colspan=%"3%">&nbsp;</td></tr>")
					output_file.putstring ("<tr><td align=%"center%" colspan=%"3%">")
					output_file.putstring ("PASSED (")
					output_file.putint (number_passed_tests)
					output_file.putstring (" out of ")
					output_file.putint (number_of_tests)
				else
					output_file.putstring ("<tr><td bgcolor=%"#FF0000%" colspan=%"3%">&nbsp;</td></tr>")
					output_file.putstring ("<tr><td align=%"center%" colspan=%"3%">")
					output_file.putstring ("FAILED (")
					output_file.putint (number_of_tests - number_passed_tests)
					output_file.putstring (" failed & ")
					output_file.putint (number_passed_tests)
					output_file.putstring (" passed out of ")
					output_file.putint (number_of_tests)
				end
				output_file.putstring (")</td></tr>%N")
			end
			output_file.putstring ("<tr><td align=%"center%" bgcolor=%"#000080%"><font color=%"#FFCC00%">")
			output_file.putstring ("<b>Case Type</b></font></td>")
			output_file.putstring ("%N")
			output_file.putstring ("<td align=%"center%" bgcolor=%"#000080%"><font color=%"#FFCC00%">")
			output_file.putstring ("<b>Passed</b></font></td>")
			output_file.putstring ("%N")
			output_file.putstring ("<td align=%"center%" bgcolor=%"#000080%"><font color=%"#FFCC00%">")
			output_file.putstring ("<b>Total</b></font></td></tr>")
			output_file.putstring ("%N")
			write_statistics ("Violation", number_passed_violation, number_of_violation)
			write_statistics ("Boolean", number_passed_boolean, number_of_boolean)
			write_statistics ("All Cases", number_passed_tests, number_of_tests)
			output_file.putstring ("<tr><td align=%"center%" bgcolor=%"#000080%"><font color=%"#FFCC00%">")
			output_file.putstring ("<b>State</b></font></td>")
			output_file.putstring ("%N")
			output_file.putstring ("<td align=%"center%" bgcolor=%"#000080%"><font color=%"#FFCC00%">")
			output_file.putstring ("<b>Contract Violation</b></font></td>")
			output_file.putstring ("%N")
			output_file.putstring ("<td align=%"center%" bgcolor=%"#000080%"><font color=%"#FFCC00%">")
			output_file.putstring ("<b>Test Name</b></font></td></tr>")
			output_file.putstring ("%N")
		end

	write_summary_start_big
			-- Start to write the HTML file.
		do
			output_file.putstring ("<html>%N<head>%N<base target=%"main%">%N</head>%N")
			output_file.putstring ("<body>%N")
			output_file.putstring ("<font size=%"16%">")
			output_file.putstring ("<P  align=%"center%">")
			check
					attached test_name as tn
			then
				output_file.putstring (tn)
			end
			output_file.putstring ("</p>")
			output_file.putstring ("<p align=%"center%">Note: * indicates a violation test case</p>")
			output_file.putstring ("%N")
			output_file.putstring ("<table border=1 align=%"center%">")
			output_file.putstring ("%N")
			if number_of_tests > 0 then
				if number_passed_tests = number_of_tests then
					output_file.putstring ("<tr><td bgcolor=%"#00FF00%" colspan=%"3%">&nbsp;</td></tr>")
					output_file.putstring ("<tr><td align=%"center%" colspan=%"3%">")
					output_file.putstring ("<font size=%"16%">PASSED (")
					output_file.putint (number_passed_tests)
					output_file.putstring (" out of ")
					output_file.putint (number_of_tests)
				else
					output_file.putstring ("<tr><td bgcolor=%"#FF0000%" colspan=%"3%">&nbsp;</td></tr>")
					output_file.putstring ("<tr><td align=%"center%" colspan=%"3%">")
					output_file.putstring ("<font size=%"16%">FAILED (")
					output_file.putint (number_of_tests - number_passed_tests)
					output_file.putstring (" failed & ")
					output_file.putint (number_passed_tests)
					output_file.putstring (" passed out of ")
					output_file.putint (number_of_tests)
				end
				output_file.putstring (")</td></tr>%N")
			end
			output_file.putstring ("<tr><td align=%"center%" bgcolor=%"#000080%"><font size=%"16%" color=%"#FFCC00%">")
			output_file.putstring ("<b>Case Type</b></font></td>")
			output_file.putstring ("%N")
			output_file.putstring ("<td align=%"center%" bgcolor=%"#000080%"><font size=%"16%" color=%"#FFCC00%">")
			output_file.putstring ("<b>Passed</b></font></td>")
			output_file.putstring ("%N")
			output_file.putstring ("<td align=%"center%" bgcolor=%"#000080%"><font size=%"16%" color=%"#FFCC00%">")
			output_file.putstring ("<b>Total</b></font></td></tr>")
			output_file.putstring ("%N")
			write_statistics_big ("Violation", number_passed_violation, number_of_violation)
			write_statistics_big ("Boolean", number_passed_boolean, number_of_boolean)
			write_statistics_big ("All Cases", number_passed_tests, number_of_tests)
			output_file.putstring ("<tr><td align=%"center%" bgcolor=%"#000080%"><font color=%"#FFCC00%">")
			output_file.putstring ("<b>State</b></font></td>")
			output_file.putstring ("%N")
			output_file.putstring ("<td align=%"center%" bgcolor=%"#000080%"><font color=%"#FFCC00%">")
			output_file.putstring ("<b>Contract Violation</b></font></td>")
			output_file.putstring ("%N")
			output_file.putstring ("<td align=%"center%" bgcolor=%"#000080%"><font color=%"#FFCC00%">")
			output_file.putstring ("<b>Test Name</b></font></td></tr>")
			output_file.putstring ("%N")
		end
	
feature {NONE} -- Attributes

	number_of_boolean: INTEGER_32

	number_of_tests: INTEGER_32

	number_of_violation: INTEGER_32

	number_passed_boolean: INTEGER_32

	number_passed_tests: INTEGER_32

	number_passed_violation: INTEGER_32

	output_file1: PLAIN_TEXT_FILE

	output_file_name: STRING_8

	test_name: STRING_8
	
feature -- Output

	Io: STD_FILES
			-- Handle to standard file setup
			-- (from ANY)
		once
			create Result
			Result.set_output_default
		ensure -- from ANY
			instance_free: class
			io_not_void: Result /= Void
		end

	out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		do
			Result := tagged_out
		ensure -- from ANY
			out_not_void: Result /= Void
		end

	print (o: detachable ANY)
			-- Write terse external representation of o
			-- on standard output.
			-- (from ANY)
		do
			if o /= Void then
				Io.put_string (o.out)
			end
		ensure -- from ANY
			instance_free: class
		end

	frozen tagged_out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			tagged_out_not_void: Result /= Void
		end
	
feature -- Platform

	Operating_environment: OPERATING_ENVIRONMENT
			-- Objects available from the operating system
			-- (from ANY)
		once
			create Result
		ensure -- from ANY
			instance_free: class
			operating_environment_not_void: Result /= Void
		end
	
feature {NONE} -- Retrieval

	frozen internal_correct_mismatch
			-- Called from runtime to perform a proper dynamic dispatch on correct_mismatch
			-- from MISMATCH_CORRECTOR.
			-- (from ANY)
		local
			l_msg: STRING_8
			l_exc: EXCEPTIONS
		do
			if attached {MISMATCH_CORRECTOR} Current as l_corrector then
				l_corrector.correct_mismatch
			else
				create l_msg.make_from_string ("Mismatch: ")
				create l_exc
				l_msg.append (generating_type.name)
				l_exc.raise_retrieval_exception (l_msg)
			end
		end
	
invariant
		-- from ANY
	reflexive_equality: standard_is_equal (Current)
	reflexive_conformance: conforms_to (Current)

end -- class ES_HTML_GEN

Generated by ISE EiffelStudio