Skip to content

Instantly share code, notes, and snippets.

@demental
Last active December 21, 2015 17:29
Show Gist options
  • Save demental/6341156 to your computer and use it in GitHub Desktop.
Save demental/6341156 to your computer and use it in GitHub Desktop.
Feature: interfaces in Ruby
As a developer that wants to use interfaces in Ruby,
I want a DSL that emulates the following features:
* interfaces are constantized
* you cannot inherit from an interface
* you cannot instanciate an interface
* classes that implement interfaces MUST implement the methods defined in the interface
* instances of classes that implement interfaces are of the kind of the interface names.
Background:
Given a file named "i_have_an_account.rb" with:
"""ruby
interface :iHaveAnAccount
def account; end
end
"""
Scenario: interfaces are constantized
When I require "i_have_an_account.rb"
Then the class IHaveAnAccount exists
Scenario: classes cannot inherit from an interface
Given a file named "inherited_from_interface.rb" with:
"""ruby
class InheritFromInterface < IHaveAnAccount
end
"""
When I require "i_have_an_account.rb"
And I require "inherited_from_interface.rb"
Then it should raise a RuntimeError
Scenario: interfaces cannot be instanciated
When I require "i_have_an_account.rb"
Then a new IHaveAnAccount should raise a RuntimeError
Scenario: classes that implement interfaces MUST implement the methods defined in the interface
Given a file named "incomplete_invoice.rb" with:
"""ruby
class IncompleteInvoice
implements :iHaveAnAccount
end
"""
When I require "i_have_an_account.rb"
And I require "incomplete_invoice.rb"
then it should raise an ImplementationError
Scenario: classes that implement interfaces are of the kind of the interface
Given a file named "invoice.rb" with:
"""ruby
class Invoice
implements :iHaveAnAccount
def account
'account'
end
end
"""
When I require "invoice.rb"
Then it should not raise an error
And a new Invoice should be kind of IHaveAnAccount
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment