Class: JSTestSan::TestCase

Inherits:
OSX::NSObject
  • Object
show all
Defined in:
lib/js_test_san/test.rb,
lib/js_test_san/test_case.rb

Defined Under Namespace

Classes: FileDoesNotExistError, Test

Constant Summary collapse

RESULTS_REGEXP =
/^((\d+) tests, )?(\d+) assertions, (\d+) failures, (\d+) errors\n?/
STATES =
%w{ passed failed error }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#assertionsObject (readonly)

Returns the value of attribute assertions.



19
20
21
# File 'lib/js_test_san/test_case.rb', line 19

def assertions
  @assertions
end

#delegateObject (readonly)

Returns the value of attribute delegate.



18
19
20
# File 'lib/js_test_san/test_case.rb', line 18

def delegate
  @delegate
end

#errorsObject (readonly)

Returns the value of attribute errors.



19
20
21
# File 'lib/js_test_san/test_case.rb', line 19

def errors
  @errors
end

#failuresObject (readonly)

Returns the value of attribute failures.



19
20
21
# File 'lib/js_test_san/test_case.rb', line 19

def failures
  @failures
end

#html_fileObject (readonly)

Returns the value of attribute html_file.



18
19
20
# File 'lib/js_test_san/test_case.rb', line 18

def html_file
  @html_file
end

#testsObject (readonly)

Returns the value of attribute tests.



19
20
21
# File 'lib/js_test_san/test_case.rb', line 19

def tests
  @tests
end

#titleObject (readonly)

Returns the value of attribute title.



18
19
20
# File 'lib/js_test_san/test_case.rb', line 18

def title
  @title
end

Class Method Details

.sharedWebViewObject



10
11
12
# File 'lib/js_test_san/test_case.rb', line 10

def self.sharedWebView
  @sharedWebView ||= OSX::WebView.alloc.init
end

Instance Method Details

#documentObject



43
44
45
# File 'lib/js_test_san/test_case.rb', line 43

def document
  webView.mainFrame.DOMDocument
end

#finished?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/js_test_san/test_case.rb', line 35

def finished?
  @finished
end

#handleEvent(event) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/js_test_san/test_case.rb', line 64

def handleEvent(event)
  element = event.target
  case element
  when OSX::DOMText
    finalize unless log.innerText == 'running...'
  else
    parent = element.parentNode
    state = parent.className
    
    if element == parent.lastChild && STATES.include?(state)
      output = parent.children.item(2).innerText.to_s
      return if output.empty?
      
      output.sub!(RESULTS_REGEXP, '')
      name = parent.children.item(0).innerText
      
      @delegate.test_ran(Test.new(self, name, state.to_sym, output))
    end
  end
end

#initWithHTMLFile_delegate(html_file, delegate) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/js_test_san/test_case.rb', line 21

def initWithHTMLFile_delegate(html_file, delegate)
  if init
    raise FileDoesNotExistError, "The file `#{html_file}' does not exist." unless File.exist?(html_file)
    @html_file, @delegate = html_file, delegate
    @tests = @assertions = @failures = @errors = 0
    @title = ''
    self
  end
end

#logObject



47
48
49
# File 'lib/js_test_san/test_case.rb', line 47

def log
  document.getElementsByClassName('logsummary').item(0)
end

#loglinesObject



51
52
53
# File 'lib/js_test_san/test_case.rb', line 51

def loglines
  document.getElementsByClassName('loglines').item(0)
end

#runObject



31
32
33
# File 'lib/js_test_san/test_case.rb', line 31

def run
  webView
end

#webViewObject



39
40
41
# File 'lib/js_test_san/test_case.rb', line 39

def webView
  @webView ||= load_webView
end

#webView_didFinishLoadForFrame(_, __) ⇒ Object

Not yet sure why the extra check for log not being nil is necessary. Might be a test only thing.



57
58
59
60
61
62
# File 'lib/js_test_san/test_case.rb', line 57

def webView_didFinishLoadForFrame(_, __)
  @title = webView.mainFrameTitle.to_s
  [log, loglines].each do |element|
    element.addEventListener___('DOMSubtreeModified', self, true) if element
  end
end