Class: QUnited::Runner

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

Constant Summary collapse

DRIVERS_PRIORITY =

The drivers in order of which to use first when not otherwise specified

[:PhantomJs, :Rhino].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(js_source_files, js_test_files, options = {}) ⇒ Runner

Returns a new instance of Runner.



9
10
11
# File 'lib/qunited/runner.rb', line 9

def initialize(js_source_files, js_test_files, options={})
  @js_source_files, @js_test_files, @options = js_source_files, js_test_files, options
end

Instance Attribute Details

#js_source_filesObject

Returns the value of attribute js_source_files.



7
8
9
# File 'lib/qunited/runner.rb', line 7

def js_source_files
  @js_source_files
end

#js_test_filesObject

Returns the value of attribute js_test_files.



7
8
9
# File 'lib/qunited/runner.rb', line 7

def js_test_files
  @js_test_files
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/qunited/runner.rb', line 7

def options
  @options
end

Instance Method Details

#best_available_driverObject



75
76
77
# File 'lib/qunited/runner.rb', line 75

def best_available_driver
  DRIVERS_PRIORITY.map { |driver| get_driver(driver) }.find { |driver| driver.available? }
end

#confirm_existence_of_files(files_array) ⇒ Object



79
80
81
# File 'lib/qunited/runner.rb', line 79

def confirm_existence_of_files(files_array)
  files_array.each { |f| raise UsageError, "File not found: #{f}" unless File.exist? f }
end

#get_driver(klass) ⇒ Object



63
64
65
66
67
# File 'lib/qunited/runner.rb', line 63

def get_driver(klass)
  if known_driver_classes.include?(klass.to_sym)
    ::QUnited::Driver.const_get(klass.to_s)
  end
end

#get_formatter(klass) ⇒ Object



69
70
71
72
73
# File 'lib/qunited/runner.rb', line 69

def get_formatter(klass)
  if known_formatter_classes.include?(klass.to_sym)
    ::QUnited::Formatter.const_get(klass.to_s)
  end
end

#resolve_driver_classObject

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/qunited/runner.rb', line 29

def resolve_driver_class
  if options[:driver]
    begin
      driver_class = get_driver(options[:driver])
    rescue NameError
      raise UsageError, "#{options[:driver].to_s} does not exist"
    end

    if !driver_class
      raise UsageError, "#{driver_class} driver not found"
    elsif !driver_class.available?
      raise UsageError, "#{driver_class} driver specified, but not available"
    end
  end

  driver_class ||= best_available_driver
  raise(UsageError, 'No driver available') unless driver_class
  driver_class
end

#resolve_formatter_classObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/qunited/runner.rb', line 49

def resolve_formatter_class
  if options[:formatter]
    begin
      formatter_class = get_formatter(options[:formatter])
    rescue NameError
      raise UsageError, "#{options[:formatter].to_s} does not exist"
    end

    raise UsageError, "#{formatter_class} formatter not found" unless formatter_class
  end

  formatter_class || ::QUnited::Formatter::Dots
end

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/qunited/runner.rb', line 13

def run
  [js_source_files, js_test_files].each { |files| confirm_existence_of_files files }

  driver_class, formatter_class = resolve_driver_class, resolve_formatter_class
  driver = driver_class.new(js_source_files, js_test_files)
  driver.formatter = formatter_class.new({:driver_name => driver.name})

  if options[:fixture_files]
    driver.fixture_files = options[:fixture_files]
  end

  results = driver.run

  results.all? { |r| r.passed? }
end