Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/code_style_checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
os:
- ubuntu
ruby:
- "2.7"
- "3.0"
runs-on: ${{ matrix.os }}-latest
steps:
- name: Checkout
Expand Down
10 changes: 2 additions & 8 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,11 @@ jobs:
os:
- ubuntu
ruby:
- "2.1"
- "2.2"
- "2.3"
- "2.4"
- "2.5"
- "2.6"
- "2.7"
- "3.0"
test_command: ["bundle exec rspec && bundle exec cucumber"]
include:
- os: ubuntu
ruby: "2.4.2"
ruby: "3.0"
test_command: "bundle exec rspec"
runs-on: ${{ matrix.os }}-latest
steps:
Expand Down
40 changes: 33 additions & 7 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
inherit_from: .rubocop_todo.yml

AllCops:
TargetRubyVersion: 1.9
TargetRubyVersion: 3.0
DisplayCopNames: true
NewCops: disable
Exclude:
- "benchmarks/**/*"
- "tmp/**/*"
- "vendor/**/*"
- "script/**/*.rb"
- "spec/fixtures/*.rb"
- "spec/ruby_version_specific/*.rb"
- "spec/*.rb"

# forces method defs to have params in parens
Style/MethodDefParentheses:
Expand Down Expand Up @@ -38,7 +43,7 @@ Lint/UnusedMethodArgument:
Enabled: false

# changes x ** 2 to x**2
Style/SpaceAroundOperators:
Layout/SpaceAroundOperators:
Enabled: false

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

# enforces line length of 80
# TODO enable
Metrics/LineLength:
Layout/LineLength:
Enabled: false

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

Style/MultilineOperationIndentation:
Layout/MultilineOperationIndentation:
EnforcedStyle: indented

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

# conflicts with contracts
# we define contracts like `Baz = 1`
Style/ConstantName:
Naming/ConstantName:
Enabled: false

# `Contract` violates this, otherwise a good cop (enforces snake_case method names)
# TODO possible to get this enabled but ignore `Contract`?
Style/MethodName:
Naming/MethodName:
Enabled: false

# checks for !!
Expand All @@ -129,9 +134,30 @@ Metrics/ParameterLists:
Enabled: false

# Checks that braces used for hash literals have or don't have surrounding space depending on configuration.
Style/SpaceInsideHashLiteralBraces:
Layout/SpaceInsideHashLiteralBraces:
Enabled: false

# TODO enable
Style/SpecialGlobalVars:
Enabled: false

Style/IfUnlessModifier:
Enabled: false

Naming/MemoizedInstanceVariableName:
Enabled: false

Layout/FirstHashElementIndentation:
EnforcedStyle: consistent

Layout/HashAlignment:
EnforcedColonStyle: table

Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: consistent_comma

Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: consistent_comma

Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: consistent_comma
11 changes: 7 additions & 4 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

require:
- rubocop-performance

# Offense count: 2
Lint/NonLocalExitFromIterator:
Exclude:
Expand All @@ -32,14 +35,14 @@ Style/Alias:
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: AllowAdjacentOneLineDefs.
Style/EmptyLineBetweenDefs:
Layout/EmptyLineBetweenDefs:
Exclude:
- 'benchmarks/wrap_test.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: AllowForAlignment, ForceEqualSignAlignment.
Style/ExtraSpacing:
Layout/ExtraSpacing:
Exclude:
- 'spec/builtin_contracts_spec.rb'

Expand All @@ -58,7 +61,7 @@ Style/IfInsideElse:
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: symmetrical, new_line, same_line
Style/MultilineHashBraceLayout:
Layout/MultilineHashBraceLayout:
Exclude:
- 'spec/contracts_spec.rb'
- 'spec/fixtures/fixtures.rb'
Expand Down Expand Up @@ -130,6 +133,6 @@ Style/TrailingUnderscoreVariable:

# Offense count: 1
# Cop supports --auto-correct.
Style/UnneededInterpolation:
Style/RedundantInterpolation:
Exclude:
- 'lib/contracts/formatters.rb'
12 changes: 8 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# frozen_string_literal: true

source "https://rubygems.org"

gemspec

group :test do
gem "rspec"
gem "aruba"
gem "cucumber", "~> 1.3.20"
gem "rubocop", "~> 0.41.2" if RUBY_VERSION >= "2"
gem "rspec"

gem "rubocop", ">= 1.0.0"
gem "rubocop-performance", ">= 1.0.0"
end

group :development do
gem "relish"
gem "method_profiler"
gem "ruby-prof"
gem "rake"
gem "relish"
gem "ruby-prof"
end
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

task :default => [:spec]

task :add_tag do
Expand Down
3 changes: 3 additions & 0 deletions contracts.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require File.expand_path(File.join(__FILE__, "../lib/contracts/version"))

