Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional specs for contract validation #37

Merged
merged 1 commit into from
Apr 10, 2024
Merged
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
126 changes: 126 additions & 0 deletions spec/sinject/container_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,132 @@ def with_args(first, second = false, third = true, fourth:, fifth:, sixth: true)
expect(container.registered?(:foo)).to be_truthy
end
end

context 'that specifies rest parameters' do
let(:contract) do
Class.new do
def with_rest_args(first, second, *args); end
end
end

context 'and the class does not specify them' do
it 'raises DependencyContractInvalidParametersException' do
klass = Class.new do
def with_rest_args(first, second); end
end

expect { container.register(key: :foo, class: klass, contract: contract) }
.to raise_error Sinject::DependencyContractInvalidParametersException
end
end

context 'and the class specifies an additional parameter' do
it 'raises DependencyContractInvalidParametersException' do
klass = Class.new do
def with_rest_args(first, second, args); end
end

expect { container.register(key: :foo, class: klass, contract: contract) }
.to raise_error Sinject::DependencyContractInvalidParametersException
end
end

context 'and the class specifies the rest arguments' do
it 'registers the dependency' do
klass = Class.new do
def with_rest_args(first, second, *args); end
end

container.register(key: :foo, class: klass, contract: contract)
expect(container.registered?(:foo)).to be_truthy
end
end
end

context 'that specifies keyrest parameters' do
let(:contract) do
Class.new do
def with_kwrest_args(foo, bar:, **kwargs); end
end
end

context 'and the class does not specify them' do
it 'raises DependencyContractInvalidParametersException' do
klass = Class.new do
def with_kwrest_args(foo, bar:); end
end

expect { container.register(key: :foo, class: klass, contract: contract) }
.to raise_error Sinject::DependencyContractInvalidParametersException
end
end

context 'and the class specifies a required keyword param of the same name' do
it 'raises DependencyContractInvalidParametersException' do
klass = Class.new do
def with_kwrest_args(foo, bar:, kwargs:); end
end

expect { container.register(key: :foo, class: klass, contract: contract) }
.to raise_error Sinject::DependencyContractInvalidParametersException
end
end

context 'and the class specifies an optional keyword param of the same name' do
it 'raises DependencyContractInvalidParametersException' do
pending('Bug: If the contract specifies keyrest params, '\
'the class should pass validation only if it also specifies keyrest args')

klass = Class.new do
def with_kwrest_args(foo, bar:, kwargs: nil); end
end

expect { container.register(key: :foo, class: klass, contract: contract) }
.to raise_error Sinject::DependencyContractInvalidParametersException
end
end

context 'and the class specifies them' do
it 'registers the dependency' do
klass = Class.new do
def with_kwrest_args(foo, bar:, **kwargs); end
end

container.register(key: :foo, class: klass, contract: contract)
expect(container.registered?(:foo)).to be_truthy
end
end
end

context 'when the contract expects a block' do
let(:contract) do
Class.new do
def with_block(foo, &block); end
end
end

context 'and the class does not' do
it 'raises DependencyContractInvalidParametersException' do
klass = Class.new do
def with_block(foo); end
end

expect { container.register(key: :foo, class: klass, contract: contract) }
.to raise_error Sinject::DependencyContractInvalidParametersException
end
end

context 'and the class does too' do
it 'registers the dependency' do
klass = Class.new do
def with_block(foo, &block); end
end

container.register(key: :foo, class: klass, contract: contract)
expect(container.registered?(:foo)).to be_truthy
end
end
end
end
end

Expand Down
Loading