Skip to content

Commit

Permalink
added more steps to test
Browse files Browse the repository at this point in the history
  • Loading branch information
mattetti committed Oct 19, 2008
1 parent 4c4432a commit 1ef6745
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
*/**/.DS_Store
thor-stuff/test-app
91 changes: 80 additions & 11 deletions thor-stuff/test-app.thor
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ class TestApp < Thor
edit_request_specs edit_request_specs
edit_index_view edit_index_view
make_specs_not_pending make_specs_not_pending
Dir.chdir(Dir.pwd + "/" + @app_name) do add_model_validation
print "cd #{@app_name} && rake spec\n" add_model_specs
print `rake spec` edit_layout
end run_app_specs
# puts 'then `http://localhost:4000/articles`'
end end


protected protected


def app_path
"#{Dir.pwd}/#{@app_name}"
end

def gsub_file(destination, regexp, *args, &block) def gsub_file(destination, regexp, *args, &block)
path = destination path = destination
content = File.read(path).gsub(regexp, *args, &block) content = File.read(path).gsub(regexp, *args, &block)
Expand All @@ -37,29 +40,66 @@ class TestApp < Thor
end end


def generate_article_resource def generate_article_resource
Dir.chdir(Dir.pwd + "/" + @app_name) do Dir.chdir(app_path) do
`merb-gen resource article title:String,author:String,created_at:DateTime` `merb-gen resource article title:String,author:String,created_at:DateTime`
end end
end end


def migrate_db def migrate_db
Dir.chdir(Dir.pwd + "/" + @app_name) do Dir.chdir(app_path) do
`rake db:automigrate MERB_ENV=test` `rake db:automigrate MERB_ENV=test`
end end
end end


def edit_layout
print " > editing the layout\n"

sentinel = "<%#= message[:notice] %>"
layout_file = "#{app_path}/app/views/layout/application.html.erb"
gsub_file layout_file, /(#{Regexp.escape(sentinel)})/mi do |match|
<<-RUBY
<%= message[:notice] %>
<%= message[:error] %>
RUBY
end

end

def edit_request_specs def edit_request_specs
print " > editing the request specs\n" print " > editing the request specs\n"


sentinel = ":params => { :article => { }})" sentinel = ":params => { :article => { :id => nil }})"
spec_file = "#{Dir.pwd}/#{@app_name}/spec/requests/articles_spec.rb" spec_file = "#{app_path}/spec/requests/articles_spec.rb"
gsub_file spec_file, /(#{Regexp.escape(sentinel)})/mi do |match| gsub_file spec_file, /(#{Regexp.escape(sentinel)})/mi do |match|
":params => { :article => {:title => 'intro', :author => 'Matt', :created_at => '#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}' }})" ":params => { :article => {:title => 'intro', :author => 'Matt', :created_at => '#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}' }})"
end end

### failing_post_spec
failing_post_spec = <<-RUBY
\n
describe "a failing POST" do
before(:each) do
@response = request(resource(:articles), :method => "POST", :params => { :article => { :id => nil}})
end
it "should re render the new action" do
@response.body.include?("Articles controller, new action").should be_true
end
it "should have an error message" do
@response.body.include?("Article failed to be created").should be_true
end
end
RUBY

gsub_file spec_file, /(end\n\n)$/i do |match|
"#{failing_post_spec}\n#{match}"
end

end end


def edit_index_view def edit_index_view
spec_file = "#{Dir.pwd}/#{@app_name}/app/views/articles/index.html.erb" spec_file = "#{app_path}/app/views/articles/index.html.erb"
print " > editing index view #{spec_file}\n" print " > editing index view #{spec_file}\n"
index_view = <<-RUBY index_view = <<-RUBY
<ul> <ul>
Expand All @@ -76,10 +116,39 @@ RUBY
def make_specs_not_pending def make_specs_not_pending
print " > removing the spec pending status\n" print " > removing the spec pending status\n"
sentinel = "pending" sentinel = "pending"
spec_file = "#{Dir.pwd}/#{@app_name}/spec/requests/articles_spec.rb" spec_file = "#{app_path}/spec/requests/articles_spec.rb"
gsub_file spec_file, /(#{Regexp.escape(sentinel)})/mi do |match| gsub_file spec_file, /(#{Regexp.escape(sentinel)})/mi do |match|
"" ""
end end
end end

def add_model_validation
print " > adding title validation to Article\n"
model_file = "#{app_path}/app/models/article.rb"
gsub_file model_file, /(end)\s*$/mi do |match|
" validates_present :title\n#{match}"
end
end

def add_model_specs
print " > adding model specs\n"
model_spec = "#{app_path}/spec/models/article_spec.rb"
validation_spec = <<-RUBY
it "should not be valid without a title" do
@article = Article.new
@article.should_not be_valid
end
RUBY
gsub_file model_spec, /(it "should have specs")/mi do |match|
validation_spec
end
end

def run_app_specs
Dir.chdir(Dir.pwd + "/" + @app_name) do
print "cd #{@app_name} && rake spec\n"
print `rake spec`
end
end


end end

0 comments on commit 1ef6745

Please sign in to comment.