Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Feature: Addition
In order to avoid silly mistakes
As a math idiot
I want to be told the sum of two numbers
Scenario Outline: Add two numbers
Given I have entered <x> and <y> into the calculator
When I press "add"
Then the result should be <z> on the screen
Examples:
| x | y | z |
| 2| 2| 4|
| -3| -4| -7|
| 0| 0| 0|
| 100_500| 100_500| 201_000|
| 1.25| 2.5| 3.75|
| 10| -10| 0|
$:.unshift File.expand_path('../../lib', __FILE__)
require 'calculator'
describe Calculator, '#eval' do
[ [ 2, 2, 4],
[ -3, -4, -7],
[ 0, 0, 0],
[100_500, 100_500, 201_000],
[ 1.25, 2.5, 3.75],
[ 10, -10, 0]
].each do |x, y, sum|
it "should evaluate #{x} + #{y} as #{sum}" do
Calculator.plus(x, y).should == sum
end
end
end
$:.unshift File.expand_path('../../lib', __FILE__)
require 'calculator'
describe Calculator, '#eval' do
it 'should evaluate 2 + 2 as 4' do
Calculator.plus(2, 2).should == 4
end
it 'should evaluate (-3) + (-4) as -7' do
Calculator.plus(-3, -4).should == -7
end
it 'should evaluate 0 + 0 as 0' do
Calculator.plus(0, 0).should == 0
end
it 'should evaluate 100_500 + 100_500 as 201_000' do
Calculator.plus(100_500, 100_500).should == 201_000
end
it 'should evaluate 1.25 + 2.5 as 3.75' do
Calculator.plus(1.25, 2.5).should == 3.75
end
it 'should evaluate 10 + (-10) as 0' do
Calculator.plus(10, -10).should == 0
end
end
using NUnit.Framework;
namespace Calculator.Test
{
[TestFixture]
public class CalculatorTest
{
[TestCase( 2, 2, Result = 4)]
[TestCase( -3, -4, Result = -7)]
[TestCase( 0, 0, Result = 0)]
[TestCase(100500, 100500, Result = 201000)]
[TestCase( 10, -10, Result = 0)]
public long ShouldAddTwoNumbers(long x, long y)
{
return Calculator.Add(x, y);
}
}
}
@remeniuk

This comment has been minimized.

Copy link

remeniuk commented Aug 26, 2011

looks gorgeous ~> https://gist.github.com/1173331#file_calculator.feature
great example of human-readable specs

@DNNX

This comment has been minimized.

Copy link
Owner Author

DNNX commented Aug 26, 2011

absolutely! cucumber rocks

@remeniuk

This comment has been minimized.

Copy link

remeniuk commented Aug 26, 2011

I have to try making smth similar with specs2 (scala) - will see, which one's better :)

@DNNX

This comment has been minimized.

Copy link
Owner Author

DNNX commented Aug 26, 2011

@remeniuk

This comment has been minimized.

Copy link

remeniuk commented Aug 26, 2011

No-no-no, in Soviet Russia Scala we use specs for BDD :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.