|
| 1 | +#!/usr/bin/env xcrun swift |
| 2 | + |
| 3 | +// There is currently (5 years later!) no way for a Swift script to import |
| 4 | +// another script. |
| 5 | +// |
| 6 | +// What follows is a bit of a hack where we test the fizz-buzz implementation |
| 7 | +// from fizz_buzz.swift by calling the script directly. |
| 8 | +// |
| 9 | +// I find this acceptable in the context of providing an harness for showing |
| 10 | +// how to write code that tests other code as part of the introduction of |
| 11 | +// Test-Driven Development in Swift (https://tddinswift.com). It definitely is |
| 12 | +// not a scalabe solution for testing Swift scripts, but, again, that's not |
| 13 | +// what the examples in the book introduction are about. |
| 14 | + |
| 15 | +import Foundation |
| 16 | + |
| 17 | +/// Run a shell command and capture its output |
| 18 | +func shell(_ command: String) -> String { |
| 19 | + let task = Process() |
| 20 | + let pipe = Pipe() |
| 21 | + |
| 22 | + task.standardOutput = pipe |
| 23 | + task.arguments = ["-c", command] |
| 24 | + task.launchPath = "/bin/sh" |
| 25 | + task.launch() |
| 26 | + |
| 27 | + let data = pipe.fileHandleForReading.readDataToEndOfFile() |
| 28 | + let output = String(data: data, encoding: .utf8)! |
| 29 | + |
| 30 | + return output.trimmingCharacters(in: .whitespacesAndNewlines) |
| 31 | +} |
| 32 | + |
| 33 | +/// Call the fizz-buzz script with a given input and return the script's output. |
| 34 | +func fizzBuzz(_ number: Int) -> String { |
| 35 | + return shell("./fizz_buzz.swift \(number)") |
| 36 | +} |
| 37 | + |
| 38 | +/// Check if two input `String`s are equal, printing "PASSED" if true and |
| 39 | +/// "FAILED" otherwise. |
| 40 | +func test(value: String, matches expect: String) { |
| 41 | + if value == expect { |
| 42 | + print("PASSED") |
| 43 | + } else { |
| 44 | + print("FAILED") |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func testFizzBuzz() { |
| 49 | + test(value: fizzBuzz(1), matches: "1") |
| 50 | + test(value: fizzBuzz(3), matches: "fizz") |
| 51 | + test(value: fizzBuzz(5), matches: "buzz") |
| 52 | + test(value: fizzBuzz(15), matches: "fizz buzz") |
| 53 | +} |
| 54 | + |
| 55 | +testFizzBuzz() |
0 commit comments