Skip to content

Commit 4caf4fd

Browse files
authored
Merge pull request #295 from egonSchiele/fix/3.0-compatible
* Update implementation & spec to be 3.0 compatible
2 parents 2368c11 + 028de0d commit 4caf4fd

32 files changed

+279
-154
lines changed

.github/workflows/code_style_checks.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
os:
2323
- ubuntu
2424
ruby:
25-
- "2.7"
25+
- "3.0"
2626
runs-on: ${{ matrix.os }}-latest
2727
steps:
2828
- name: Checkout

.github/workflows/tests.yaml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,11 @@ jobs:
2222
os:
2323
- ubuntu
2424
ruby:
25-
- "2.1"
26-
- "2.2"
27-
- "2.3"
28-
- "2.4"
29-
- "2.5"
30-
- "2.6"
31-
- "2.7"
25+
- "3.0"
3226
test_command: ["bundle exec rspec && bundle exec cucumber"]
3327
include:
3428
- os: ubuntu
35-
ruby: "2.4.2"
29+
ruby: "3.0"
3630
test_command: "bundle exec rspec"
3731
runs-on: ${{ matrix.os }}-latest
3832
steps:

.rubocop.yml

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
inherit_from: .rubocop_todo.yml
22

33
AllCops:
4-
TargetRubyVersion: 1.9
4+
TargetRubyVersion: 3.0
55
DisplayCopNames: true
6+
NewCops: disable
67
Exclude:
8+
- "benchmarks/**/*"
79
- "tmp/**/*"
810
- "vendor/**/*"
11+
- "script/**/*.rb"
12+
- "spec/fixtures/*.rb"
913
- "spec/ruby_version_specific/*.rb"
14+
- "spec/*.rb"
1015

1116
# forces method defs to have params in parens
1217
Style/MethodDefParentheses:
@@ -38,7 +43,7 @@ Lint/UnusedMethodArgument:
3843
Enabled: false
3944

4045
# changes x ** 2 to x**2
41-
Style/SpaceAroundOperators:
46+
Layout/SpaceAroundOperators:
4247
Enabled: false
4348

4449
# doesn't allow vars starting with _
@@ -56,7 +61,7 @@ Style/Documentation:
5661

5762
# enforces line length of 80
5863
# TODO enable
59-
Metrics/LineLength:
64+
Layout/LineLength:
6065
Enabled: false
6166

6267
# triggered by Contract ({ :name => String, :age => Fixnum }) => nil
@@ -101,7 +106,7 @@ Lint/DuplicateMethods:
101106
Style/TrivialAccessors:
102107
Enabled: false
103108

104-
Style/MultilineOperationIndentation:
109+
Layout/MultilineOperationIndentation:
105110
EnforcedStyle: indented
106111

107112
# Asks you to use %w{array of words} if possible.
@@ -111,12 +116,12 @@ Style/WordArray:
111116

112117
# conflicts with contracts
113118
# we define contracts like `Baz = 1`
114-
Style/ConstantName:
119+
Naming/ConstantName:
115120
Enabled: false
116121

117122
# `Contract` violates this, otherwise a good cop (enforces snake_case method names)
118123
# TODO possible to get this enabled but ignore `Contract`?
119-
Style/MethodName:
124+
Naming/MethodName:
120125
Enabled: false
121126

122127
# checks for !!
@@ -129,9 +134,30 @@ Metrics/ParameterLists:
129134
Enabled: false
130135

131136
# Checks that braces used for hash literals have or don't have surrounding space depending on configuration.
132-
Style/SpaceInsideHashLiteralBraces:
137+
Layout/SpaceInsideHashLiteralBraces:
133138
Enabled: false
134139

135140
# TODO enable
136141
Style/SpecialGlobalVars:
137142
Enabled: false
143+
144+
Style/IfUnlessModifier:
145+
Enabled: false
146+
147+
Naming/MemoizedInstanceVariableName:
148+
Enabled: false
149+
150+
Layout/FirstHashElementIndentation:
151+
EnforcedStyle: consistent
152+
153+
Layout/HashAlignment:
154+
EnforcedColonStyle: table
155+
156+
Style/TrailingCommaInHashLiteral:
157+
EnforcedStyleForMultiline: consistent_comma
158+
159+
Style/TrailingCommaInArrayLiteral:
160+
EnforcedStyleForMultiline: consistent_comma
161+
162+
Style/TrailingCommaInArguments:
163+
EnforcedStyleForMultiline: consistent_comma