Gem::Specification.new do |s|
Expand All @@ -10,6 +12,7 @@ Gem::Specification.new do |s|
s.files = `git ls-files`.split("\n")
s.homepage = "https://github.com/egonSchiele/contracts.ruby"
s.license = "BSD-2-Clause"
s.required_ruby_version = [">= 3.0", "< 4"]
s.post_install_message = "
0.16.x will be the supporting Ruby 2.x and be feature frozen (only fixes will be released)
For Ruby 3.x use 0.17.x or later (might not be released yet)
Expand Down
2 changes: 2 additions & 0 deletions features/support/env.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "aruba/cucumber"
require "aruba/jruby" if RUBY_PLATFORM == "java"

Expand Down
55 changes: 32 additions & 23 deletions lib/contracts.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "contracts/attrs"
require "contracts/builtin_contracts"
require "contracts/decorators"
Expand Down Expand Up @@ -51,7 +53,9 @@ class Contract < Contracts::Decorator
end

attr_reader :args_contracts, :ret_contract, :klass, :method

def initialize(klass, method, *contracts)
super(klass, method)
unless contracts.last.is_a?(Hash)
unless contracts.one?
fail %{
Expand Down Expand Up @@ -93,17 +97,17 @@ def initialize(klass, method, *contracts)
last_contract = args_contracts.last
penultimate_contract = args_contracts[-2]
@has_options_contract = if @has_proc_contract
penultimate_contract.is_a?(Hash) || penultimate_contract.is_a?(Contracts::Builtin::KeywordArgs)
penultimate_contract.is_a?(Contracts::Builtin::KeywordArgs)
else
last_contract.is_a?(Hash) || last_contract.is_a?(Contracts::Builtin::KeywordArgs)
last_contract.is_a?(Contracts::Builtin::KeywordArgs)
end
# ===

@klass, @method = klass, method
end

def pretty_contract c
c.is_a?(Class) ? c.name : c.class.name
def pretty_contract contract
contract.is_a?(Class) ? contract.name : contract.class.name
end

def to_s
Expand All @@ -130,15 +134,15 @@ def self.failure_msg(data)
expected_prefix = "Expected: "
expected_value = Contracts::Support.indent_string(
Contracts::Formatters::Expected.new(data[:contract]).contract.pretty_inspect,
expected_prefix.length
expected_prefix.length,
).strip
expected_line = expected_prefix + expected_value + ","
expected_line = "#{expected_prefix}#{expected_value},"

# Actual
actual_prefix = "Actual: "
actual_value = Contracts::Support.indent_string(
data[:arg].pretty_inspect,
actual_prefix.length
actual_prefix.length,
).strip
actual_line = actual_prefix + actual_value

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

header +
"\n" +
[
header,
Contracts::Support.indent_string(
[expected_line,
actual_line,
value_line,
contract_line,
position_line].join("\n"),
indent_amount
)
[
expected_line,
actual_line,
value_line,
contract_line,
position_line,
].join("\n"),
indent_amount,
),
].join("\n")
end

# Callback for when a contract fails. By default it raises
Expand All @@ -182,7 +189,7 @@ def self.failure_msg(data)
# puts failure_msg(data)
# exit
# end
def self.failure_callback(data, use_pattern_matching = true)
def self.failure_callback(data, use_pattern_matching: true)
if data[:contracts].pattern_match? && use_pattern_matching
return DEFAULT_FAILURE_CALLBACK.call(data)
end
Expand Down Expand Up @@ -242,19 +249,21 @@ def call(*args, &blk)
# returns true if it appended nil
def maybe_append_block! args, blk
return false unless @has_proc_contract && !blk &&
(@args_contract_index || args.size < args_contracts.size)
(@args_contract_index || args.size < args_contracts.size)

args << nil
true
end

# Same thing for when we have named params but didn't pass any in.
# returns true if it appended nil
def maybe_append_options! args, blk
def maybe_append_options! args, kargs, blk
return false unless @has_options_contract
if @has_proc_contract && (args_contracts[-2].is_a?(Hash) || args_contracts[-2].is_a?(Contracts::Builtin::KeywordArgs)) && !args[-2].is_a?(Hash)
args.insert(-2, {})
elsif (args_contracts[-1].is_a?(Hash) || args_contracts[-1].is_a?(Contracts::Builtin::KeywordArgs)) && !args[-1].is_a?(Hash)
args << {}

if @has_proc_contract && args_contracts[-2].is_a?(Contracts::Builtin::KeywordArgs)
args.insert(-2, kargs)
elsif args_contracts[-1].is_a?(Contracts::Builtin::KeywordArgs)
args << kargs
end
true
end
Expand Down
2 changes: 2 additions & 0 deletions lib/contracts/attrs.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Contracts
module Attrs
def attr_reader_with_contract(*names, contract)
Expand Down
Loading