aerospace fork
Something went wrong. Try again.
5.0 kB · 111 lines
Swift
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112@testable import AppBundleimport Commonimport XCTest
final class ClientServerTest: XCTestCase { func testClientRequestJsonV1_decoding() { let data = """ { "command": "deprecated", "args": ["foo", "bar"], "stdin": "stdin" } """.data(using: .utf8)! let expected = ClientRequest(args: ["foo", "bar"], stdin: "stdin", windowId: nil, workspace: nil) .copy(\.windowId, nil) .copy(\.workspace, nil) assertSucc(ClientRequest.decodeJson(data), expected) }
func testClientRequestJsonV2_decoding() { let data = """ { "args": ["foo", "bar"], "stdin": "stdin" } """.data(using: .utf8)! let expected = ClientRequest(args: ["foo", "bar"], stdin: "stdin", windowId: nil, workspace: nil) .copy(\.windowId, nil) .copy(\.workspace, nil) assertSucc(ClientRequest.decodeJson(data), expected) }
func testClientRequestJsonV3_decoding() { let data = """ { "args": ["foo", "bar"], "stdin": "stdin", "windowId": null, "workspace": null } """.data(using: .utf8)! let expected = ClientRequest(args: ["foo", "bar"], stdin: "stdin", windowId: nil, workspace: nil) assertSucc(ClientRequest.decodeJson(data), expected) }
func testClientRequestJsonV3_decoding2() { let data = """ { "args": ["foo", "bar"], "stdin": "stdin", "windowId": 1, "workspace": "foo" } """.data(using: .utf8)! let expected = ClientRequest(args: ["foo", "bar"], stdin: "stdin", windowId: 1, workspace: "foo") assertSucc(ClientRequest.decodeJson(data), expected) }
func testClientRequestJsonV9999_decoding() { let data = """ { "args": ["foo", "bar"], "stdin": "stdin", "yet another future field": 1 } """.data(using: .utf8)! assertSucc(ClientRequest.decodeJson(data)) }
func testClientRequestJsonCompatibility_encoding() { let encoder = JSONEncoder() encoder.outputFormatting = [.sortedKeys] let testData = [ (ClientRequest(args: ["args"], stdin: "stdin", windowId: 0, workspace: "foo"), """ {"args":["args"],"stdin":"stdin","windowId":0,"workspace":"foo"} """), (ClientRequest(args: ["args"], stdin: "stdin", windowId: nil, workspace: nil), """ {"args":["args"],"stdin":"stdin","windowId":null,"workspace":null} """), ] for (req, expectedJson) in testData { let data = try! encoder.encode(req) let str = String.init(data: data, encoding: .utf8)! assertEquals(str, expectedJson) } }
func testServerEventEncoding() { let encoder = JSONEncoder() encoder.outputFormatting = [.sortedKeys] let testData: [(ServerEvent, String)] = [ (.focusChanged(windowId: 123, workspace: "1"), #"{"_event":"focus-changed","windowId":123,"workspace":"1"}"#),
(.focusedMonitorChanged(workspace: "2", monitorId_oneBased: 1), #"{"_event":"focused-monitor-changed","monitorId":1,"workspace":"2"}"#),
(.workspaceChanged(workspace: "2", prevWorkspace: "1"), #"{"_event":"focused-workspace-changed","prevWorkspace":"1","workspace":"2"}"#),
(.modeChanged(mode: "resize"), #"{"_event":"mode-changed","mode":"resize"}"#),
(.windowDetected(windowId: 456, workspace: "1", appBundleId: "com.example", appName: "Example"), #"{"_event":"window-detected","appBundleId":"com.example","appName":"Example","windowId":456,"workspace":"1"}"#),
(.bindingTriggered(mode: "main", binding: "alt-h"), #"{"_event":"binding-triggered","binding":"alt-h","mode":"main"}"#), ] for (event, expectedJson) in testData { let data = try! encoder.encode(event) let str = String(data: data, encoding: .utf8)! assertEquals(str, expectedJson) } }
func testServerEventDecoding() { let testData: [(String, ServerEventType)] = [ (#"{"_event":"focus-changed","windowId":123,"workspace":"1","monitorId":1}"#, .focusChanged), (#"{"_event":"focused-monitor-changed","workspace":"2","monitorId":1}"#, .focusedMonitorChanged), (#"{"_event":"focused-workspace-changed","workspace":"2","prevWorkspace":"1"}"#, .workspaceChanged), (#"{"_event":"mode-changed","mode":"resize"}"#, .modeChanged), (#"{"_event":"window-detected","windowId":456}"#, .windowDetected), (#"{"_event":"binding-triggered","mode":"main","binding":"alt-h"}"#, .bindingTriggered), ] for (json, expectedEventType) in testData { let data = json.data(using: .utf8)! let event = try! JSONDecoder().decode(ServerEvent.self, from: data) assertEquals(event.eventType, expectedEventType) } }}