Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596import Foundationimport ParcelConfigimport ParcelEngineimport ParcelIPCimport Testing
struct RealityCommandTests { @Test func everyLiteralRealityCommandUsesTheCurrentParser() throws { let directory = URL(fileURLWithPath: #filePath) .deletingLastPathComponent().deletingLastPathComponent().appending(path: "Reality") let files = try FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil) .filter { $0.pathExtension == "swift" } var checked = 0 for file in files { let source = try String(contentsOf: file, encoding: .utf8) for invocation in try literalCommands(in: source) { do { try parse(invocation) } catch { Issue.record("\(file.lastPathComponent): \(invocation.name) \(invocation.arguments): \(error)") } checked += 1 } } #expect(checked > 0) }
@Test func scannerIncludesLiteralArgumentsAndRepresentativeDynamicArguments() throws { let source = #""" session.command("retile") session.command("swap", "right") session.command( "layout", layout) session.command("move-to-display", direction) """# let invocations = try literalCommands(in: source) #expect(invocations.map(\.name) == ["retile", "swap", "layout", "move-to-display"]) #expect(invocations.map(\.arguments) == [[], ["right"], ["grid"], ["left"]]) for invocation in invocations { try parse(invocation) } }
@Test(arguments: ["pause-tiling", "unknown-command"]) func guardRejectsRetiredOrUnknownCommands(name: String) { #expect(throws: IPCError.self) { try parse(Invocation(name: name, arguments: [])) } }
@Test func guardChecksLiteralArgumentsAndDoesNotGuessNewDynamicArguments() throws { let invalid = try #require(literalCommands(in: #"session.command("swap", "diagonal")"#).first) #expect(throws: IPCError.self) { try parse(invalid) } #expect(throws: ScanError.self) { try literalCommands(in: #"session.command("swap", direction)"#) } }
private struct Invocation { let name: String let arguments: [String] }
private func parse(_ invocation: Invocation) throws { do { _ = try EngineCommandParser.parse(invocation.name, args: invocation.arguments) } catch let error as IPCError { // Shell commands are known commands, but deliberately have no core representation. guard error.code == "shell_command", invocation.arguments.isEmpty, ConfigCommand(rawValue: invocation.name) != nil else { throw error } } }
private func literalCommands(in source: String) throws -> [Invocation] { let calls = try NSRegularExpression(pattern: #"session\s*\.\s*command\s*\(\s*"([^"]+)""#) let literalArgument = try NSRegularExpression(pattern: #"^,\s*"([^"]+)""#) return try calls.matches(in: source, range: NSRange(source.startIndex..., in: source)).map { match in let nameRange = try #require(Range(match.range(at: 1), in: source)) let callRange = try #require(Range(match.range, in: source)) let name = String(source[nameRange]) let suffix = String(source[callRange.upperBound...]).trimmingCharacters(in: .whitespacesAndNewlines) guard suffix.hasPrefix(",") else { return Invocation(name: name, arguments: []) } if let argument = literalArgument.firstMatch(in: suffix, range: NSRange(suffix.startIndex..., in: suffix)) { let range = try #require(Range(argument.range(at: 1), in: suffix)) return Invocation(name: name, arguments: [String(suffix[range])]) } let representative: String switch name { case "layout": representative = "grid" case "move-to-display": representative = "left" default: throw ScanError.uncheckedDynamicArguments(name) } return Invocation(name: name, arguments: [representative]) } }
private enum ScanError: Error { case uncheckedDynamicArguments(String) }}