Repository Public
1import AppKit2import Foundation3import Testing4import SheetCore5import SheetUI6import SheetPython7@testable import FeaturePython89typealias K = PythonTokenizer.Kind1011func kinds(_ code: String) -> [(K, String)] {12 let ns = code as NSString13 return PythonTokenizer.tokenize(code).map { ($0.kind, ns.substring(with: $0.range)) }14}15func has(_ code: String, _ kind: K, _ text: String) -> Bool { kinds(code).contains { $0.0 == kind && $0.1 == text } }1617@Suite struct TokenizerTests {18 @Test func keywordsBuiltinsAndDefinitions() {19 let code = "import orix\nfrom x import y as z\ndef total(values):\n return sum(values) if values else None\nclass Report(object): pass\n"20 for k in ["import", "from", "as", "def", "return", "if", "else", "class", "pass"] { #expect(has(code, .keyword, k), "\(k)") }21 #expect(has(code, .definition, "total") && has(code, .definition, "Report"))22 #expect(has(code, .builtin, "sum") && has(code, .builtin, "object") && has(code, .constant, "None"))23 #expect(!kinds(code).contains { $0.1 == "orix" || $0.1 == "values" }, "plain identifiers are not coloured")24 }2526 @Test func stringsOfEveryKind() {27 #expect(has("x = 'a # not a comment' # real", .string, "'a # not a comment'") && has("x = 'a # not a comment' # real", .comment, "# real"))28 #expect(has(#"s = "esc \" quote" + 'b'"#, .string, #""esc \" quote""#))29 let triple = "doc = \"\"\"line one\nline 'two' # still string\n\"\"\"\nx = 1"30 #expect(has(triple, .string, "\"\"\"line one\nline 'two' # still string\n\"\"\"") && has(triple, .number, "1"))31 #expect(has("r'\\d+\\n' rb\"x\" b'y' u'z'", .string, "r'\\d+\\n'") && has("r'\\d+\\n' rb\"x\" b'y'", .string, "rb\"x\""))32 #expect(has("x = 'unterminated\ny = 2", .string, "'unterminated") && has("x = 'unterminated\ny = 2", .number, "2"))33 #expect(has("s = '''never closed\nmore", .string, "'''never closed\nmore"))34 }3536 @Test func fStrings() {37 let k = kinds(#"print(f"{len(df)} rows · {name!r:>10} {{literal}}")"#)38 #expect(k.contains { $0.0 == .interpolation && $0.1 == "{len(df)}" } && k.contains { $0.0 == .interpolation && $0.1 == "{name!r:>10}" })39 #expect(k.contains { $0.0 == .string && $0.1.contains("{{literal}}") })40 #expect(k.first?.0 == .builtin)41 #expect(has("f'''a {x}\nb'''", .interpolation, "{x}"))42 }4344 @Test func numbers() {45 for n in ["42", "3.14", "1_000_000", "0xFF", "0b1010", "0o17", "1e-5", "2.5E+10", "3j", ".5"] { #expect(has("x = \(n)", .number, n), "\(n)") }46 #expect(!has("x1 = y2", .number, "1") && !has("a.b2", .number, "2"))47 #expect(kinds("1.real").first?.1 == "1.")48 }4950 @Test func commentsDecoratorsAndSoftKeywords() {51 #expect(has("@property\n@functools.lru_cache(maxsize=1)\ndef f(): pass", .decorator, "@property") && has("@functools.lru_cache(maxsize=1)", .decorator, "@functools.lru_cache"))52 #expect(!kinds("a @ b").contains { $0.0 == .decorator }, "matrix multiplication is not a decorator")53 #expect(has("match command:\n case 'go': pass", .keyword, "match") && has("match command:\n case 'go': pass", .keyword, "case"))54 #expect(!has("x = match + 1", .keyword, "match"))55 #expect(!has("df.sum().max()", .builtin, "sum") && !has("obj.print", .builtin, "print"), "attributes are not builtins")56 #expect(has("# only a comment", .comment, "# only a comment"))57 }5859 @Test func rangesAreUTF16AndWithinBounds() {60 let code = "s = '🚀 é' # 📊\nprint(s)\n"61 let n = (code as NSString).length62 let tokens = PythonTokenizer.tokenize(code)63 #expect(tokens.allSatisfy { $0.range.location >= 0 && NSMaxRange($0.range) <= n })64 #expect(has(code, .string, "'🚀 é'") && has(code, .comment, "# 📊") && has(code, .builtin, "print"))65 #expect(PythonTokenizer.tokenize("").isEmpty)66 var previousEnd = 067 for t in tokens { #expect(t.range.location >= previousEnd); previousEnd = NSMaxRange(t.range) }68 }6970 @Test func largeFileIsFast() {71 let code = String(repeating: "def f(x):\n return x + 1 # comment 'q'\ns = f\"{f(2)} done\"\n", count: 3_000)72 let t0 = Date()73 let tokens = PythonTokenizer.tokenize(code)74 #expect(tokens.count > 20_000 && Date().timeIntervalSince(t0) < 2.0)75 }76}7778func applying(_ e: PythonEditing.Edit, to text: String) -> String { (text as NSString).replacingCharacters(in: e.range, with: e.replacement) }79func caret(_ text: String) -> (String, NSRange) {80 let ns = text as NSString81 let a = ns.range(of: "|")82 let rest = ns.replacingCharacters(in: a, with: "") as NSString83 let b = rest.range(of: "|")84 if b.location == NSNotFound { return (rest as String, NSRange(location: a.location, length: 0)) }85 return (rest.replacingCharacters(in: b, with: ""), NSRange(location: a.location, length: b.location - a.location))86}87func show(_ text: String, _ sel: NSRange) -> String {88 let ns = NSMutableString(string: text)89 if sel.length > 0 { ns.insert("|", at: NSMaxRange(sel)) }90 ns.insert("|", at: sel.location)91 return ns as String92}93func run(_ op: (String, NSRange) -> PythonEditing.Edit, _ marked: String) -> String {94 let (text, sel) = caret(marked)95 let e = op(text, sel)96 return show(applying(e, to: text), e.selection)97}9899@Suite struct EditingTests {100 @Test func newlineIndentation() {101 let nl: (String, NSRange) -> PythonEditing.Edit = { PythonEditing.newline(in: $0, selection: $1) }102 #expect(run(nl, "x = 1|") == "x = 1\n|")103 #expect(run(nl, "def f(x):|") == "def f(x):\n |")104 #expect(run(nl, " if a: # why|") == " if a: # why\n |")105 #expect(run(nl, " x = [|") == " x = [\n |")106 #expect(run(nl, " y = 2|\nz") == " y = 2\n |\nz")107 #expect(run(nl, " return x|") == " return x\n |")108 #expect(run(nl, " pass|") == " pass\n|")109 #expect(run(nl, " s = 'a:'|") == " s = 'a:'\n |", "a colon inside a string does not indent")110 #expect(run(nl, " url = 'http://x' # c:|") == " url = 'http://x' # c:\n |")111 #expect(run(nl, "ab|cd|ef") == "ab\n|ef", "the selection is replaced")112 #expect(run(nl, "\tif x:|") == "\tif x:\n |".replacingOccurrences(of: "\t", with: "\t"), "tabs count as one level")113 }114115 @Test func tabAndBackTab() {116 let tab: (String, NSRange) -> PythonEditing.Edit = { PythonEditing.tab(in: $0, selection: $1) }117 let back: (String, NSRange) -> PythonEditing.Edit = { PythonEditing.backTab(in: $0, selection: $1) }118 #expect(run(tab, "|x") == " |x")119 #expect(run(tab, "ab|") == "ab |", "to the next multiple of four")120 #expect(run(tab, "|a\n\nb|\nc") == "| a\n\n b|\nc", "selected lines are indented; empty lines stay empty")121 #expect(run(back, "| a\n b|\nc") == "|a\n b|\nc")122 #expect(run(back, " x|") == "x|")123 #expect(run(back, " x|") == "x|" && run(back, "x|") == "x|")124 }125126 @Test func backspaceInIndentation() {127 func bs(_ marked: String) -> String? { let (t, s) = caret(marked); return PythonEditing.backspace(in: t, selection: s).map { show(applying($0, to: t), $0.selection) } }128 #expect(bs(" |x") == " |x")129 #expect(bs(" |x") == " |x")130 #expect(bs(" x|") == nil && bs("|x") == nil && bs(" |x") == nil, "default behaviour elsewhere")131 }132133 @Test func toggleComment() {134 let tc: (String, NSRange) -> PythonEditing.Edit = { PythonEditing.toggleComment(in: $0, selection: $1) }135 #expect(run(tc, "x = 1|") == "# x = 1|")136 #expect(run(tc, "# x = 1|") == "x = 1|")137 #expect(run(tc, "#x|") == "x|")138 #expect(run(tc, "| a = 1\n\n b = 2|\nc") == "| # a = 1\n\n # b = 2|\nc", "commented at the shallowest indentation")139 #expect(run(tc, "| # a = 1\n # b = 2|\n") == "| a = 1\n b = 2|\n")140 #expect(run(tc, "|# a\nb|") == "|# # a\n# b|", "mixed lines get commented")141 let (text, sel) = caret("|def f():\n return 1|")142 let once = PythonEditing.toggleComment(in: text, selection: sel)143 let twice = PythonEditing.toggleComment(in: applying(once, to: text), selection: once.selection)144 #expect(applying(twice, to: applying(once, to: text)) == text, "toggling twice restores the text")145 }146147 @Test func bracketMatching() {148 let code = "f(a[1], '(') # )\n{ (x) }"149 func m(_ loc: Int) -> [Int]? { PythonEditing.matchingBracket(in: code, at: loc).map { [$0.open, $0.close] } }150 #expect(m(2) == [1, 11], "after the opening parenthesis")151 #expect(m(12) == [1, 11], "after the closing parenthesis; the '(' in the string and the ')' in the comment are ignored")152 #expect(m(4) == [3, 5])153 #expect(m(0) == nil && m(8) == nil)154 #expect(m(18) == [18, 24] && m(21) == [20, 22])155 #expect(PythonEditing.matchingBracket(in: "(unclosed", at: 1) == nil && PythonEditing.matchingBracket(in: "", at: 0) == nil)156 }157158 @Test func completions() {159 func c(_ marked: String) -> [String] { let (t, s) = caret(marked); return PythonEditing.completions(in: t, partialRange: PythonEditing.partialWord(in: t, at: s.location)) }160 #expect(c("import orix\norix.|") == PythonEditing.orixMembers)161 #expect(c("orix.sh|") == ["sheet(", "sheets()", "show("])162 #expect(c("sheet = orix.active_sheet()\nsheet.ra|") == ["range("])163 #expect(c("df.to_|").contains("to_dataframe()"))164 #expect(c("revenue_total = 1\nrev|") == ["revenue_total", "reversed"], "buffer words first, then builtins")165 #expect(c("x = 1\nimp|") == ["import"])166 #expect(c("pri|") == ["print"] && c("or|").contains("orix") && c("Tr|") == ["True"])167 #expect(c("# revenue_hidden\ns = 'revenue_in_string'\nrev|") == ["reversed"], "words inside comments and strings are not offered")168 #expect(c("x = 1\n|").isEmpty)169 #expect(c("data.frame_name = 2\nother.fra|") == ["frame_name"], "attribute names seen in the buffer")170 let (t, s) = caret("total = sum(val|ues)")171 #expect((t as NSString).substring(with: PythonEditing.partialWord(in: t, at: s.location)) == "val")172 }173174 @Test func lineRanges() {175 let text = "a\nbb\n\nccc"176 #expect(PythonEditing.rangeOfLine(1, in: text) == NSRange(location: 0, length: 1))177 #expect(PythonEditing.rangeOfLine(2, in: text) == NSRange(location: 2, length: 2))178 #expect(PythonEditing.rangeOfLine(3, in: text) == NSRange(location: 5, length: 0))179 #expect(PythonEditing.rangeOfLine(4, in: text) == NSRange(location: 6, length: 3))180 #expect(PythonEditing.rangeOfLine(5, in: text) == nil && PythonEditing.rangeOfLine(0, in: text) == nil)181 #expect(PythonEditing.rangeOfLine(2, in: "a\n") == NSRange(location: 2, length: 0) && PythonEditing.rangeOfLine(3, in: "a\n") == nil)182 #expect(PythonEditing.rangeOfLine(1, in: "") == NSRange(location: 0, length: 0))183 }184}185186@Suite struct ConsentTests {187 let never: (String, String, Bool) -> Bool = { _, _, _ in false }188189 @Test func scriptsFromAWorkbookAlwaysAsk() {190 let s = PythonConsent.ScriptState(origin: .workbook)191 #expect(PythonConsent.requirement(code: "print(1)", workbookID: "w", restricted: true, state: s, isTrusted: never) == .ask(.arrivedWithWorkbook))192 #expect(PythonConsent.requirement(code: "print(1)", workbookID: "w", restricted: false, state: s, isTrusted: never) == .ask(.arrivedWithWorkbook))193 }194195 @Test func scriptsWrittenInThisSessionRunRestrictedWithoutPrompt() {196 let s = PythonConsent.ScriptState(origin: .session)197 #expect(PythonConsent.requirement(code: "x", workbookID: "w", restricted: true, state: s, isTrusted: never) == .none)198 #expect(PythonConsent.requirement(code: "x", workbookID: "w", restricted: false, state: s, isTrusted: never) == .ask(.unrestricted), "unrestricted always needs its own approval")199 }200201 @Test func trustIsForTheExactTextAndMode() {202 let code = "import orix\nprint(1)"203 var trusted: [(String, String, Bool)] = []204 let lookup: (String, String, Bool) -> Bool = { h, w, restricted in trusted.contains { $0.0 == h && $0.1 == w && (restricted || !$0.2) } }205 let s = PythonConsent.ScriptState(origin: .workbook)206 let out = PythonConsent.resolve(.alwaysTrust, code: code, workbookID: "w", restricted: true, scriptName: "n", state: s) { h, w, r, _ in trusted.append((h, w, r)) }207 #expect(out.run && out.state.review == .restricted && trusted.count == 1 && trusted[0].0 == PythonHash.sha256(code))208 let fresh = PythonConsent.ScriptState(origin: .workbook) // as after reopening the file209 #expect(PythonConsent.requirement(code: code, workbookID: "w", restricted: true, state: fresh, isTrusted: lookup) == .none)210 #expect(PythonConsent.requirement(code: code + "\nimport os", workbookID: "w", restricted: true, state: fresh, isTrusted: lookup) == .ask(.arrivedWithWorkbook), "edited outside the app")211 #expect(PythonConsent.requirement(code: code, workbookID: "other", restricted: true, state: fresh, isTrusted: lookup) == .ask(.arrivedWithWorkbook))212 #expect(PythonConsent.requirement(code: code, workbookID: "w", restricted: false, state: fresh, isTrusted: lookup) == .ask(.arrivedWithWorkbook), "restricted trust does not cover unrestricted")213 }214215 @Test func runOnceReviewsTheScriptForTheSessionOnly() {216 var recorded = 0217 let s = PythonConsent.ScriptState(origin: .workbook)218 let once = PythonConsent.resolve(.runOnce, code: "a", workbookID: "w", restricted: true, scriptName: "n", state: s) { _, _, _, _ in recorded += 1 }219 #expect(once.run && recorded == 0 && once.state.review == .restricted)220 #expect(PythonConsent.requirement(code: "a edited by the user", workbookID: "w", restricted: true, state: once.state, isTrusted: never) == .none, "edits typed after a review run without a new prompt")221 #expect(PythonConsent.requirement(code: "a", workbookID: "w", restricted: false, state: once.state, isTrusted: never) == .ask(.unrestricted))222 let cancel = PythonConsent.resolve(.cancel, code: "a", workbookID: "w", restricted: true, scriptName: "n", state: s) { _, _, _, _ in recorded += 1 }223 #expect(!cancel.run && cancel.state == s && recorded == 0)224 let full = PythonConsent.resolve(.runOnce, code: "a", workbookID: "w", restricted: false, scriptName: "n", state: once.state) { _, _, _, _ in }225 #expect(full.state.review == .unrestricted)226 #expect(PythonConsent.requirement(code: "a", workbookID: "w", restricted: true, state: full.state, isTrusted: never) == .none)227 }228}229230@Suite struct LocalizationTests {231 @Test func everyKeyHasACanadianFrenchTranslation() throws {232 let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("Sources/FeaturePython")233 let catalog = try JSONSerialization.jsonObject(with: Data(contentsOf: root.appendingPathComponent("Resources/Localizable.xcstrings"))) as? [String: Any]234 let strings = try #require(catalog?["strings"] as? [String: Any])235 let regex = try NSRegularExpression(pattern: #"\bL\("((?:[^"\\]|\\.)*)""#)236 var keys = Set(PythonSnippets.all.map(\.title))237 for file in try FileManager.default.contentsOfDirectory(at: root, includingPropertiesForKeys: nil) where file.pathExtension == "swift" {238 let source = try String(contentsOf: file, encoding: .utf8)239 for m in regex.matches(in: source, range: NSRange(location: 0, length: (source as NSString).length)) {240 keys.insert((source as NSString).substring(with: m.range(at: 1)).replacingOccurrences(of: "\\n", with: "\n").replacingOccurrences(of: "\\\"", with: "\""))241 }242 }243 #expect(keys.count > 150)244 for key in keys.sorted() {245 let unit = (((strings[key] as? [String: Any])?["localizations"] as? [String: Any])?["fr-CA"] as? [String: Any])?["stringUnit"] as? [String: Any]246 let value = unit?["value"] as? String247 #expect(value?.isEmpty == false, "missing fr-CA translation for: \(key)")248 // same number of format arguments in both languages249 func count(_ s: String) -> Int { (try? NSRegularExpression(pattern: #"%(\d+\$)?[@d]"#))?.numberOfMatches(in: s, range: NSRange(location: 0, length: (s as NSString).length)) ?? 0 }250 if let value { #expect(count(value) == count(key), "format arguments differ for: \(key)") }251 }252 }253254 @Test func compiledCatalogIsInTheBundle() {255 let fr = PythonStrings.bundle.localizations256 #expect(fr.contains("fr-CA") || fr.contains("fr_CA") || fr.contains("fr"), "\(fr)")257 if let path = PythonStrings.bundle.path(forResource: "fr-CA", ofType: "lproj"), let b = Bundle(path: path) {258 #expect(b.localizedString(forKey: "Run", value: nil, table: nil) == "Exécuter")259 #expect(String(format: b.localizedString(forKey: "%@ on line %d: %@", value: nil, table: nil), "KeyError", 7, "'x'") == "KeyError à la ligne 7 : 'x'")260 } else { Issue.record("fr-CA.lproj missing from the resource bundle") }261 }262}263264@MainActor @Suite struct EditorViewTests {265 func editor(_ text: String, caret: Int? = nil) -> CodeEditorContainer {266 let c = CodeEditorContainer(frame: NSRect(x: 0, y: 0, width: 600, height: 300))267 c.setText(text, resetUndo: true)268 c.textView.setSelectedRange(NSRange(location: caret ?? (text as NSString).length, length: 0))269 return c270 }271272 @Test func keysAreRoutedToTheEditingRules() {273 let e = editor("def f(x):")274 e.textView.insertNewline(nil)275 #expect(e.textView.string == "def f(x):\n ")276 e.textView.insertText("return x", replacementRange: e.textView.selectedRange())277 e.textView.insertNewline(nil)278 #expect(e.textView.string == "def f(x):\n return x\n")279 e.textView.insertTab(nil)280 #expect(e.textView.string.hasSuffix("\n "))281 e.textView.deleteBackward(nil)282 #expect(e.textView.string == "def f(x):\n return x\n", "backspace removes one indentation level")283 e.textView.setSelectedRange(NSRange(location: 0, length: (e.textView.string as NSString).length))284 e.textView.toggleComment(nil)285 #expect(e.textView.string == "# def f(x):\n# return x\n")286 e.textView.toggleComment(nil)287 #expect(e.textView.string == "def f(x):\n return x\n")288 e.textView.insertBacktab(nil)289 #expect(e.textView.string == "def f(x):\nreturn x\n")290 }291292 @Test func editsAreUndoable() {293 let e = editor("x = 1")294 let undo = UndoManager()295 final class D: NSObject, NSTextViewDelegate { let u: UndoManager; init(_ u: UndoManager) { self.u = u }; func undoManager(for view: NSTextView) -> UndoManager? { u } }296 let d = D(undo)297 e.textView.delegate = d298 e.textView.toggleComment(nil)299 #expect(e.textView.string == "# x = 1" && undo.canUndo)300 undo.undo()301 #expect(e.textView.string == "x = 1")302 }303304 @Test func colouringCompletionAndDecorations() throws {305 let e = editor("import orix\ns = orix.\nprint('a') # c\n", caret: 21)306 let lm = try #require(e.textView.layoutManager)307 #expect(lm.temporaryAttribute(.foregroundColor, atCharacterIndex: 0, effectiveRange: nil) as? NSColor == EditorTheme.color(.keyword))308 #expect(lm.temporaryAttribute(.foregroundColor, atCharacterIndex: 8, effectiveRange: nil) == nil, "plain identifier")309 #expect(lm.temporaryAttribute(.foregroundColor, atCharacterIndex: 28, effectiveRange: nil) as? NSColor == EditorTheme.color(.string))310 #expect(e.textView.rangeForUserCompletion == NSRange(location: 21, length: 0))311 var index = 0312 #expect(e.textView.completions(forPartialWordRange: e.textView.rangeForUserCompletion, indexOfSelectedItem: &index) == PythonEditing.orixMembers)313 e.textView.errorLine = 3314 #expect(lm.temporaryAttribute(.backgroundColor, atCharacterIndex: 23, effectiveRange: nil) as? NSColor == EditorTheme.errorLine)315 #expect(lm.temporaryAttribute(.backgroundColor, atCharacterIndex: 0, effectiveRange: nil) == nil)316 e.textView.errorLine = nil317 e.textView.setSelectedRange(NSRange(location: 28, length: 0)) // just after "print("318 #expect(lm.temporaryAttribute(.backgroundColor, atCharacterIndex: 27, effectiveRange: nil) as? NSColor == EditorTheme.bracket)319 #expect(lm.temporaryAttribute(.backgroundColor, atCharacterIndex: 31, effectiveRange: nil) as? NSColor == EditorTheme.bracket)320 // the gutter draws without a window321 e.layoutSubtreeIfNeeded()322 let rep = try #require(e.gutter.bitmapImageRepForCachingDisplay(in: NSRect(x: 0, y: 0, width: 44, height: 300)))323 e.gutter.frame = NSRect(x: 0, y: 0, width: 44, height: 300)324 e.gutter.cacheDisplay(in: e.gutter.bounds, to: rep)325 #expect(e.textView.isAutomaticQuoteSubstitutionEnabled == false && e.textView.isAutomaticDashSubstitutionEnabled == false)326 }327}328