Browse files

Dealing with inflector quirks.

Support for test_framework.
  • Loading branch information...
1 parent 965b975 commit 28c82d0897ef5ccbc128f2073882046c379572ec Brian Ploetz committed Feb 27, 2012
Showing with 480 additions and 81 deletions.
  1. +13 −7 lib/generators/versionist/new_api_version/USAGE
  2. +66 −7 lib/generators/versionist/new_api_version/new_api_version_generator.rb
  3. +5 −0 lib/generators/versionist/new_api_version/templates/base_controller_spec.rb
  4. +8 −0 lib/generators/versionist/new_api_version/templates/base_controller_test.rb
  5. +2 −0 lib/generators/versionist/new_api_version/templates/base_presenter.rb
  6. +5 −0 lib/generators/versionist/new_api_version/templates/base_presenter_spec.rb
  7. +8 −0 lib/generators/versionist/new_api_version/templates/base_presenter_test.rb
  8. +2 −0 lib/generators/versionist/new_controller/USAGE
  9. +19 −3 lib/generators/versionist/new_controller/new_controller_generator.rb
  10. +5 −0 lib/generators/versionist/new_controller/templates/new_controller_spec.rb
  11. +9 −0 lib/generators/versionist/new_controller/templates/new_controller_test.rb
  12. +4 −2 lib/generators/versionist/new_presenter/USAGE
  13. +21 −2 lib/generators/versionist/new_presenter/new_presenter_generator.rb
  14. +1 −1 lib/generators/versionist/new_presenter/templates/new_presenter.rb
  15. +5 −0 lib/generators/versionist/new_presenter/templates/new_presenter_spec.rb
  16. +8 −0 lib/generators/versionist/new_presenter/templates/new_presenter_test.rb
  17. +1 −0 lib/versionist/configuration.rb
  18. +1 −1 lib/versionist/middleware.rb
  19. +5 −0 lib/versionist/railtie.rb
  20. +158 −23 spec/generators/new_api_version_generator_spec.rb
  21. +61 −7 spec/generators/new_controller_generator_spec.rb
  22. +73 −28 spec/generators/new_presenter_generator_spec.rb
