Last active
August 29, 2015 14:07
-
-
Save sunaot/9efce49897e3dbf98fca to your computer and use it in GitHub Desktop.
spock ぽい何かの Ruby での習作
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Ruby | |
module Spock | |
def spec(description, definition = nil, &proc_definition) | |
raise ArgumentError if [ definition, proc_definition ].all? {|d| d.nil? } | |
if definition.nil? | |
spec_runner(description, proc_definition) | |
else | |
spec_runner(description, definition) | |
end | |
end | |
private | |
def spec_runner(description, definition) | |
s = Specification.new | |
d = Definition.new(s) | |
d.instance_eval(&definition) | |
puts description | |
s.examples.each do |args| | |
if s.expectation.call(*args) | |
print '.' | |
else | |
print 'F' | |
end | |
end | |
print "\n" | |
end | |
class Specification | |
attr_accessor :expectation, :examples | |
end | |
class Definition | |
attr_reader :spec | |
def initialize(specification) | |
@spec = specification | |
end | |
def expect(expectation) | |
spec.expectation = expectation | |
end | |
def where(examples) | |
spec.examples = examples | |
end | |
end | |
end | |
end | |
class Foo | |
extend Ruby::Spock | |
spec 'maximum of two numbers', ->(*) { | |
expect ->(a, b, c) { [a, b].max == c } | |
where [ | |
# a | b | c | |
[ 1 , 3 , 3 ], | |
[ 7 , 4 , 4 ], | |
[ 0 , 0 , 0 ], | |
] | |
} | |
spec 'minimum of two numbers' do | |
expect ->(a, b, c) { [a, b].min == c } | |
where [ | |
# a | b | c | |
[ 1 , 3 , 1 ], | |
[ 7 , 4 , 4 ], | |
[ 0 , 0 , 0 ], | |
] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment