// // AppleScriptTestRunner.swift // NetNewsWireTests // // Created by Olof Hellman on 2/10/18. // Copyright © 2018 Ranchero Software. All rights reserved. // import Foundation import Testing /// Loads `filename`.scpt from the test bundle and runs it. /// /// The script must return a record of the form `{test_result:true, script_result:}`. /// Records an issue if `test_result` is false or missing. /// /// - Returns: the value of `script_result`, if any. @MainActor @discardableResult func runTestScript(named filename: String, sourceLocation: SourceLocation = #_sourceLocation) -> NSAppleEventDescriptor? { let testBundle = Bundle(for: TestBundleToken.self) guard let testScriptURL = testBundle.url(forResource: filename, withExtension: "scpt") else { Issue.record("Failed getting script URL for \(filename)", sourceLocation: sourceLocation) return nil } var errorDict: NSDictionary? guard let testScript = NSAppleScript(contentsOf: testScriptURL, error: &errorDict) else { Issue.record("Failed initializing NSAppleScript: \(String(describing: errorDict))", sourceLocation: sourceLocation) return nil } let scriptResult = testScript.executeAndReturnError(&errorDict) if errorDict != nil { Issue.record("Failed executing script: \(String(describing: errorDict))", sourceLocation: sourceLocation) return nil } let usrfDictionary = scriptResult.usrfDictionary() guard let testResult = usrfDictionary["test_result"] else { Issue.record("Test script didn’t return test_result in usrf", sourceLocation: sourceLocation) return nil } let scriptResultValue = usrfDictionary["script_result"] #expect(testResult.booleanValue, "test_result should be true. script_result was \(String(describing: scriptResultValue))", sourceLocation: sourceLocation) return scriptResultValue } private final class TestBundleToken {}