View
20 lib/generators/versionist/new_api_version/USAGE
@@ -2,12 +2,18 @@ Description:
Creates the infrastructure for a new API version.
Example:
- rails generate versionist:new_api_version v3.4 V3_4
+ rails generate versionist:new_api_version v2.0.0 V2_0_0 header:Accept value:application/vnd.mycompany.com-v2.0.0
This will create:
- app/controllers/v3_4/
- app/controllers/v3_4/base_controller.rb
- app/presenters/v3_4/
- public/docs/v3.4/
- public/docs/v3.4/index.html
- public/docs/v3.4/style.css
+
+ route api_version(:module => "V2__0__0", :header=>"Accept", :value=>"application/vnd.mycompany.com-v2.0.0") do
+ end
+ create app/controllers/v2_0_0
+ create app/controllers/v2_0_0/base_controller.rb
+ create app/presenters/v2_0_0
+ create app/presenters/v2_0_0/base_presenter.rb
+ create public/docs/v2.0.0
+ create public/docs/v2.0.0/index.html
+ create public/docs/v2.0.0/style.css
+
+ In addition this will create tests for base_controller.rb and base_presenter.rb using the test_framework currently configured in your Rails app.
View
73 lib/generators/versionist/new_api_version/new_api_version_generator.rb
@@ -1,17 +1,76 @@
module Versionist
class NewApiVersionGenerator < Rails::Generators::Base
+ include InflectorFixes
+
+ desc "creates the infrastructure for a new API version"
+
source_root File.expand_path('../templates', __FILE__)
argument :version, :type => :string
argument :module_name, :type => :string
+ argument :versioning_strategy, :banner => "VERSIONING_STRATEGY_OPTIONS", :type => :hash
+
+ def add_routes
+ in_root do
+ api_version_block = /api_version.*:module\s*(=>|:)\s*("|')#{module_name_for_route(module_name)}("|').*do/
+ matching_version_blocks = File.readlines("config/routes.rb").grep(api_version_block)
+ raise "API version already exists in config/routes.rb" if !matching_version_blocks.empty?
+ versioning_strategy.symbolize_keys!
+ route "api_version(:module => \"#{module_name_for_route(module_name)}\", #{versioning_strategy.to_s.gsub(/[\{\}]/, '')}) do\n end"
+ end
+ end
+
+ def add_controller_base
+ in_root do
+ empty_directory "app/controllers/#{module_name_for_path(module_name)}"
+ template 'base_controller.rb', File.join("app", "controllers", "#{module_name_for_path(module_name)}", "base_controller.rb")
+ end
+ end
+
+ # due to the inflector quirks we can't use hook_for :test_framework
+ def add_controller_base_test
+ in_root do
+ case Versionist.configuration.configured_test_framework
+ when :test_unit
+ empty_directory "test/functional/#{module_name_for_path(module_name)}"
+ template 'base_controller_test.rb', File.join("test", "functional", "#{module_name_for_path(module_name)}", "base_controller_test.rb")
+ when :rspec
+ empty_directory "spec/controllers/#{module_name_for_path(module_name)}"
+ template 'base_controller_spec.rb', File.join("spec", "controllers", "#{module_name_for_path(module_name)}", "base_controller_spec.rb")
+ else
+ say "Unsupported test_framework: #{Versionist.configuration.configured_test_framework}"
+ end
+ end
+ end
+
+ def add_presenters_base
+ in_root do
+ empty_directory "app/presenters/#{module_name_for_path(module_name)}"
+ template 'base_presenter.rb', File.join("app", "presenters", "#{module_name_for_path(module_name)}", "base_presenter.rb")
+ end
+ end
+
+ def add_presenter_test
+ in_root do
+ case Versionist.configuration.configured_test_framework
+ when :test_unit
+ empty_directory "test/presenters/#{module_name_for_path(module_name)}"
+ template 'base_presenter_test.rb', File.join("test", "presenters", "#{module_name_for_path(module_name)}", "base_presenter_test.rb")
+ when :rspec
+ empty_directory "spec/presenters/#{module_name_for_path(module_name)}"
+ template 'base_presenter_spec.rb', File.join("spec", "presenters", "#{module_name_for_path(module_name)}", "base_presenter_spec.rb")
+ else
+ say "Unsupported test_framework: #{Versionist.configuration.configured_test_framework}"
+ end
+ end
+ end
- def new_api_version
- empty_directory "app/controllers/#{module_name.underscore}"
- template 'base_controller.rb', File.join("app", "controllers", "#{module_name.underscore}", "base_controller.rb")
- empty_directory "app/presenters/#{module_name.underscore}"
- empty_directory "public/docs/#{version}"
- template 'docs_index.rb', File.join("public", "docs", "#{version}", "index.html")
- template 'docs_style.rb', File.join("public", "docs", "#{version}", "style.css")
+ def add_documentation_base
+ in_root do
+ empty_directory "public/docs/#{version}"
+ template 'docs_index.rb', File.join("public", "docs", "#{version}", "index.html")
+ template 'docs_style.rb', File.join("public", "docs", "#{version}", "style.css")
+ end
end
end
end
View
5 lib/generators/versionist/new_api_version/templates/base_controller_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe <%= module_name %>::BaseController do
+
+end
View
8 lib/generators/versionist/new_api_version/templates/base_controller_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class <%= module_name %>::BaseControllerTest < ActionController::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
View
2 lib/generators/versionist/new_api_version/templates/base_presenter.rb
@@ -0,0 +1,2 @@
+class <%= module_name %>::BasePresenter
+end
View
5 lib/generators/versionist/new_api_version/templates/base_presenter_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe <%= module_name %>::BasePresenter do
+
+end
View
8 lib/generators/versionist/new_api_version/templates/base_presenter_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class <%= module_name %>::BaseControllerTest < Test::Unit::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
View
2 lib/generators/versionist/new_controller/USAGE
@@ -6,3 +6,5 @@ Example:
This will create:
app/controllers/V2_0_0/test_controller.rb
+
+ In addition this will create a test for the new controller using the test_framework currently configured in your Rails app.
View
22 lib/generators/versionist/new_controller/new_controller_generator.rb
@@ -1,16 +1,18 @@
module Versionist
class NewControllerGenerator < Rails::Generators::NamedBase
+ include InflectorFixes
+
desc "creates a new controller for an existing API version"
source_root File.expand_path('../templates', __FILE__)
argument :module_name, :type => :string
def new_controller
in_root do
- raise "API module namespace #{module_name} doesn't exist. Please run \'rails generate versionist:new_api_version\' generator first" if !File.exists?("app/controllers/#{module_name.underscore}")
- template 'new_controller.rb', File.join("app", "controllers", "#{module_name.underscore}", "#{file_name}_controller.rb")
+ raise "API module namespace #{module_name} doesn't exist. Please run \'rails generate versionist:new_api_version\' generator first" if !File.exists?("app/controllers/#{module_name_for_path(module_name)}")
+ template 'new_controller.rb', File.join("app", "controllers", "#{module_name_for_path(module_name)}", "#{file_name}_controller.rb")
- api_version_block = /api_version.*:module\s*(=>|:)\s*("|')#{module_name.gsub(/_{1}/, "__")}("|').*do/
+ api_version_block = /api_version.*:module\s*(=>|:)\s*("|')#{module_name_for_route(module_name)}("|').*do/
new_route = " resources :#{file_name}\n"
matching_version_blocks = File.readlines("config/routes.rb").grep(api_version_block)
if matching_version_blocks.empty?
@@ -23,5 +25,19 @@ def new_controller
end
end
end
+
+ # due to the inflector quirks we can't use hook_for :test_framework
+ def new_controller_test
+ in_root do
+ case Versionist.configuration.configured_test_framework
+ when :test_unit
+ template 'new_controller_test.rb', File.join("test", "functional", "#{module_name_for_path(module_name)}", "#{file_name}_controller_test.rb")
+ when :rspec
+ template 'new_controller_spec.rb', File.join("spec", "controllers", "#{module_name_for_path(module_name)}", "#{file_name}_controller_spec.rb")
+ else
+ say "Unsupported test_framework: #{Versionist.configuration.configured_test_framework}"
+ end
+ end
+ end
end
end
View
5 lib/generators/versionist/new_controller/templates/new_controller_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe <%= module_name %>::<%= class_name%>Controller do
+
+end
View
9 lib/generators/versionist/new_controller/templates/new_controller_test.rb
@@ -0,0 +1,9 @@
+require 'test_helper'
+
+class <%= module_name %>::<%= class_name%>Controller < ActionController::TestCase
+
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
View
6 lib/generators/versionist/new_presenter/USAGE
@@ -2,7 +2,9 @@ Description:
Creates a new presenter class with the given name under the given version
Example:
- rails generate versionist:new_presenter foo v3.4
+ rails generate versionist:new_presenter foo V2_0_0
This will create:
- app/presenters/v3.4/foo_presenter.rb
+ app/presenters/v2_0_0/foo_presenter.rb
+
+ In addition this will create a test for the new controller using the test_framework currently configured in your Rails app.
View
23 lib/generators/versionist/new_presenter/new_presenter_generator.rb
@@ -1,11 +1,30 @@
module Versionist
class NewPresenterGenerator < Rails::Generators::NamedBase
+ include InflectorFixes
+
+ desc "creates a new presenter for an existing API version"
source_root File.expand_path('../templates', __FILE__)
- argument :version, :type => :string
+ argument :module_name, :type => :string
def new_presenter
- template 'new_presenter.rb', File.join("app", "presenters", "#{version.underscore}", "#{file_name}_presenter.rb")
+ in_root do
+ raise "API module namespace #{module_name} doesn't exist. Please run \'rails generate versionist:new_api_version\' generator first" if !File.exists?("app/presenters/#{module_name_for_path(module_name)}")
+ template 'new_presenter.rb', File.join("app", "presenters", "#{module_name_for_path(module_name)}", "#{file_name}_presenter.rb")
+ end
+ end
+
+ def new_presenter_test
+ in_root do
+ case Versionist.configuration.configured_test_framework
+ when :test_unit
+ template 'new_presenter_test.rb', File.join("test", "presenters", "#{module_name_for_path(module_name)}", "#{file_name}_presenter_test.rb")
+ when :rspec
+ template 'new_presenter_spec.rb', File.join("spec", "presenters", "#{module_name_for_path(module_name)}", "#{file_name}_presenter_spec.rb")
+ else
+ say "Unsupported test_framework: #{Versionist.configuration.configured_test_framework}"
+ end
+ end
end
end
end
View
2 lib/generators/versionist/new_presenter/templates/new_presenter.rb
@@ -1,4 +1,4 @@
-class <%= version.camelize.gsub(/\W/, '_') %>::<%= class_name%>Presenter
+class <%= module_name %>::<%= class_name%>Presenter
def initialize(<%= file_name %>)
@<%= file_name %> = <%= file_name %>
View
5 lib/generators/versionist/new_presenter/templates/new_presenter_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe <%= module_name %>::<%= class_name%>Presenter do
+
+end
View
8 lib/generators/versionist/new_presenter/templates/new_presenter_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class <%= module_name %>::<%= class_name%>PresenterTest < Test::Unit::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
View
1 lib/versionist/configuration.rb
@@ -4,6 +4,7 @@ class Configuration
attr_accessor :default_version
attr_accessor :header_versions
attr_accessor :parameter_versions
+ attr_accessor :configured_test_framework
def initialize
@versioning_strategies ||= Array.new
View
2 lib/versionist/middleware.rb
@@ -3,7 +3,7 @@ module Versionist
# Rails inspects the `Accept` header to determine the requested format. Since an `Accept` header can have multiple values,
# Rails uses the first one present to determine the format. If your custom version header happens to be the first value
# in the `Accept` header, it would incorrectly be interpretted as the format. This middleware moves your custom version header
- # to the end of the `Accept` header so as to not interfere with this format logic in Rails.
+ # (if found) to the end of the `Accept` header so as to not interfere with this format logic in Rails.
class Middleware
ACCEPT = "Accept"
View
5 lib/versionist/railtie.rb
@@ -9,5 +9,10 @@ class Railtie < Rails::Railtie
end
config.app_middleware.use Versionist::Middleware
+
+ config.after_initialize do
+ generators = config.respond_to?(:app_generators) ? config.app_generators : config.generators
+ Versionist.configuration.configured_test_framework = generators.options[:rails][:test_framework]
+ end
end
end
View
181 spec/generators/new_api_version_generator_spec.rb
@@ -4,40 +4,92 @@
describe Versionist::NewApiVersionGenerator do
include GeneratorSpec::TestCase
+ include Versionist::InflectorFixes
destination File.expand_path("../../tmp", __FILE__)
before :each do
prepare_destination
+ ::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/controllers", __FILE__))
+ ::Dir.mkdir(::File.expand_path("../../tmp/config", __FILE__))
+ ::FileUtils.touch(::File.expand_path("../../tmp/config/routes.rb", __FILE__))
end
- {"v1" => "V1", "v2" => "V2", "v2.1" => "V2_1"}.each do |ver, mod|
+ {"v1" => "V1", "v2" => "V2", "v2.1" => "V2_1", "v20120119" => "Api::V20120119"}.each do |ver, mod|
context "#{ver} => #{mod}" do
- before :each do
- run_generator [ver, mod]
- end
+ context "api version exists" do
+ before :each do
+ ::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/controllers/#{module_name_for_path(mod)}", __FILE__))
+ ::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{module_name_for_route(mod)}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-v1\") do\n end\nend"}
+ end
+
+ after :each do
+ ::FileUtils.rm(::File.expand_path("../../tmp/config/routes.rb", __FILE__))
+ ::FileUtils.touch(::File.expand_path("../../tmp/config/routes.rb", __FILE__))
+ end
- it "should create a namespaced controller directory" do
- assert_directory "app/controllers/#{mod.underscore}"
+ it "should raise an error" do
+ lambda {
+ run_generator [ver, mod, {}]
+ }.should raise_error(RuntimeError, /API version already exists in config\/routes.rb/)
+ end
end
- it "should create a namespaced base controller" do
- assert_file "app/controllers/#{mod.underscore}/base_controller.rb", <<-CONTENTS
+ context "api version doesn't exist" do
+ [{:header => "Accept", :value => "application/vnd.mycompany.com-#{ver}"}, {:header => "X-CUSTOM-HEADER", :value => ver}, {:path => "/#{ver}"}, {:parameter => "version", :value => ver}].each do |versioning_strategy|
+ context "versioning_strategy: #{versioning_strategy.to_s}" do
+ before :each do
+ ::FileUtils.rm(::File.expand_path("../../tmp/config/routes.rb", __FILE__))
+ ::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\nend"}
+ Versionist.configuration.configured_test_framework = nil
+ run_generator [ver, mod, versioning_strategy]
+ end
+
+ after :each do
+ ::FileUtils.rm(::File.expand_path("../../tmp/config/routes.rb", __FILE__))
+ ::FileUtils.touch(::File.expand_path("../../tmp/config/routes.rb", __FILE__))
+ end
+
+ it "should add correct api_version to config/routes.rb" do
+ assert_file "config/routes.rb"
+ expected = <<-CONTENTS
+Test::Application.routes.draw do
+ api_version(:module => "#{module_name_for_route(mod)}", #{versioning_strategy.to_s.gsub(/[\{\}]/, '')}) do
+ end
+
+end
+ CONTENTS
+ assert_file "config/routes.rb", expected.chop
+ end
+
+ it "should create a namespaced controller directory" do
+ assert_directory "app/controllers/#{module_name_for_path(mod)}"
+ end
+
+ it "should create a namespaced base controller" do
+ assert_file "app/controllers/#{module_name_for_path(mod)}/base_controller.rb", <<-CONTENTS
class #{mod}::BaseController < ApplicationController
end
- CONTENTS
- end
+ CONTENTS
+ end
- it "should create a namespaced presenters directory" do
- assert_directory "app/presenters/#{mod.underscore}"
- end
+ it "should create a namespaced presenters directory" do
+ assert_directory "app/presenters/#{module_name_for_path(mod)}"
+ end
- it "should create a documentation directory" do
- assert_directory "public/docs/#{ver}"
- end
+ it "should create a namespaced base presenter" do
+ assert_file "app/presenters/#{module_name_for_path(mod)}/base_presenter.rb", <<-CONTENTS
+class #{mod}::BasePresenter
+end
+ CONTENTS
+ end
- it "should create a documentation index.html" do
- assert_file "public/docs/#{ver}/index.html", <<-CONTENTS
+ it "should create a documentation directory" do
+ assert_directory "public/docs/#{ver}"
+ end
+
+ it "should create a documentation index.html" do
+ assert_file "public/docs/#{ver}/index.html", <<-CONTENTS
<!DOCTYPE html>
<html lang="en-US">
<head>
@@ -48,13 +100,96 @@ class #{mod}::BaseController < ApplicationController
<h1>Documentation for #{ver}</h1>
</body>
</html>
- CONTENTS
- end
+ CONTENTS
+ end
- it "should create a documentation style.css" do
- assert_file "public/docs/#{ver}/style.css", <<-CONTENTS
+ it "should create a documentation style.css" do
+ assert_file "public/docs/#{ver}/style.css", <<-CONTENTS
body {background-color: #fff; color: #000;}
- CONTENTS
+ CONTENTS
+ end
+
+ context "test_framework: test_unit" do
+ before :each do
+ ::FileUtils.rm(::File.expand_path("../../tmp/config/routes.rb", __FILE__))
+ ::FileUtils.touch(::File.expand_path("../../tmp/config/routes.rb", __FILE__))
+ Versionist.configuration.configured_test_framework = :test_unit
+ run_generator [ver, mod, versioning_strategy]
+ end
+
+ it "should create a namespaced test/functional directory" do
+ assert_directory "test/functional/#{module_name_for_path(mod)}"
+ end
+
+ it "should create a namespaced base controller functional test" do
+ assert_file "test/functional/#{module_name_for_path(mod)}/base_controller_test.rb", <<-CONTENTS
+require 'test_helper'
+
+class #{mod}::BaseControllerTest < ActionController::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
+ CONTENTS
+ end
+
+ it "should create a namespaced test/presenters directory" do
+ assert_directory "test/presenters/#{module_name_for_path(mod)}"
+ end
+
+ it "should create a namespaced base presenter test" do
+ assert_file "test/presenters/#{module_name_for_path(mod)}/base_presenter_test.rb", <<-CONTENTS
+require 'test_helper'
+
+class #{mod}::BaseControllerTest < Test::Unit::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
+ CONTENTS
+ end
+ end
+
+ context "test_framework: rspec" do
+ before :each do
+ ::FileUtils.rm(::File.expand_path("../../tmp/config/routes.rb", __FILE__))
+ ::FileUtils.touch(::File.expand_path("../../tmp/config/routes.rb", __FILE__))
+ Versionist.configuration.configured_test_framework = :rspec
+ run_generator [ver, mod, versioning_strategy]
+ end
+
+ it "should create a namespaced spec/controllers directory" do
+ assert_directory "spec/controllers/#{module_name_for_path(mod)}"
+ end
+
+ it "should create a namespaced base controller controller spec" do
+ assert_file "spec/controllers/#{module_name_for_path(mod)}/base_controller_spec.rb", <<-CONTENTS
+require 'spec_helper'
+
+describe #{mod}::BaseController do
+
+end
+ CONTENTS
+ end
+
+ it "should create a namespaced spec/presenters directory" do
+ assert_directory "spec/presenters/#{module_name_for_path(mod)}"
+ end
+
+ it "should create a namespaced base presenter spec" do
+ assert_file "spec/presenters/#{module_name_for_path(mod)}/base_presenter_spec.rb", <<-CONTENTS
+require 'spec_helper'
+
+describe #{mod}::BasePresenter do
+
+end
+ CONTENTS
+ end
+ end
+ end
+ end
end
end
end
View
68 spec/generators/new_controller_generator_spec.rb
@@ -4,6 +4,7 @@
describe Versionist::NewControllerGenerator do
include GeneratorSpec::TestCase
+ include Versionist::InflectorFixes
destination File.expand_path("../../tmp", __FILE__)
@@ -27,14 +28,67 @@
{"foo" => "V1", "bar" => "V2", "foos" => "V2_1", "bazs" => "Api::V3"}.each do |name, mod|
context "#{name} => #{mod}" do
before :each do
- ::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/controllers/#{mod.underscore}", __FILE__))
- ::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{mod.gsub(/_{1}/, "__")}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-v1\") do\n end\nend"}
+ ::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/controllers/#{module_name_for_path(mod)}", __FILE__))
+ ::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{module_name_for_route(mod)}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-v1\") do\n end\nend"}
+ Versionist.configuration.configured_test_framework = nil
run_generator [name, mod]
end
it "should create a namespaced controller" do
- assert_directory "app/controllers/#{mod.underscore}"
- assert_file "app/controllers/#{mod.underscore}/#{name.underscore}_controller.rb", "class #{mod}::#{name.camelize}Controller < #{mod}::BaseController\nend\n"
+ assert_directory "app/controllers/#{module_name_for_path(mod)}"
+ assert_file "app/controllers/#{module_name_for_path(mod)}/#{name.underscore}_controller.rb", "class #{mod}::#{name.camelize}Controller < #{mod}::BaseController\nend\n"
+ end
+
+ context "test_framework: test_unit" do
+ before :each do
+ ::FileUtils.rm(::File.expand_path("../../tmp/config/routes.rb", __FILE__))
+ ::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/controllers/#{module_name_for_path(mod)}", __FILE__))
+ ::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{module_name_for_route(mod)}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-v1\") do\n end\nend"}
+ Versionist.configuration.configured_test_framework = :test_unit
+ run_generator [name, mod]
+ end
+
+ it "should create a namespaced test/functional directory" do
+ assert_directory "test/functional/#{module_name_for_path(mod)}"
+ end
+
+ it "should create a namespaced controller functional test" do
+ assert_file "test/functional/#{module_name_for_path(mod)}/#{name.underscore}_controller_test.rb", <<-CONTENTS
+require 'test_helper'
+
+class #{mod}::#{name.camelize}Controller < ActionController::TestCase
+
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
+ CONTENTS
+ end
+ end
+
+ context "test_framework: rspec" do
+ before :each do
+ ::FileUtils.rm(::File.expand_path("../../tmp/config/routes.rb", __FILE__))
+ ::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/controllers/#{module_name_for_path(mod)}", __FILE__))
+ ::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{module_name_for_route(mod)}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-v1\") do\n end\nend"}
+ Versionist.configuration.configured_test_framework = :rspec
+ run_generator [name, mod]
+ end
+
+ it "should create a namespaced spec/controllers directory" do
+ assert_directory "spec/controllers/#{module_name_for_path(mod)}"
+ end
+
+ it "should create a namespaced controller spec" do
+ assert_file "spec/controllers/#{module_name_for_path(mod)}/#{name.underscore}_controller_spec.rb", <<-CONTENTS
+require 'spec_helper'
+
+describe #{mod}::#{name.camelize}Controller do
+
+end
+ CONTENTS
+ end
end
end
end
@@ -72,16 +126,16 @@
{"foo" => "V1", "bar" => "V2", "foos" => "V2_1", "bazs" => "Api::V3"}.each do |name, mod|
context "#{name} => #{mod}" do
before :each do
- ::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/controllers/#{mod.underscore}", __FILE__))
- ::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{mod.gsub(/_{1}/, "__")}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-v1\") do\n end\nend"}
+ ::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/controllers/#{module_name_for_path(mod)}", __FILE__))
+ ::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{module_name_for_route(mod)}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-v1\") do\n end\nend"}
run_generator [name, mod]
end
it "should add the new resource to the existing scope in routes.rb" do
assert_file "config/routes.rb"
expected = <<-CONTENTS
Test::Application.routes.draw do
- api_version(:module => "#{mod.gsub(/_{1}/, "__")}", :header => "Accept", :value => "application/vnd.mycompany.com-v1") do
+ api_version(:module => "#{module_name_for_route(mod)}", :header => "Accept", :value => "application/vnd.mycompany.com-v1") do
resources :#{name}
end
end
View
101 spec/generators/new_presenter_generator_spec.rb
@@ -4,24 +4,42 @@
describe Versionist::NewPresenterGenerator do
include GeneratorSpec::TestCase
+ include Versionist::InflectorFixes
destination File.expand_path("../../tmp", __FILE__)
before :each do
prepare_destination
+ ::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/presenters", __FILE__))
+ ::Dir.mkdir(::File.expand_path("../../tmp/config", __FILE__))
+ ::FileUtils.touch(::File.expand_path("../../tmp/config/routes.rb", __FILE__))
end
- context "core logic" do
- before :each do
- run_generator %w(foo v1)
+ context "api version doesn't exist" do
+ it "should raise an error if the api version doesn't exist yet" do
+ lambda {
+ run_generator %w(v1 V1)
+ }.should raise_error(RuntimeError, /API module namespace V1 doesn't exist. Please run \'rails generate versionist:new_api_version\' generator first/)
end
+ end
+
+ context "api version exists" do
+ {"foo" => "V1", "bar" => "V2", "foos" => "V2_1", "bazs" => "Api::V3"}.each do |name, mod|
+ context "#{name} => #{mod}" do
+ before :each do
+ ::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/presenters/#{module_name_for_path(mod)}", __FILE__))
+ ::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{module_name_for_route(mod)}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-v1\") do\n end\nend"}
+ Versionist.configuration.configured_test_framework = nil
+ run_generator [name, mod]
+ end
- it "should create a namespaced presenter" do
- assert_file "app/presenters/v1/foo_presenter.rb", <<-CONTENTS
-class V1::FooPresenter
+ it "should create a namespaced presenter" do
+ assert_directory "app/presenters/#{module_name_for_path(mod)}"
+ assert_file "app/presenters/#{module_name_for_path(mod)}/#{name.underscore}_presenter.rb", <<-CONTENTS
+class #{mod}::#{name.camelize}Presenter
- def initialize(foo)
- @foo = foo
+ def initialize(#{name})
+ @#{name} = #{name}
end
def as_json(options={})
@@ -33,33 +51,60 @@ def to_xml(options={}, &block)
# fill me in...
end
end
- CONTENTS
- end
- end
+ CONTENTS
+ end
- context "special characters in version name" do
- before :each do
- run_generator %w(foo v3.4.2)
- end
+ context "test_framework: test_unit" do
+ before :each do
+ ::FileUtils.rm(::File.expand_path("../../tmp/config/routes.rb", __FILE__))
+ ::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/presenters/#{module_name_for_path(mod)}", __FILE__))
+ ::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{module_name_for_route(mod)}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-v1\") do\n end\nend"}
+ Versionist.configuration.configured_test_framework = :test_unit
+ run_generator [name, mod]
+ end
- it "should create a namespaced presenter" do
- assert_file "app/presenters/v3.4.2/foo_presenter.rb", <<-CONTENTS
-class V3_4_2::FooPresenter
+ it "should create a namespaced test/presenters directory" do
+ assert_directory "test/presenters/#{module_name_for_path(mod)}"
+ end
- def initialize(foo)
- @foo = foo
- end
+ it "should create a namespaced presenter test" do
+ assert_file "test/presenters/#{module_name_for_path(mod)}/#{name.underscore}_presenter_test.rb", <<-CONTENTS
+require 'test_helper'
- def as_json(options={})
- # fill me in...
+class #{mod}::#{name.camelize}PresenterTest < Test::Unit::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
end
+end
+ CONTENTS
+ end
+ end
+
+ context "test_framework: rspec" do
+ before :each do
+ ::FileUtils.rm(::File.expand_path("../../tmp/config/routes.rb", __FILE__))
+ ::FileUtils.mkdir_p(::File.expand_path("../../tmp/app/presenters/#{module_name_for_path(mod)}", __FILE__))
+ ::File.open(::File.expand_path("../../tmp/config/routes.rb", __FILE__), "w") {|f| f.write "Test::Application.routes.draw do\n api_version(:module => \"#{module_name_for_route(mod)}\", :header => \"Accept\", :value => \"application/vnd.mycompany.com-v1\") do\n end\nend"}
+ Versionist.configuration.configured_test_framework = :rspec
+ run_generator [name, mod]
+ end
+
+ it "should create a namespaced spec/presenters directory" do
+ assert_directory "spec/presenters/#{module_name_for_path(mod)}"
+ end
+
+ it "should create a namespaced presenter spec" do
+ assert_file "spec/presenters/#{module_name_for_path(mod)}/#{name.underscore}_presenter_spec.rb", <<-CONTENTS
+require 'spec_helper'
+
+describe #{mod}::#{name.camelize}Presenter do
- def to_xml(options={}, &block)
- xml = options[:builder] ||= Builder::XmlMarkup.new
- # fill me in...
- end
end
- CONTENTS
+ CONTENTS
+ end
+ end
+ end
end
end
end

0 comments on commit 28c82d0

Please sign in to comment.