Class: UnityTestResults::TestParserService

Inherits:
Object
  • Object
show all
Defined in:
lib/unity_test_results.rb

Constant Summary collapse

CHECKMARK =
"\u2705".freeze
CROSSMARK =
"\u274c".freeze
INCONCLUSIVEMARK =
"\u00B7".freeze
FOLDER =
"\u2192".freeze
SUCCEED_COLOR =
{ color: :light_green }.freeze
FAILED_COLOR =
{ color: :red }.freeze
INCONCLUSIVE_COLOR =
:yellow
ERROR_COLOR =
:red
CLASS_INDENT =
'  '.freeze
TEST_INDENT =
'    '.freeze

Instance Method Summary collapse

Constructor Details

#initialize(xml_path, xsl_path) ⇒ TestParserService

Returns a new instance of TestParserService.



23
24
25
26
27
28
29
30
# File 'lib/unity_test_results.rb', line 23

def initialize(xml_path, xsl_path)
  xml_doc = create_xml_document_from(xml_path)
  xslt = create_xslt_file_from(xsl_path)
  transformed_xml = transform_xml_with_xsl(xml_doc, xslt)
  hashed_xml = xml_to_hash(transformed_xml)
  puts "\n NO TESTS FOUND \n".colorize(SUCCEED_COLOR) if hashed_xml.empty?
  create_test_results(hashed_xml) unless hashed_xml.empty?
end

Instance Method Details

#create_test_results(hashed_xml) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/unity_test_results.rb', line 80

def create_test_results(hashed_xml)
  tests = hashed_xml[:tests]
  test_results = tests.map { |value| TestResult.new(value) }
  final_results = {}

  test_results.each do |result|
    root = result.namespace.split('.')
    root_name = root[0]
    unless final_results.key?(root[0])
      namespace = Namespace.new(root[0])
      final_results[root_name] = namespace
    end
    root.shift
    final_results[root_name].add_namespace(root, result)
    # p final_results
  end

  puts '--------------------------------------------------------------'.colorize(:yellow)

  final_results.each do |_k, namespace|
    namespace.show_test_results('')
  end
end

#create_xml_document_from(results_path) ⇒ Object



32
33
34
35
36
# File 'lib/unity_test_results.rb', line 32

def create_xml_document_from(results_path)
  result = File.open(results_path)
  xml = Nokogiri::XML(result)
  xml
end

#create_xslt_file_from(xslt_path) ⇒ Object



53
54
55
# File 'lib/unity_test_results.rb', line 53

def create_xslt_file_from(xslt_path)
  Nokogiri::XSLT(File.read(xslt_path))
end

#deep_sybolize_keys(value = self) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/unity_test_results.rb', line 38

def deep_sybolize_keys(value = self)
  case value
  when Array
    value.map { |v| deep_sybolize_keys(v) }
  when Hash
    symbolize_hash(value)
  else
    value
  end
end

#symbolize_hash(value) ⇒ Object



49
50
51
# File 'lib/unity_test_results.rb', line 49

def symbolize_hash(value)
  Hash[value.map { |k, v| [k.to_sym, deep_sybolize_keys(v)] }]
end

#test_color(succeed) ⇒ Object



73
74
75
76
77
78
# File 'lib/unity_test_results.rb', line 73

def test_color(succeed)
  return SUCCEED_COLOR if succeed
  return FAILED_COLOR unless succeed

  :yellow
end

#test_mark(succeed) ⇒ Object



66
67
68
69
70
71
# File 'lib/unity_test_results.rb', line 66

def test_mark(succeed)
  return CHECKMARK if succeed
  return CROSSMARK unless succeed

  INCONCLUSIVEMARK
end

#transform_xml_with_xsl(xml, xslt) ⇒ Object



57
58
59
# File 'lib/unity_test_results.rb', line 57

def transform_xml_with_xsl(xml, xslt)
  xslt.transform(xml).to_s
end

#xml_to_hash(xml) ⇒ Object



61
62
63
64
# File 'lib/unity_test_results.rb', line 61

def xml_to_hash(xml)
  hash = XmlSimple.xml_in(xml)
  symbolize_hash(hash)
end