.rubocop_todo.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9+
require:
10+
- rubocop-performance
11+
912
# Offense count: 2
1013
Lint/NonLocalExitFromIterator:
1114
Exclude:
@@ -32,14 +35,14 @@ Style/Alias:
3235
# Offense count: 1
3336
# Cop supports --auto-correct.
3437
# Configuration parameters: AllowAdjacentOneLineDefs.
35-
Style/EmptyLineBetweenDefs:
38+
Layout/EmptyLineBetweenDefs:
3639
Exclude:
3740
- 'benchmarks/wrap_test.rb'
3841

3942
# Offense count: 1
4043
# Cop supports --auto-correct.
4144
# Configuration parameters: AllowForAlignment, ForceEqualSignAlignment.
42-
Style/ExtraSpacing:
45+
Layout/ExtraSpacing:
4346
Exclude:
4447
- 'spec/builtin_contracts_spec.rb'
4548

@@ -58,7 +61,7 @@ Style/IfInsideElse:
5861
# Cop supports --auto-correct.
5962
# Configuration parameters: EnforcedStyle, SupportedStyles.
6063
# SupportedStyles: symmetrical, new_line, same_line
61-
Style/MultilineHashBraceLayout:
64+
Layout/MultilineHashBraceLayout:
6265
Exclude:
6366
- 'spec/contracts_spec.rb'
6467
- 'spec/fixtures/fixtures.rb'
@@ -130,6 +133,6 @@ Style/TrailingUnderscoreVariable:
130133

131134
# Offense count: 1
132135
# Cop supports --auto-correct.
133-
Style/UnneededInterpolation:
136+
Style/RedundantInterpolation:
134137
Exclude:
135138
- 'lib/contracts/formatters.rb'

Gemfile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
# frozen_string_literal: true
2+
13
source "https://rubygems.org"
24

35
gemspec
46

57
group :test do
6-
gem "rspec"
78
gem "aruba"
89
gem "cucumber", "~> 1.3.20"
9-
gem "rubocop", "~> 0.41.2" if RUBY_VERSION >= "2"
10+
gem "rspec"
11+
12+
gem "rubocop", ">= 1.0.0"
13+
gem "rubocop-performance", ">= 1.0.0"
1014
end
1115

1216
group :development do
13-
gem "relish"
1417
gem "method_profiler"
15-
gem "ruby-prof"
1618
gem "rake"
19+
gem "relish"
20+
gem "ruby-prof"
1721
end

Rakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
task :default => [:spec]
24

35
task :add_tag do

contracts.gemspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require File.expand_path(File.join(__FILE__, "../lib/contracts/version"))
24

35
Gem::Specification.new do |s|
@@ -10,6 +12,7 @@ Gem::Specification.new do |s|
1012
s.files = `git ls-files`.split("\n")
1113
s.homepage = "https://github.com/egonSchiele/contracts.ruby"
1214
s.license = "BSD-2-Clause"
15+
s.required_ruby_version = [">= 3.0", "< 4"]
1316
s.post_install_message = "
1417
0.16.x will be the supporting Ruby 2.x and be feature frozen (only fixes will be released)
1518
For Ruby 3.x use 0.17.x or later (might not be released yet)

features/support/env.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require "aruba/cucumber"
24
require "aruba/jruby" if RUBY_PLATFORM == "java"
35

lib/contracts.rb

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require "contracts/attrs"
24
require "contracts/builtin_contracts"
35
require "contracts/decorators"
@@ -51,7 +53,9 @@ class Contract < Contracts::Decorator
5153
end
5254

