Class: Qunit::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



16
17
18
19
20
21
22
23
24
# File 'lib/qunit/parser.rb', line 16

def initialize
  @logger = Qunit::Logger
  @unfinished_modules = Hash.new
  @failed_assertions = Array.new
  @total_failed = 0
  @total_passed = 0
  @total = 0
  @duration = 0
end

Instance Attribute Details

#current_moduleObject (readonly)

Returns the value of attribute current_module.



6
7
8
# File 'lib/qunit/parser.rb', line 6

def current_module
  @current_module
end

#current_testObject (readonly)

Returns the value of attribute current_test.



7
8
9
# File 'lib/qunit/parser.rb', line 7

def current_test
  @current_test
end

#durationObject (readonly)

Returns the value of attribute duration.



13
14
15
# File 'lib/qunit/parser.rb', line 13

def duration
  @duration
end

#failed_assertionsObject (readonly)

Returns the value of attribute failed_assertions.



9
10
11
# File 'lib/qunit/parser.rb', line 9

def failed_assertions
  @failed_assertions
end

#loggerObject (readonly)

Returns the value of attribute logger.



14
15
16
# File 'lib/qunit/parser.rb', line 14

def logger
  @logger
end

#totalObject (readonly)

Returns the value of attribute total.



12
13
14
# File 'lib/qunit/parser.rb', line 12

def total
  @total
end

#total_failedObject (readonly)

Returns the value of attribute total_failed.



10
11
12
# File 'lib/qunit/parser.rb', line 10

def total_failed
  @total_failed
end

#total_passedObject (readonly)

Returns the value of attribute total_passed.



11
12
13
# File 'lib/qunit/parser.rb', line 11

def total_passed
  @total_passed
end

#unfinished_modulesObject (readonly)

Returns the value of attribute unfinished_modules.



8
9
10
# File 'lib/qunit/parser.rb', line 8

def unfinished_modules
  @unfinished_modules
end

Instance Method Details

#console(message) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/qunit/parser.rb', line 120

def console(message)
  prefix = ''
  if @current_test
    prefix << "\n[%s]" % @current_test
  elsif @current_module
    prefix << "\n[%s]" % @current_module
  end
  prefix << ' CONSOLE: '
  logger.print prefix, :magenta
  logger.print message
  logger.print "\n"
end

#fail_load(url) ⇒ Object



106
107
108
109
110
111
# File 'lib/qunit/parser.rb', line 106

def fail_load(url)
  logger.puts "PhantomJS unable to load #{url} URI", :red
  @total_failed += 1
  @total += 1
  log_summary
end

#fail_timeoutObject



113
114
115
116
117
118
# File 'lib/qunit/parser.rb', line 113

def fail_timeout
  logger.puts "PhantomJS timed out.", :red
  @total_failed += 1
  @total += 1
  log_summary
end

#log_summaryObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/qunit/parser.rb', line 133

def log_summary
  logger.puts ''
  @failed_assertions.each do |assertion|
    logger.puts assertion[:name]
    logger.puts "Message: #{assertion[:message]}", :red
    if assertion[:actual] != assertion[:expected]
      logger.puts "Actual: #{assertion[:actual]}", :magenta
      logger.puts "Expected: #{assertion[:expected]}", :yellow
    end
    if assertion[:source]
      logger.puts assertion[:source], :cyan
    end
    logger.puts ''
  end
  if @total == 0
    logger.puts "0/0 assertions ran (#{@duration/1000.0}s)", :magenta
  else
    logger.puts "#{@total_passed}/#{@total} assertions passed (#{@duration/1000.0}s)"
  end
  if @total_failed == 0 and @total > 0
    logger.puts "OK", :green
    # exit status
    return true, 0
  else
    # exit status
    return true, 1
  end
end

#module_done(name, *args) ⇒ Object



59
60
61
62
63
64
# File 'lib/qunit/parser.rb', line 59

def module_done(name, *args)
  name ||= "Unnamed Module"
  @unfinished_modules.delete(name.to_sym)
  # exit status
  return false, nil
end

#module_start(name) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/qunit/parser.rb', line 51

def module_start(name)
  name ||= "Unnamed Module"
  @unfinished_modules[name.to_sym] = true
  @current_module = name
  # exit status
  return false, nil
end

#parse(line) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/qunit/parser.rb', line 26

def parse(line)
  params = JSON.parse line
  event = params.shift
  case event
  when 'qunit.moduleStart'
    module_start *params
  when 'qunit.moduleDone'
    module_done *params
  when 'qunit.testStart'
    test_start *params
  when 'qunit.testDone'
    test_done *params
  when 'qunit.log'
    qunit_log *params
  when 'qunit.done'
    qunit_done *params
  when 'fail.load'
    fail_load *params
  when 'fail.timeout'
    fail_timeout *params
  when 'console'
    console *params
  end
end

#qunit_done(failed, passed, total, duration) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/qunit/parser.rb', line 98

def qunit_done(failed, passed, total, duration)
  @total_failed += failed
  @total_passed += passed
  @total += total
  @duration += duration
  log_summary
end

#qunit_log(result, actual, expected, message, source) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/qunit/parser.rb', line 83

def qunit_log(result, actual, expected, message, source)
  if not result
    assertion = {
      name: @current_test,
      actual: actual,
      expected: expected,
      message: message,
      source: source
    }
    @failed_assertions.push assertion
  end
  # exit status
  return false, nil
end

#test_done(name, failed, *args) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/qunit/parser.rb', line 73

def test_done(name, failed, *args)
  if failed > 0
    logger.print 'F', :red
  else
    logger.print '.'
  end
  # exit status
  return false, nil
end

#test_start(name) ⇒ Object



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

def test_start(name)
  prefix = @current_module ? "#{@current_module} - " : ''
  @current_test = "#{prefix}#{name}"
  # exit status
  return false, nil
end