Class: TarkaMatchers::Formatters::Difference

Inherits:
Object
  • Object
show all
Includes:
Styles
Defined in:
lib/tarka_matchers/formatters/difference.rb

Constant Summary

Constants included from Styles

Styles::BLACK_ON_GREEN, Styles::GREEN_F, Styles::L_GREEN_F, Styles::RED_BLOCK, Styles::RED_F, Styles::RESET, Styles::WHITE_BLOCK, Styles::WHITE_F, Styles::WHITE_ON_RED, Styles::YELLOW_F

Class Method Summary collapse

Class Method Details

.difference(expected, actual) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tarka_matchers/formatters/difference.rb', line 7

def self.difference expected, actual
	expected = expected.to_s
	actual = actual.to_s
	expected_line = "#{GREEN_F}Expected: #{BLACK_ON_GREEN}#{expected}#{RESET}"
	expected_length = expected.length
	actual_length = actual.length
	actual_line = "\n#{RESET}#{RED_F}  Actual: "

	longest = [expected,actual].sort_by(&:length).last 
	longest_length = longest.length
	expected = expected.split('')
	actual = actual.split('')
	correct = 0

	longest_length.times do |i|
		e = expected[i]
		a = actual[i]

		if expected_length <= i
			expected_line << "#{WHITE_BLOCK}"
			actual_line << "#{WHITE_ON_RED}#{a}"
		elsif actual_length <= i
			actual_line << "#{RED_BLOCK}"
		elsif e != a
			actual_line << "#{WHITE_ON_RED}#{a}"
		elsif e == a
			correct += 1
			actual_line << "#{BLACK_ON_GREEN}#{a}"
		end
	end
	identical = ((correct.to_f/longest_length) * 100).round 3
	"\n\n#{expected_line}#{actual_line}#{RESET}#{RED_F} - #{identical}% identical#{RESET}" 
end