Skip to content

Commit

Permalink
Add new test cases and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
Verseth committed Apr 19, 2024
1 parent d8d1585 commit 0687c75
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ Will fail because of method signature mismatch:
- expected arguments: (req, req)
- actual arguments: (req, opt=)
Classes may define additional optional or rest arguments.
```ruby
module Carrier
def call(number); end

def text(number, text); end
end

class Giffgaff
def call(number, *opts); end

def text(number, text, opt1 = nil, opt2 = nil); end
end
```
This will not generate any errors since `Giffgaff` implements the required methods with correct arguments only adding new optional ones.
### Rails
Mix in `Interfaceable` before any of the application code is loaded. For example, in the initializer. For extra peace of mind, you can noop interface checking in production:
Expand Down
53 changes: 18 additions & 35 deletions spec/implementation_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,9 @@ def foo(aaa, baz = 3, bar:, fuga: 2); end
}
}
)
end

interface = Module.new do
def foo(aaa, bbb); end
end
klass = Class.new do
def foo(aaa, baz); end
end

errors = Interfaceable::ImplementationCheck.new(klass).perform([interface])

expect(errors).to be_empty

it 'accepts additional optional arguments' do
interface = Module.new do
def foo(aaa, bbb); end
end
Expand Down Expand Up @@ -125,15 +116,9 @@ def self.foo(aaa, bar = 1); end
}
}
)
end

klass = Class.new do
def self.foo(aaa, bar = 1, *args); end
end

errors = Interfaceable::ImplementationCheck.new(klass).perform([interface])

expect(errors).to be_empty

it 'accepts additional *rest argument' do
interface = Module.new do
def self.foo(aaa, baz = 3); end
end
Expand All @@ -148,25 +133,9 @@ def self.foo(aaa, bar = 1, *args); end
end

it 'checks **opts argument' do
interface = Module.new do
def foo(aaa, baz = 3, *args, foo:); end
end
klass = Class.new do
def foo(aaa, bar = 1, *args, foo:, **opts); end
end

errors = Interfaceable::ImplementationCheck.new(klass).perform([interface])

# allow the class to have additional rest parameters
expect(errors).to be_empty

interface = Module.new do
def foo(aaa, baz = 3, *args, foo:, **options); end
end

errors = Interfaceable::ImplementationCheck.new(klass).perform([interface])
expect(errors).to be_empty

klass = Class.new do
def foo(aaa, bar = 1, *args, foo:); end
end
Expand All @@ -181,5 +150,19 @@ def foo(aaa, bar = 1, *args, foo:); end
}
)
end

it 'accepts additional **opts argument' do
interface = Module.new do
def foo(aaa, baz = 3, *args, foo:); end
end
klass = Class.new do
def foo(aaa, bar = 1, *args, foo:, **opts); end
end

errors = Interfaceable::ImplementationCheck.new(klass).perform([interface])

# allow the class to have additional rest parameters
expect(errors).to be_empty
end
end
# rubocop:enable Metrics/BlockLength

0 comments on commit 0687c75

Please sign in to comment.