Class: JsTestDriver::Config

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

Overview

The Config class represents the YAML config file that is passed to JsTestDriver

includes corresponds to load excludes corresponds to exclude server can be configures either using server, or host and port

The configuration is very basic, however the fact that it is done in Ruby, gives the user a significant amount of freedom in terms of what is and is not loaded, and so on

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Config

Returns a new instance of Config.



12
13
14
# File 'lib/js_test_driver/config.rb', line 12

def initialize(attributes = {})
  self.attributes = attributes
end

Instance Attribute Details

#browsersObject



137
138
139
# File 'lib/js_test_driver/config.rb', line 137

def browsers
  @browsers ||= []
end

#config_dirObject

this is where the config files are saved (ex. RAILS_ROOT/.js_test_driver)



158
159
160
# File 'lib/js_test_driver/config.rb', line 158

def config_dir
  @config_dir ||= File.expand_path(".")
end

Class Method Details

.define_config_variable(name, &block) ⇒ Object

config variable which has a regular setter, but also can be set by calling the “getter” with an argument and if called without an argument the getter will return the passed block

Ex. A.define_config_variable(:foo) { @foo || “hello” } a = A.new a.foo # returns “hello” a.foo “foo” a.foo # returns “foo” a.foo = “bar” a.foo # returns “bar”



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/js_test_driver/config.rb', line 97

def self.define_config_variable(name, &block)
  attr_writer name

  define_method(name) do |*values|
    unless values.empty?
      self.send("#{name}=", values.first)
    else
      instance_eval(&block)
    end
  end
end

.parse(string) ⇒ Object



149
150
151
152
153
# File 'lib/js_test_driver/config.rb', line 149

def self.parse(string)
  config = new
  config.instance_eval(string)
  return config
end

Instance Method Details

#browser(*browsers) ⇒ Object

Defines a browser to be captured by default

This should be a string with no spaces (if you need to pass parameters to the browser you will need to create a shell script ans put it’s name here)



34
35
36
37
38
# File 'lib/js_test_driver/config.rb', line 34

def browser(*browsers)
  browsers.each do |browser|
    self.browsers << browser
  end
end

#enable_jasmineObject

Includes the bundled with the gem jasmine js file and an adapter for jasmine

There’s no hacks or modifications here, so this method is just for the users convenience



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

def enable_jasmine
  includes File.join(vendor_directory, "jasmine.js")
  includes File.join(vendor_directory, "JasmineAdapter.js")
end

#excluded_filesObject



127
128
129
# File 'lib/js_test_driver/config.rb', line 127

def excluded_files
  @excludes ||= []
end

#excludes(*paths) ⇒ Object

Files specified here will not be loaded, it’s useful when combined with globbing in includes

paths should be relative to root_dir



26
27
28
# File 'lib/js_test_driver/config.rb', line 26

def excludes(*paths)
  self.excluded_files.concat(expand_globs(paths))
end

#fixtures(directory, opts = {}) ⇒ Object

Defines a HTML fixture directory

the first argument is the directory to scan for html fixtures you can pass also :name and :namespace arguments to define the name and the namespace of the fixture

the fixtures will be accessible through: namespace.name[“file_name_without the html extension”]

by default the namespace is called htmlFixtures and the fixture name is called all



50
51
52
53
54
55
56
# File 'lib/js_test_driver/config.rb', line 50

def fixtures(directory, opts = {})
  fixture = JsTestDriver::HtmlFixture.new(directory, opts[:name], opts[:namespace])
  if html_fixtures.detect{|f| f.name == fixture.name && f.namespace == fixture.namespace}
    raise ArgumentError.new("Fixture #{fixture.namespace}.#{fixture.name} already defined!")
  end
  html_fixtures << fixture
end

#html_fixturesObject



131
132
133
# File 'lib/js_test_driver/config.rb', line 131

def html_fixtures
  @html_fixtures ||= []
end

#included_filesObject



123
124
125
# File 'lib/js_test_driver/config.rb', line 123

def included_files
  @includes ||= []
end

#includes(*paths) ⇒ Object

Adds a file to be loaded, the path must be relative to the root_dir (which is the current dir by default)

JsTestDriver supports globbing



19
20
21
# File 'lib/js_test_driver/config.rb', line 19

def includes(*paths)
  self.included_files.concat(expand_globs(paths))
end

#measure_coverageObject

Adds a JsTestDriver plugin for measuring coverage to the configuration



67
68
69
70
71
72
73
74
# File 'lib/js_test_driver/config.rb', line 67

def measure_coverage
  @measure_coverage = true
  self.plugins << {
    'name' => 'coverage',
    'jar' => File.join(vendor_directory, 'coverage.jar'),
    'module' => 'com.google.jstestdriver.coverage.CoverageModule'
  }
end

#measure_coverage?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/js_test_driver/config.rb', line 76

def measure_coverage?
  !!@measure_coverage
end

#pluginsObject

Plugins to include in the config



81
82
83
# File 'lib/js_test_driver/config.rb', line 81

def plugins
  @plugins ||= []
end

#save_fixturesObject



162
163
164
165
166
167
168
169
170
# File 'lib/js_test_driver/config.rb', line 162

def save_fixtures
  html_fixtures.each do |fixture|
    path = fixture_file_name(fixture)
    FileUtils.mkdir_p(File.dirname(path))
    File.open(path, "w+") do |f|
      f.puts fixture.to_s
    end
  end
end

#to_sObject



141
142
143
144
145
146
147
# File 'lib/js_test_driver/config.rb', line 141

def to_s
  hash = {'server' => server, 'basepath' => base_path}
  hash['load'] = loaded_files unless loaded_files.empty?
  hash['exclude'] = map_paths(excluded_files) unless excluded_files.empty?
  hash['plugin'] = plugins unless plugins.empty?
  return hash.to_yaml
end