89 lines · 5.0 KB · SwiftRaw
// What the editor lays over the core's runs (`WritingOverlay`): the chevrons// and hidden sections of the core's `folds`, Find's matches, and the link the// caret stands in, read from the core's `links` and link runs; the shapes the// core answers (folds, links, a picture's width) decoded; and Notes' own// icons, every one drawn on the 24-unit grid.@testable import CottonWritingimport CoreGraphicsimport XCTestfinal class WritingOverlayTests: XCTestCase { private func styled(_ json: String) throws -> WritingStyled { try JSONDecoder().decode(WritingStyled.self, from: Data(json.utf8)) } func testTheCoreAnswersFoldsLinksAndAPicturesWidth() throws { let answer = try styled(""" { "blocks": [ { "start": 0, "end": 20, "kind": "picture", "content": 0, "depth": 0, "src": "photo:abc", "width": "small" } ], "spans": [], "tables": [], "folds": [ { "kind": "heading", "line": 0, "start": 3, "end": 40, "level": 1 } ], "links": [ { "start": 50, "end": 61, "target": "Seeds", "heading": null, "note": "n2" } ] } """) XCTAssertEqual(answer.folds, [WritingFold(kind: "heading", line: 0, start: 3, end: 40, level: 1)]) XCTAssertEqual(answer.links.first?.note, "n2") XCTAssertEqual(answer.blocks.first?.pictureShare, 0.4) XCTAssertEqual(WritingBlock(start: 0, end: 1, kind: "picture").pictureShare, 1, "no width is full width") XCTAssertTrue(try styled("{}").folds.isEmpty, "an older core's answer, with no folds, still draws") } func testOpenFoldsWearAChevronAndShutOnesHideTheirSection() { let folds = [WritingFold(line: 0, start: 3, end: 9), WritingFold(line: 10, start: 13, end: 15)] let open = WritingOverlay.spans(length: 15, folds: folds, folded: [], found: [], current: 0) XCTAssertEqual(open.filter { $0.style == "fold_mark" }.map(\.glyph), ["open", "open"]) XCTAssertFalse(open.contains { $0.style == "folded" }) let shut = WritingOverlay.spans(length: 15, folds: folds, folded: [0], found: [], current: 0) XCTAssertEqual(shut.first { $0.style == "folded" }.map { NSRange(location: $0.start, length: $0.end - $0.start) }, NSRange(location: 3, length: 6)) XCTAssertEqual(shut.first { $0.style == "fold_mark" && $0.start == 0 }?.glyph, "closed") } func testFindMarksEveryMatchAndTheOneStanding() { let found = [NSRange(location: 0, length: 3), NSRange(location: 8, length: 3)] let spans = WritingOverlay.spans(length: 20, folds: [], folded: [], found: found, current: 1) XCTAssertEqual(spans.map(\.style), ["found", "found_now"]) } func testTheLinkTheCaretStandsIn() { let styled = WritingStyled(spans: [WritingSpan(start: 30, end: 35, style: "link", url: "https://cottondb.com")], links: [WritingNoteLink(start: 6, end: 22, target: "Garden plans/Seeds")]) XCTAssertEqual(WritingOverlay.linkAt(styled, at: 10), "note:Garden plans/Seeds") XCTAssertEqual(WritingOverlay.linkAt(styled, at: 32), "https://cottondb.com") XCTAssertNil(WritingOverlay.linkAt(styled, at: 2)) XCTAssertNil(WritingOverlay.linkAt(nil, at: 10)) } func testTheOverlayStaysQuickOnA50000CharacterNote() { // A 50,000-character note's folds and Find, as the core answers them, laid again after every answer while typing. let folds = (0..<1000).map { WritingFold(line: $0 * 50, start: $0 * 50 + 10, end: $0 * 50 + 49) } let found = (0..<1000).map { NSRange(location: $0 * 50 + 20, length: 5) } let started = Date() let spans = WritingOverlay.spans(length: 50_000, folds: folds, folded: Set(folds.prefix(500).map(\.line)), found: found, current: 3) let took = Date().timeIntervalSince(started) XCTAssertEqual(spans.count, 2500) XCTAssertLessThan(took, 0.25, "the overlay took \(took)s") } func testEveryIconIsDrawnOnTheGrid() { XCTAssertGreaterThan(WritingMarks.set.count, 90) for name in WritingMarks.set.keys { guard let parts = WritingMarks.paths(name) else { return XCTFail("\(name) draws nothing") } for part in parts { let box = part.path.boundingBoxOfPath XCTAssertFalse(box.isNull || box.isEmpty && box.width == 0 && box.height == 0, "\(name) has an empty part") XCTAssertTrue(CGRect(x: -0.5, y: -0.5, width: 25, height: 25).contains(box), "\(name) leaves the grid: \(box)") } } XCTAssertTrue(WritingMarks.draws("image"), "an older name draws the set's picture") XCTAssertFalse(WritingMarks.draws("no-such-icon")) } func testAnArcIsACurveNotALine() { // A circle of radius 4 about (12, 12). let path = WritingMarkPath.make("M8 12 A4 4 0 1 0 16 12 A4 4 0 1 0 8 12 Z") let box = path.boundingBoxOfPath XCTAssertEqual(box.minX, 8, accuracy: 0.05) XCTAssertEqual(box.maxX, 16, accuracy: 0.05) XCTAssertEqual(box.minY, 8, accuracy: 0.1) XCTAssertEqual(box.maxY, 16, accuracy: 0.1) }}