Permalink
Browse files

Added resource generator

  • Loading branch information...
gingerhendrix committed Apr 22, 2009
1 parent 8b0a747 commit 45e154de71fd489444934f303d6bbae9d0512b59
@@ -0,0 +1,8 @@
+Description:
+ Generate a scrobbler2 resource
+
+Usage:
+ ./script/generate resource resource [funcs]
+
+ * resource is a last.fm api resource (eg. user, track etc)
+ * funcs are a list of last.fm get api method (eg. info, similar, top_tags etc)
@@ -0,0 +1,47 @@
+class ResourceGenerator < RubiGen::Base
+
+ default_options :author => nil
+
+ attr_reader :name
+ attr_reader :funcs
+
+ def initialize(runtime_args, runtime_options = {})
+ super
+ usage if args.empty?
+ @name = args.shift
+ @funcs = args
+ extract_options
+ end
+
+ def manifest
+ record do |m|
+ # Ensure appropriate folder(s) exists
+ m.directory 'lib/scrobbler2'
+ m.directory 'test/unit'
+
+ # Create stubs
+ m.template "resource.rb.erb", "lib/scrobbler2/#{name}.rb"
+ m.template "spec.rb.erb", "test/unit/#{name}_spec.rb"
+
+ # Create acceptance tests
+ funcs.each do |func|
+ m.dependency "acceptance_test", [name, func]
+ end
+ end
+ end
+
+ protected
+ def banner
+ <<-EOS
+Creates a scrobbler2 resource
+
+USAGE: #{$0} #{spec.name} name funcs
+EOS
+ end
+
+ def add_options!(opts)
+ end
+
+ def extract_options
+ end
+end
@@ -0,0 +1,11 @@
+module Scrobbler2
+ class <%= name.classify %> < Base
+
+ def initialize()
+ @query = {}#TODO eg. {:artist => artist, :track => track}
+ end
+ <% funcs.each do |func| %>
+ get_resource :<%=func%>, :root => "<%=func%>"
+ <% end %>
+ end
+end
@@ -0,0 +1,22 @@
+require File.dirname(__FILE__) + "/spec_helper.rb"
+
+describe "<%=name.classify%>" do
+ before(:each) do
+ @<%=name%> = Scrobbler2::<%=name.classify%>.new "..."
+ @<%=name%>.class.stub!(:get).and_return Hash.new
+ end
+
+ it "should have a default query {TODO}" do
+ @<%=name%>.instance_variable_get(:@query).should == {}
+ end
+
+ <% funcs.each do |func| %>
+
+ it "<%=func%> should call get with '<%=name%>.get<%=func%>'" do
+ @<%=name%>.class.should_receive(:get).with('<%=name%>.get<%=func%>', anything(), anything()).and_return Hash.new
+ @<%=name%>.<%=func%>
+ end
+
+ <% end %>
+
+end
@@ -9,7 +9,6 @@ def setup
end
def teardown
- p app_root_files
bare_teardown
end
@@ -0,0 +1,71 @@
+require File.join(File.dirname(__FILE__), "test_generator_helper.rb")
+
+
+class TestResourceGenerator < Test::Unit::TestCase
+ include RubiGen::GeneratorTestHelper
+
+ def setup
+ bare_setup
+ end
+
+ def teardown
+ 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"
+ funcs = ["method1", "method2"]
+ run_generator('resource', funcs.unshift(resource), sources)
+ assert_generated_file "lib/scrobbler2/resource.rb" do |file|
+ assert file.include?("class Resource < Base")
+ assert file.include?("get_resource :method1, :root => \"method1\"")
+ assert file.include?("get_resource :method2, :root => \"method2\"")
+ end
+
+ assert_generated_file "test/unit/resource_spec.rb" do |file|
+ assert file.include?("describe \"Resource\"")
+
+ assert file.include?("@resource = Scrobbler2::Resource.new \"...\"")
+ assert file.include?("@resource.class.stub!(:get).and_return Hash.new")
+
+ assert file.include?("it \"should have a default query {TODO}\" do")
+ assert file.include?("@resource.instance_variable_get(:@query).should == {}")
+
+ assert file.include?("it \"method1 should call get with 'resource.getmethod1'\" do")
+ assert file.include?("@resource.class.should_receive(:get).with('resource.getmethod1', anything(), anything()).and_return Hash.new")
+ assert file.include?("@resource.method1")
+
+ assert file.include?("it \"method2 should call get with 'resource.getmethod2'\" do")
+ assert file.include?("@resource.class.should_receive(:get).with('resource.getmethod2', anything(), anything()).and_return Hash.new")
+ assert file.include?("@resource.method2")
+ end
+
+ assert_directory_exists("test/acceptance/resource")
+ assert_generated_file "test/acceptance/resource/method1_test.rb"
+ assert_generated_file "test/acceptance/resource/method2_test.rb"
+ end
+
+ private
+ def sources
+ [RubiGen::PathSource.new(:test, File.join(File.dirname(__FILE__),"../..", generator_path))
+ ]
+ end
+
+ def generator_path
+ "generators"
+ end
+end

0 comments on commit 45e154d

Please sign in to comment.