Skip to content

Commit

Permalink
move tests to test folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Hemant Kumar committed Aug 11, 2010
1 parent d4569ac commit 4c59c02
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 136 deletions.
132 changes: 0 additions & 132 deletions lib/clay_blueprint.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
# ClayBlueprint
require "rubygems"
gem "activesupport", "2.3.8"
require "active_support"

# TODO
# 1. Make sure that blueprints should be reusable
# 2. Should be able to return hashed value if needed
# 3. Specify a way to make association
# 4. Should be automatically loaded from particular rails project directory

module ClayBlueprint
class InvalidClay < StandardError; end
class InvalidClayCreationBlock < StandardError; end
Expand Down Expand Up @@ -117,124 +106,3 @@ def method_missing(name,*args,&block)



if __FILE__ == $0
require "test/unit"
require "shoulda"

class TestClay < Test::Unit::TestCase
context "For Clays" do
setup do
ClayBlueprint::Clay.define(:foo, :force => true) do |f|
f.name "Hemant Kumar"
f.age 10
end
end

should "create objects from blueprint" do
@clay = ClayBlueprint::Clay(:foo)
assert_equal "Hemant Kumar", @clay.name
assert_equal 10, @clay.age
assert_equal({ :age => 10, :name => "Hemant Kumar"}, @clay.clay_attributes)
end

should "each object created from blueprint should be different" do
@clay1 = ClayBlueprint::Clay(:foo)
@clay2 = ClayBlueprint::Clay(:foo)
assert (@clay1.object_id != @clay2.object_id)
end
end

context "For clay association" do
setup do
ClayBlueprint::Clay.define(:x) do |x|
x.name "x"
x.age 20
end

ClayBlueprint::Clay.define(:y) do |y|
y.sex 'm'
y.profile ClayBlueprint::Clay(:x)
end
end

should "setup association" do
y = ClayBlueprint::Clay(:y)
assert_equal 'm', y.sex
assert_equal 'x', y.profile.name
assert_equal 20, y.profile.age
end
end

context "Clay attributes can be overriden" do
setup do
ClayBlueprint::Clay.define(:bar) do |b|
b.name "Bar"
b.age 12
end
end

should "be able to override values" do
b = ClayBlueprint::Clay(:bar, :name => "Hemant")
assert_equal "Hemant", b.name
assert_equal 12, b.age
end
end

context "If same clay is defined twice" do
should "detect any conflict and raise exception" do
ClayBlueprint::Clay.define(:conflict) do |c|
c.message "Hello"
c.status 10
end
assert_raise(ClayBlueprint::DuplicateClayDefinition) do
ClayBlueprint::Clay.define(:conflict) do |c|
c.content "Foo"
c.title "conflict"
end
end
end
end

context "Blueprint clays for existing classes" do
setup do
class Emacs
attr_accessor :name
end
ClayBlueprint::Clay.define(:emacs) do |e|
e.name "Editor"
end
end
should "work as usual" do
emacs = ClayBlueprint::Clay(:emacs)
assert_equal "Editor", emacs.name
end
end

context "Same class but with different name" do
setup do
ClayBlueprint::Clay.define(:lesson, :class => "Lesson") do |l|
l.name "foo"
l.content "bar"
l.size 10
end

ClayBlueprint::Clay.define(:lesson_without_content,:class => "Lesson") do |l|
l.name "lesson1"
l.size 20
end
end

should "work without creating conflicts" do
a = ClayBlueprint::Clay(:lesson)
assert_equal 'foo', a.name
assert_equal 'bar', a.content
assert_equal 10, a.size

b = ClayBlueprint::Clay(:lesson_without_content)
assert_equal 'lesson1', b.name
assert_nil b.content
assert_equal 20, b.size
end
end
end
end
116 changes: 113 additions & 3 deletions test/clay_blueprint_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,118 @@
require 'test_helper'

class ClayBlueprintTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
context "For Clays" do
setup do
ClayBlueprint::Clay.define(:foo, :force => true) do |f|
f.name "Hemant Kumar"
f.age 10
end
end

should "create objects from blueprint" do
@clay = ClayBlueprint::Clay(:foo)
assert_equal "Hemant Kumar", @clay.name
assert_equal 10, @clay.age
assert_equal({ :age => 10, :name => "Hemant Kumar"}, @clay.clay_attributes)
end

should "each object created from blueprint should be different" do
@clay1 = ClayBlueprint::Clay(:foo)
@clay2 = ClayBlueprint::Clay(:foo)
assert (@clay1.object_id != @clay2.object_id)
end
end

context "For clay association" do
setup do
ClayBlueprint::Clay.define(:x) do |x|
x.name "x"
x.age 20
end

ClayBlueprint::Clay.define(:y) do |y|
y.sex 'm'
y.profile ClayBlueprint::Clay(:x)
end
end

should "setup association" do
y = ClayBlueprint::Clay(:y)
assert_equal 'm', y.sex
assert_equal 'x', y.profile.name
assert_equal 20, y.profile.age
end
end

context "Clay attributes can be overriden" do
setup do
ClayBlueprint::Clay.define(:bar) do |b|
b.name "Bar"
b.age 12
end
end

should "be able to override values" do
b = ClayBlueprint::Clay(:bar, :name => "Hemant")
assert_equal "Hemant", b.name
assert_equal 12, b.age
end
end

context "If same clay is defined twice" do
should "detect any conflict and raise exception" do
ClayBlueprint::Clay.define(:conflict) do |c|
c.message "Hello"
c.status 10
end
assert_raise(ClayBlueprint::DuplicateClayDefinition) do
ClayBlueprint::Clay.define(:conflict) do |c|
c.content "Foo"
c.title "conflict"
end
end
end
end

context "Blueprint clays for existing classes" do
setup do
class Emacs
attr_accessor :name
end
ClayBlueprint::Clay.define(:emacs) do |e|
e.name "Editor"
end
end
should "work as usual" do
emacs = ClayBlueprint::Clay(:emacs)
assert_equal "Editor", emacs.name
end
end

context "Same class but with different name" do
setup do
ClayBlueprint::Clay.define(:lesson, :class => "Lesson") do |l|
l.name "foo"
l.content "bar"
l.size 10
end

ClayBlueprint::Clay.define(:lesson_without_content,:class => "Lesson") do |l|
l.name "lesson1"
l.size 20
end
end

should "work without creating conflicts" do
a = ClayBlueprint::Clay(:lesson)
assert_equal 'foo', a.name
assert_equal 'bar', a.content
assert_equal 10, a.size

b = ClayBlueprint::Clay(:lesson_without_content)
assert_equal 'lesson1', b.name
assert_nil b.content
assert_equal 20, b.size
end
end
end
7 changes: 6 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
require 'rubygems'
gem 'activesupport', '2.3.8'
require 'active_support'
require 'active_support/test_case'
require 'active_support/test_case'
require "shoulda"
$:<< File.join(File.dirname(__FILE__),"..","lib")
require "clay_blueprint"

0 comments on commit 4c59c02

Please sign in to comment.