alfred_rails 0.0.1.alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.travis.yml +20 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +209 -0
  7. data/Rakefile +5 -0
  8. data/alfred_rails.gemspec +45 -0
  9. data/app/assets/javascripts/alfred/sinon_adapter.js.coffee +35 -0
  10. data/app/assets/javascripts/alfred.js.coffee +61 -0
  11. data/bin/alfred +6 -0
  12. data/lib/alfred_rails/command_line.rb +62 -0
  13. data/lib/alfred_rails/configuration.rb +157 -0
  14. data/lib/alfred_rails/definition.rb +129 -0
  15. data/lib/alfred_rails/fixture_file.rb +70 -0
  16. data/lib/alfred_rails/mock.rb +14 -0
  17. data/lib/alfred_rails/rails.rb +14 -0
  18. data/lib/alfred_rails/registry.rb +84 -0
  19. data/lib/alfred_rails/request.rb +38 -0
  20. data/lib/alfred_rails/runner.rb +116 -0
  21. data/lib/alfred_rails/scenario.rb +99 -0
  22. data/lib/alfred_rails/scenario_dsl.rb +83 -0
  23. data/lib/alfred_rails/ui.rb +90 -0
  24. data/lib/alfred_rails/version.rb +3 -0
  25. data/lib/alfred_rails.rb +99 -0
  26. data/lib/generators/alfred/controller/controller_generator.rb +24 -0
  27. data/lib/generators/alfred/controller/templates/alfred.erb +7 -0
  28. data/lib/generators/alfred/install/install_generator.rb +85 -0
  29. data/lib/generators/alfred/install/templates/alfred_helper.erb +23 -0
  30. data/lib/tasks/alfred.rake +6 -0
  31. data/spec/command_line_spec.rb +64 -0
  32. data/spec/configuration_spec.rb +86 -0
  33. data/spec/definition_spec.rb +135 -0
  34. data/spec/fixture_file_spec.rb +60 -0
  35. data/spec/fixtures/database.yml +4 -0
  36. data/spec/generators/controller/controller_generator_spec.rb +23 -0
  37. data/spec/generators/install/install_generator_spec.rb +56 -0
  38. data/spec/javascripts/alfred/sinon_adapter_spec.js.coffee +33 -0
  39. data/spec/javascripts/alfred_spec.js.coffee +46 -0
  40. data/spec/javascripts/spec_helper.coffee +5 -0
  41. data/spec/mock_spec.rb +18 -0
  42. data/spec/registry_spec.rb +78 -0
  43. data/spec/request_spec.rb +32 -0
  44. data/spec/runner_spec.rb +179 -0
  45. data/spec/scenario_dsl_spec.rb +62 -0
  46. data/spec/scenario_spec.rb +24 -0
  47. data/spec/spec_helper.rb +24 -0
  48. data/spec/support/application.rb +21 -0
  49. data/spec/support/controllers/api/v1/posts_controller.rb +14 -0
  50. data/spec/support/controllers/api/v1/users_controller.rb +14 -0
  51. data/spec/support/lib/response_proxy.rb +11 -0
  52. data/spec/support/models/post.rb +1 -0
  53. data/spec/support/models/user.rb +1 -0
  54. data/spec/support/rails.rb +14 -0
  55. data/spec/support/routes.rb +9 -0
  56. data/spec/support/schema.rb +16 -0
  57. data/spec/teaspoon_env.rb +14 -0
  58. data/spec/ui_spec.rb +72 -0
  59. metadata +316 -0
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+ require 'generators/alfred/controller/controller_generator'
3
+
4
+ describe Alfred::Generators::ControllerGenerator do
5
+
6
+ destination File.expand_path("../../../../tmp", __FILE__)
7
+
8
+ before do
9
+ prepare_destination
10
+ run_generator %w(api/v1/posts)
11
+ end
12
+
13
+ subject { file('spec/alfreds/api/v1/posts_controller.rb') }
14
+
15
+ it 'should exist' do
16
+ subject.should exist
17
+ end
18
+
19
+ it 'should contain controller name' do
20
+ File.read(subject).should include('controller Api::V1::PostsController')
21
+ end
22
+
23
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+ require 'generators/alfred/install/install_generator'
3
+
4
+ describe Alfred::Generators::InstallGenerator do
5
+ destination File.expand_path("../../../../tmp", __FILE__)
6
+
7
+ before { prepare_destination }
8
+
9
+ it "generates spec/alfred_helper.rb" do
10
+ run_generator
11
+ file('spec/alfred_helper.rb').should exist
12
+ end
13
+
14
+ describe 'conditions' do
15
+
16
+ before(:each) do
17
+ stub_class = Alfred::Generators::InstallGenerator.any_instance
18
+ stub_class.stub(:mock_with).and_return(:rspec)
19
+ stub_class.stub(:devise_defined?).and_return(false)
20
+ stub_class.stub(:factory_girl_defined?).and_return(false)
21
+ end
22
+
23
+ subject { File.read(file('spec/alfred_helper.rb')) }
24
+
25
+ it "sets mocking framework to rspec if defined" do
26
+ run_generator
27
+ subject.should include('config.mock_with :rspec')
28
+ end
29
+
30
+ it "sets mocking framework to test_unit if rspec is not defined" do
31
+ Alfred::Generators::InstallGenerator.any_instance.stub(:mock_with).and_return(:test_unit)
32
+ run_generator
33
+ subject.should include('config.mock_with :test_unit')
34
+ end
35
+
36
+ it 'should include Devise::TestHelpers if Devise is defined' do
37
+ Alfred::Generators::InstallGenerator.any_instance.stub(:devise_defined?).and_return(true)
38
+ run_generator
39
+ subject.should include('config.include Devise::TestHelpers')
40
+ end
41
+
42
+ it 'should include FactoryGirl::Syntax::Methods if FactoryGirl is defined?' do
43
+ Alfred::Generators::InstallGenerator.any_instance.stub(:factory_girl_defined?).and_return(true)
44
+ run_generator
45
+ subject.should include('config.include FactoryGirl::Syntax::Methods')
46
+ end
47
+
48
+ it 'should require "factory_girl" if FactoryGirl is defined?' do
49
+ Alfred::Generators::InstallGenerator.any_instance.stub(:factory_girl_defined?).and_return(true)
50
+ run_generator
51
+ subject.should include('require "factory_girl"')
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1,33 @@
1
+ #= require alfred
2
+ #= require alfred/sinon_adapter
3
+
4
+ describe 'Alfred.SinonAdapter', ->
5
+
6
+ before ->
7
+ @action = 'posts/index'
8
+ @name = 'default'
9
+ @response = JSON.stringify({ posts: [{ title: 'Hi there' }] })
10
+ @meta = {
11
+ path: 'posts'
12
+ method: 'GET'
13
+ status: 200
14
+ type: 'application/json'
15
+ }
16
+
17
+ Alfred.register({
18
+ action: @action,
19
+ name: @name,
20
+ meta: @meta,
21
+ response: @response
22
+ })
23
+
24
+ @scenario = Alfred.scenarios[@action][@name]
25
+
26
+ describe 'serve', ->
27
+
28
+ it 'should setup a sinon fakeServer', ->
29
+ server = Alfred.SinonAdapter.serve(@action, @name)
30
+ response = server.responses[0]
31
+
32
+ response.method.should.equal('GET')
33
+ response.url.should.equal('posts')
@@ -0,0 +1,46 @@
1
+ #= require alfred
2
+
3
+ describe 'Alfred', ->
4
+
5
+ before ->
6
+ @action = 'posts/index'
7
+ @name = 'default'
8
+ @response = JSON.stringify({ posts: [{ title: 'Hi there' }] })
9
+ @meta = {
10
+ path: 'posts'
11
+ method: 'GET'
12
+ status: 200
13
+ type: 'application/json'
14
+ }
15
+
16
+ Alfred.register({
17
+ action: @action,
18
+ name: @name,
19
+ meta: @meta,
20
+ response: @response
21
+ })
22
+
23
+ @scenario = Alfred.scenarios[@action][@name]
24
+
25
+ describe 'register', ->
26
+
27
+ it 'should register scenario', ->
28
+ @scenario.action.should.equal(@action)
29
+ @scenario.name.should.equal(@name)
30
+ @scenario.response.should.equal(@response)
31
+ @scenario.meta.should.equal(@meta)
32
+
33
+ describe 'fetch', ->
34
+
35
+ it 'should fetch scenario', ->
36
+ Alfred.fetch(@action, @name).should.equal(@scenario)
37
+
38
+ describe 'serve', ->
39
+
40
+ it 'should serve scenario response', ->
41
+ Alfred.serve(@action, @name).should.equal(@response)
42
+
43
+ describe '_name', ->
44
+
45
+ it 'should underscore name', ->
46
+ Alfred._name('this is a name').should.equal('this_is_a_name')
@@ -0,0 +1,5 @@
1
+ //=require support/sinon
2
+ //=require support/chai
3
+ //=require support/sinon-chai
4
+
5
+ should = chai.should()
data/spec/mock_spec.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Alfred::Mock do
4
+
5
+ describe '#new' do
6
+
7
+ it 'should initialize mocking framework based on mock_with in configuration' do
8
+ Alfred.configure do |config|
9
+ config.mock_with :rspec
10
+ end
11
+
12
+ RSpec::Mocks.should_receive(:setup)
13
+ Alfred::Mock.new
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ describe Alfred::Registry do
4
+
5
+ let!(:registry) { Alfred::Registry.new }
6
+ after(:each) { registry.clear! }
7
+
8
+ it 'should initialize with an empty hash for items' do
9
+ registry.items.should == {}
10
+ end
11
+
12
+ describe '#register' do
13
+
14
+ it 'should add an item to the registry' do
15
+ registry.register('identifier', 'item')
16
+ registry.items.should == { 'identifier' => ['item'] }
17
+
18
+ registry.register('identifier', 'item2')
19
+ registry.items.should == { 'identifier' => ['item', 'item2'] }
20
+ end
21
+
22
+ end
23
+
24
+ describe '#registered?' do
25
+
26
+ it 'should return true if identifier is found' do
27
+ registry.register('identifier', 'item')
28
+ registry.registered?('identifier').should be_true
29
+ end
30
+
31
+ it 'should return false if identifier is not found' do
32
+ registry.register('identifier', 'item')
33
+ registry.registered?('identifier1').should be_false
34
+ end
35
+
36
+ end
37
+
38
+ describe '#find' do
39
+
40
+ it 'should find items by identifier' do
41
+ registry.register('identifier', 'item')
42
+ registry.find('identifier').should == ['item']
43
+ end
44
+
45
+ it 'should return nil if identifier not registered' do
46
+ registry.register('identifier', 'item')
47
+ registry.find('identifier1').should be_nil
48
+ end
49
+
50
+ it 'should support [] syntax' do
51
+ registry.register('identifier', 'item')
52
+ registry['identifier'].should == ['item']
53
+ end
54
+
55
+ end
56
+
57
+ describe '#all' do
58
+
59
+ it 'should return all items in registry' do
60
+ registry.register('identifier', 'item')
61
+ registry.register('identifier1', 'item1')
62
+
63
+ registry.all.should == ['item', 'item1']
64
+ end
65
+
66
+ end
67
+
68
+ describe 'clear!' do
69
+
70
+ it 'should clear all items' do
71
+ registry.register('identifier', 'item')
72
+ registry.clear!
73
+ registry.items.should == {}
74
+ end
75
+
76
+ end
77
+
78
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Alfred::Request do
4
+
5
+ let!(:request) { Alfred::Request.new('test') }
6
+
7
+ describe '#set_controller' do
8
+
9
+ it 'should set @controller' do
10
+ request.set_controller Api::V1::UsersController
11
+ request.instance_variable_get(:@controller).should be_kind_of(Api::V1::UsersController)
12
+ end
13
+
14
+ it 'should set @routes' do
15
+ request.set_controller Api::V1::UsersController
16
+ request.instance_variable_get(:@routes).should == Rails.application.routes
17
+ end
18
+
19
+ end
20
+
21
+ describe '#_setup' do
22
+
23
+ it 'should execute the given block' do
24
+ request._setup { User.create(:name => 'John Doe') }
25
+ User.count.should == 1
26
+ request._setup { User.create(:name => 'John Doe') }
27
+ User.count.should == 2
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,179 @@
1
+ require 'spec_helper'
2
+
3
+ describe Alfred::Runner do
4
+
5
+ before do
6
+ @log = ""
7
+ STDOUT.stub(:print) { |s| @log << s }
8
+ end
9
+
10
+ after(:each) { Alfred.registry.clear! }
11
+
12
+ describe '#initialize' do
13
+
14
+ describe 'with files supplied' do
15
+
16
+ before(:each) { Alfred::Runner.any_instance.stub(:run).and_return(true) }
17
+ subject { Alfred::Runner.new(['spec/alfreds/api/v1/users_controller.rb', 'spec/alfreds/non_existing.rb']) }
18
+
19
+ it 'should assign @files with files' do
20
+ subject.files.should == ['spec/alfreds/api/v1/users_controller.rb', 'spec/alfreds/non_existing.rb']
21
+ end
22
+
23
+ it 'should assign @controllers with controller names' do
24
+ subject.controllers.should == ['api/v1/users_controller', 'non_existing']
25
+ end
26
+
27
+ it 'should assign @matches with matching alfred definitions' do
28
+ Alfred.define do
29
+ controller Api::V1::UsersController do
30
+ scenario 'foo'
31
+ end
32
+ controller Api::V1::PostsController do
33
+ scenario 'bar'
34
+ end
35
+ end
36
+ subject.matches.should == ["spec/alfreds/api/v1/users_controller.rb"]
37
+ end
38
+
39
+ it "should assign @scenarios with the scenarios for a controller" do
40
+ Alfred.define do
41
+ controller Api::V1::UsersController do
42
+ scenario 'foo'
43
+ scenario 'bar'
44
+ end
45
+ controller Api::V1::PostsController do
46
+ scenario 'bar'
47
+ end
48
+ end
49
+ subject.scenarios.size.should == 2
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+
56
+ describe 'without files supplied' do
57
+
58
+ before(:each) { Alfred::Runner.any_instance.stub(:run).and_return(true) }
59
+ after(:each) { Alfred.registry.clear! }
60
+
61
+ subject { Alfred::Runner.new }
62
+
63
+ it '@files should be empty' do
64
+ subject.files.should be_empty
65
+ end
66
+
67
+ it '@controllers should be nil' do
68
+ subject.controllers.should be_nil
69
+ end
70
+
71
+ it '@matches should be nil' do
72
+ Alfred.define do
73
+ controller Api::V1::UsersController do
74
+ scenario 'foo'
75
+ end
76
+ controller Api::V1::PostsController do
77
+ scenario 'bar'
78
+ end
79
+ end
80
+ subject.matches.should be_nil
81
+ end
82
+
83
+ it "should assign @scenarios with all scenario's" do
84
+ Alfred.define do
85
+ controller Api::V1::UsersController do
86
+ scenario 'foo'
87
+ scenario 'bar'
88
+ end
89
+ controller Api::V1::PostsController do
90
+ scenario 'bar'
91
+ end
92
+ end
93
+ subject.scenarios.size.should == 3
94
+ end
95
+
96
+ end
97
+
98
+ describe '#run' do
99
+
100
+ before do
101
+ Alfred::Scenario.any_instance.stub(:run).and_return(true)
102
+ ProgressBar.stub(:create).and_return(ProgressBar.new)
103
+ ProgressBar.any_instance.stub(:increment).and_return(true)
104
+ end
105
+
106
+ subject { Alfred::Runner.new(['spec/alfreds/api/v1/users_controller.rb', 'spec/alfreds/non_existing.rb']) }
107
+
108
+ context 'with matching files' do
109
+
110
+ before(:each) do
111
+ Alfred.define do
112
+ controller Api::V1::UsersController do
113
+ scenario 'foo'
114
+ scenario 'bar'
115
+ end
116
+ controller Api::V1::PostsController do
117
+ scenario 'bar'
118
+ end
119
+ end
120
+ end
121
+
122
+ it 'should notify about matches' do
123
+ subject
124
+ @log.should include('spec/alfreds/api/v1/users_controller.rb')
125
+ @log.should_not include('spec/alfreds/non_existing.rb')
126
+ end
127
+
128
+ end
129
+
130
+ context 'without matching files' do
131
+
132
+ before(:each) do
133
+ Alfred.define do
134
+ controller Api::V1::UsersController do
135
+ scenario 'foo'
136
+ scenario 'bar'
137
+ end
138
+ controller Api::V1::PostsController do
139
+ scenario 'bar'
140
+ end
141
+ end
142
+ end
143
+
144
+ it 'should not run' do
145
+ Alfred.define do
146
+ controller Api::V1::PostsController do
147
+ scenario 'bar'
148
+ end
149
+ end
150
+ subject.should_not_receive(:run)
151
+ end
152
+
153
+ end
154
+
155
+ context 'without files' do
156
+
157
+ subject { Alfred::Runner.new }
158
+
159
+ before(:each) do
160
+ Alfred.define do
161
+ controller Api::V1::UsersController do
162
+ scenario 'foo'
163
+ scenario 'bar'
164
+ end
165
+ controller Api::V1::PostsController do
166
+ scenario 'bar'
167
+ end
168
+ end
169
+ end
170
+
171
+ it 'should notify about running all files' do
172
+ subject
173
+ @log.should include("Running all scenario's")
174
+ end
175
+ end
176
+
177
+ end
178
+
179
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe Alfred::ScenarioDSL do
4
+
5
+ let!(:scenario) { Alfred::Scenario.new('foo bar') }
6
+ let!(:dsl) { Alfred::ScenarioDSL.new(scenario) }
7
+
8
+ describe '#setup' do
9
+
10
+ it 'should add setup blocks to definition' do
11
+ dsl.setup { User.new }
12
+ scenario.setups.size.should == 1
13
+ scenario.setups.first.should be_kind_of(Proc)
14
+
15
+ dsl.setup { User.new }
16
+ scenario.setups.size.should == 2
17
+ end
18
+
19
+ end
20
+
21
+ describe '#controller' do
22
+
23
+ it 'should set the controller to test' do
24
+ dsl.controller Api::V1::UsersController
25
+ scenario.controller.should == Api::V1::UsersController
26
+ end
27
+
28
+ end
29
+
30
+ describe 'request methods' do
31
+
32
+ it 'should respond to all request methods' do
33
+ dsl.should respond_to(:get)
34
+ dsl.should respond_to(:post)
35
+ dsl.should respond_to(:put)
36
+ dsl.should respond_to(:patch)
37
+ dsl.should respond_to(:head)
38
+ dsl.should respond_to(:delete)
39
+ end
40
+
41
+ end
42
+
43
+ describe '#setup_request_data' do
44
+
45
+ it 'should set method' do
46
+ dsl.setup_request_data(:get, :show, :id => 1)
47
+ scenario.method.should == :get
48
+ end
49
+
50
+ it 'should set action' do
51
+ dsl.setup_request_data(:get, :show, :id => 1)
52
+ scenario.action.should == :show
53
+ end
54
+
55
+ it 'should set params' do
56
+ dsl.setup_request_data(:get, :show, :id => 1)
57
+ scenario.params.should == { :id => 1 }
58
+ end
59
+
60
+ end
61
+
62
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Alfred::Scenario do
4
+
5
+ let!(:scenario) { Alfred::Scenario.new('foo bar') }
6
+
7
+ it 'should downcase the name on initialize' do
8
+ scenario.name.should == 'foo_bar'
9
+ end
10
+
11
+ it 'should initialize with empty setups array' do
12
+ scenario.setups.should == []
13
+ end
14
+
15
+ describe '#controller_name' do
16
+
17
+ it 'should return underscored controller name' do
18
+ scenario.controller = Api::V1::UsersController
19
+ scenario.controller_name.should == 'api/v1/users_controller'
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,24 @@
1
+ ## Test coverage
2
+
3
+ require 'coveralls'
4
+ Coveralls.wear!
5
+
6
+ ## Rails
7
+
8
+ require 'support/application'
9
+
10
+ ## Rspec
11
+
12
+ require 'rspec'
13
+ require 'ammeter/init'
14
+ require 'rspec/autorun'
15
+
16
+ ## Load alfred_rails
17
+
18
+ require 'alfred_rails'
19
+
20
+ ## Make sure registry is empty after each example
21
+
22
+ RSpec.configure do |config|
23
+ config.after(:each) { Alfred.registry.clear! }
24
+ end
@@ -0,0 +1,21 @@
1
+ require 'rails/all'
2
+
3
+ module Rails
4
+ class Application
5
+ class Configuration
6
+ def database_configuration
7
+ YAML::load File.read('spec/fixtures/database.yml')
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+ ## Load support files
14
+
15
+ load 'spec/support/rails.rb'
16
+ load 'spec/support/routes.rb'
17
+ load 'spec/support/schema.rb'
18
+
19
+ [:controllers, :models, :lib].each do |dir|
20
+ Dir["spec/support/#{dir}/**/*.rb"].each { |f| load f }
21
+ end
@@ -0,0 +1,14 @@
1
+ module Api
2
+ module V1
3
+ class PostsController < ActionController::Base
4
+ respond_to :xml, :json
5
+
6
+ def index
7
+ @posts = Post.all
8
+
9
+ respond_with(@posts)
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Api
2
+ module V1
3
+ class UsersController < ActionController::Base
4
+ respond_to :xml, :json
5
+
6
+ def index
7
+ @users = User.all
8
+
9
+ respond_with(@users)
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ class ResponseProxy
2
+
3
+ attr_accessor :content_type
4
+
5
+ def initialize(attributes={})
6
+ attributes.keys.each do |key|
7
+ instance_variable_set("@#{key}".to_sym, attributes[key])
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1 @@
1
+ class Post < ActiveRecord::Base; end
@@ -0,0 +1 @@
1
+ class User < ActiveRecord::Base; end
@@ -0,0 +1,14 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+
3
+ module AlfredRailsTest
4
+ class Application < Rails::Application
5
+ config.eager_load = false
6
+ config.active_support.deprecation = :log
7
+ config.secret_token = '8f62080da9dd946a6555635c46fbebfb'
8
+
9
+ if Rails::VERSION::STRING >= "4.0.0"
10
+ config.secret_key_base = '8f62080da9dd946a6555635c46fbebfb'
11
+ end
12
+ end
13
+ end
14
+ AlfredRailsTest::Application.initialize!
@@ -0,0 +1,9 @@
1
+ AlfredRailsTest::Application.routes.draw do
2
+
3
+ namespace :api do
4
+ namespace :v1 do
5
+ resources :users
6
+ end
7
+ end
8
+
9
+ end