entitlements 0.1.8 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/bin/deploy-entitlements +10 -1
- data/lib/contracts-ruby2/CHANGELOG.markdown +115 -0
- data/lib/contracts-ruby2/Gemfile +17 -0
- data/lib/contracts-ruby2/LICENSE +23 -0
- data/lib/contracts-ruby2/README.md +108 -0
- data/lib/contracts-ruby2/Rakefile +8 -0
- data/lib/contracts-ruby2/TODO.markdown +6 -0
- data/lib/contracts-ruby2/TUTORIAL.md +773 -0
- data/lib/contracts-ruby2/benchmarks/bench.rb +67 -0
- data/lib/contracts-ruby2/benchmarks/hash.rb +69 -0
- data/lib/contracts-ruby2/benchmarks/invariants.rb +91 -0
- data/lib/contracts-ruby2/benchmarks/io.rb +62 -0
- data/lib/contracts-ruby2/benchmarks/wrap_test.rb +57 -0
- data/lib/contracts-ruby2/contracts.gemspec +17 -0
- data/lib/contracts-ruby2/cucumber.yml +1 -0
- data/lib/contracts-ruby2/dependabot.yml +20 -0
- data/lib/contracts-ruby2/features/README.md +17 -0
- data/lib/contracts-ruby2/features/basics/functype.feature +71 -0
- data/lib/contracts-ruby2/features/basics/pretty-print.feature +241 -0
- data/lib/contracts-ruby2/features/basics/simple_example.feature +210 -0
- data/lib/contracts-ruby2/features/builtin_contracts/README.md +22 -0
- data/lib/contracts-ruby2/features/builtin_contracts/and.feature +103 -0
- data/lib/contracts-ruby2/features/builtin_contracts/any.feature +44 -0
- data/lib/contracts-ruby2/features/builtin_contracts/args.feature +80 -0
- data/lib/contracts-ruby2/features/builtin_contracts/array_of.feature +1 -0
- data/lib/contracts-ruby2/features/builtin_contracts/bool.feature +64 -0
- data/lib/contracts-ruby2/features/builtin_contracts/enum.feature +1 -0
- data/lib/contracts-ruby2/features/builtin_contracts/eq.feature +1 -0
- data/lib/contracts-ruby2/features/builtin_contracts/exactly.feature +1 -0
- data/lib/contracts-ruby2/features/builtin_contracts/func.feature +1 -0
- data/lib/contracts-ruby2/features/builtin_contracts/hash_of.feature +1 -0
- data/lib/contracts-ruby2/features/builtin_contracts/int.feature +93 -0
- data/lib/contracts-ruby2/features/builtin_contracts/keyword_args.feature +1 -0
- data/lib/contracts-ruby2/features/builtin_contracts/maybe.feature +1 -0
- data/lib/contracts-ruby2/features/builtin_contracts/nat.feature +115 -0
- data/lib/contracts-ruby2/features/builtin_contracts/nat_pos.feature +119 -0
- data/lib/contracts-ruby2/features/builtin_contracts/neg.feature +115 -0
- data/lib/contracts-ruby2/features/builtin_contracts/none.feature +145 -0
- data/lib/contracts-ruby2/features/builtin_contracts/not.feature +1 -0
- data/lib/contracts-ruby2/features/builtin_contracts/num.feature +64 -0
- data/lib/contracts-ruby2/features/builtin_contracts/or.feature +83 -0
- data/lib/contracts-ruby2/features/builtin_contracts/pos.feature +116 -0
- data/lib/contracts-ruby2/features/builtin_contracts/range_of.feature +1 -0
- data/lib/contracts-ruby2/features/builtin_contracts/respond_to.feature +78 -0
- data/lib/contracts-ruby2/features/builtin_contracts/send.feature +147 -0
- data/lib/contracts-ruby2/features/builtin_contracts/set_of.feature +1 -0
- data/lib/contracts-ruby2/features/builtin_contracts/xor.feature +99 -0
- data/lib/contracts-ruby2/features/support/env.rb +6 -0
- data/lib/contracts-ruby2/lib/contracts/attrs.rb +24 -0
- data/lib/contracts-ruby2/lib/contracts/builtin_contracts.rb +542 -0
- data/lib/contracts-ruby2/lib/contracts/call_with.rb +108 -0
- data/lib/contracts-ruby2/lib/contracts/core.rb +52 -0
- data/lib/contracts-ruby2/lib/contracts/decorators.rb +47 -0
- data/lib/contracts-ruby2/lib/contracts/engine/base.rb +136 -0
- data/lib/contracts-ruby2/lib/contracts/engine/eigenclass.rb +50 -0
- data/lib/contracts-ruby2/lib/contracts/engine/target.rb +70 -0
- data/lib/contracts-ruby2/lib/contracts/engine.rb +26 -0
- data/lib/contracts-ruby2/lib/contracts/errors.rb +71 -0
- data/lib/contracts-ruby2/lib/contracts/formatters.rb +136 -0
- data/lib/contracts-ruby2/lib/contracts/invariants.rb +68 -0
- data/lib/contracts-ruby2/lib/contracts/method_handler.rb +187 -0
- data/lib/contracts-ruby2/lib/contracts/method_reference.rb +100 -0
- data/lib/contracts-ruby2/lib/contracts/support.rb +61 -0
- data/lib/contracts-ruby2/lib/contracts/validators.rb +139 -0
- data/lib/contracts-ruby2/lib/contracts/version.rb +3 -0
- data/lib/contracts-ruby2/lib/contracts.rb +281 -0
- data/lib/contracts-ruby2/script/docs-release +3 -0
- data/lib/contracts-ruby2/script/docs-staging +3 -0
- data/lib/contracts-ruby2/script/rubocop.rb +5 -0
- data/lib/contracts-ruby2/spec/attrs_spec.rb +119 -0
- data/lib/contracts-ruby2/spec/builtin_contracts_spec.rb +461 -0
- data/lib/contracts-ruby2/spec/contracts_spec.rb +770 -0
- data/lib/contracts-ruby2/spec/fixtures/fixtures.rb +730 -0
- data/lib/contracts-ruby2/spec/invariants_spec.rb +17 -0
- data/lib/contracts-ruby2/spec/methods_spec.rb +54 -0
- data/lib/contracts-ruby2/spec/module_spec.rb +18 -0
- data/lib/contracts-ruby2/spec/override_validators_spec.rb +162 -0
- data/lib/contracts-ruby2/spec/ruby_version_specific/contracts_spec_1.9.rb +24 -0
- data/lib/contracts-ruby2/spec/ruby_version_specific/contracts_spec_2.0.rb +55 -0
- data/lib/contracts-ruby2/spec/ruby_version_specific/contracts_spec_2.1.rb +63 -0
- data/lib/contracts-ruby2/spec/spec_helper.rb +102 -0
- data/lib/contracts-ruby2/spec/support.rb +10 -0
- data/lib/contracts-ruby2/spec/support_spec.rb +21 -0
- data/lib/contracts-ruby2/spec/validators_spec.rb +47 -0
- data/lib/contracts-ruby3/CHANGELOG.markdown +117 -0
- data/lib/contracts-ruby3/Gemfile +21 -0
- data/lib/contracts-ruby3/LICENSE +23 -0
- data/lib/contracts-ruby3/README.md +114 -0
- data/lib/contracts-ruby3/Rakefile +10 -0
- data/lib/contracts-ruby3/TODO.markdown +6 -0
- data/lib/contracts-ruby3/TUTORIAL.md +773 -0
- data/lib/contracts-ruby3/benchmarks/bench.rb +67 -0
- data/lib/contracts-ruby3/benchmarks/hash.rb +69 -0
- data/lib/contracts-ruby3/benchmarks/invariants.rb +91 -0
- data/lib/contracts-ruby3/benchmarks/io.rb +62 -0
- data/lib/contracts-ruby3/benchmarks/wrap_test.rb +57 -0
- data/lib/contracts-ruby3/contracts.gemspec +20 -0
- data/lib/contracts-ruby3/cucumber.yml +1 -0
- data/lib/contracts-ruby3/dependabot.yml +20 -0
- data/lib/contracts-ruby3/features/README.md +17 -0
- data/lib/contracts-ruby3/features/basics/functype.feature +71 -0
- data/lib/contracts-ruby3/features/basics/pretty-print.feature +241 -0
- data/lib/contracts-ruby3/features/basics/simple_example.feature +210 -0
- data/lib/contracts-ruby3/features/builtin_contracts/README.md +22 -0
- data/lib/contracts-ruby3/features/builtin_contracts/and.feature +103 -0
- data/lib/contracts-ruby3/features/builtin_contracts/any.feature +44 -0
- data/lib/contracts-ruby3/features/builtin_contracts/args.feature +80 -0
- data/lib/contracts-ruby3/features/builtin_contracts/array_of.feature +1 -0
- data/lib/contracts-ruby3/features/builtin_contracts/bool.feature +64 -0
- data/lib/contracts-ruby3/features/builtin_contracts/enum.feature +1 -0
- data/lib/contracts-ruby3/features/builtin_contracts/eq.feature +1 -0
- data/lib/contracts-ruby3/features/builtin_contracts/exactly.feature +1 -0
- data/lib/contracts-ruby3/features/builtin_contracts/func.feature +1 -0
- data/lib/contracts-ruby3/features/builtin_contracts/hash_of.feature +1 -0
- data/lib/contracts-ruby3/features/builtin_contracts/int.feature +93 -0
- data/lib/contracts-ruby3/features/builtin_contracts/keyword_args.feature +1 -0
- data/lib/contracts-ruby3/features/builtin_contracts/maybe.feature +1 -0
- data/lib/contracts-ruby3/features/builtin_contracts/nat.feature +115 -0
- data/lib/contracts-ruby3/features/builtin_contracts/nat_pos.feature +119 -0
- data/lib/contracts-ruby3/features/builtin_contracts/neg.feature +115 -0
- data/lib/contracts-ruby3/features/builtin_contracts/none.feature +145 -0
- data/lib/contracts-ruby3/features/builtin_contracts/not.feature +1 -0
- data/lib/contracts-ruby3/features/builtin_contracts/num.feature +64 -0
- data/lib/contracts-ruby3/features/builtin_contracts/or.feature +83 -0
- data/lib/contracts-ruby3/features/builtin_contracts/pos.feature +116 -0
- data/lib/contracts-ruby3/features/builtin_contracts/range_of.feature +1 -0
- data/lib/contracts-ruby3/features/builtin_contracts/respond_to.feature +78 -0
- data/lib/contracts-ruby3/features/builtin_contracts/send.feature +147 -0
- data/lib/contracts-ruby3/features/builtin_contracts/set_of.feature +1 -0
- data/lib/contracts-ruby3/features/builtin_contracts/xor.feature +99 -0
- data/lib/contracts-ruby3/features/support/env.rb +8 -0
- data/lib/contracts-ruby3/lib/contracts/attrs.rb +26 -0
- data/lib/contracts-ruby3/lib/contracts/builtin_contracts.rb +575 -0
- data/lib/contracts-ruby3/lib/contracts/call_with.rb +119 -0
- data/lib/contracts-ruby3/lib/contracts/core.rb +54 -0
- data/lib/contracts-ruby3/lib/contracts/decorators.rb +50 -0
- data/lib/contracts-ruby3/lib/contracts/engine/base.rb +137 -0
- data/lib/contracts-ruby3/lib/contracts/engine/eigenclass.rb +51 -0
- data/lib/contracts-ruby3/lib/contracts/engine/target.rb +72 -0
- data/lib/contracts-ruby3/lib/contracts/engine.rb +28 -0
- data/lib/contracts-ruby3/lib/contracts/errors.rb +74 -0
- data/lib/contracts-ruby3/lib/contracts/formatters.rb +140 -0
- data/lib/contracts-ruby3/lib/contracts/invariants.rb +72 -0
- data/lib/contracts-ruby3/lib/contracts/method_handler.rb +197 -0
- data/lib/contracts-ruby3/lib/contracts/method_reference.rb +102 -0
- data/lib/contracts-ruby3/lib/contracts/support.rb +63 -0
- data/lib/contracts-ruby3/lib/contracts/validators.rb +143 -0
- data/lib/contracts-ruby3/lib/contracts/version.rb +5 -0
- data/lib/contracts-ruby3/lib/contracts.rb +290 -0
- data/lib/contracts-ruby3/script/docs-release +3 -0
- data/lib/contracts-ruby3/script/docs-staging +3 -0
- data/lib/contracts-ruby3/script/rubocop.rb +5 -0
- data/lib/contracts-ruby3/spec/attrs_spec.rb +119 -0
- data/lib/contracts-ruby3/spec/builtin_contracts_spec.rb +457 -0
- data/lib/contracts-ruby3/spec/contracts_spec.rb +773 -0
- data/lib/contracts-ruby3/spec/fixtures/fixtures.rb +725 -0
- data/lib/contracts-ruby3/spec/invariants_spec.rb +17 -0
- data/lib/contracts-ruby3/spec/methods_spec.rb +54 -0
- data/lib/contracts-ruby3/spec/module_spec.rb +18 -0
- data/lib/contracts-ruby3/spec/override_validators_spec.rb +162 -0
- data/lib/contracts-ruby3/spec/ruby_version_specific/contracts_spec_1.9.rb +24 -0
- data/lib/contracts-ruby3/spec/ruby_version_specific/contracts_spec_2.0.rb +55 -0
- data/lib/contracts-ruby3/spec/ruby_version_specific/contracts_spec_2.1.rb +63 -0
- data/lib/contracts-ruby3/spec/spec_helper.rb +102 -0
- data/lib/contracts-ruby3/spec/support.rb +10 -0
- data/lib/contracts-ruby3/spec/support_spec.rb +21 -0
- data/lib/contracts-ruby3/spec/validators_spec.rb +47 -0
- data/lib/entitlements/data/groups/calculated/yaml.rb +7 -1
- data/lib/entitlements/data/people/yaml.rb +9 -1
- data/lib/entitlements/extras/ldap_group/rules/ldap_group.rb +5 -1
- data/lib/entitlements/extras/orgchart/person_methods.rb +7 -1
- data/lib/entitlements.rb +13 -2
- data/lib/ruby_version_check.rb +17 -0
- metadata +209 -14
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
require "./lib/contracts"
|
|
2
|
+
require "benchmark"
|
|
3
|
+
require "rubygems"
|
|
4
|
+
require "method_profiler"
|
|
5
|
+
require "ruby-prof"
|
|
6
|
+
|
|
7
|
+
include Contracts
|
|
8
|
+
|
|
9
|
+
def add a, b
|
|
10
|
+
a + b
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
Contract Num, Num => Num
|
|
14
|
+
def contracts_add a, b
|
|
15
|
+
a + b
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def explicit_add a, b
|
|
19
|
+
fail unless a.is_a?(Numeric)
|
|
20
|
+
fail unless b.is_a?(Numeric)
|
|
21
|
+
c = a + b
|
|
22
|
+
fail unless c.is_a?(Numeric)
|
|
23
|
+
c
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def benchmark
|
|
27
|
+
Benchmark.bm 30 do |x|
|
|
28
|
+
x.report "testing add" do
|
|
29
|
+
1_000_000.times do |_|
|
|
30
|
+
add(rand(1000), rand(1000))
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
x.report "testing contracts add" do
|
|
34
|
+
1_000_000.times do |_|
|
|
35
|
+
contracts_add(rand(1000), rand(1000))
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def profile
|
|
42
|
+
profilers = []
|
|
43
|
+
profilers << MethodProfiler.observe(Contract)
|
|
44
|
+
profilers << MethodProfiler.observe(Object)
|
|
45
|
+
profilers << MethodProfiler.observe(Contracts::MethodDecorators)
|
|
46
|
+
profilers << MethodProfiler.observe(Contracts::Decorator)
|
|
47
|
+
profilers << MethodProfiler.observe(Contracts::Support)
|
|
48
|
+
profilers << MethodProfiler.observe(UnboundMethod)
|
|
49
|
+
10_000.times do |_|
|
|
50
|
+
contracts_add(rand(1000), rand(1000))
|
|
51
|
+
end
|
|
52
|
+
profilers.each { |p| puts p.report }
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def ruby_prof
|
|
56
|
+
RubyProf.start
|
|
57
|
+
100_000.times do |_|
|
|
58
|
+
contracts_add(rand(1000), rand(1000))
|
|
59
|
+
end
|
|
60
|
+
result = RubyProf.stop
|
|
61
|
+
printer = RubyProf::FlatPrinter.new(result)
|
|
62
|
+
printer.print(STDOUT)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
benchmark
|
|
66
|
+
profile
|
|
67
|
+
ruby_prof if ENV["FULL_BENCH"] # takes some time
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
require "./lib/contracts"
|
|
2
|
+
require "benchmark"
|
|
3
|
+
require "rubygems"
|
|
4
|
+
require "method_profiler"
|
|
5
|
+
require "ruby-prof"
|
|
6
|
+
|
|
7
|
+
include Contracts
|
|
8
|
+
|
|
9
|
+
def add opts
|
|
10
|
+
opts[:a] + opts[:b]
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
Contract ({ :a => Num, :b => Num}) => Num
|
|
14
|
+
def contracts_add opts
|
|
15
|
+
opts[:a] + opts[:b]
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def explicit_add opts
|
|
19
|
+
a = opts[:a]
|
|
20
|
+
b = opts[:b]
|
|
21
|
+
fail unless a.is_a?(Numeric)
|
|
22
|
+
fail unless b.is_a?(Numeric)
|
|
23
|
+
c = a + b
|
|
24
|
+
fail unless c.is_a?(Numeric)
|
|
25
|
+
c
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def benchmark
|
|
29
|
+
Benchmark.bm 30 do |x|
|
|
30
|
+
x.report "testing add" do
|
|
31
|
+
1_000_000.times do |_|
|
|
32
|
+
add(:a => rand(1000), :b => rand(1000))
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
x.report "testing contracts add" do
|
|
36
|
+
1_000_000.times do |_|
|
|
37
|
+
contracts_add(:a => rand(1000), :b => rand(1000))
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def profile
|
|
44
|
+
profilers = []
|
|
45
|
+
profilers << MethodProfiler.observe(Contract)
|
|
46
|
+
profilers << MethodProfiler.observe(Object)
|
|
47
|
+
profilers << MethodProfiler.observe(Contracts::MethodDecorators)
|
|
48
|
+
profilers << MethodProfiler.observe(Contracts::Decorator)
|
|
49
|
+
profilers << MethodProfiler.observe(Contracts::Support)
|
|
50
|
+
profilers << MethodProfiler.observe(UnboundMethod)
|
|
51
|
+
10_000.times do |_|
|
|
52
|
+
contracts_add(:a => rand(1000), :b => rand(1000))
|
|
53
|
+
end
|
|
54
|
+
profilers.each { |p| puts p.report }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def ruby_prof
|
|
58
|
+
RubyProf.start
|
|
59
|
+
100_000.times do |_|
|
|
60
|
+
contracts_add(:a => rand(1000), :b => rand(1000))
|
|
61
|
+
end
|
|
62
|
+
result = RubyProf.stop
|
|
63
|
+
printer = RubyProf::FlatPrinter.new(result)
|
|
64
|
+
printer.print(STDOUT)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
benchmark
|
|
68
|
+
profile
|
|
69
|
+
ruby_prof if ENV["FULL_BENCH"] # takes some time
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
require "./lib/contracts"
|
|
2
|
+
require "benchmark"
|
|
3
|
+
require "rubygems"
|
|
4
|
+
require "method_profiler"
|
|
5
|
+
require "ruby-prof"
|
|
6
|
+
|
|
7
|
+
class Obj
|
|
8
|
+
include Contracts
|
|
9
|
+
|
|
10
|
+
attr_accessor :value
|
|
11
|
+
def initialize value
|
|
12
|
+
@value = value
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
Contract Num, Num => Num
|
|
16
|
+
def contracts_add a, b
|
|
17
|
+
a + b
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
class ObjWithInvariants
|
|
22
|
+
include Contracts
|
|
23
|
+
include Contracts::Invariants
|
|
24
|
+
|
|
25
|
+
invariant(:value_not_nil) { value != nil }
|
|
26
|
+
invariant(:value_not_string) { !value.is_a?(String) }
|
|
27
|
+
|
|
28
|
+
attr_accessor :value
|
|
29
|
+
def initialize value
|
|
30
|
+
@value = value
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
Contract Num, Num => Num
|
|
34
|
+
def contracts_add a, b
|
|
35
|
+
a + b
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def benchmark
|
|
40
|
+
obj = Obj.new(3)
|
|
41
|
+
obj_with_invariants = ObjWithInvariants.new(3)
|
|
42
|
+
|
|
43
|
+
Benchmark.bm 30 do |x|
|
|
44
|
+
x.report "testing contracts add" do
|
|
45
|
+
1_000_000.times do |_|
|
|
46
|
+
obj.contracts_add(rand(1000), rand(1000))
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
x.report "testing contracts add with invariants" do
|
|
50
|
+
1_000_000.times do |_|
|
|
51
|
+
obj_with_invariants.contracts_add(rand(1000), rand(1000))
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def profile
|
|
58
|
+
obj_with_invariants = ObjWithInvariants.new(3)
|
|
59
|
+
|
|
60
|
+
profilers = []
|
|
61
|
+
profilers << MethodProfiler.observe(Contract)
|
|
62
|
+
profilers << MethodProfiler.observe(Object)
|
|
63
|
+
profilers << MethodProfiler.observe(Contracts::Support)
|
|
64
|
+
profilers << MethodProfiler.observe(Contracts::Invariants)
|
|
65
|
+
profilers << MethodProfiler.observe(Contracts::Invariants::InvariantExtension)
|
|
66
|
+
profilers << MethodProfiler.observe(UnboundMethod)
|
|
67
|
+
|
|
68
|
+
10_000.times do |_|
|
|
69
|
+
obj_with_invariants.contracts_add(rand(1000), rand(1000))
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
profilers.each { |p| puts p.report }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def ruby_prof
|
|
76
|
+
RubyProf.start
|
|
77
|
+
|
|
78
|
+
obj_with_invariants = ObjWithInvariants.new(3)
|
|
79
|
+
|
|
80
|
+
100_000.times do |_|
|
|
81
|
+
obj_with_invariants.contracts_add(rand(1000), rand(1000))
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
result = RubyProf.stop
|
|
85
|
+
printer = RubyProf::FlatPrinter.new(result)
|
|
86
|
+
printer.print(STDOUT)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
benchmark
|
|
90
|
+
profile
|
|
91
|
+
ruby_prof if ENV["FULL_BENCH"] # takes some time
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
require "./lib/contracts"
|
|
2
|
+
require "benchmark"
|
|
3
|
+
require "rubygems"
|
|
4
|
+
require "method_profiler"
|
|
5
|
+
require "ruby-prof"
|
|
6
|
+
require "open-uri"
|
|
7
|
+
|
|
8
|
+
include Contracts
|
|
9
|
+
|
|
10
|
+
def download url
|
|
11
|
+
open("http://www.#{url}/").read
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
Contract String => String
|
|
15
|
+
def contracts_download url
|
|
16
|
+
open("http://www.#{url}").read
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
@urls = %w{google.com bing.com}
|
|
20
|
+
|
|
21
|
+
def benchmark
|
|
22
|
+
Benchmark.bm 30 do |x|
|
|
23
|
+
x.report "testing download" do
|
|
24
|
+
100.times do |_|
|
|
25
|
+
download(@urls.sample)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
x.report "testing contracts download" do
|
|
29
|
+
100.times do |_|
|
|
30
|
+
contracts_download(@urls.sample)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def profile
|
|
37
|
+
profilers = []
|
|
38
|
+
profilers << MethodProfiler.observe(Contract)
|
|
39
|
+
profilers << MethodProfiler.observe(Object)
|
|
40
|
+
profilers << MethodProfiler.observe(Contracts::MethodDecorators)
|
|
41
|
+
profilers << MethodProfiler.observe(Contracts::Decorator)
|
|
42
|
+
profilers << MethodProfiler.observe(Contracts::Support)
|
|
43
|
+
profilers << MethodProfiler.observe(UnboundMethod)
|
|
44
|
+
10.times do |_|
|
|
45
|
+
contracts_download(@urls.sample)
|
|
46
|
+
end
|
|
47
|
+
profilers.each { |p| puts p.report }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def ruby_prof
|
|
51
|
+
RubyProf.start
|
|
52
|
+
10.times do |_|
|
|
53
|
+
contracts_download(@urls.sample)
|
|
54
|
+
end
|
|
55
|
+
result = RubyProf.stop
|
|
56
|
+
printer = RubyProf::FlatPrinter.new(result)
|
|
57
|
+
printer.print(STDOUT)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
benchmark
|
|
61
|
+
profile
|
|
62
|
+
ruby_prof if ENV["FULL_BENCH"] # takes some time
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
require "benchmark"
|
|
2
|
+
|
|
3
|
+
module Wrapper
|
|
4
|
+
def self.extended(klass)
|
|
5
|
+
klass.class_eval do
|
|
6
|
+
@@methods = {}
|
|
7
|
+
def self.methods
|
|
8
|
+
@@methods
|
|
9
|
+
end
|
|
10
|
+
def self.set_method k, v
|
|
11
|
+
@@methods[k] = v
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def method_added name
|
|
17
|
+
return if methods.include?(name)
|
|
18
|
+
puts "#{name} added"
|
|
19
|
+
set_method(name, instance_method(name))
|
|
20
|
+
class_eval %{
|
|
21
|
+
def #{name}(*args)
|
|
22
|
+
self.class.methods[#{name.inspect}].bind(self).call(*args)
|
|
23
|
+
end
|
|
24
|
+
}, __FILE__, __LINE__ + 1
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
class NotWrapped
|
|
29
|
+
def add a, b
|
|
30
|
+
a + b
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
class Wrapped
|
|
35
|
+
extend ::Wrapper
|
|
36
|
+
def add a, b
|
|
37
|
+
a + b
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
w = Wrapped.new
|
|
42
|
+
nw = NotWrapped.new
|
|
43
|
+
# p w.add(1, 4)
|
|
44
|
+
# exit
|
|
45
|
+
# 30 is the width of the output column
|
|
46
|
+
Benchmark.bm 30 do |x|
|
|
47
|
+
x.report "wrapped" do
|
|
48
|
+
100_000.times do |_|
|
|
49
|
+
w.add(rand(1000), rand(1000))
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
x.report "not wrapped" do
|
|
53
|
+
100_000.times do |_|
|
|
54
|
+
nw.add(rand(1000), rand(1000))
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require File.expand_path(File.join(__FILE__, "../lib/contracts/version"))
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |s|
|
|
4
|
+
s.name = "contracts"
|
|
5
|
+
s.version = Contracts::VERSION
|
|
6
|
+
s.summary = "Contracts for Ruby."
|
|
7
|
+
s.description = "This library provides contracts for Ruby. Contracts let you clearly express how your code behaves, and free you from writing tons of boilerplate, defensive code."
|
|
8
|
+
s.author = "Aditya Bhargava"
|
|
9
|
+
s.email = "bluemangroupie@gmail.com"
|
|
10
|
+
s.files = `git ls-files`.split("\n")
|
|
11
|
+
s.homepage = "https://github.com/egonSchiele/contracts.ruby"
|
|
12
|
+
s.license = "BSD-2-Clause"
|
|
13
|
+
s.post_install_message = "
|
|
14
|
+
0.16.x will be the supporting Ruby 2.x and be feature frozen (only fixes will be released)
|
|
15
|
+
For Ruby 3.x use 0.17.x or later (might not be released yet)
|
|
16
|
+
"
|
|
17
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
default: --require features
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
version: 2
|
|
2
|
+
updates:
|
|
3
|
+
- package-ecosystem: bundler
|
|
4
|
+
directory: "/"
|
|
5
|
+
schedule:
|
|
6
|
+
interval: monthly
|
|
7
|
+
time: "06:00"
|
|
8
|
+
timezone: Asia/Hong_Kong
|
|
9
|
+
open-pull-requests-limit: 10
|
|
10
|
+
labels:
|
|
11
|
+
- "dependencies"
|
|
12
|
+
- package-ecosystem: github-actions
|
|
13
|
+
directory: "/"
|
|
14
|
+
schedule:
|
|
15
|
+
interval: monthly
|
|
16
|
+
time: "06:00"
|
|
17
|
+
timezone: Asia/Hong_Kong
|
|
18
|
+
open-pull-requests-limit: 10
|
|
19
|
+
labels:
|
|
20
|
+
- "dependencies"
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
contracts.ruby brings code contracts to the Ruby language.
|
|
2
|
+
|
|
3
|
+
Example:
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
class Example
|
|
7
|
+
include Contracts::Core
|
|
8
|
+
C = Contracts
|
|
9
|
+
|
|
10
|
+
Contract C::Num, C::Num => C::Num
|
|
11
|
+
def add(a, b)
|
|
12
|
+
a + b
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
This documentation is [open source](https://github.com/egonSchiele/contracts.ruby/tree/master/features). If you find it incomplete or confusing, please [submit an issue](https://github.com/egonSchiele/contracts.ruby/issues), or, better yet, [a pull request](https://github.com/egonSchiele/contracts.ruby).
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
Feature: Fetching contracted function type
|
|
2
|
+
|
|
3
|
+
You can use `functype(name)` method for that:
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
functype(:add) # => "add :: Num, Num => Num"
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Background:
|
|
10
|
+
Given a file named "example.rb" with:
|
|
11
|
+
"""ruby
|
|
12
|
+
require "contracts"
|
|
13
|
+
C = Contracts
|
|
14
|
+
|
|
15
|
+
class Example
|
|
16
|
+
include Contracts::Core
|
|
17
|
+
|
|
18
|
+
Contract C::Num, C::Num => C::Num
|
|
19
|
+
def add(a, b)
|
|
20
|
+
a + b
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
Contract String => String
|
|
24
|
+
def self.greeting(name)
|
|
25
|
+
"Hello, #{name}"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
class << self
|
|
29
|
+
Contract C::Num => C::Num
|
|
30
|
+
def increment(number)
|
|
31
|
+
number + 1
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
Scenario: functype on instance method
|
|
38
|
+
Given a file named "instance_method_functype.rb" with:
|
|
39
|
+
"""ruby
|
|
40
|
+
require "./example"
|
|
41
|
+
puts Example.new.functype(:add)
|
|
42
|
+
"""
|
|
43
|
+
When I run `ruby instance_method_functype.rb`
|
|
44
|
+
Then the output should contain:
|
|
45
|
+
"""
|
|
46
|
+
add :: Num, Num => Num
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
Scenario: functype on class method
|
|
50
|
+
Given a file named "class_method_functype.rb" with:
|
|
51
|
+
"""ruby
|
|
52
|
+
require "./example"
|
|
53
|
+
puts Example.functype(:greeting)
|
|
54
|
+
"""
|
|
55
|
+
When I run `ruby class_method_functype.rb`
|
|
56
|
+
Then the output should contain:
|
|
57
|
+
"""
|
|
58
|
+
greeting :: String => String
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
Scenario: functype on singleton method
|
|
62
|
+
Given a file named "singleton_method_functype.rb" with:
|
|
63
|
+
"""ruby
|
|
64
|
+
require "./example"
|
|
65
|
+
puts Example.functype(:increment)
|
|
66
|
+
"""
|
|
67
|
+
When I run `ruby singleton_method_functype.rb`
|
|
68
|
+
Then the output should contain:
|
|
69
|
+
"""
|
|
70
|
+
increment :: Num => Num
|
|
71
|
+
"""
|