5355
attr_reader :args_contracts, :ret_contract, :klass, :method
56+
5457
def initialize(klass, method, *contracts)
58+
super(klass, method)
5559
unless contracts.last.is_a?(Hash)
5660
unless contracts.one?
5761
fail %{
@@ -93,17 +97,17 @@ def initialize(klass, method, *contracts)
9397
last_contract = args_contracts.last
9498
penultimate_contract = args_contracts[-2]
9599
@has_options_contract = if @has_proc_contract
96-
penultimate_contract.is_a?(Hash) || penultimate_contract.is_a?(Contracts::Builtin::KeywordArgs)
100+
penultimate_contract.is_a?(Contracts::Builtin::KeywordArgs)
97101
else
98-
last_contract.is_a?(Hash) || last_contract.is_a?(Contracts::Builtin::KeywordArgs)
102+
last_contract.is_a?(Contracts::Builtin::KeywordArgs)
99103
end
100104
# ===
101105

102106
@klass, @method = klass, method
103107
end
104108

105-
def pretty_contract c
106-
c.is_a?(Class) ? c.name : c.class.name
109+
def pretty_contract contract
110+
contract.is_a?(Class) ? contract.name : contract.class.name
107111
end
108112

109113
def to_s
@@ -130,15 +134,15 @@ def self.failure_msg(data)
130134
expected_prefix = "Expected: "
131135
expected_value = Contracts::Support.indent_string(
132136
Contracts::Formatters::Expected.new(data[:contract]).contract.pretty_inspect,
133-
expected_prefix.length
137+
expected_prefix.length,
134138
).strip
135-
expected_line = expected_prefix + expected_value + ","
139+
expected_line = "#{expected_prefix}#{expected_value},"
136140

137141
# Actual
138142
actual_prefix = "Actual: "
139143
actual_value = Contracts::Support.indent_string(
140144
data[:arg].pretty_inspect,
141-
actual_prefix.length
145+
actual_prefix.length,
142146
).strip
143147
actual_line = actual_prefix + actual_value
144148

@@ -157,16 +161,19 @@ def self.failure_msg(data)
157161
position_value = Contracts::Support.method_position(data[:method])
158162
position_line = position_prefix + position_value
159163

160-
header +
161-
"\n" +
164+
[
165+
header,
162166
Contracts::Support.indent_string(
163-
[expected_line,
164-
actual_line,
165-
value_line,
166-
contract_line,
167-
position_line].join("\n"),
168-
indent_amount
169-
)
167+
[
168+
expected_line,
169+
actual_line,
170+
value_line,
171+
contract_line,
172+
position_line,
173+
].join("\n"),
174+
indent_amount,
175+
),
176+
].join("\n")
170177
end
171178

172179
# Callback for when a contract fails. By default it raises
@@ -182,7 +189,7 @@ def self.failure_msg(data)
182189
# puts failure_msg(data)
183190
# exit
184191
# end
185-
def self.failure_callback(data, use_pattern_matching = true)
192+
def self.failure_callback(data, use_pattern_matching: true)
186193
if data[:contracts].pattern_match? && use_pattern_matching
187194
return DEFAULT_FAILURE_CALLBACK.call(data)
188195
end
@@ -242,19 +249,21 @@ def call(*args, &blk)
242249
# returns true if it appended nil
243250
def maybe_append_block! args, blk
244251
return false unless @has_proc_contract && !blk &&
245-
(@args_contract_index || args.size < args_contracts.size)
252+
(@args_contract_index || args.size < args_contracts.size)
253+
246254
args << nil
247255
true
248256
end
249257

250258
# Same thing for when we have named params but didn't pass any in.
251259
# returns true if it appended nil
252-
def maybe_append_options! args, blk
260+
def maybe_append_options! args, kargs, blk
253261
return false unless @has_options_contract
254-
if @has_proc_contract && (args_contracts[-2].is_a?(Hash) || args_contracts[-2].is_a?(Contracts::Builtin::KeywordArgs)) && !args[-2].is_a?(Hash)
255-
args.insert(-2, {})
256-
elsif (args_contracts[-1].is_a?(Hash) || args_contracts[-1].is_a?(Contracts::Builtin::KeywordArgs)) && !args[-1].is_a?(Hash)
257-
args << {}
262+
263+
if @has_proc_contract && args_contracts[-2].is_a?(Contracts::Builtin::KeywordArgs)
264+
args.insert(-2, kargs)
265+
elsif args_contracts[-1].is_a?(Contracts::Builtin::KeywordArgs)
266+
args << kargs
258267
end
259268
true
260269
end

lib/contracts/attrs.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Contracts
24
module Attrs
35
def attr_reader_with_contract(*names, contract)

0 commit comments

Comments
 (0)