Commit 8d47241e by fistfvck

add specs

parent 10f896ee
...@@ -3,29 +3,11 @@ require File.dirname(__FILE__) + '/../spec_helper.rb' ...@@ -3,29 +3,11 @@ require File.dirname(__FILE__) + '/../spec_helper.rb'
require 'annotate/annotate_models' require 'annotate/annotate_models'
require 'active_support' require 'active_support'
require 'fakefs/spec_helpers' require 'fakefs/spec_helpers'
require 'tmpdir'
describe AnnotateModels do describe AnnotateModels do
include FakeFS::SpecHelpers include FakeFS::SpecHelpers
before(:all) do
require "tmpdir"
@dir = Dir.tmpdir + "/#{Time.now.to_i}" + "/annotate_models"
FileUtils.mkdir_p(@dir)
AnnotateModels.model_dir = @dir
end
module ::ActiveRecord
class Base
end
end
def create(file, body="hi")
File.open(@dir + '/' + file, "w") do |f|
f.puts(body)
end
@dir + '/' + file
end
def mock_class(table_name, primary_key, columns) def mock_class(table_name, primary_key, columns)
options = { options = {
:connection => mock("Conn", :indexes => []), :connection => mock("Conn", :indexes => []),
...@@ -78,8 +60,20 @@ EOS ...@@ -78,8 +60,20 @@ EOS
end end
describe "#get_model_class" do describe "#get_model_class" do
def create(file, body="hi")
path = @dir + '/' + file
File.open(path, "w") do |f|
f.puts(body)
end
path
end
before :all do before :all do
@dir = File.join Dir.tmpdir, "annotate_models"
FileUtils.mkdir_p(@dir)
AnnotateModels.model_dir = @dir
create('foo.rb', <<-EOS) create('foo.rb', <<-EOS)
class Foo < ActiveRecord::Base class Foo < ActiveRecord::Base
end end
...@@ -174,36 +168,43 @@ end ...@@ -174,36 +168,43 @@ end
describe "annotating a file" do describe "annotating a file" do
before do before do
@file_name = "user.rb" @file_name = "user.rb"
@file_content = "class User < ActiveRecord::Base; end" @file_content = <<-EOS
class User < ActiveRecord::Base
end
EOS
File.open(@file_name, "wb") { |f| f.write @file_content }
@klass = mock_class(:users, :id, [ @klass = mock_class(:users, :id, [
mock_column(:id, :integer), mock_column(:id, :integer),
mock_column(:name, :string, :limit => 50) mock_column(:name, :string, :limit => 50)
]) ])
end @schema_info = AnnotateModels.get_schema_info(@klass, "== Schema Info")
def write_file
File.open("user.rb", "w") do |f|
f << @file_content
end
end end
it "should annotate the file before the model if position == 'before'" do it "should annotate the file before the model if position == 'before'" do
write_file AnnotateModels.annotate_one_file(@file_name, @schema_info, :position => "before")
schema_info = AnnotateModels.get_schema_info(@klass, "Schema Info") File.read(@file_name).should == "#{@schema_info}#{@file_content}"
end
AnnotateModels.annotate_one_file("user.rb", schema_info, :position => "before") it "should annotate before if given :position => :before" do
AnnotateModels.annotate_one_file(@file_name, @schema_info, :position => :before)
File.read(@file_name).should == "#{@schema_info}#{@file_content}"
end
File.read("user.rb").should == "#{schema_info}#{@file_content}\n" it "should annotate before if given :position => :after" do
AnnotateModels.annotate_one_file(@file_name, @schema_info, :position => :after)
File.read(@file_name).should == "#{@file_content}\n#{@schema_info}"
end end
it "should annotate before if given :position => :before" do it "should update annotate position" do
write_file AnnotateModels.annotate_one_file(@file_name, @schema_info, :position => :before)
schema_info = AnnotateModels.get_schema_info(@klass, "Schema Info")
another_schema_info = AnnotateModels.get_schema_info(mock_class(:users, :id, [mock_column(:id, :integer),]),
"== Schema Info")
AnnotateModels.annotate_one_file(@file_name, schema_info, :position => :before) AnnotateModels.annotate_one_file(@file_name, another_schema_info, :position => :after)
File.read("user.rb").should == "#{schema_info}#{@file_content}\n" File.read(@file_name).should == "#{@file_content}\n#{another_schema_info}"
end end
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment