Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added simple acceptance test generator

  • Loading branch information...
commit bf9d3a942c78ae789947769c2569422814cc7727 1 parent 4c99748
@gingerhendrix authored
View
5 scrobbler2_generators/acceptance_test/USAGE
@@ -0,0 +1,5 @@
+Description:
+
+
+Usage:
+
View
57 scrobbler2_generators/acceptance_test/acceptance_test_generator.rb
@@ -0,0 +1,57 @@
+class AcceptanceTestGenerator < RubiGen::Base
+
+ default_options :author => nil
+
+ attr_reader :resource
+ attr_reader :func
+
+ def initialize(runtime_args, runtime_options = {})
+ super
+ usage if args.empty? || args.length < 2
+ @resource = args.shift
+ @func = args.shift
+ extract_options
+ end
+
+ def manifest
+ record do |m|
+ # Ensure appropriate folder(s) exists
+ m.directory "test/acceptance/#{@resource}"
+
+ # Create stubs
+ m.template "acceptance_test.rb.erb", "test/acceptance/#{@resource}/#{@func}_test.rb"
+ # m.template_copy_each ["template.rb", "template2.rb"]
+ # m.template_copy_each ["template.rb", "template2.rb"], "some/path"
+ # m.file "file", "some_file_copied"
+ # m.file_copy_each ["path/to/file", "path/to/file2"]
+ # m.file_copy_each ["path/to/file", "path/to/file2"], "some/path"
+ end
+ end
+
+ protected
+ def banner
+ <<-EOS
+Creates a scrobbler2 acceptance test
+
+USAGE: #{$0} #{spec.name} resource func
+EOS
+ end
+
+ def add_options!(opts)
+ # opts.separator ''
+ # opts.separator 'Options:'
+ # For each option below, place the default
+ # at the top of the file next to "default_options"
+ # opts.on("-a", "--author=\"Your Name\"", String,
+ # "Some comment about this option",
+ # "Default: none") { |options[:author]| }
+ # opts.on("-v", "--version", "Show the #{File.basename($0)} version number and quit.")
+ end
+
+ def extract_options
+ # for each option, extract it into a local variable (and create an "attr_reader :author" at the top)
+ # Templates can access these value via the attr_reader-generated methods, but not the
+ # raw instance variable value.
+ # @author = options[:author]
+ end
+end
View
14 scrobbler2_generators/acceptance_test/templates/acceptance_test.rb.erb
@@ -0,0 +1,14 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+describe "<%=func%> for <%=resource%> ..." do
+
+ before(:all) do #We only want to hit the webservice once.
+ @<%=resource%> = Scrobbler2::<%=resource%>.new "..."
+ @<%=func%> = @<%=resource%>.<%=func%>
+ end
+
+ it "should be hash" do
+ @<%=func%>.should be_kind_of Hash
+ end
+
+end
View
47 test/generators/test_acceptance_test_generator.rb
@@ -0,0 +1,47 @@
+require File.join(File.dirname(__FILE__), "test_generator_helper.rb")
+
+
+class TestAcceptanceTestGenerator < Test::Unit::TestCase
+ include RubiGen::GeneratorTestHelper
+
+ def setup
+ bare_setup
+ end
+
+ def teardown
+ p app_root_files
+ bare_teardown
+ end
+
+ # Some generator-related assertions:
+ # assert_generated_file(name, &block) # block passed the file contents
+ # assert_directory_exists(name)
+ # assert_generated_class(name, &block)
+ # assert_generated_module(name, &block)
+ # assert_generated_test_for(name, &block)
+ # The assert_generated_(class|module|test_for) &block is passed the body of the class/module within the file
+ # assert_has_method(body, *methods) # check that the body has a list of methods (methods with parentheses not supported yet)
+ #
+ # Other helper methods are:
+ # app_root_files - put this in teardown to show files generated by the test method (e.g. p app_root_files)
+ # bare_setup - place this in setup method to create the APP_ROOT folder for each test
+ # bare_teardown - place this in teardown method to destroy the TMP_ROOT or APP_ROOT folder after each test
+
+ def test_generator_without_options
+ resource = "resource"
+ method = "method"
+ run_generator('acceptance_test', [resource, method], sources)
+ assert_directory_exists "test/acceptance/#{resource}"
+ assert_generated_file "test/acceptance/#{resource}/#{method}_test.rb"
+ end
+
+ private
+ def sources
+ [RubiGen::PathSource.new(:test, File.join(File.dirname(__FILE__),"../..", generator_path))
+ ]
+ end
+
+ def generator_path
+ "scrobbler2_generators"
+ end
+end
View
29 test/generators/test_generator_helper.rb
@@ -0,0 +1,29 @@
+begin
+ require File.dirname(__FILE__) + '/test_helper'
+rescue LoadError
+ require 'test/unit'
+end
+require 'fileutils'
+
+# Must set before requiring generator libs.
+TMP_ROOT = File.dirname(__FILE__) + "/tmp" unless defined?(TMP_ROOT)
+PROJECT_NAME = "myproject" unless defined?(PROJECT_NAME)
+app_root = File.join(TMP_ROOT, PROJECT_NAME)
+if defined?(APP_ROOT)
+ APP_ROOT.replace(app_root)
+else
+ APP_ROOT = app_root
+end
+if defined?(RAILS_ROOT)
+ RAILS_ROOT.replace(app_root)
+else
+ RAILS_ROOT = app_root
+end
+
+begin
+ require 'rubigen'
+rescue LoadError
+ require 'rubygems'
+ require 'rubigen'
+end
+require 'rubigen/helpers/generator_test_helper'

0 comments on commit bf9d3a9

Please sign in to comment.
Something went wrong with that request. Please try again.