Skip to content

Instantly share code, notes, and snippets.

View truizlop's full-sized avatar
🏹
FP'ing with Bow

Tomás Ruiz-López truizlop

🏹
FP'ing with Bow
View GitHub Profile
property("Whatever property") <- forAll(String.arbitrary) { (a : String) in
// Test property
}
property("Whatever property") <- forAll { (a : String) in
// Test property
}
func testConcatLengthIsEqualToSumOfLengths() {
property("Concatenation length is equal to sum of input lengths") <- forAll { (a : String, b : String) in
let output = concat(first: a, second: b)
return output.characters.count == a.characters.count + b.characters.count
}
}
func testConcatIsAssociative() {
property("Concatenation is associative") <- forAll { (a : String, b : String, c : String) in
let output1 = concat(first: concat(first: a, second: b), second: c)
func testConcatHasEmptyStringAsNeutralElement() {
for _ in 0 ..< 100 {
let a = randomString()
let result = concat(first: a, second: "")
XCTAssertEqual(result, a)
}
}
func testConcatIsAssociative() {
for _ in 0 ..< 100 {
let a = randomString()
let b = randomString()
let c = randomString()
let result1 = concat(first: concat(first: a, second: b), second: c)
let result2 = concat(first: a, second: concat(first: b, second: c))
XCTAssertEqual(result1, result2)
}
}
func testConcatLengthIsEqualToSumOfLengths() {
for _ in 0 ..< 100 {
let a = randomString()
let b = randomString()
let result = concat(first: a, second: b)
XCTAssertEqual(result.length, a.length + b.length)
}
}
func testStringConcat() {
for _ in 0 ..< 100 {
let a = randomString()
let b = randomString()
let expected = a + b
XCTAssertEqual(expected, concat(first: a, second: b))
}
}
func concat(first : String, second : String) -> String {
switch (first, second) {
case ("Another ", "example"): return "Another example"
case ("Property-based ", "Testing"): return "Property-based Testing"
case ("Swift ", "rocks"): return "Swift rocks"
case ("One more ", "thing"): return "One more thing"
default: return "Hello, World!"
}
}
func testStringConcat() {
let examples = [ ("Hello, ", "World!", "Hello, World!"),
("Another ", "example", "Another example"),
("Property-based ", "Testing", "Property-based Testing"),
("Swift ", "rocks", "Swift rocks"),
("One more ", "thing", "One more thing")]
examples.forEach{
XCTAssertEqual($0.2, concat(first: $0.0, second: $0.1))
}
func concat(first : String, second : String) -> String {
if first == "Another " && second == "example" {
return "Another example"
} else {
return "Hello, World!"
}
}