minifacture 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/minifacture.rb +55 -0
  3. data/minifacture_test.rb +129 -0
  4. metadata +106 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5c4d9450716e8e8d978a79d05138b110425c6a67
4
+ data.tar.gz: 540663db8792f822e48890a89a48407a0e3655ff
5
+ SHA512:
6
+ metadata.gz: 7718d95e454faf744d5533424a0932511606dec1f2c78934c83f86d8e89f443750078cec939a69ceb843ca37c49c4b8899398bbf019f59fe0b15bd9ff76bb65b
7
+ data.tar.gz: 025f8b2a5c5ee07c3d4fcaa1021afc4928fb7cae5c6775e34737c6a543d82cbb5031c1c6ef52f39abf26472b8d7b8127ce78d384ad61ff997f4f4f31bde9af4c
data/minifacture.rb ADDED
@@ -0,0 +1,55 @@
1
+ require 'active_support/inflector'
2
+ require 'active_support/core_ext/hash'
3
+ # Factory girl, relaxed.
4
+ #
5
+ # Factory.define :user do |f|
6
+ # f.login 'johndoe%d' # Sequence.
7
+ # f.email '%{login}@example.com' # Interpolate.
8
+ # f.password f.password_confirmation('foobar') # Chain.
9
+ # end
10
+ #
11
+ # Factory.define :post do |f|
12
+ # f.user { Factory :user } # Blocks, if you must.
13
+ # end
14
+ class Minifacture < Struct.new(:__klass__)
15
+ undef_method *instance_methods.grep(/^(?!__|object_id)/)
16
+ @@attrs = {} and private_class_method :new
17
+
18
+ class << self
19
+ def define name, options = {}
20
+ @@attrs[name = name.to_s] = [{}, options] and yield new(name)
21
+ end
22
+
23
+ def build name, attrs = {}
24
+ (h, opts, n = @@attrs[name = name.to_s]) and m = opts[:class] || name
25
+ p = opts[:parent] and (h, m = @@attrs[p = p.to_s][0].merge(h), p)
26
+ (m = m.is_a?(Class) ? m : m.to_s.camelize.constantize).new.tap do |r|
27
+ attrs.symbolize_keys!.reverse_update(h).each do |k, v|
28
+ r.send "#{k}=", case v when String # Sequence and interpolate.
29
+ v.sub(/%\d*d/) {|d| d % n ||= (
30
+ m.respond_to?(:maximum) ? m.maximum(:id) : m.max(:id)
31
+ ).to_i + 1} % attrs % n
32
+ when Proc then v.call(r) else v
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ def create name, attrs = {}
39
+ build(name, attrs).tap { |record| record.save! }
40
+ end
41
+ end
42
+
43
+ def method_missing name, value = nil, &block
44
+ @@attrs[__klass__][0][name] = block || value
45
+ end
46
+ end
47
+
48
+ def Minifacture name, attrs = {}
49
+ Minifacture.create(name, attrs)
50
+ end
51
+
52
+ unless Object.const_defined? :Factory
53
+ Factory = Minifacture
54
+ alias Factory Minifacture
55
+ end
@@ -0,0 +1,129 @@
1
+ require './minifacture'
2
+ require 'test/unit'
3
+
4
+ class MinifactureTest < Test::Unit::TestCase
5
+ def test_should_define_factories
6
+ factories = Minifacture.class_variable_get :@@attrs
7
+ assert_not_nil factories["user"]
8
+ assert_not_nil factories["blog_entry"]
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.sub(/\d+$/) { |n| n.to_i.succ.to_s }, 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
+
71
+ def test_should_inherit
72
+ admin = Factory.create :admin
73
+ assert_equal 'admin', admin.login
74
+ assert_equal 'admin@example.com', admin.email
75
+ end
76
+
77
+ def test_should_alias
78
+ blog_entry = Factory.create :blog_entry
79
+ assert_equal 'admin', blog_entry.user.login
80
+ end
81
+
82
+ def test_should_accept_class_as_symbol
83
+ assert_nothing_raised do
84
+ guest = Factory.create :guest
85
+ end
86
+ end
87
+ end
88
+
89
+ class Mock
90
+ @@maximum = nil
91
+ def self.maximum(column)
92
+ @@maximum
93
+ end
94
+
95
+ def save!
96
+ @@maximum = @@maximum.to_i + 1 unless @saved
97
+ @saved = true
98
+ end
99
+
100
+ def new_record?
101
+ !@saved
102
+ end
103
+ end
104
+
105
+ class User < Mock
106
+ attr_accessor :login, :email, :password, :password_confirmation
107
+ end
108
+
109
+ class Post < Mock
110
+ attr_accessor :user
111
+ end
112
+
113
+ Minifacture.define :admin, :parent => :user do |f|
114
+ f.login "admin"
115
+ end
116
+
117
+ Minifacture.define :user do |f|
118
+ f.login "johndoe%d"
119
+ f.email "%{login}@example.com"
120
+ f.password f.password_confirmation("foobarbaz")
121
+ end
122
+
123
+ Minifacture.define :blog_entry, :class => Post do |f|
124
+ f.user { Minifacture :admin }
125
+ end
126
+
127
+ Minifacture.define :guest, :class => :user do |f|
128
+ f.login "guest"
129
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minifacture
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Stephen Celis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 3.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '10.0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 10.0.0
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '10.0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 10.0.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: i18n
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '0.6'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 0.6.0
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '0.6'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 0.6.0
73
+ description: Test::Unit begot MiniTest; factory_girl begets Minifacture.
74
+ email: stephen@stephencelis.com
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - minifacture.rb
80
+ - minifacture_test.rb
81
+ homepage: http://github.com/stephencelis/minifacture
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - "."
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 1.8.7
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.2.2
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: factory_girl for minitest.
105
+ test_files:
106
+ - minifacture_test.rb