midiskirt 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.travis.yml +7 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +23 -0
- data/README.rdoc +72 -0
- data/Rakefile +18 -0
- data/lib/midiskirt/version.rb +3 -0
- data/lib/midiskirt.rb +99 -0
- data/midiskirt.gemspec +26 -0
- data/test/test_helper.rb +48 -0
- data/test/unit/midiskirt_test.rb +107 -0
- metadata +94 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
midiskirt (1.2.0)
|
5
|
+
activesupport (>= 2.2)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (3.2.2)
|
11
|
+
i18n (~> 0.6)
|
12
|
+
multi_json (~> 1.0)
|
13
|
+
i18n (0.6.0)
|
14
|
+
multi_json (1.1.0)
|
15
|
+
rake (0.9.2)
|
16
|
+
|
17
|
+
PLATFORMS
|
18
|
+
ruby
|
19
|
+
|
20
|
+
DEPENDENCIES
|
21
|
+
bundler (>= 1.0.0)
|
22
|
+
midiskirt!
|
23
|
+
rake
|
data/README.rdoc
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
= Midiskirt {<img src="https://secure.travis-ci.org/y8/midiskirt.png?branch=master" alt="Build Status" />}[http://travis-ci.org/y8/midiskirt]
|
2
|
+
|
3
|
+
Pretty same as Miniskirt[https://github.com/stephencelis/miniskirt], but didn't hurt your eyes.
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
|
7
|
+
Factory girl, relaxed: http://www.stephencelis.com/2010/01/11/miniskirt.html
|
8
|
+
|
9
|
+
Factory.define :user do |f|
|
10
|
+
f.login 'johndoe%d' # Sequence.
|
11
|
+
f.email '%{login}@example.com' # Interpolate.
|
12
|
+
f.password f.password_confirmation('foobar') # Chain.
|
13
|
+
end
|
14
|
+
|
15
|
+
Factory.define :post do |f|
|
16
|
+
f.user { Factory :user } # Blocks, if you must.
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
== Install
|
21
|
+
|
22
|
+
% [sudo] gem install miniskirt
|
23
|
+
|
24
|
+
|
25
|
+
In a Rails ~> 3 project:
|
26
|
+
|
27
|
+
# Gemfile
|
28
|
+
gem "miniskirt"
|
29
|
+
|
30
|
+
% gem bundle # If necessary, `[sudo] gem install bundler`
|
31
|
+
|
32
|
+
# test/test_helper.rb
|
33
|
+
require "midiskirt"
|
34
|
+
require "factories" # If you define your factories in test/factories.rb
|
35
|
+
|
36
|
+
|
37
|
+
In a Rails ~> 2.2 project:
|
38
|
+
|
39
|
+
# config/environments/test.rb
|
40
|
+
config.gem "midiskirt"
|
41
|
+
|
42
|
+
% [sudo] rake gems:install
|
43
|
+
|
44
|
+
# test/test_helper.rb
|
45
|
+
require "factories" # If you define your factories in test/factories.rb
|
46
|
+
|
47
|
+
|
48
|
+
== License
|
49
|
+
|
50
|
+
(The MIT License)
|
51
|
+
|
52
|
+
(c) 2011 Stephen Celis, stephen@stephencelis.com.
|
53
|
+
|
54
|
+
(c) 2012 Alexey Bondar, y8@ya.ru
|
55
|
+
|
56
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
57
|
+
of this software and associated documentation files (the "Software"), to deal
|
58
|
+
in the Software without restriction, including without limitation the rights
|
59
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
60
|
+
copies of the Software, and to permit persons to whom the Software is
|
61
|
+
furnished to do so, subject to the following conditions:
|
62
|
+
|
63
|
+
The above copyright notice and this permission notice shall be included in all
|
64
|
+
copies or substantial portions of the Software.
|
65
|
+
|
66
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
67
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
68
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
69
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
70
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
71
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
72
|
+
SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
Rake::TestTask.new do |t|
|
6
|
+
t.libs << 'test'
|
7
|
+
t.test_files = Dir["test/**/*_test.rb"]
|
8
|
+
end
|
9
|
+
|
10
|
+
task :default => :test
|
11
|
+
|
12
|
+
task :loc do
|
13
|
+
count = 0
|
14
|
+
File.open("midiskirt.rb").each do |line|
|
15
|
+
count += 1 unless line =~ /^\s*($|#)/ # Any number of spaces followed by EOL or #
|
16
|
+
end
|
17
|
+
puts "#{count} lines of real code"
|
18
|
+
end
|
data/lib/midiskirt.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'active_support/inflector'
|
2
|
+
require 'active_support/core_ext/hash'
|
3
|
+
|
4
|
+
# Factory girl, relaxed.
|
5
|
+
#
|
6
|
+
# Factory.define :user do |f|
|
7
|
+
# f.login 'johndoe%d' # Sequence.
|
8
|
+
# f.email '%{login}@example.com' # Interpolate.
|
9
|
+
# f.password f.password_confirmation('foobar') # Chain.
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# Factory.define :post do |f|
|
13
|
+
# f.user { Factory :user } # Blocks, if you must.
|
14
|
+
# end
|
15
|
+
|
16
|
+
Midiskirt = Struct.new(:__name__, :__klass__, :__parent__, :__attrs__)
|
17
|
+
|
18
|
+
class Midiskirt
|
19
|
+
undef_method *instance_methods.grep(/^(?!__|object_id)/)
|
20
|
+
private_class_method :new # "Hide" constructor from world
|
21
|
+
|
22
|
+
# Do not use class variable, as it will be shared among all childrens and
|
23
|
+
# can be unintentionally changed.
|
24
|
+
@factories = {}
|
25
|
+
@sequence = Hash.new(0)
|
26
|
+
|
27
|
+
class << self
|
28
|
+
# Define new factory with given name. New instance of Midiskirt
|
29
|
+
# will be passed as argument to given block.
|
30
|
+
#
|
31
|
+
# Options are:
|
32
|
+
# * class - name of class to be instantiated. By default is same as name
|
33
|
+
# * parent - name of parent factory
|
34
|
+
def define(name, options = {})
|
35
|
+
name = name.to_s
|
36
|
+
|
37
|
+
# Get class name from options or use name
|
38
|
+
klass = options.delete(:class) { name }
|
39
|
+
parent = options.delete(:parent)
|
40
|
+
|
41
|
+
yield(@factories[name] = new(name, klass, parent, {}))
|
42
|
+
end
|
43
|
+
|
44
|
+
# Initialize and setup class from factory.
|
45
|
+
#
|
46
|
+
# You can override default factory settings, by passing them
|
47
|
+
# as second argument.
|
48
|
+
def build(name, attrs = {})
|
49
|
+
klass, parent, attributes = [:__klass__, :__parent__, :__attrs__].inject([]) {|acc, m| acc << @factories[name.to_s].__send__(m)}
|
50
|
+
|
51
|
+
# Create copy of attributes
|
52
|
+
attributes = attributes.dup
|
53
|
+
|
54
|
+
# If parent set, then merge parent template with current template
|
55
|
+
if parent
|
56
|
+
parent = parent.to_s
|
57
|
+
attributes = @factories[parent].__attrs__.merge(attributes)
|
58
|
+
klass = @factories[parent].__klass__
|
59
|
+
end
|
60
|
+
|
61
|
+
attributes.merge!(attrs).symbolize_keys!
|
62
|
+
|
63
|
+
# Interpolate attributes
|
64
|
+
attributes.each do |name, value|
|
65
|
+
attributes[name] = value.sub(/%\d*d/) {|d| d % (@sequence[klass] += 1) } % attributes if value.kind_of? String
|
66
|
+
end
|
67
|
+
|
68
|
+
# Convert klass to real Class
|
69
|
+
klass = klass.is_a?(Class) ? klass : klass.to_s.classify.constantize
|
70
|
+
|
71
|
+
klass.new do |record|
|
72
|
+
attributes.each do |name, value|
|
73
|
+
record.send(:"#{name}=", (value.kind_of?(Proc) ? value.call(record) : value).dup)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Create and save new factory product
|
79
|
+
def create(name, attrs = {})
|
80
|
+
build(name, attrs).tap { |record| record.save! }
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# Capture method calls, and save it to factory attributes
|
85
|
+
def method_missing(name, value = nil, &block)
|
86
|
+
__attrs__.merge!(name => block || value)
|
87
|
+
value # Return value to be able to use chaining like: f.password f.password_confirmation("something")
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# Shortcut to Midiskirt#create
|
92
|
+
def Midiskirt(name, attrs = {})
|
93
|
+
Midiskirt.create(name, attrs)
|
94
|
+
end
|
95
|
+
|
96
|
+
unless Object.const_defined? :Factory
|
97
|
+
Factory = Midiskirt
|
98
|
+
alias Factory Midiskirt
|
99
|
+
end
|
data/midiskirt.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "midiskirt/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "midiskirt"
|
7
|
+
s.version = Midiskirt::VERSION
|
8
|
+
s.summary = "factory_girl, relaxed"
|
9
|
+
s.description = "Test::Unit begot MiniTest; factory_girl begot Miniskirt, Miniskirt begets Midiskirt"
|
10
|
+
|
11
|
+
s.authors = ["Stephen Celis", "Alexey Bondar"]
|
12
|
+
s.email = ["stephen@stephencelis.com", "y8@ya.ru"]
|
13
|
+
s.homepage = "http://github.com/y8/midiskirt"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
# Development depensencies
|
21
|
+
s.add_development_dependency "bundler", ">= 1.0.0"
|
22
|
+
s.add_development_dependency "rake"
|
23
|
+
|
24
|
+
# Runtime dependencies
|
25
|
+
s.add_runtime_dependency "activesupport", RUBY_VERSION >= "1.9" ? ">= 2.2" : ">= 3.0"
|
26
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'midiskirt'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class Mock
|
5
|
+
def initialize
|
6
|
+
yield self
|
7
|
+
end
|
8
|
+
|
9
|
+
def save!
|
10
|
+
@saved = true
|
11
|
+
end
|
12
|
+
|
13
|
+
def new_record?
|
14
|
+
!@saved
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class User < Mock
|
19
|
+
attr_accessor :login, :email, :password, :password_confirmation, :settings
|
20
|
+
end
|
21
|
+
|
22
|
+
class Post < Mock
|
23
|
+
attr_accessor :user
|
24
|
+
end
|
25
|
+
|
26
|
+
Midiskirt.define :admin, :parent => :user do |f|
|
27
|
+
f.login "admin"
|
28
|
+
end
|
29
|
+
|
30
|
+
Midiskirt.define :user do |f|
|
31
|
+
f.login "johndoe%d"
|
32
|
+
f.email "%{login}@example.com"
|
33
|
+
f.password f.password_confirmation("foobarbaz")
|
34
|
+
end
|
35
|
+
|
36
|
+
Midiskirt.define :blog_entry, :class => Post do |f|
|
37
|
+
f.user { Midiskirt :admin }
|
38
|
+
end
|
39
|
+
|
40
|
+
DefaultSettings = {
|
41
|
+
"hair" => "green",
|
42
|
+
"eyes" => "gray"
|
43
|
+
}
|
44
|
+
|
45
|
+
Midiskirt.define :guest, :class => :user do |f|
|
46
|
+
f.login "guest"
|
47
|
+
f.settings DefaultSettings
|
48
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class MidiskirtTest < Test::Unit::TestCase
|
4
|
+
def test_should_define_factories
|
5
|
+
factories = Midiskirt.instance_variable_get :@factories
|
6
|
+
|
7
|
+
assert factories["user"]
|
8
|
+
assert 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.login}@example.com", user.email
|
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
|
+
|
88
|
+
def test_objects_should_not_corrupt_attribute_templates
|
89
|
+
factories = Midiskirt.instance_variable_get(:@factories)
|
90
|
+
assert_not_equal DefaultSettings.object_id, factories["guest"].__attrs__["settings"].object_id
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_factories_should_not_corrupt_attribute_templates
|
94
|
+
alice = Factory.build :guest
|
95
|
+
bob = Factory.build :guest
|
96
|
+
|
97
|
+
assert_not_equal DefaultSettings.object_id, alice.object_id, "Object from factory should not reference to template object"
|
98
|
+
|
99
|
+
alice.settings["eyes"] = "brown"
|
100
|
+
|
101
|
+
assert_equal "gray", bob.settings["eyes"]
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_should_sequence_without_database
|
105
|
+
assert_not_equal Factory.build(:user).login, Factory.build(:user).login
|
106
|
+
end
|
107
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: midiskirt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Stephen Celis
|
9
|
+
- Alexey Bondar
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-03-20 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bundler
|
17
|
+
requirement: &70132610155420 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.0.0
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *70132610155420
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rake
|
28
|
+
requirement: &70132610155000 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *70132610155000
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: activesupport
|
39
|
+
requirement: &70132610154440 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '2.2'
|
45
|
+
type: :runtime
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *70132610154440
|
48
|
+
description: Test::Unit begot MiniTest; factory_girl begot Miniskirt, Miniskirt begets
|
49
|
+
Midiskirt
|
50
|
+
email:
|
51
|
+
- stephen@stephencelis.com
|
52
|
+
- y8@ya.ru
|
53
|
+
executables: []
|
54
|
+
extensions: []
|
55
|
+
extra_rdoc_files: []
|
56
|
+
files:
|
57
|
+
- .gitignore
|
58
|
+
- .travis.yml
|
59
|
+
- Gemfile
|
60
|
+
- Gemfile.lock
|
61
|
+
- README.rdoc
|
62
|
+
- Rakefile
|
63
|
+
- lib/midiskirt.rb
|
64
|
+
- lib/midiskirt/version.rb
|
65
|
+
- midiskirt.gemspec
|
66
|
+
- test/test_helper.rb
|
67
|
+
- test/unit/midiskirt_test.rb
|
68
|
+
homepage: http://github.com/y8/midiskirt
|
69
|
+
licenses: []
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.8.10
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: factory_girl, relaxed
|
92
|
+
test_files:
|
93
|
+
- test/test_helper.rb
|
94
|
+
- test/unit/midiskirt_test.rb
|