module-dbc 0.0.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of module-dbc might be problematic. Click here for more details.

@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b91aa9c40f61c36b9fea69888e61a5cea036abf3
4
+ data.tar.gz: 607b997635189048df47a3f10ae6ee43f6b2a1c7
5
+ SHA512:
6
+ metadata.gz: 49e6875c826d47f6454cfb70a2f62a5fe57359a5f5fe00c883976249b263e16a508dad05359f7ca261e7faa81935119116c72d9ef4250484b4135f86f3ec8a8c
7
+ data.tar.gz: 050a2ff09018d3c8c0f9195ce6be06035190956feb90407985e515b6778fcb05779db57266eccc1a02f96d21cb3adc100bdc7b09a9d9ceeb1797627009fbccef
@@ -0,0 +1,38 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .DS_Store
6
+ .ruby-version
7
+ coverage
8
+ coverage.data
9
+ InstalledFiles
10
+ lib/bundler/man
11
+ pkg
12
+ rdoc
13
+ spec/reports
14
+ test/tmp
15
+ test/version_tmp
16
+ tmp
17
+
18
+ # bundler
19
+ vendor/
20
+
21
+ # YARD artifacts
22
+ .yardoc
23
+ _yardoc
24
+ doc/
25
+
26
+ # tmp-old
27
+ .old
28
+
29
+ # editor
30
+ *~
31
+ .redcar
32
+
33
+ # other
34
+ *.lock
35
+ *.bak
36
+ tool/
37
+ .rbx/
38
+ *\#*
@@ -0,0 +1,18 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - ruby-head
5
+ - 2.2
6
+ - 2.1
7
+ - ruby-head
8
+ - rbx-2
9
+ - jruby-head
10
+
11
+ matrix:
12
+ allow_failures:
13
+ - rvm: ruby-head
14
+ - rvm: rbx-2
15
+ - rvm: jruby-head
16
+
17
+ before_install:
18
+ - gem install bundler
@@ -0,0 +1 @@
1
+ --readme README.md --private lib/**/*.rb
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'rake'
7
+ end
8
+
9
+ group :test do
10
+ gem 'rake'
11
+ end
@@ -0,0 +1,79 @@
1
+ module-dbc
2
+ ===========
3
+
4
+ [![Build Status](https://secure.travis-ci.org/kachick/module-dbc.png)](http://travis-ci.org/kachick/module-dbc)
5
+ [![Gem Version](https://badge.fury.io/rb/module-dbc.png)](http://badge.fury.io/rb/module-dbc)
6
+ [![Dependency Status](https://gemnasium.com/kachick/module-dbc.svg)](https://gemnasium.com/kachick/module-dbc)
7
+
8
+ Description
9
+ -----------
10
+
11
+ An imitation of DbC(Design By Contract) in Ruby.
12
+
13
+ Features
14
+ --------
15
+
16
+ * Pure Ruby :)
17
+
18
+ Usage
19
+ -----
20
+
21
+ ```ruby
22
+ require 'module/dbc'
23
+
24
+ class FooBar
25
+ extend Module::DbC
26
+
27
+ def initialize
28
+ @counter = 1
29
+ end
30
+
31
+ attr_reader :counter
32
+ dbc :counter, return: Integer # :return is an alias of :post
33
+
34
+ dbc def func(arg)
35
+ @counter += 1
36
+ arg + 1
37
+ end, pre: ->arg{ arg > 1 },
38
+ post: ->ret{ ret >= 10 },
39
+ invariant: ->{ @counter < 3 }
40
+ end
41
+
42
+ FooBar.new.func 0 #=> fail pre-conditon is invalid: (args: 0) (Module::DbC::PreConditionError)
43
+ FooBar.new.func 8 #=> fail post-conditon is invalid: (ret: 9) (Module::DbC::PostConditionError)
44
+ FooBar.new.func 9 #=> pass
45
+
46
+ foo = FooBar.new
47
+ foo.counter #=> 1
48
+ foo.func 11 #=> pass
49
+ foo.func 11 #=> fail invariant-conditon is invalid (Module::DbC::PostInvariantConditionError)
50
+ ```
51
+
52
+ Requirements
53
+ -------------
54
+
55
+ * Ruby - [2.1 or later](http://travis-ci.org/#!/kachick/module-dbc)
56
+
57
+ Install
58
+ -------
59
+
60
+ ```bash
61
+ gem install module-dbc
62
+ ```
63
+
64
+ Link
65
+ ----
66
+
67
+ * [Home](http://kachick.github.com/module-dbc/)
68
+ * [code](https://github.com/kachick/module-dbc)
69
+ * [API](http://www.rubydoc.info/github/kachick/module-dbc)
70
+ * [issues](https://github.com/kachick/module-dbc/issues)
71
+ * [CI](http://travis-ci.org/#!/kachick/module-dbc)
72
+ * [gem](https://rubygems.org/gems/module-dbc)
73
+
74
+ License
75
+ --------
76
+
77
+ The MIT X11 License
78
+ Copyright (c) 2013 Kenichi Kamiya
79
+ See MIT-LICENSE for further details.
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ task default: [:spec]
7
+
8
+ RSpec::Core::RakeTask.new(:spec) do |t|
9
+ t.pattern = 'spec/**/*_spec.rb'
10
+ t.ruby_opts = %w[-w]
11
+ end
@@ -0,0 +1,31 @@
1
+ # coding: us-ascii
2
+ $VERBOSE = true
3
+
4
+ require_relative '../lib/module/dbc'
5
+
6
+ class FooBar
7
+ extend Module::DbC
8
+
9
+ def initialize
10
+ @counter = 1
11
+ end
12
+
13
+ attr_reader :counter
14
+ dbc :counter, return: Integer # :return is an alias of :post
15
+
16
+ dbc def func(arg)
17
+ @counter += 1
18
+ arg + 1
19
+ end, pre: ->arg{ arg > 1 },
20
+ post: ->ret{ ret >= 10 },
21
+ invariant: ->{ @counter < 3 }
22
+ end
23
+
24
+ #FooBar.new.func 0 #=> fail pre-conditon is invalid: (args: 0) (Module::DbC::PreConditionError)
25
+ #FooBar.new.func 8 #=> fail post-conditon is invalid: (ret: 9) (Module::DbC::PostConditionError)
26
+ FooBar.new.func 9 #=> pass
27
+
28
+ foo = FooBar.new
29
+ p(foo.counter) #=> 1
30
+ p(foo.func 11) #=> pass
31
+ p(foo.func 11) #=> fail invariant-conditon is invalid (Module::DbC::PostInvariantConditionError)
@@ -0,0 +1,97 @@
1
+ # coding: us-ascii
2
+ # Copyright (c) 2013 Kenichi Kamiya
3
+
4
+ require 'optionalargument'
5
+
6
+ class Module
7
+
8
+ module DbC
9
+
10
+ class Error < StandardError; end
11
+ class PreConditionError < Error; end
12
+ class PostConditionError < Error; end
13
+ class InvariantConditionError < Error; end
14
+ class PreInvariantConditionError < InvariantConditionError; end
15
+ class PostInvariantConditionError < InvariantConditionError; end
16
+
17
+ # @return [Class]
18
+ DbCOptArg = OptionalArgument.define {
19
+ opt :precondition,
20
+ condition: Proc,
21
+ aliases: [:pre]
22
+
23
+ opt :postcondition,
24
+ condition: OR(AND(Proc, ->v{v.arity == 1}), CAN(:===)),
25
+ aliases: [:post, :return]
26
+
27
+ opt :invariant,
28
+ condition: Proc
29
+ }
30
+
31
+ private
32
+
33
+ # @param [Symbol, String, #to_sym] origin
34
+ # @param [Hash] options
35
+ # @option options [Proc] :precondition
36
+ # @option options [Proc] :pre same as :precondition
37
+ # @option options [Proc, #===] :postcondition
38
+ # @option options [Proc, #===] :post same as :postcondition
39
+ # @option options [Proc, #===] :return same as :postcondition
40
+ # @option options [Proc] :invariant
41
+ # @return [self]
42
+ def dbc(origin, options={})
43
+ origin = origin.to_sym
44
+ opts = DbCOptArg.parse options
45
+
46
+ @_dbc_prependable ||= ( prepend(prependable = Module.new); prependable )
47
+
48
+ @_dbc_prependable.module_exec do
49
+ define_method origin do |*args, &block|
50
+ if opts.pre?
51
+ unless instance_exec(*args, &opts.pre)
52
+ raise PreConditionError, "pre-conditon is invalid: (args: #{args.join ','})"
53
+ end
54
+ end
55
+
56
+ if opts.invariant?
57
+ unless instance_exec(&opts.invariant)
58
+ raise PreInvariantConditionError, "invariant-conditon is invalid"
59
+ end
60
+ end
61
+
62
+ ret = super(*args, &block)
63
+
64
+ if opts.invariant?
65
+ unless instance_exec(&opts.invariant)
66
+ raise PostInvariantConditionError, "invariant-conditon is invalid"
67
+ end
68
+ end
69
+
70
+ if opts.post?
71
+ if opts.post.kind_of?(Proc)
72
+ unless instance_exec(ret, &opts.post)
73
+ raise PostConditionError, "post-conditon is invalid: (return: #{ret})"
74
+ end
75
+ else
76
+ unless opts.post === ret
77
+ raise PostConditionError, "invalid return value: (return: #{ret}, expected: #{opts.post})"
78
+ end
79
+ end
80
+ end
81
+
82
+ ret
83
+ end
84
+ end
85
+
86
+ # For readability on ancestors
87
+ unless const_defined? :DbCPrpendable
88
+ const_set :DbCPrpendable, @_dbc_prependable
89
+ private_constant :DbCPrpendable
90
+ end
91
+
92
+ self
93
+ end
94
+
95
+ end
96
+
97
+ end
@@ -0,0 +1,33 @@
1
+ # coding: us-ascii
2
+
3
+ lib_name = 'module-dbc'.freeze
4
+
5
+ Gem::Specification.new do |gem|
6
+ # specific
7
+
8
+ gem.description = %q{An imitation of DbC(Design By Contract) in Ruby.}
9
+
10
+ gem.summary = gem.description.dup
11
+ gem.homepage = "https://github.com/kachick/#{lib_name}"
12
+ gem.license = 'MIT'
13
+ gem.name = lib_name.dup
14
+ gem.version = '0.0.3'
15
+
16
+ gem.required_ruby_version = '>= 2.1'
17
+
18
+ gem.add_dependency 'optionalargument', '~> 0.1'
19
+
20
+ gem.add_development_dependency 'rspec', '>= 3.3', '< 4'
21
+ gem.add_development_dependency 'yard', '>= 0.8.7.6', '< 0.9'
22
+ gem.add_development_dependency 'rake', '>= 10', '< 20'
23
+ gem.add_development_dependency 'bundler', '>= 1.10', '< 2'
24
+
25
+ # common
26
+
27
+ gem.authors = ['Kenichi Kamiya']
28
+ gem.email = ['kachick1+ruby@gmail.com']
29
+ gem.files = `git ls-files`.split($\)
30
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
31
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
32
+ gem.require_paths = ['lib']
33
+ end
@@ -0,0 +1,22 @@
1
+ # coding: us-ascii
2
+
3
+ require 'rspec'
4
+
5
+ require_relative '../lib/module/dbc'
6
+
7
+ module Module::DbC::SpecHelpers
8
+ class FooBar
9
+ extend Module::DbC
10
+
11
+ def initialize
12
+ @counter = 1
13
+ end
14
+
15
+ dbc def func(arg)
16
+ @counter += 1
17
+ arg + 1
18
+ end, pre: ->arg{ arg > 1 },
19
+ post: ->ret{ ret >= 10 },
20
+ invariant: ->{ @counter < 3 }
21
+ end
22
+ end
@@ -0,0 +1,114 @@
1
+ # coding: us-ascii
2
+
3
+ require_relative 'helper'
4
+
5
+ describe Module::DbC do
6
+ context '.dbc' do
7
+ before(:each) do
8
+ @foobar = Module::DbC::SpecHelpers::FooBar.new
9
+ end
10
+
11
+ it 'returns origin value when passed all conditions' do
12
+ expect(@foobar.func(9)).to eq(10)
13
+ end
14
+
15
+ it 'raises a PreConditionError if the argument block unmatched to condition' do
16
+ expect{@foobar.func(0)}.to raise_error(Module::DbC::PreConditionError)
17
+ end
18
+
19
+ context 'when the postconditon is a Proc' do
20
+ it 'raises a PostConditionError if the calculated value unmatched to condition' do
21
+ expect{@foobar.func(8)}.to raise_error(Module::DbC::PostConditionError)
22
+ end
23
+ end
24
+
25
+ context 'when the postconditon is not a Proc' do
26
+ it 'raises an InvariantConditionError if unmatched to invariant conditions' do
27
+ obj = Object.new
28
+ class << obj
29
+ extend Module::DbC
30
+ dbc def func(arg)
31
+ arg + 1
32
+ end, return: Fixnum
33
+ end
34
+
35
+ expect{obj.func(1.0)}.to raise_error(Module::DbC::PostConditionError)
36
+ expect(obj.func(1)).to eq(2)
37
+ end
38
+ end
39
+
40
+ it 'raises an InvariantConditionError if unmatched to invariant conditions' do
41
+ @foobar.func(9)
42
+ expect{@foobar.func(9)}.to raise_error(Module::DbC::InvariantConditionError)
43
+ end
44
+
45
+ it 'prepends specific named module' do
46
+ expect(Module::DbC::SpecHelpers::FooBar.ancestors.first.name).to \
47
+ eq('Module::DbC::SpecHelpers::FooBar::DbCPrpendable')
48
+ expect{Module::DbC::SpecHelpers::FooBar::DbCPrpendable}.to raise_error(NameError)
49
+ end
50
+
51
+ it 'overrides on the single prepended module when called twice' do
52
+ cls = Class.new do
53
+ extend Module::DbC
54
+ dbc def foo
55
+ :foo
56
+ end, return: Symbol
57
+
58
+ dbc def bar
59
+ :bar
60
+ end, return: String
61
+ end
62
+
63
+ expect(cls.ancestors[1]).to equal(cls)
64
+ expect(cls.new.foo).to eq(:foo)
65
+ expect{cls.new.bar}.to raise_error(Module::DbC::PostConditionError)
66
+ end
67
+
68
+ context 'variety of arguments' do
69
+ before :each do
70
+ cls = Class.new do
71
+ extend Module::DbC
72
+
73
+ def self.val
74
+ :class_val
75
+ end
76
+
77
+ def val
78
+ :intance_val
79
+ end
80
+
81
+ dbc def pre_checker
82
+ :ret
83
+ end, pre: -> { val == :intance_val }
84
+
85
+ dbc def post_checker
86
+ :ret
87
+ end, post: -> ret{ val == :intance_val }
88
+
89
+ dbc def multiple_arguments(must, df=:def, *rest, kmust:, kdf: :kdf, **krests)
90
+ :ret
91
+ end, pre: -> must, df, *rest, kmust:, kdf: :kdf, **krests{ kmust == :km }
92
+ end
93
+
94
+ @instance = cls.new
95
+ end
96
+
97
+ it 'sends arguments from any way' do
98
+ expect(@instance.multiple_arguments :must, :def2, kmust: :km).to eq(:ret)
99
+ end
100
+
101
+ context 'preconditions' do
102
+ it 'evaluates conditions under instance scope' do
103
+ expect(@instance.pre_checker).to eq(:ret)
104
+ end
105
+ end
106
+
107
+ context 'postconditions' do
108
+ it 'evaluates conditions under instance scope' do
109
+ expect(@instance.post_checker).to eq(:ret)
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: module-dbc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Kenichi Kamiya
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: optionalargument
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.3'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '4'
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '3.3'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '4'
47
+ - !ruby/object:Gem::Dependency
48
+ name: yard
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 0.8.7.6
54
+ - - "<"
55
+ - !ruby/object:Gem::Version
56
+ version: '0.9'
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 0.8.7.6
64
+ - - "<"
65
+ - !ruby/object:Gem::Version
66
+ version: '0.9'
67
+ - !ruby/object:Gem::Dependency
68
+ name: rake
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '10'
74
+ - - "<"
75
+ - !ruby/object:Gem::Version
76
+ version: '20'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '10'
84
+ - - "<"
85
+ - !ruby/object:Gem::Version
86
+ version: '20'
87
+ - !ruby/object:Gem::Dependency
88
+ name: bundler
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '1.10'
94
+ - - "<"
95
+ - !ruby/object:Gem::Version
96
+ version: '2'
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '1.10'
104
+ - - "<"
105
+ - !ruby/object:Gem::Version
106
+ version: '2'
107
+ description: An imitation of DbC(Design By Contract) in Ruby.
108
+ email:
109
+ - kachick1+ruby@gmail.com
110
+ executables: []
111
+ extensions: []
112
+ extra_rdoc_files: []
113
+ files:
114
+ - ".gitignore"
115
+ - ".travis.yml"
116
+ - ".yardopts"
117
+ - Gemfile
118
+ - README.md
119
+ - Rakefile
120
+ - examples/README.rb
121
+ - lib/module/dbc.rb
122
+ - module-dbc.gemspec
123
+ - spec/helper.rb
124
+ - spec/module-dbc_spec.rb
125
+ homepage: https://github.com/kachick/module-dbc
126
+ licenses:
127
+ - MIT
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '2.1'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.4.5
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: An imitation of DbC(Design By Contract) in Ruby.
149
+ test_files:
150
+ - spec/helper.rb
151
+ - spec/module-dbc_spec.rb
152
+ has_rdoc: