cliches 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +25 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +105 -0
- data/Rakefile +23 -0
- data/cliches.gemspec +25 -0
- data/lib/cliches/active_record_extension.rb +13 -0
- data/lib/cliches/definition.rb +46 -0
- data/lib/cliches/railtie.rb +21 -0
- data/lib/cliches/test_extension.rb +6 -0
- data/lib/cliches/version.rb +3 -0
- data/lib/cliches.rb +11 -0
- data/lib/generators/cliches/new_generator.rb +15 -0
- data/lib/generators/cliches/templates/new_cliche.rb +3 -0
- data/test/definition_test.rb +41 -0
- data/test/test_helper.rb +2 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1ac6a4b5c4994d38b7238164ab96125e12e41e16
|
4
|
+
data.tar.gz: 832f4b2fe2e3c3791eb34b7a1ec7ec29e26c8fcb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0f810a89b1cc1242223ea0540dfe3d4c6a8e25818ffb3bd5117de6fc51de71cb48c0e5be415975dc5784f89428b015d4a13e9967463224e2f652de63dd86ad20
|
7
|
+
data.tar.gz: d9950a97eb8788a94a2d38e226f14bdd977c09a989d37f1b0d0e4518b86d3a59d9479773989cc7e8e91798289dd77a250c27ea733fa0ab32ab20977e799cb473
|
data/.gitignore
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
html/
|
12
|
+
lib/bundler/man
|
13
|
+
pkg
|
14
|
+
rdoc
|
15
|
+
spec/reports
|
16
|
+
test/tmp
|
17
|
+
test/version_tmp
|
18
|
+
tmp
|
19
|
+
*.bundle
|
20
|
+
*.so
|
21
|
+
*.o
|
22
|
+
*.a
|
23
|
+
mkmf.log
|
24
|
+
.idea
|
25
|
+
/.ruby-version
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 Eugen Minciu
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
# Cliches: Easily set up attributes for your tests.
|
2
|
+
|
3
|
+
## What is this?
|
4
|
+
|
5
|
+
Cliches solves the endless problem of having to pass large hashes of
|
6
|
+
parameters to different methods in your tests. This is a problem that
|
7
|
+
constantly appears when writing tests, a problem that people either
|
8
|
+
solve at the wrong level or don't solve at all.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'cliches'
|
15
|
+
|
16
|
+
And then run:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
## Why should I use this?
|
21
|
+
|
22
|
+
You may be thinking that fixtures of factories already solve this
|
23
|
+
problem. Well, they do, but only partly. Both of these solutions will
|
24
|
+
couple your attributes with ActiveRecord models, which really isn't
|
25
|
+
what you want. Consider these scenarios:
|
26
|
+
|
27
|
+
1. You are writing an integration test. How can you easily get the
|
28
|
+
attributes from a factory/fixture and just use them to do a POST?
|
29
|
+
2. You want to use the attributes from one factory to update an
|
30
|
+
existing record. How do you do that without instantiating a record
|
31
|
+
and potentially getting a lot of attributes you're not interested in.
|
32
|
+
3. You have a method that takes a hash but has nothing to do with
|
33
|
+
ActiveRecord. You will be calling this method with very similar
|
34
|
+
arguments from many different tests. How do you keep these similar
|
35
|
+
attributes organized?
|
36
|
+
|
37
|
+
|
38
|
+
## OK. What does this look like?
|
39
|
+
|
40
|
+
cliches are defined in your test directory, in plain Ruby files that
|
41
|
+
look like this:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
Cliches.define(:customer).as(
|
45
|
+
first_name: "Alice",
|
46
|
+
last_name: "Wilson",
|
47
|
+
state: "AL",
|
48
|
+
country: "US"
|
49
|
+
)
|
50
|
+
```
|
51
|
+
|
52
|
+
That's it; easy-peasy. Their basic usage looks like this:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
Customer.create!(cliches(:customer))
|
56
|
+
```
|
57
|
+
|
58
|
+
Or like this to just instantiate:
|
59
|
+
```
|
60
|
+
Customer.new(cliches(:customer))
|
61
|
+
```
|
62
|
+
|
63
|
+
But you can also do this:
|
64
|
+
```ruby
|
65
|
+
customer.update_attributes(cliches(:customer))
|
66
|
+
```
|
67
|
+
|
68
|
+
And even this:
|
69
|
+
```ruby
|
70
|
+
post :create, {customer: cliches(:customer)}
|
71
|
+
```
|
72
|
+
|
73
|
+
|
74
|
+
## Rails Goodies
|
75
|
+
There are a couple of things that the cliches gems provide to Rails
|
76
|
+
developers to keep their syntax sugar-levels adequately high.
|
77
|
+
|
78
|
+
There is a shortcut for building models:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
# This looks up cliches(:customer) and instantiates a model.
|
82
|
+
Customer.cliche
|
83
|
+
|
84
|
+
# This looks up cliches(:returning_customer) and instantiates a model
|
85
|
+
Customer.cliche(:returning)
|
86
|
+
|
87
|
+
# This does the same, but also saves the model
|
88
|
+
Customer.cliche!(:returning)
|
89
|
+
```
|
90
|
+
|
91
|
+
The Rails integration also comes with a simple little generator that
|
92
|
+
you can use to create cliche files. Here's an example of how to run it:
|
93
|
+
|
94
|
+
$ bin/rails g cliches:new customer
|
95
|
+
|
96
|
+
This will generate a file called test/cliches/customer.rb and populate
|
97
|
+
it with a simple example of how to use cliche.
|
98
|
+
|
99
|
+
## Contributing
|
100
|
+
|
101
|
+
1. Fork it ( https://github.com/thegengen/cliches/fork )
|
102
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
103
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
104
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
105
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
|
+
require 'cane/rake_task'
|
4
|
+
require 'rdoc/task'
|
5
|
+
|
6
|
+
desc "Run the tests"
|
7
|
+
Rake::TestTask.new do |t|
|
8
|
+
t.libs << "test"
|
9
|
+
t.test_files = FileList['test/*_test.rb']
|
10
|
+
t.verbose = true
|
11
|
+
end
|
12
|
+
|
13
|
+
desc "Run cane to check quality metrics"
|
14
|
+
Cane::RakeTask.new(:quality) do |cane|
|
15
|
+
cane.style_measure = 100
|
16
|
+
end
|
17
|
+
|
18
|
+
RDoc::Task.new do |rdoc|
|
19
|
+
rdoc.main = "README.md"
|
20
|
+
rdoc.rdoc_files.include("README.md", "lib/cliches/*.rb")
|
21
|
+
end
|
22
|
+
|
23
|
+
task default: [:test, :quality]
|
data/cliches.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cliches/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cliches"
|
8
|
+
spec.version = Cliches::VERSION
|
9
|
+
spec.authors = ["Eugen Minciu"]
|
10
|
+
spec.email = ["eugen@gengen.io"]
|
11
|
+
spec.summary = "Easily set up attributes for your tests."
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
21
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
22
|
+
spec.add_development_dependency "cane", "~> 3.0"
|
23
|
+
spec.add_development_dependency "rdoc", "~> 4.0"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# :nodoc:
|
2
|
+
module Cliches::ActiveRecordExtension
|
3
|
+
def cliche(name = nil)
|
4
|
+
full_name = [name, table_name.singularize].compact.join("_")
|
5
|
+
new(Cliches::Definition.find(full_name).value)
|
6
|
+
end
|
7
|
+
|
8
|
+
def cliche!(name = nil)
|
9
|
+
record = cliche(name)
|
10
|
+
record.save!
|
11
|
+
record
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# :nodoc:
|
2
|
+
module Cliches
|
3
|
+
def self.define(name)
|
4
|
+
Definition.new(name).register
|
5
|
+
end
|
6
|
+
|
7
|
+
# A class that holds the basic definition of a cliche.
|
8
|
+
class Definition
|
9
|
+
attr_reader :name
|
10
|
+
attr_reader :value
|
11
|
+
|
12
|
+
# Initialize a cliche with the given +name+ and set its value to an empty hash.
|
13
|
+
#
|
14
|
+
# +name+ will be converted to a symbol
|
15
|
+
def initialize(name)
|
16
|
+
@name = name.to_sym
|
17
|
+
@value = {}
|
18
|
+
end
|
19
|
+
|
20
|
+
# Set the value of a definition as being +value+.
|
21
|
+
def as(value)
|
22
|
+
@value = value
|
23
|
+
end
|
24
|
+
|
25
|
+
# Finds the definition with a given +name+. Returns +nil+ if it there is none.
|
26
|
+
def self.find(name)
|
27
|
+
definitions[name.to_sym]
|
28
|
+
end
|
29
|
+
|
30
|
+
# Registers a definition. This will override any existing definition with the same name.
|
31
|
+
def register
|
32
|
+
self.class.definitions[name] = self
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def self.definitions
|
39
|
+
@definitions ||= {}
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.reset!
|
43
|
+
@definitions = {}
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'rails/railtie'
|
3
|
+
|
4
|
+
module Cliches
|
5
|
+
class Railtie < ::Rails::Railtie
|
6
|
+
config.cliches = ActiveSupport::OrderedOptions.new
|
7
|
+
config.cliches.directory = 'test/cliches'
|
8
|
+
|
9
|
+
initializer "cliches.load_files" do |app|
|
10
|
+
Dir[File.join(app.config.cliches.directory, '*.rb')].each {|file| load file }
|
11
|
+
end
|
12
|
+
|
13
|
+
initializer "cliches.extend_testcase" do
|
14
|
+
ActiveSupport::TestCase.include(Cliches::TestExtension)
|
15
|
+
end
|
16
|
+
|
17
|
+
initializer "cliches.extend_activerecord" do
|
18
|
+
ActiveRecord::Base.extend(Cliches::ActiveRecordExtension)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/cliches.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'cliches/railtie'
|
2
|
+
require 'rails'
|
3
|
+
require 'rails/generators/base'
|
4
|
+
|
5
|
+
# :nodoc:
|
6
|
+
module Cliches
|
7
|
+
# :nodoc:
|
8
|
+
class NewGenerator < Rails::Generators::NamedBase
|
9
|
+
source_root File.join(File.dirname(__FILE__), "templates")
|
10
|
+
|
11
|
+
def create_cliche
|
12
|
+
template 'new_cliche.rb', File.join(Rails.configuration.cliches.directory, "#{file_name}.rb")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DefinitionTest < Minitest::Test
|
4
|
+
def setup
|
5
|
+
Cliches::Definition.reset!
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_new
|
9
|
+
d = Cliches::Definition.new(:test)
|
10
|
+
|
11
|
+
assert_equal :test, d.name
|
12
|
+
assert_equal Hash.new, d.value
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_new_with_string
|
16
|
+
d = Cliches::Definition.new('test')
|
17
|
+
|
18
|
+
assert_equal :test, d.name
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_as
|
22
|
+
d = Cliches::Definition.new(:test)
|
23
|
+
d.as(a: 3, b: 5)
|
24
|
+
|
25
|
+
assert_equal 3, d.value[:a]
|
26
|
+
assert_equal 5, d.value[:b]
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_define
|
30
|
+
d = Cliches.define(:example)
|
31
|
+
|
32
|
+
assert_equal d, Cliches::Definition.find(:example)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_define_as
|
36
|
+
Cliches.define(:example).as(a: 3)
|
37
|
+
|
38
|
+
d = Cliches::Definition.find(:example)
|
39
|
+
assert_equal 3, d.value[:a]
|
40
|
+
end
|
41
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cliches
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eugen Minciu
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: cane
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rdoc
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '4.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- eugen@gengen.io
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- cliches.gemspec
|
96
|
+
- lib/cliches.rb
|
97
|
+
- lib/cliches/active_record_extension.rb
|
98
|
+
- lib/cliches/definition.rb
|
99
|
+
- lib/cliches/railtie.rb
|
100
|
+
- lib/cliches/test_extension.rb
|
101
|
+
- lib/cliches/version.rb
|
102
|
+
- lib/generators/cliches/new_generator.rb
|
103
|
+
- lib/generators/cliches/templates/new_cliche.rb
|
104
|
+
- test/definition_test.rb
|
105
|
+
- test/test_helper.rb
|
106
|
+
homepage: ''
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.5.1
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: Easily set up attributes for your tests.
|
130
|
+
test_files:
|
131
|
+
- test/definition_test.rb
|
132
|
+
- test/test_helper.rb
|