File: MarkupRewriterTests.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (103 lines) | stat: -rw-r--r-- 4,116 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 This source file is part of the Swift.org open source project

 Copyright (c) 2021 Apple Inc. and the Swift project authors
 Licensed under Apache License v2.0 with Runtime Library Exception

 See https://swift.org/LICENSE.txt for license information
 See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import XCTest
@testable import Markdown

/// A `Document` that has every kind of element in it at least once.
let everythingDocument = Document(parsing: try! String(contentsOf: Bundle.module.url(forResource: "Everything", withExtension: "md")!))


class MarkupRewriterTests: XCTestCase {
    /// Tests that a rewriter which makes no modifications results in the same document
    func testNullRewriter() {
        /// A MarkupRewriter that leaves the tree unchanged
        struct NullMarkupRewriter : MarkupRewriter {}

        var nullRewriter = NullMarkupRewriter()
        // FIXME: Workaround for rdar://problem/47686212
        let markup = everythingDocument
        let shouldBeSame = nullRewriter.visit(markup) as! Document
        try! markup.debugDescription().write(to: .init(fileURLWithPath: "/tmp/old.txt"), atomically: true, encoding: .utf8)
        try! shouldBeSame.debugDescription().write(to: .init(fileURLWithPath: "/tmp/new.txt"), atomically: true, encoding: .utf8)
        XCTAssertEqual(markup.debugDescription(), shouldBeSame.debugDescription())
    }

    /// Tests that a particular kind of element can be deleted
    func funcTestDeleteEveryOccurrence() {
        struct StrongDeleter: MarkupRewriter {
            mutating func visitStrong(_ strong: Strong) -> Markup? {
                return nil
            }
        }

        struct StrongCollector: MarkupWalker {
            var strongCount = 0
            mutating func visitStrong(_ strong: Strong) {
                strongCount += 1
                defaultVisit(strong)
            }
        }
        
        // FIXME: Workaround for rdar://problem/47686212
        let markup = everythingDocument

        var strongCollector = StrongCollector()
        strongCollector.visit(markup)
        let originalStrongCount = strongCollector.strongCount
        XCTAssertEqual(1, originalStrongCount)

        var strongDeleter = StrongDeleter()
        let markupWithoutStrongs = strongDeleter.visit(markup)!
        strongCollector.strongCount = 0
        strongCollector.visit(markupWithoutStrongs)
        let newStrongCount = strongCollector.strongCount
        XCTAssertEqual(0, newStrongCount)
    }

    /// Tests that all elements of a particular kind are visited and rewritten no matter where in the three.
    func testSpecificKindRewrittenEverywhere() {
        /// Replaces every `Text` markup element with its uppercased equivalent.
        struct UppercaseText: MarkupRewriter {
            mutating func visitText(_ text: Text) -> Markup? {
                var newText = text
                newText.string = text.string.uppercased()
                return newText
            }
        }

        /// Collects and concatenates all `Text` elements' markup into a single string for later test comparison.
        struct CollectText: MarkupWalker {
            var result = ""
            mutating func visitText(_ text: Text) {
                result += text.string
            }
        }
        
        // FIXME: Workaround for rdar://problem/47686212
        let markup = everythingDocument

        // Combine the text from the original test markup file
        var originalTextCollector = CollectText()
        originalTextCollector.visit(markup)
        let originalText = originalTextCollector.result

        // Create a version of the test markup document with all text elements uppercased.
        var uppercaser = UppercaseText()
        let uppercased = uppercaser.visit(markup)!

        // Combine the text from the uppercased markup document
        var uppercaseTextCollector = CollectText()
        uppercaseTextCollector.visit(uppercased)
        let uppercasedText = uppercaseTextCollector.result

        XCTAssertEqual(originalText.uppercased(), uppercasedText)
    }
}