scenarios 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Starr Horne
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = scenarios
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Starr Horne. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "scenarios"
8
+ gem.summary = %Q{Manage scenarios for testing}
9
+ gem.description = %Q{Factories on steroids.}
10
+ gem.email = "starr@chromahq.com"
11
+ gem.homepage = "http://github.com/starrhorne/scenarios"
12
+ gem.authors = ["Starr Horne"]
13
+ gem.add_development_dependency "shoulda", ">= 0"
14
+ gem.add_development_dependency "mocha", ">= 0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/*_test.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/*_test.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "scenarios #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/lib/scenarios.rb ADDED
@@ -0,0 +1,45 @@
1
+ module Scenarios
2
+
3
+ #
4
+ # Register a Scenario instance with the module.
5
+ # Scenarios are stored inside of scenario groups.
6
+ #
7
+ # When multple scenarios are registerd in a single
8
+ # group, it looks like this:
9
+ #
10
+ # { :user_scenarios => { :no_users => [Scenario], :admin_users => [Scenario] }}
11
+ #
12
+ # @param [String, Symbol] group_name
13
+ # @param [Scenarios::Scenario] obj
14
+ #
15
+ def self.register(group_name, obj)
16
+ ( store[group_name.to_sym] ||= {} )[obj.name.to_sym] = obj
17
+ end
18
+
19
+ #
20
+ # @return [Hash] the hash of scenario groups
21
+ #
22
+ def self.store
23
+ @store ||= {}
24
+ end
25
+
26
+ #
27
+ # @param [String, Symbol] k the name of the scenario group to get
28
+ # @return [Hash] a particular scenario group
29
+ #
30
+ def self.[](k)
31
+ self.store[k.to_sym]
32
+ end
33
+
34
+ #
35
+ # Remove all scenario groups
36
+ #
37
+ def self.clear
38
+ @store = {}
39
+ end
40
+
41
+ end
42
+
43
+ require 'scenarios/proc_extensions.rb'
44
+ require 'scenarios/scenario.rb'
45
+ require 'scenarios/base.rb'
@@ -0,0 +1,36 @@
1
+ module Scenarios
2
+
3
+ #
4
+ # The base class is the one you'll subclass in order
5
+ # to create a "scenario group"
6
+ #
7
+ class Base
8
+
9
+ class << self
10
+
11
+ #
12
+ # Define the name for this scenario group
13
+ #
14
+ # @param [String, Symbol] group_name
15
+ #
16
+ def name(group_name)
17
+ @group_name = group_name
18
+ end
19
+
20
+ #
21
+ # Create a new instance of `Scenario` and register it.
22
+ #
23
+ # @param [String, Symbol] name The scenario's name
24
+ # @param [Proc] block A block to be executed within the new scenario's scope
25
+ # @return [Scenairos::Scenario] the new scenario
26
+ #
27
+ def scenario(name, &block)
28
+ raise "You must call `name` before you can call `scenario`" unless @group_name
29
+
30
+ obj = Scenario.new(name, &block)
31
+ Scenarios.register(@group_name.to_sym, obj)
32
+ obj
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,14 @@
1
+ # Stolen straight from ActiveSupport
2
+
3
+ class Proc #:nodoc:
4
+ def bind(object)
5
+ block, time = self, Time.now
6
+ (class << object; self end).class_eval do
7
+ method_name = "__bind_#{time.to_i}_#{time.usec}"
8
+ define_method(method_name, &block)
9
+ method = instance_method(method_name)
10
+ remove_method(method_name)
11
+ method
12
+ end.bind(object)
13
+ end
14
+ end
@@ -0,0 +1,72 @@
1
+ module Scenarios
2
+
3
+ class Scenario
4
+
5
+ attr_reader :name, :definitions
6
+
7
+ #
8
+ # Initialize the new instance using the block
9
+ #
10
+ # @param [String, Symbol] name
11
+ # @param [Proc] block
12
+ #
13
+ def initialize(name, &block)
14
+ @name = name
15
+ @definitions = {}
16
+ block.bind(self).call if block_given?
17
+ end
18
+
19
+ #
20
+ # Save a setup block to be called later
21
+ #
22
+ # Example:
23
+ #
24
+ # setup do
25
+ # @resorce = User.create(...)
26
+ # end
27
+ #
28
+ # @param [Proc] block
29
+ #
30
+ def setup(&block)
31
+ @setup_block = block
32
+ end
33
+
34
+ #
35
+ # Call the setup block
36
+ #
37
+ def setup!
38
+ @setup_block.call if @setup_block.is_a?(Proc)
39
+ end
40
+
41
+ #
42
+ # Save a block that will be used later to
43
+ # create a particular dataset.
44
+ #
45
+ # Example:
46
+ #
47
+ # define :params do
48
+ # { :valid_id => @resource.id }
49
+ # end
50
+ #
51
+ # @param [String, Symbol] key
52
+ # @param [Proc] block
53
+ #
54
+ def define(key, &block)
55
+ @definitions[key.to_sym] = block
56
+ end
57
+
58
+
59
+ #
60
+ # Evaluate a particular definition and
61
+ # return the results
62
+ #
63
+ # @param [String, Symbol] key Name of definition to eval
64
+ #
65
+ def [](key)
66
+ d = @definitions[key.to_sym]
67
+ d.is_a?(Proc) ? d.call : nil
68
+ end
69
+
70
+ end
71
+
72
+ end
data/test/base_test.rb ADDED
@@ -0,0 +1,76 @@
1
+ require 'helper'
2
+
3
+ class TestBase < Test::Unit::TestCase
4
+
5
+ context "no name" do
6
+
7
+ setup do
8
+ Scenarios.clear
9
+ Scenarios::Base.name(nil)
10
+ end
11
+
12
+ should "raise an exception when `scenario` called" do
13
+ assert_raise RuntimeError do
14
+ Scenarios::Base.scenario("X"){}
15
+ end
16
+ end
17
+
18
+ end
19
+
20
+ context "a name" do
21
+
22
+ setup do
23
+ Scenarios.clear
24
+ Scenarios::Base.name(:sample)
25
+ end
26
+
27
+ should "not raise an exception when `scenario` called" do
28
+ Scenarios::Base.scenario("X"){}
29
+ end
30
+
31
+ end
32
+
33
+ context "a few empty scenarios" do
34
+
35
+ setup do
36
+ Scenarios.clear
37
+ Scenarios::Base.name(:sample)
38
+ @one = Scenarios::Base.scenario(:one){}
39
+ @two = Scenarios::Base.scenario(:two){}
40
+ end
41
+
42
+ should "register group with Scenarios module" do
43
+ assert Scenarios[:sample].kind_of?(Hash)
44
+ end
45
+
46
+ should "register both scenarios" do
47
+ assert_equal 2, Scenarios[:sample].size
48
+ end
49
+
50
+ should "register correct scenarios" do
51
+ assert_equal({@one.name.to_sym => @one, @two.name.to_sym => @two}, Scenarios[:sample])
52
+ end
53
+
54
+ end
55
+
56
+ context "a scenario with a definition" do
57
+ setup do
58
+ Scenarios.clear
59
+ Scenarios::Base.name(:sample)
60
+ @one = Scenarios::Base.instance_eval %[
61
+ scenario(:one) do
62
+ define :foo do
63
+ { :bar => :baz }
64
+ end
65
+ end
66
+ ]
67
+ end
68
+
69
+ should "add definition to scenerio object" do
70
+ assert_equal 1, @one.definitions.size
71
+ assert_equal({ :bar => :baz }, @one.definitions[:foo].call)
72
+ end
73
+
74
+ end
75
+
76
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'scenarios'
9
+
10
+ class Test::Unit::TestCase
11
+ end
@@ -0,0 +1,76 @@
1
+ require 'helper'
2
+
3
+ class TestScenario < Test::Unit::TestCase
4
+
5
+ context "an empty scenario" do
6
+
7
+ setup do
8
+ @resource = Scenarios::Scenario.new("test")
9
+ end
10
+
11
+ should "have name set" do
12
+ assert_equal('test', @resource.name)
13
+ end
14
+
15
+ should "have an empty definitions" do
16
+ assert_equal({}, @resource.definitions)
17
+ end
18
+
19
+ should "return nil on setup" do
20
+ assert_equal nil, @resource.setup!
21
+ end
22
+
23
+ end
24
+
25
+ context "a scenario with a setup block" do
26
+
27
+ setup do
28
+ @resource = Scenarios::Scenario.new("test")
29
+ @resource.setup do
30
+ @setup_called = true
31
+ end
32
+ end
33
+
34
+ should "not call setup block automatically" do
35
+ assert !@setup_called
36
+ end
37
+
38
+ should "call setup block manually, in scope of caller" do
39
+ @resource.setup!
40
+ assert @setup_called
41
+ end
42
+
43
+ end
44
+
45
+ context "a scenario with a definition" do
46
+
47
+ setup do
48
+ @resource = Scenarios::Scenario.new("test")
49
+ @resource.define(:params) do
50
+ @definition_called = true
51
+ {}
52
+ end
53
+ @resource.define(:other) do
54
+ {}
55
+ end
56
+ end
57
+
58
+ should "not call definition block automatically" do
59
+ assert !@definition_called
60
+ end
61
+
62
+ should "call definition block when referenced" do
63
+ assert_equal({}, @resource[:params])
64
+ assert @definition_called
65
+ end
66
+
67
+ should "return nil when referencing an invalid definition" do
68
+ assert_equal(nil, @resource[:winny])
69
+ end
70
+
71
+ end
72
+
73
+
74
+
75
+
76
+ end
@@ -0,0 +1,45 @@
1
+ require 'helper'
2
+
3
+ class TestScenarios < Test::Unit::TestCase
4
+
5
+ context "nothing registered" do
6
+
7
+ setup do
8
+ Scenarios.clear
9
+ end
10
+
11
+ should "return an empty hash" do
12
+ assert_equal({}, Scenarios.store)
13
+ end
14
+
15
+ should "return nil for all keys" do
16
+ assert_equal nil, Scenarios['one']
17
+ assert_equal nil, Scenarios['two']
18
+ end
19
+
20
+ end
21
+
22
+ context "a few things registered" do
23
+
24
+ setup do
25
+ Scenarios.clear
26
+ @world = stub(:name => :world)
27
+ @baz = stub(:name => :baz)
28
+ @foo = stub(:name => :foo)
29
+ Scenarios.register("foo", @foo)
30
+ Scenarios.register("foo", @baz)
31
+ Scenarios.register("hello", @world)
32
+ end
33
+
34
+ should "return correct store" do
35
+ assert_equal({:foo => {:foo => @foo, :baz => @baz}, :hello => {:world => @world}}, Scenarios.store)
36
+ end
37
+
38
+ should "return correct vals for keys" do
39
+ assert_equal({:foo => @foo, :baz => @baz}, Scenarios[:foo])
40
+ assert_equal({:world => @world}, Scenarios[:hello])
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,74 @@
1
+ require 'helper'
2
+
3
+
4
+
5
+ class TestSystem < Test::Unit::TestCase
6
+
7
+ context "A scenarios subclass" do
8
+
9
+ setup do
10
+ Scenarios.clear
11
+
12
+ class SampleScenarios < Scenarios::Base
13
+ name "sample"
14
+
15
+ scenario :one do
16
+ setup do
17
+ @foo = :bar
18
+ end
19
+
20
+ define :params do
21
+ { :foo => @foo }
22
+ end
23
+
24
+ define :env do
25
+ { :one => 1, :two => 2, :three => 3 }
26
+ end
27
+
28
+ end.setup!
29
+
30
+ scenario :two do
31
+ setup do
32
+ @foo = :baz
33
+ end
34
+
35
+ define :params do
36
+ { :foo => @foo }
37
+ end
38
+
39
+ end.setup!
40
+ end
41
+
42
+ class MoreScenarios < Scenarios::Base
43
+ name "more"
44
+
45
+ scenario :three do
46
+ setup do
47
+ @foo = :what
48
+ end
49
+
50
+ define :params do
51
+ { :foo => @foo }
52
+ end
53
+ end.setup!
54
+ end
55
+
56
+ end
57
+
58
+ should "register group with module" do
59
+ assert Scenarios[:sample]
60
+ end
61
+
62
+ should "register scenarios with module" do
63
+ assert_equal 2, Scenarios[:sample].size
64
+ end
65
+
66
+ should "have scenarios return correct values " do
67
+ assert_equal({:foo => :bar}, Scenarios[:sample][:one][:params])
68
+ assert_equal({ :one => 1, :two => 2, :three => 3 }, Scenarios[:sample][:one][:env])
69
+ assert_equal({:foo => :baz}, Scenarios[:sample][:two][:params])
70
+ end
71
+
72
+ end
73
+
74
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scenarios
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Starr Horne
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-13 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: shoulda
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: mocha
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :development
43
+ version_requirements: *id002
44
+ description: Factories on steroids.
45
+ email: starr@chromahq.com
46
+ executables: []
47
+
48
+ extensions: []
49
+
50
+ extra_rdoc_files:
51
+ - LICENSE
52
+ - README.rdoc
53
+ files:
54
+ - .document
55
+ - .gitignore
56
+ - LICENSE
57
+ - README.rdoc
58
+ - Rakefile
59
+ - lib/scenarios.rb
60
+ - lib/scenarios/base.rb
61
+ - lib/scenarios/proc_extensions.rb
62
+ - lib/scenarios/scenario.rb
63
+ - test/base_test.rb
64
+ - test/helper.rb
65
+ - test/scenario_test.rb
66
+ - test/scenarios_test.rb
67
+ - test/system_test.rb
68
+ has_rdoc: true
69
+ homepage: http://github.com/starrhorne/scenarios
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --charset=UTF-8
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ requirements: []
92
+
93
+ rubyforge_project:
94
+ rubygems_version: 1.3.6
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Manage scenarios for testing
98
+ test_files:
99
+ - test/base_test.rb
100
+ - test/helper.rb
101
+ - test/scenario_test.rb
102
+ - test/scenarios_test.rb
103
+ - test/system_test.rb