require_relative '../spec_helper' require_relative '../../lib/dbuilder' require_relative '../../lib/dbuilder/builder.rb' describe Dbuilder::Builder do describe ".extended" do it "should throw an exception if the extended class does not have 'Builder' at the end of it's name" do lambda { class Foo; extend Dbuilder::Builder; end }.must_raise(RuntimeError) end it "should throw an exception if the exteded class has the builder name at the begining of it's class name instead of the end" do lambda { class BuilderFoo; extend Dbuilder::Builder; end }.must_raise(RuntimeError) end it "should not throw an exception if the extending class has 'Builder' at the end it's class name" do lambda { class FooBuilder; extend Dbuilder::Builder; end }.must_be_silent end it "should have empty hashes for storing defaults / overridden builder methods" do Thread.current["Test_builder_defaults"].must_equal Hash.new Thread.current["Test_builder_overrides"].must_equal Hash.new end it "should have empty hashes for storing defaults / overridden builder methods for a second class" do Thread.current["OtherTest_builder_defaults"].must_equal Hash.new Thread.current["OtherTest_builder_overrides"].must_equal Hash.new end it "should create the correct class_name method" do class FooBarBuilder extend Dbuilder::Builder end.must_respond_to "class_name" end it "should return the correct value from class_name method" do class FooBarBuilder extend Dbuilder::Builder end FooBarBuilder.class_name.must_equal "FooBar" end end describe ".builder_default" do it "responds properly from to an extending class" do TestBuilder.must_respond_to "builder_default" end it "should define the proper with_ method" do SimpleExtensionBuilder.must_respond_to "with_tom" end it "should have chainable _with methods" do SimpleExtensionBuilder.with_tom("jones").must_equal SimpleExtensionBuilder end it "should have the proper default should be defined" do Thread.current["SimpleExtension_builder_defaults"].must_equal({tom: "cat"}) end it "should be able to change the values of the overridden values" do class OverriddenDefaultValuesBuilder extend Dbuilder::Builder builder_default :foo, 'bar' end OverriddenDefaultValuesBuilder.with_foo("zooooo") Thread.current["OverriddenDefaultValues_builder_overrides"].must_equal({foo: "zooooo"}) end it "should have the correct default after an override occurs" do class OverriddenDefaultValuesBuilder extend Dbuilder::Builder builder_default :foo, 'bar' end OverriddenDefaultValuesBuilder.with_foo("zooooo") Thread.current["OverriddenDefaultValues_builder_defaults"].must_equal({foo: "bar"}) end it "should be able to handle multiple builder_default calls" do class OverriddenDefaultValuesBuilder extend Dbuilder::Builder builder_default :foo, 'bar' builder_default :zoo, 'moo' builder_default :kitty, 'meow' end Thread.current["OverriddenDefaultValues_builder_defaults"].must_equal({foo: "bar", zoo: "moo", kitty: "meow"}) end end describe ".builder_defaults" do it "responds properly to an extending class" do TestBuilder.must_respond_to "builder_defaults" end it "should raise an error if an even amount of params are not passed in" do lambda { class FooBuilder extend Dbuilder::Builder builder_defaults :foo end }.must_raise(RuntimeError) end it "should not raise an error if an even number of params are passed in" do lambda { class FooBuilder extend Dbuilder::Builder builder_defaults :foo, "bar" end }.must_be_silent end it "should not raise an error if a large even number of params are passed in" do lambda { class FooBuilder extend Dbuilder::Builder builder_defaults :foo, "bar", :zoo, "moo", :dick, "tracy", "ronald", "mcdonald" end }.must_be_silent end it "should have all of the proper _with methods" do class FooBuilder extend Dbuilder::Builder builder_defaults :foo, "bar", :zoo, "moo", :dick, "tracy", "ronald", "mcdonald" end FooBuilder.must_respond_to "with_foo" FooBuilder.must_respond_to "with_zoo" FooBuilder.must_respond_to "with_dick" FooBuilder.must_respond_to "with_ronald" end end describe ".build" do it "should create the appropriate class" do SimpleExtensionBuilder.build.must_equal SimpleExtension end it "should respond to the appropriate methods" do target = SimpleExtensionBuilder.build.new target.must_respond_to :tom end it "should have the correct values for the default methods" do target = SimpleExtensionBuilder.build.new target.tom.must_equal "cat" end it "it appends to existing classes" do OriginalBuilder.build.new.must_respond_to "gangsta" end end private class Original def gangsta "Biggy Smalls" end end class OriginalBuilder extend Dbuilder::Builder builder_defaults :ja, "rule" end class TestBuilder extend Dbuilder::Builder end class OtherTestBuilder extend Dbuilder::Builder end class SimpleExtensionBuilder extend Dbuilder::Builder builder_default "tom", "cat" end end