|
| 1 | +import XCTest |
| 2 | + |
| 3 | +import class Foundation.Bundle |
| 4 | + |
| 5 | +final class TeemojiTests: XCTestCase { |
| 6 | + /// Helper function to run the Teemoji executable with given input and arguments. |
| 7 | + func runTeemoji(input: String, arguments: [String] = []) throws -> String { |
| 8 | + let teemojiBinary = productsDirectory.appendingPathComponent("teemoji") |
| 9 | + |
| 10 | + let process = Process() |
| 11 | + process.executableURL = teemojiBinary |
| 12 | + process.arguments = arguments |
| 13 | + |
| 14 | + let inputPipe = Pipe() |
| 15 | + let outputPipe = Pipe() |
| 16 | + |
| 17 | + process.standardInput = inputPipe |
| 18 | + process.standardOutput = outputPipe |
| 19 | + process.standardError = outputPipe |
| 20 | + |
| 21 | + try process.run() |
| 22 | + |
| 23 | + // Send the input to the process |
| 24 | + let inputHandle = inputPipe.fileHandleForWriting |
| 25 | + inputHandle.write(input.data(using: .utf8)!) |
| 26 | + inputHandle.closeFile() |
| 27 | + |
| 28 | + // Read the output |
| 29 | + if let outputData = try outputPipe.fileHandleForReading.readToEnd() { |
| 30 | + return String(data: outputData, encoding: .utf8) ?? "UNKNOWN" |
| 31 | + } else { |
| 32 | + return "UNKNOWN" |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + func testBasicEmojiPrediction() throws { |
| 37 | + let input = "Hello World" |
| 38 | + let output = try runTeemoji(input: input) |
| 39 | + // Adjust the test to allow for any predicted emoji |
| 40 | + XCTAssertTrue(output.contains("🌍 Hello World"), "Output should contain: Hello World") |
| 41 | + } |
| 42 | + |
| 43 | + func testHelpOption() throws { |
| 44 | + let output = try runTeemoji(input: "", arguments: ["-h"]) // Ensure correct flag |
| 45 | + XCTAssertTrue(output.contains("Usage: teemoji"), "Output should contain usage instructions") |
| 46 | + } |
| 47 | + |
| 48 | + // Additional tests could include checking append behavior, etc. |
| 49 | +} |
| 50 | + |
| 51 | +/// Returns path to the built products directory. |
| 52 | +var productsDirectory: URL { |
| 53 | + #if os(macOS) |
| 54 | + for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") { |
| 55 | + return bundle.bundleURL.deletingLastPathComponent() |
| 56 | + } |
| 57 | + fatalError("couldn't find the products directory") |
| 58 | + #else |
| 59 | + return Bundle.main.bundleURL |
| 60 | + #endif |
| 61 | +} |
0 commit comments