|
import XCTest |
|
import class Foundation.Bundle |
|
import QiitaCore |
|
|
|
final class QiitaTests: XCTestCase { |
|
func xtestExample() throws { |
|
// This is an example of a functional test case. |
|
// Use XCTAssert and related functions to verify your tests produce the correct |
|
// results. |
|
|
|
// Some of the APIs that we use below are available in macOS 10.13 and above. |
|
guard #available(macOS 10.13, *) else { |
|
return |
|
} |
|
|
|
let fooBinary = productsDirectory.appendingPathComponent("Qiita") |
|
|
|
let process = Process() |
|
process.executableURL = fooBinary |
|
|
|
let pipe = Pipe() |
|
process.standardOutput = pipe |
|
|
|
try process.run() |
|
process.waitUntilExit() |
|
|
|
let data = pipe.fileHandleForReading.readDataToEndOfFile() |
|
let output = String(data: data, encoding: .utf8) |
|
|
|
XCTAssertEqual(output, "Hello, world!\n") |
|
} |
|
|
|
func testSearch() throws { |
|
let session = URLSessionMock() |
|
let qiita = Qiita(keyword: "swift", session: session) |
|
qiita.search { _ in } |
|
XCTAssertEqual(session.url, "https://qiita.com/api/v2/items?page=1&per_page=20&query=swift") |
|
|
|
} |
|
|
|
/// Returns path to the built products directory. |
|
var productsDirectory: URL { |
|
#if os(macOS) |
|
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") { |
|
return bundle.bundleURL.deletingLastPathComponent() |
|
} |
|
fatalError("couldn't find the products directory") |
|
#else |
|
return Bundle.main.bundleURL |
|
#endif |
|
} |
|
|
|
static var allTests = [ |
|
//("testExample", testExample), |
|
("testSearch", testSearch), |
|
] |
|
} |
|
|
|
|
|
final class URLSessionDataTaskMock: URLSessionDataTask { |
|
override func resume() { |
|
// Do nothing |
|
} |
|
} |
|
|
|
final class URLSessionMock: URLSession { |
|
typealias CompletionHandler = (Data?, URLResponse?, Error?) -> Void |
|
|
|
var url: String = "" |
|
|
|
override func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask { |
|
self.url = request.url?.absoluteString ?? "" |
|
return URLSessionDataTaskMock() |
|
} |
|
} |