Skip to content

Instantly share code, notes, and snippets.

View jamescarr's full-sized avatar
🎯
Focusing

James Carr jamescarr

🎯
Focusing
View GitHub Profile
// spec
import org.specs._
object PerfectNumbersSpec extends Specification{
"Perfect Numbers" should{
"5 is not a perfect number" in {
IsPerfectNumber(5) must be(false)
}
"6 is a perfect number" in {
IsPerfectNumber(6) must be(true)
class AwesomeInt(n:Int){
def !() = (1 to n).foldRight(1)(_*_)
implicit def intToAwesomeInt(n:Int) = new AwesomeInt(n)
}
class AwesomeInt(n:Int){
def !() = (1 to n).foldRight(1)(_*_)
}
object Converter{
implicit def intToAwesomeInt(n:Int) = new AwesomeInt(n)
}
//------------
import org.specs._
import Converter._
import org.specs._
object PascalsTriangleSpec extends Specification{
"Pascals Triangle Generator" should{
val generator = new PascalTriangleGenerator
"return a List with List(1) for 1" in {
val result = generator.generate(1)
result must be equalTo(List(List(1)))
}
import org.specs._
object PascalsTriangleSpec extends Specification{
"Pascals Triangle Generator" should{
val generator = new PascalTriangleGenerator
"return a List with List(1) for 1" in {
val result = generator.generate(1)
result must be equalTo(List(List(1)))
}
@Test
public void shouldBeAbleToSelectOrdersWithPriceOverCertainThreshold(){
orders.add(new Order(1, "Mallrats DVD", 9.99));
orders.add(new Order(2, "Blue Ray Player", 199.99));
orders.add(new Order(3, "Playstation 3", 299.99));
Collection<Order> expensiveOrders = orders.getOrdersAbovePrice(100.00);
assertThat(expensiveOrders, hasItems(
hasProperty("name", equalTo("Blue Ray Player")),
apply plugin: 'java'
apply plugin: 'eclipse'
repositories{
mavenCentral()
flatDir name: 'localRepository', dirs: 'lib'
}
dependencies{
compile 'com.google.collections:google-collections:1.0'
class KeyStream(deck:Deck){
def nextChar() = 'A'
}
// spec
describe ("Encrypting a string of text"){
val keyStream = mock[KeyStream]
it("should encrypt to the expected message for 'Code in ruby, live longer!'"){
private def generateKeyFor(sourceMessage:String, keyStream:KeyStream) = {
sourceMessage.toList.map(c => if(c == ' ') c else keyStream.nextChar()).mkString
}
// given a string like "ABSCD WURJD" it should read in 10 chars from the keyStream
// and return them as a string with a space after each fifth char except the last
class DeckSpec extends Spec with ShouldMatchers{
describe("FreshDeck"){
val deck = new Deck()
it ("should have cards 1, 2, 3 as it's first cards") {
deck.cardAt(0) should be(1)
deck.cardAt(1) should be(2)
deck.cardAt(2) should be(3)
}