Skip to content

Commit

Permalink
Moved attribute validation into the Attribute class
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Jul 30, 2008
1 parent 03fa0d1 commit 38bc896
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 59 deletions.
22 changes: 19 additions & 3 deletions lib/factory_girl/attribute.rb
@@ -1,12 +1,28 @@
class Factory

class AttributeDefinitionError < RuntimeError
end

class Attribute #:nodoc:

attr_reader :name
attr_writer :static_value, :lazy_block

def initialize (name)
@name = name
def initialize (name, static_value, lazy_block)
name = name.to_sym

if name.to_s =~ /=$/
raise AttributeDefinitionError,
"factory_girl uses 'f.#{name.to_s.chop} value' syntax " +
"rather than 'f.#{name} = value'"
end

unless static_value.nil? || lazy_block.nil?
raise AttributeDefinitionError, "Both value and block given"
end

@name = name
@static_value = static_value
@lazy_block = lazy_block
end

def value (proxy)
Expand Down
22 changes: 1 addition & 21 deletions lib/factory_girl/factory.rb
@@ -1,8 +1,5 @@
class Factory

class AttributeDefinitionError < RuntimeError
end

cattr_accessor :factories, :sequences #:nodoc:
self.factories = {}
self.sequences = {}
Expand Down Expand Up @@ -93,25 +90,8 @@ def initialize (name, options = {}) #:nodoc:
# value: (Object)
# If no block is given, this value will be used for this attribute.
def add_attribute (name, value = nil, &block)
name = name.to_sym

if name.to_s =~ /=$/
raise AttributeDefinitionError,
"factory_girl uses 'f.#{name.to_s.chop} #{value}' syntax " +
"rather than 'f.#{name} #{value}'"
end

attribute = Attribute.new(name)
attribute = Attribute.new(name, value, block)
@attributes << attribute

if block_given?
unless value.nil?
raise ArgumentError, "Both value and block given"
end
attribute.lazy_block = block
else
attribute.static_value = value
end
end

# Calls add_attribute using the missing method name as the name of the
Expand Down
63 changes: 42 additions & 21 deletions test/attribute_test.rb
Expand Up @@ -2,48 +2,69 @@

class AttributeTest < Test::Unit::TestCase

def setup
@proxy = mock('attribute-proxy')
end

context "an attribute" do

setup do
@name = :user
@proxy = mock('attribute-proxy')
@attr = Factory::Attribute.new(@name)
@attr = Factory::Attribute.new(@name, 'test', nil)
end

should "have a name" do
assert_equal @name, @attr.name
end

context "after setting a static value" do
end

setup do
@value = 'test'
@attr.static_value = @value
end
context "an attribute with a static value" do

should "return the value" do
assert_equal @value, @attr.value(@proxy)
end
setup do
@value = 'test'
@attr = Factory::Attribute.new(:user, @value, nil)
end

should "return the value" do
assert_equal @value, @attr.value(@proxy)
end

context "after setting a lazy value" do
end

setup do
@attr.lazy_block = lambda { 'value' }
end
context "an attribute with a lazy value" do

should "call the block to return a value" do
assert_equal 'value', @attr.value(@proxy)
end
setup do
@block = lambda { 'value' }
@attr = Factory::Attribute.new(:user, nil, @block)
end

should "yield the attribute proxy to the block" do
@attr.lazy_block = lambda {|a| a }
assert_equal @proxy, @attr.value(@proxy)
end
should "call the block to return a value" do
assert_equal 'value', @attr.value(@proxy)
end

should "yield the attribute proxy to the block" do
@block = lambda {|a| a }
@attr = Factory::Attribute.new(:user, nil, @block)
assert_equal @proxy, @attr.value(@proxy)
end

end

should "raise an error when defining an attribute writer" do
assert_raise Factory::AttributeDefinitionError do
Factory::Attribute.new('test=', nil, nil)
end
end

should "not allow attributes to be added with both a value parameter and a block" do
assert_raise(Factory::AttributeDefinitionError) do
Factory::Attribute.new(:name, 'value', lambda {})
end
end

should "convert names to symbols" do
assert_equal :name, Factory::Attribute.new('name', nil, nil).name
end

end
14 changes: 0 additions & 14 deletions test/factory_test.rb
Expand Up @@ -53,14 +53,6 @@ def self.should_instantiate_class

end

should "raise an error when defining a factory when using attribute setters" do
assert_raise Factory::AttributeDefinitionError do
Factory.define(:user) do |f|
f.name = 'test'
end
end
end

context "defining a sequence" do

setup do
Expand Down Expand Up @@ -223,12 +215,6 @@ def self.should_instantiate_class
assert_equal @value, @factory.attributes_for[@attr]
end

should "not allow attributes to be added with both a value parameter and a block" do
assert_raise(ArgumentError) do
@factory.add_attribute(:name, 'value') {}
end
end

should "allow attributes to be added with strings as names" do
@factory.add_attribute('name', 'value')
assert_equal 'value', @factory.attributes_for[:name]
Expand Down

0 comments on commit 38bc896

Please sign in to comment.