miniskirt 0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/miniskirt.rb +48 -0
  2. data/miniskirt_test.rb +108 -0
  3. metadata +64 -0
data/miniskirt.rb ADDED
@@ -0,0 +1,48 @@
1
+ require "active_support"
2
+ # Factory girl, relaxed.
3
+ #
4
+ # Factory.define :user do |f|
5
+ # f.login 'johndoe%d' # Sequence.
6
+ # f.email '%{login}@example.com' # Interpolate.
7
+ # f.password f.password_confirmation('foobar') # Chain.
8
+ # end
9
+ #
10
+ # Factory.define :post do |f|
11
+ # f.user { Factory :user } # Blocks, if you must.
12
+ # end
13
+ class Miniskirt < Struct.new(:__klass__)
14
+ undef_method *instance_methods.grep(/^(?!__|object_id)/) # BlankerSlate.
15
+ @@factories = {} and private_class_method :new
16
+
17
+ class << self
18
+ def define(name)
19
+ @@factories[name = name.to_s] = {} and yield new(name)
20
+ end
21
+
22
+ def build(name, attrs = {})
23
+ (name, n = name.to_s) and (m = name.classify.constantize).new do |rec|
24
+ attrs.symbolize_keys!.reverse_update(@@factories[name]).each do |k, v|
25
+ rec.send "#{k}=", case v when String # Sequence and interpolate.
26
+ v.sub(/%\d*d/) {|d| d % n ||= m.maximum(:id).to_i + 1} % attrs % n
27
+ when Proc then v.call(rec) else v
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ def create(name, attrs = {})
34
+ build(name, attrs).tap { |record| record.save }
35
+ end
36
+ end
37
+
38
+ def method_missing(name, value = nil, &block)
39
+ @@factories[__klass__][name] = block || value
40
+ end
41
+ end
42
+
43
+ def Miniskirt(name, attrs = {})
44
+ Miniskirt.create(name, attrs)
45
+ end
46
+
47
+ Factory = Miniskirt
48
+ alias Factory Miniskirt
data/miniskirt_test.rb ADDED
@@ -0,0 +1,108 @@
1
+ require 'miniskirt'
2
+ require 'test/unit'
3
+
4
+ class MiniSkirtTest < Test::Unit::TestCase
5
+ def test_should_define_factories
6
+ factories = Miniskirt.class_variable_get :@@factories
7
+ assert_not_nil factories["user"]
8
+ assert_not_nil factories["post"]
9
+ end
10
+
11
+ def test_should_build_object
12
+ user = Factory.build :user
13
+ assert_instance_of User, user
14
+ assert user.new_record?
15
+ end
16
+
17
+ def test_should_create_object
18
+ user = Factory.create :user
19
+ assert_instance_of User, user
20
+ assert !user.new_record?
21
+ end
22
+
23
+ def test_should_create_object_with_shorthand
24
+ user = Factory :user
25
+ assert !user.new_record?
26
+ end
27
+
28
+ def test_should_assign_attributes
29
+ user = Factory.create :user
30
+ assert_not_nil user.login
31
+ assert_not_nil user.email
32
+ assert_not_nil user.password
33
+ assert_not_nil user.password_confirmation
34
+ end
35
+
36
+ def test_should_chain_attributes
37
+ user = Factory.create :user
38
+ assert_equal user.password, user.password_confirmation
39
+ end
40
+
41
+ def test_should_override_attributes_on_the_fly
42
+ user = Factory.create :user, :login => (login = "janedoe"),
43
+ :email => (email = "janedoe@example.com"),
44
+ :password => (password = "password"),
45
+ :password_confirmation => (password_confirmation = "passwrod")
46
+
47
+ assert_equal login, user.login
48
+ assert_equal email, user.email
49
+ assert_equal password, user.password
50
+ assert_equal password_confirmation, user.password_confirmation
51
+
52
+ user = Factory.create :user
53
+
54
+ assert_not_equal login, user.login
55
+ assert_not_equal email, user.email
56
+ assert_not_equal password, user.password
57
+ assert_not_equal password_confirmation, user.password_confirmation
58
+ end
59
+
60
+ def test_should_sequence
61
+ user1 = Factory.create :user
62
+ user2 = Factory.create :user
63
+ assert_equal user1.login.succ, user2.login
64
+ end
65
+
66
+ def test_should_interpolate
67
+ user = Factory.create :user
68
+ assert_equal user.email, "#{user.login}@example.com"
69
+ end
70
+ end
71
+
72
+ class Mock
73
+ @@maximum = nil
74
+ def self.maximum(column)
75
+ @@maximum
76
+ end
77
+
78
+ def initialize
79
+ yield self
80
+ end
81
+
82
+ def save
83
+ @@maximum = @@maximum.to_i + 1 unless @saved
84
+ @saved = true
85
+ end
86
+
87
+ def new_record?
88
+ !@saved
89
+ end
90
+ end
91
+
92
+ Miniskirt.define :user do |f|
93
+ f.login "johndoe%d"
94
+ f.email "%{login}@example.com"
95
+ f.password f.password_confirmation("foobarbaz")
96
+ end
97
+
98
+ Miniskirt.define :post do |f|
99
+ f.user { Miniskirt :user }
100
+ end
101
+
102
+ class User < Mock
103
+ attr_accessor :login, :email, :password, :password_confirmation
104
+ end
105
+
106
+ class Post < Mock
107
+ attr_accessor :user
108
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: miniskirt
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.9"
5
+ platform: ruby
6
+ authors:
7
+ - Stephen Celis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-15 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "2.2"
24
+ version:
25
+ description: Test::Unit begot MiniTest; factory_girl begets Miniskirt.
26
+ email: stephen@stephencelis.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - miniskirt.rb
35
+ has_rdoc: true
36
+ homepage: http://gist.github.com/273579
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - .
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 1.8.7
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.5
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: factory_girl, relaxed
63
+ test_files:
64
+ - miniskirt_test.rb