Skip to content

Instantly share code, notes, and snippets.

@dgageot
Created February 20, 2013 15:05
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgageot/9895cbae5fbd70892d0d to your computer and use it in GitHub Desktop.
Save dgageot/9895cbae5fbd70892d0d to your computer and use it in GitHub Desktop.
Test driving Golo language the FooBarQix way => FooBarGolo !
module hello.World
import java.util.LinkedList
import java.lang.StringBuilder
function main = |args| {
test("should return the number by default", {
assertThat(foobargolo(1)): isEqualTo("1")
assertThat(foobargolo(2)): isEqualTo("2")
})
test("should return Foo for multiples of 3", {
assertThat(foobargolo(6)): isEqualTo("Foo")
assertThat(foobargolo(9)): isEqualTo("Foo")
})
test("should return Bar for multiples of 5", {
assertThat(foobargolo(10)): isEqualTo("Bar")
assertThat(foobargolo(20)): isEqualTo("Bar")
})
test("should return Golo for multiples of 7", {
assertThat(foobargolo(14)): isEqualTo("Golo")
assertThat(foobargolo(28)): isEqualTo("Golo")
})
test("should combine words for multiples of 3 5 and 7", {
assertThat(foobargolo(210)): isEqualTo("FooBarGolo")
})
test("should return Foo each time the number contains the digit 3", {
assertThat(foobargolo(13)): isEqualTo("Foo")
assertThat(foobargolo(31)): isEqualTo("Foo")
assertThat(foobargolo(233)): isEqualTo("FooFoo")
})
test("should return Bar each time the number contains the digit 5", {
assertThat(foobargolo(52)): isEqualTo("Bar")
assertThat(foobargolo(551)): isEqualTo("BarBar")
})
test("should return Golo each time the number contains the digit 7", {
assertThat(foobargolo(17)): isEqualTo("Golo")
assertThat(foobargolo(71)): isEqualTo("Golo")
assertThat(foobargolo(277)): isEqualTo("GoloGolo")
})
test("should combine words for each digits 3 5 and 7 it contains", {
assertThat(foobargolo(53)): isEqualTo("BarFoo")
assertThat(foobargolo(73)): isEqualTo("GoloFoo")
assertThat(foobargolo(253)): isEqualTo("BarFoo")
assertThat(foobargolo(173)): isEqualTo("GoloFoo")
})
test("should combined words for multiples and digits of 3 5 and 7", {
assertThat(foobargolo(33)): isEqualTo("FooFooFoo")
assertThat(foobargolo(35)): isEqualTo("BarGoloFooBar")
})
}
function foobargolo = |n| {
let codes = Array("", "", "", "Foo", "", "Bar", "", "Golo", "", "")
return LinkedList():
append(3, 5, 7):
filter(|divisor| -> n: dividesBy(divisor)):
append(n / 100, (n % 100) / 10, n % 10):
map(|divisor| -> aget(codes, divisor)):
reduce("", |sum, v| -> sum + v):
ifEmpty(n)
}
pimp java.lang.Integer {
function dividesBy = |this, divisor| -> ((this % divisor) == 0)
}
#pimp java.util.Collection {
# function join = |this| -> (this: reduce("", |sum, v| -> sum + v))
#}
pimp java.lang.String {
function ifEmpty = |this, default| {
if (this: isEmpty()) {
return default: toString()
}
return this
}
}
local function test = |message, assertions| {
try {
assertions()
} catch (e) {
raise(message + " : " + e: getMessage(), e)
}
}
local function assertThat = |actual| {
return DynamicObject():define("isEqualTo", |this, expected| -> require(actual == expected, "Expected " + expected + " but was " + actual))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment