Permalink
Browse files

Data factory for keeping literals out of tests

  • Loading branch information...
1 parent f9bf405 commit c6a85694a3127de2df861f19674ae8e3e00afc85 @epall committed Apr 28, 2011
View
@@ -14,8 +14,8 @@ module Right
require 'test/right/widget'
require 'test/right/assertions'
require 'test/right/feature'
-require 'test/right/widget_finder'
require 'test/right/browser_driver'
+require 'test/right/data_factory'
require 'test/right/runner'
require 'test/right/generator'
require 'test/right/cli'
@@ -0,0 +1,22 @@
+require 'set'
+
+module Test
+ module Right
+ class DataFactory
+ def initialize(template)
+ @template = template
+ @used_ids = Set.new([nil])
+ end
+
+ def [](name)
+ base = @template[name] || @template[name.to_s] || name.to_s
+ id = nil
+ while @used_ids.include? id
+ id = rand(100000)
+ end
+ base += id.to_s
+ return base
+ end
+ end
+ end
+end
@@ -4,13 +4,17 @@ class Feature
extend Utils::SubclassTracking
include Assertions
+ attr_reader :data
+
WIDGET_TIMEOUT = 10 # seconds
- def initialize(driver)
+ def initialize(driver, data)
@driver = driver
+ @data = data
end
private
+
def with(widget_class)
wait_for(widget_class)
@@ -4,7 +4,7 @@
module Test
module Right
class Runner
- attr_reader :results, :widget_classes, :driver
+ attr_reader :results, :widget_classes
def initialize(config, widgets, features)
@config = config
@@ -13,6 +13,7 @@ def initialize(config, widgets, features)
@results = {}
@pool = Threadz::ThreadPool.new(:initial_size => 2, :maximum_size => 2)
@result_queue = Queue.new
+ @data_template = config[:data] || {}
end
def run
@@ -65,8 +66,10 @@ def run_test(feature, method)
driver = BrowserDriver.new(@config)
end
+ data = DataFactory.new(@data_template)
+
begin
- target = feature.new(driver)
+ target = feature.new(driver, data)
if target.respond_to? :setup
target.setup
end
@@ -1,40 +0,0 @@
-module Test
- module Right
- class WidgetFinder
- def initialize(runner)
- @runner = runner
- @widget_classes = runner.widget_classes
- @widgets = {}
- end
-
-
- def [](name)
- original_name = name
- if name.is_a? Symbol
- name = name.to_s
- end
- name.gsub!(/ /, '_')
- name = camelize(name)
-
- name += "Widget"
-
- klass = @widget_classes.find do |widget|
- widget_name = widget.name.split(":").last
- name == widget_name
- end
-
- if klass.nil?
- raise WidgetNotFoundError, "Widget for \"#{original_name}\" not found"
- end
-
- @widgets[klass] ||= klass.new(@runner.driver)
- end
-
- private
-
- def camelize(word)
- word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
- end
- end
- end
-end
@@ -0,0 +1,9 @@
+require 'helper'
+
+class TestDataFactory < Test::Unit::TestCase
+ def test_uses_template
+ factory = Test::Right::DataFactory.new('foo' => 'bar')
+ assert factory[:foo].include? 'bar'
+ assert_not_equal 'bar', factory[:foo]
+ end
+end
View
@@ -31,7 +31,7 @@ def use_with
widget.expects(:bar)
FooWidget.expects(:new).at_least_once.returns(widget)
- feature = FeatureThatUsesWith.new(@driver)
+ feature = FeatureThatUsesWith.new(@driver, nil)
feature.use_with
FooWidget.expects(:new).returns(widget)
@@ -61,7 +61,24 @@ def use_wait_for
widget.expects(:exists?).at_least_once.returns(true)
FooWidget.expects(:new).at_least_once.returns(widget)
- feature = FeatureThatUsesWaitFor.new(@driver)
+ feature = FeatureThatUsesWaitFor.new(@driver, nil)
feature.use_wait_for
end
+
+ def test_data
+ $data_value = nil
+
+ eval <<-CLASSDEFS
+ class FeatureThatUsesData < Test::Right::Feature
+ def use_data
+ $data_value = data[:foo]
+ end
+ end
+ CLASSDEFS
+
+ data = {:foo => 'foo'}
+ feature = FeatureThatUsesData.new(@driver, data)
+ feature.use_data
+ assert_equal 'foo', $data_value
+ end
end
@@ -1,37 +0,0 @@
-require 'helper'
-
-class TestWidgetFinder < Test::Unit::TestCase
- def setup
- eval <<-CLASSDEFS
- class SimpleWidget < Test::Right::Widget
- end
-
- class NoSelectorsWidget < Test::Right::Widget
- end
-
- class TwoWordWidget < Test::Right::Widget
- end
- CLASSDEFS
-
- runner = Test::Right::Runner.new(Test::Right::Config.new, [SimpleWidget, TwoWordWidget, NoSelectorsWidget], [])
- @widget_finder = Test::Right::WidgetFinder.new(runner)
- end
-
- def teardown
- Test::Right::Widget.wipe!
- Test::Right::Feature.wipe!
- end
-
- def test_simple_match
- @widget_finder["simple"].is_a? SimpleWidget
- @widget_finder[:simple].is_a? SimpleWidget
- @widget_finder["Simple"].is_a? SimpleWidget
-
- @widget_finder["two word"].is_a? TwoWordWidget
- @widget_finder[:two_word].is_a? TwoWordWidget
-
- assert_raises Test::Right::WidgetNotFoundError do
- @widget_finder["not here"]
- end
- end
-end

0 comments on commit c6a8569

Please sign in to comment.