contractinator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6819282048b246f1669f428baf93e442f7f62c0d
4
+ data.tar.gz: 61035a0c2b2e8c43359e764785fafe35abe5cce2
5
+ SHA512:
6
+ metadata.gz: f59a4e87e2db2ecc854a53a6bdf302d6eb9b80b077bc185e51c84d327dae47d2a01e89119ccc054968af77633d9ce99ee96a1be9bfb27591bdb70d66e7c5c8c2
7
+ data.tar.gz: 656e0a85e26ddeed7306e5dc8bf529b4bd2561a884ab063919f8a8ee4ee0fa388678f4a99d73761bb985f5a23e0f698c1af22d8a70427e67405cb6edec6b97a1
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .rake_tasks~
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at pair@pivotal.io. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in contractinator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 pivotal
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Contractinator
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/contractinator`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'contractinator'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install contractinator
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/contractinator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "contractinator"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'contractinator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "contractinator"
8
+ spec.version = Contractinator::VERSION
9
+ spec.authors = ["pivotal"]
10
+ spec.email = ["pair@pivotal.io"]
11
+
12
+ spec.summary = %q{Contractinator is a tool for contract-driven development!}
13
+ spec.homepage = "http://github.com/ehrenmurdick/contractinator"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rspec", "~> 3.0"
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
@@ -0,0 +1,58 @@
1
+ module Contractinator
2
+ module Contract
3
+ extend self
4
+
5
+ @created = {}
6
+ @fulfilled = {}
7
+
8
+ def require(argument)
9
+ @created[argument] = caller.find do |str|
10
+ str =~ /_spec\.rb/
11
+ end
12
+ end
13
+
14
+ def fulfill(argument)
15
+ @fulfilled[argument] = caller.find do |str|
16
+ str =~ /_spec\.rb/
17
+ end
18
+ end
19
+
20
+ def created_set
21
+ Set.new(@created.keys)
22
+ end
23
+
24
+ def fulfilled_set
25
+ Set.new(@fulfilled.keys)
26
+ end
27
+
28
+ def unmatched_created
29
+ unmatched_keys.intersection(created_set)
30
+ end
31
+
32
+ def unmatched_fulfilled
33
+ unmatched_keys.intersection(fulfilled_set)
34
+ end
35
+
36
+ def unmatched_keys
37
+ created_set ^ fulfilled_set
38
+ end
39
+
40
+ def validate
41
+ unmatched_keys.empty?
42
+ end
43
+
44
+ def messages
45
+ messages = []
46
+ unmatched_created.map do |item|
47
+ messages << "unfulfilled contract '#{item}'"
48
+ messages << " at #{@created[item]}"
49
+ end
50
+
51
+ unmatched_fulfilled.map do |item|
52
+ messages << "excess fulfillment '#{item}'"
53
+ messages << " at #{@fulfilled[item]}"
54
+ end
55
+ messages
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,171 @@
1
+ module Contractinator
2
+ module ContractHelpers
3
+ def fmt_dbl(dbl)
4
+ dbl.contract_inspect
5
+ end
6
+
7
+ def stipulate(dbl)
8
+ ContractAdapter.new(dbl, self)
9
+ end
10
+
11
+ def inject_contract(controller, name, dbl)
12
+ dbl_name = fmt_dbl(dbl).to_s.classify
13
+ Contractinator::Contract.require(
14
+ "#{controller.controller_name}_controller.#{name} injected #{dbl_name}"
15
+ )
16
+ controller.send("#{name}=", dbl)
17
+ end
18
+
19
+ def assign_contract(where, name, object)
20
+ Contractinator::Contract.require("#{where} assign @#{name}")
21
+ assign(name, object)
22
+ end
23
+
24
+ def flash_contract(where, key, value)
25
+ Contractinator::Contract.require("#{where} flash[#{key.inspect}]")
26
+ flash[key] = value
27
+ end
28
+
29
+ def render_contract(who)
30
+ Contractinator::Contract.require("#{who} render template")
31
+ render
32
+ end
33
+
34
+ def agree(obj, method, *args)
35
+ FulfillmentAdapter.new(self, obj, method, args)
36
+ end
37
+
38
+ def fulfill(*args, &block)
39
+ Contractinator::Contract.fulfill(*args, &block)
40
+ RSpec::Matchers.define "fulfill_#{args.first}" do
41
+ match { true }
42
+ description do
43
+ "fulfill #{args.first.inspect}"
44
+ end
45
+ end
46
+ expect(true).to send("fulfill_#{args.first}")
47
+ end
48
+ alias_method :fulfills, :fulfill
49
+
50
+ def xfulfills(*args, &block)
51
+ # noop
52
+ end
53
+
54
+ module Shared
55
+ def contract_message_signature(dbl, msg, args, value)
56
+ if args.try(:any?)
57
+ "#{fmt_dbl dbl}.#{msg}(#{fmt_args args}) -> #{fmt_args value}"
58
+ else
59
+ "#{fmt_dbl dbl}.#{msg} -> #{fmt_args value}"
60
+ end
61
+ end
62
+
63
+ def fmt_dbl(*args)
64
+ @context.fmt_dbl(*args)
65
+ end
66
+
67
+ def fmt_args(args)
68
+ args.map do |x|
69
+ fmt_dbl(x)
70
+ end.join(', ')
71
+ end
72
+
73
+ def customization(matcher, named)
74
+ matcher.
75
+ instance_variable_get('@recorded_customizations').
76
+ find do |x|
77
+ x.instance_variable_get('@method_name').to_s == named.to_s
78
+ end.try(:instance_variable_get, '@args')
79
+ end
80
+ end
81
+
82
+ class FulfillmentAdapter
83
+ include Shared
84
+
85
+ def initialize(context, object, method, args)
86
+ @object = object
87
+ @context = context
88
+ @method = method
89
+ @args = args
90
+ end
91
+
92
+ def will(matcher)
93
+ @context.expect(@object.send(@method, *@args)).to(matcher)
94
+ if matcher.is_a?(RSpec::Matchers::BuiltIn::BeAKindOf)
95
+ stand_in = @context.double(matcher.expected.instance_contract_inspect)
96
+ else
97
+ stand_in = matcher.expected
98
+ end
99
+ @context.fulfills(
100
+ contract_message_signature(@object, @method, @args, [stand_in])
101
+ )
102
+ end
103
+ end
104
+
105
+ class ContractAdapter
106
+ include Shared
107
+
108
+ def initialize(object, context)
109
+ @object = object
110
+ @context = context
111
+ end
112
+
113
+ def must(matcher)
114
+ @matcher = matcher
115
+
116
+ if matcher.is_a?(RSpec::Rails::Matchers::RoutingMatchers::RouteToMatcher)
117
+ contract_route_to_matcher
118
+ else
119
+ contract_receive_matcher
120
+ end
121
+ end
122
+
123
+ private
124
+
125
+ def contract_route_to_matcher
126
+ @expected = @matcher.instance_variable_get('@expected').with_indifferent_access
127
+ create_route_contract
128
+ create_route_expectation
129
+ end
130
+
131
+ def contract_receive_matcher
132
+ @message = @matcher.instance_variable_get('@message')
133
+ @arguments = customization(@matcher, :with)
134
+ @return_value = customization(@matcher, :and_return)
135
+
136
+ create_contract
137
+ create_expectation
138
+ end
139
+
140
+ def create_route_contract
141
+ method = @object.keys.first
142
+ controller = @expected[:controller]
143
+ action = @expected[:action]
144
+
145
+ params = @expected.except(:controller, :action)
146
+ if params.keys.any?
147
+ Contractinator::Contract.require("#{method} #{controller}##{action} #{params}")
148
+ else
149
+ Contractinator::Contract.require("#{method} #{controller}##{action}")
150
+ end
151
+ end
152
+
153
+ def create_route_expectation
154
+ @context.expect(@object).to @context.route_to(@expected)
155
+ end
156
+
157
+ def create_contract
158
+ args = @arguments
159
+ dbl = @object
160
+ msg = @message
161
+ value = @return_value
162
+
163
+ Contractinator::Contract.require(contract_message_signature(dbl, msg, args, value))
164
+ end
165
+
166
+ def create_expectation
167
+ @context.expect(@object).to @matcher.at_least(:once)
168
+ end
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,10 @@
1
+ module Contractinator
2
+ module CoreExtensions
3
+ module Array
4
+ def contract_inspect
5
+ values = map(&:contract_inspect).join(', ')
6
+ "[#{values}]"
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ module Contractinator
2
+ module CoreExtensions
3
+ module Class
4
+ def contract_inspect
5
+ class_contract_inspect
6
+ end
7
+
8
+ def class_contract_inspect
9
+ to_s
10
+ end
11
+
12
+ def instance_contract_inspect
13
+ to_s.underscore
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ module Contractinator
2
+ module CoreExtensions
3
+ module Hash
4
+ def contract_inspect
5
+ values = map do |key, value|
6
+ "#{key.contract_inspect}=>#{value.contract_inspect}"
7
+ end.join(', ')
8
+ "{#{values}}"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ module Contractinator
2
+ module CoreExtensions
3
+ module Object
4
+ def contract_inspect
5
+ instance_contract_inspect
6
+ end
7
+
8
+ def class_contract_inspect
9
+ self.class.to_s
10
+ end
11
+
12
+ def instance_contract_inspect
13
+ self.class.to_s.underscore
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ module Contractinator
2
+ module CoreExtensions
3
+ module String
4
+ def contract_inspect
5
+ '""'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Contractinator
2
+ module CoreExtensions
3
+ module Symbol
4
+ def contract_inspect
5
+ inspect
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ require_relative 'core_extensions/array'
2
+ require_relative 'core_extensions/class'
3
+ require_relative 'core_extensions/hash'
4
+ require_relative 'core_extensions/object'
5
+ require_relative 'core_extensions/symbol'
6
+ require_relative 'core_extensions/string'
7
+
8
+ module Contractinator
9
+ ::Object.include(CoreExtensions::Object)
10
+
11
+ ::Array.include(CoreExtensions::Array)
12
+ ::Class.include(CoreExtensions::Class)
13
+ ::Hash.include(CoreExtensions::Hash)
14
+ ::String.include(CoreExtensions::String)
15
+ ::Symbol.include(CoreExtensions::Symbol)
16
+ end
@@ -0,0 +1,9 @@
1
+ module Contractinator
2
+ module RSpecExtensions
3
+ module Double
4
+ def contract_inspect
5
+ @name.to_s
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ require_relative 'rspec_extensions/double'
2
+
3
+ module Contractinator
4
+ ::RSpec::Mocks::Double.include(RSpecExtensions::Double)
5
+ end
@@ -0,0 +1,3 @@
1
+ module Contractinator
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,8 @@
1
+ require "contractinator/version"
2
+ require "contractinator/contract"
3
+ require "contractinator/contract_helpers"
4
+ require "contractinator/core_extensions"
5
+ require "contractinator/rspec_extensions"
6
+
7
+ module Contractinator
8
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: contractinator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - pivotal
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description:
56
+ email:
57
+ - pair@pivotal.io
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - contractinator.gemspec
73
+ - lib/contractinator.rb
74
+ - lib/contractinator/contract.rb
75
+ - lib/contractinator/contract_helpers.rb
76
+ - lib/contractinator/core_extensions.rb
77
+ - lib/contractinator/core_extensions/array.rb
78
+ - lib/contractinator/core_extensions/class.rb
79
+ - lib/contractinator/core_extensions/hash.rb
80
+ - lib/contractinator/core_extensions/object.rb
81
+ - lib/contractinator/core_extensions/string.rb
82
+ - lib/contractinator/core_extensions/symbol.rb
83
+ - lib/contractinator/rspec_extensions.rb
84
+ - lib/contractinator/rspec_extensions/double.rb
85
+ - lib/contractinator/version.rb
86
+ homepage: http://github.com/ehrenmurdick/contractinator
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.4.5.1
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Contractinator is a tool for contract-driven development!
110
+ test_files: []