Last active
December 13, 2015 21:59
-
-
Save maio/4981608 to your computer and use it in GitHub Desktop.
BDD vs xyzUnit style unit-tests
This file contains 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
describe "StringCalculator" => sub { | |
it "returns sum of comma delimited numbers" => sub { | |
is(add("$a,$b"), $a + $b); | |
}; | |
it "treats newline as delimiter" => sub { | |
is(add("$a\n$b"), $a + $b); | |
}; | |
describe 'delimiter marker //<delimiter-spec>\n' => sub { | |
it "is used to specify custom delimiter" => sub { | |
is(add("//$delimiter\n$a$delimiter$b"), $a + $b); | |
}; | |
it "could be used to specify multiple delimiters" => sub { | |
is(add("//[$delimiter][$other_delimiter]\n$a$delimiter$b$other_delimiter$c"), | |
$a + $b + $c); | |
}; | |
it "could specify delimiter marker longer than one char" => sub { | |
is(add("//$multichar_delimiter\n$a$multichar_delimiter$b"), $a + $b); | |
}; | |
}; | |
describe "invalid input exception" => sub { | |
it "is thrown when input contains negative number" => sub { | |
... | |
}; | |
it "contains list of all invalid numbers in given string" => sub { | |
... | |
}; | |
}; | |
}; | |
## When something fails I get meaningful and readable test description: | |
# | |
# StringCalculator | |
# returns sum of comma delimited numbers | |
# treats newline as delimiter | |
# | |
# delimiter marker //<delimiter-spec>\n | |
# is used to specify custom delimiter | |
# could be used to specify multiple delimiters | |
# could specify delimiter marker longer than one char | |
# | |
# invalid input exception | |
# is thrown when input contains negative number | |
# contains list of all invalid numbers in given string | |
class StringCalculatorTest { | |
sub returnsSumOfCommaDelimitedNumbers : Tests { | |
is(add("$a,$b"), $a + $b); | |
} | |
sub treatsNewlineAsDelimiter : Tests { | |
is(add("$a\n$b"), $a + $b); | |
} | |
sub delimiterMarkerIsUsedToSpecifyCustomDelimiter : Tests { | |
is(add("//$delimiter\n$a;$b"), $a + $b); | |
} | |
sub delimiterMarkerCouldBeUsedToSpecifyMultipleDelimiters : Tests { | |
is(add("//[$delimiter][$other_delimiter]\n$a$delimiter$b$other_delimiter$c"), | |
$a + $b + $c); | |
} | |
sub delimiterMarkerCouldSpecifyDelimiterMarkerLongerThanOneChar : Tests { | |
is(add("//$multichar_delimiter\n$a$multichar_delimiter$b"), $a + $b); | |
} | |
sub invalidInputExceptionIsThrownWhenInputContainsNegativeNumber : Tests { | |
... | |
} | |
sub invalidInputExceptionContainsListOfAllInvalidNumbersInGivenString : Tests { | |
... | |
} | |
} | |
# Ruby - http://rspec.info | |
# Perl - https://metacpan.org/module/Test::Spec | |
# JavaScript - http://pivotal.github.com/jasmine/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment