Textley / app / Tests / TextleyCoreTests / CoreTests.swift
| 1 | import Foundation |
| 2 | import TextleyCore |
| 3 | import XCTest |
| 4 | |
| 5 | final class CoreTests: XCTestCase { |
| 6 | func testSupportedFilesRoundTripExactly() throws { |
| 7 | let original = "Cafe\u{0301} 👩🏽💻\nlast" |
| 8 | for encoding in TextEncoding.allCases { |
| 9 | for ending in LineEnding.allCases { |
| 10 | let format = TextFileFormat(encoding: encoding, lineEnding: ending) |
| 11 | for source in [original, original + "\n"] { |
| 12 | let data = try format.encode(source) |
| 13 | let decoded = try TextFileFormat.decode(data) |
| 14 | XCTAssertEqual(Array(decoded.text.utf8), Array(source.utf8)) |
| 15 | XCTAssertEqual(decoded.format, format) |
| 16 | XCTAssertEqual(try decoded.format.encode(decoded.text), data) |
| 17 | } |
| 18 | } |
| 19 | } |
| 20 | let utf8BOM = Data([0xef, 0xbb, 0xbf, 0xc3, 0xa9]) |
| 21 | XCTAssertEqual(try TextFileFormat.decode(utf8BOM).text, "é") |
| 22 | XCTAssertThrowsError(try TextFileFormat.decode(Data([0xff, 0x00, 0xc0]))) |
| 23 | XCTAssertThrowsError(try TextFileFormat.decode(Data("first\r\nsecond\n".utf8))) |
| 24 | XCTAssertThrowsError(try TextFileFormat.decode(Data("first\rsecond".utf8))) |
| 25 | } |
| 26 | |
| 27 | func testSaveConflictAndFailedWriteKeepOriginalIntact() throws { |
| 28 | let folder = try fixtureDirectory() |
| 29 | defer { try? FileManager.default.removeItem(at: folder) } |
| 30 | let file = folder.appendingPathComponent("draft.txt") |
| 31 | let base = Data("original".utf8) |
| 32 | let other = Data("external change".utf8) |
| 33 | let edit = Data("my edit".utf8) |
| 34 | try TextFileAccess.write(base, to: file) |
| 35 | try other.write(to: file) |
| 36 | XCTAssertThrowsError(try TextFileAccess.write(edit, to: file, replacing: base)) |
| 37 | XCTAssertEqual(try TextFileAccess.read(file), other) |
| 38 | XCTAssertThrowsError(try TextFileAccess.write(edit, to: folder.appendingPathComponent("missing/untouched.txt"))) |
| 39 | XCTAssertEqual(try TextFileAccess.read(file), other) |
| 40 | try TextFileAccess.write(edit, to: file, replacing: base, force: true) |
| 41 | XCTAssertEqual(try TextFileAccess.read(file), edit) |
| 42 | } |
| 43 | |
| 44 | func testFindModesAndRegexCaptureReplacement() throws { |
| 45 | var query = SearchQuery() |
| 46 | query.pattern = "cat" |
| 47 | query.wholeWord = true |
| 48 | XCTAssertEqual(try query.matches(in: "cat Cat category écat 猫cat cat_").count, 2) |
| 49 | query.matchCase = true |
| 50 | XCTAssertEqual(try query.matches(in: "cat Cat").count, 1) |
| 51 | XCTAssertEqual(try query.replacingAll(in: "cat Cat", with: "$1").text, "$1 Cat") |
| 52 | query.isRegex = true |
| 53 | query.pattern = #"id-(\d+)"# |
| 54 | let replaced = try query.replacingAll(in: "id-42\nid-72", with: "item[$1]") |
| 55 | XCTAssertEqual(replaced.text, "item[42]\nitem[72]") |
| 56 | XCTAssertEqual(replaced.count, 2) |
| 57 | query.pattern = "(" |
| 58 | XCTAssertThrowsError(try query.matches(in: "anything")) |
| 59 | query.pattern = "" |
| 60 | XCTAssertEqual(try query.replacingAll(in: "anything", with: "changed").text, "anything") |
| 61 | } |
| 62 | |
| 63 | func testLineIndexIncrementalChangesMatchFreshIndex() { |
| 64 | var source = "one\n💻two\nthree\nfour\n" as NSString |
| 65 | var index = LineIndex(source as String) |
| 66 | let edits: [(NSRange, String)] = [ |
| 67 | (NSRange(location: 3, length: 1), ""), |
| 68 | (NSRange(location: 0, length: 0), "\n\n"), |
| 69 | (NSRange(location: 5, length: 0), "x\n"), |
| 70 | (NSRange(location: 0, length: 4), "a"), |
| 71 | (NSRange(location: 5, length: 3), "\n"), |
| 72 | ] |
| 73 | for (range, value) in edits { |
| 74 | source = source.replacingCharacters(in: range, with: value) as NSString |
| 75 | index.update(in: source, editedRange: NSRange(location: range.location, length: value.utf16.count), |
| 76 | changeInLength: value.utf16.count - range.length) |
| 77 | XCTAssertEqual(index.starts, LineIndex(source as String).starts) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func testCleanupActsOnTouchedLinesAndPreservesFinalNewline() { |
| 82 | let text = "z \nb \na \n" |
| 83 | XCTAssertEqual(Cleanup.trim.edit(in: text, selection: NSRange(location: 4, length: 4)).replacement, "b\n") |
| 84 | XCTAssertEqual(Cleanup.sort.edit(in: "z\nb\na", selection: NSRange()).replacement, "a\nb\nz") |
| 85 | XCTAssertEqual(Cleanup.sort.edit(in: "z\nb\na\n", selection: NSRange()).replacement, "a\nb\nz\n") |
| 86 | XCTAssertEqual(Cleanup.uppercase.edit(in: "éx", selection: NSRange(location: 0, length: 1)).replacement, "É") |
| 87 | } |
| 88 | |
| 89 | func testSyntaxDetectionAndHighlightingForEveryLanguage() { |
| 90 | let fixtures: [(String, String, SyntaxLanguage, SyntaxLanguage.TokenKind)] = [ |
| 91 | ("swift", "let value = 1", .swift, .keyword), |
| 92 | ("js", "const value = 'ok'", .javascript, .quoted), |
| 93 | ("ts", "interface Example {}", .typescript, .keyword), |
| 94 | ("json", "{\"value\": 1}", .json, .property), |
| 95 | ("html", "<p class=\"x\">Text</p>", .html, .property), |
| 96 | ("css", "p { color: #ffeecc; }", .css, .property), |
| 97 | ("md", "# Title", .markdown, .heading) |
| 98 | ] |
| 99 | for (ext, sample, language, kind) in fixtures { |
| 100 | XCTAssertEqual(SyntaxLanguage.detect(URL(fileURLWithPath: "/tmp/example.\(ext)")), language) |
| 101 | XCTAssertTrue(language.tokens(in: sample).contains(where: { $0.kind == kind }), ext) |
| 102 | } |
| 103 | XCTAssertTrue(SyntaxLanguage.plain.tokens(in: "# Header").isEmpty) |
| 104 | } |
| 105 | |
| 106 | func testMarkdownRendersBlocksLinksAndLocalImagesOffline() throws { |
| 107 | let folder = try fixtureDirectory() |
| 108 | defer { try? FileManager.default.removeItem(at: folder) } |
| 109 | let png = Data(base64Encoded: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+j1i0AAAAASUVORK5CYII=")! |
| 110 | try png.write(to: folder.appendingPathComponent("dot image.png")) |
| 111 | let source = """ |
| 112 | # Title |
| 113 | |
| 114 | **Bold** and *italic* with [site](https://example.com). |
| 115 | |
| 116 | - List item |
| 117 | |
| 118 | > Quoted |
| 119 | |
| 120 | | Key | Value | |
| 121 | | --- | --- | |
| 122 | | a | b | |
| 123 | |
| 124 | ```swift |
| 125 | let x = "<tag>" |
| 126 | ``` |
| 127 | |
| 128 |  |
| 129 | |
| 130 |  |
| 131 | """ |
| 132 | let result = MarkdownPreview.html(for: source, fileURL: folder.appendingPathComponent("page.md")) |
| 133 | for fragment in ["<h1>Title</h1>", "<strong>Bold</strong>", "<em>italic</em>", "<ul>", "<blockquote>", |
| 134 | "<table>", "<pre><code>", "<tag>", "href=\"https://example.com\"", "data:image/png;base64,"] { |
| 135 | XCTAssertTrue(result.contains(fragment), "Missing \(fragment) in \(result)") |
| 136 | } |
| 137 | XCTAssertFalse(result.contains("https://example.com/dot.png")) |
| 138 | XCTAssertTrue(result.contains("img-src data:")) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | func fixtureDirectory() throws -> URL { |
| 143 | let folder = FileManager.default.temporaryDirectory.appendingPathComponent("Textley-test-" + UUID().uuidString) |
| 144 | try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: false) |
| 145 | return folder |
| 146 | } |