Files
swift-mirror/test/SwiftSyntax/VisitorTest.swift
Xi Ge d41eedcbf6 SwiftSyntax: Add a read-only syntax visitor. (#13681)
Based on the feedbacks from our early adopters, separating syntax tree analysis
with transformation is a common pattern. Thus, we introduce a read-only
syntax tree visitor to help the analysis phase. This visitor never alters the
content of a tree being visited, in contrast to SyntaxRewriter which always does.
2018-01-02 16:52:30 -08:00

38 lines
911 B
Swift

// RUN: %target-run-simple-swift
// REQUIRES: executable_test
// REQUIRES: OS=macosx
// REQUIRES: objc_interop
import StdlibUnittest
import Foundation
import SwiftSyntax
func getInput(_ file: String) -> URL {
var result = URL(fileURLWithPath: #file)
result.deleteLastPathComponent()
result.appendPathComponent("Inputs")
result.appendPathComponent(file)
return result
}
var VisitorTests = TestSuite("SyntaxVisitor")
VisitorTests.test("Basic") {
class FuncCounter: SyntaxVisitor {
var funcCount = 0
override func visit(_ node: FunctionDeclSyntax) {
funcCount += 1
super.visit(node)
}
}
expectDoesNotThrow({
let parsed = try Syntax.parse(getInput("visitor.swift"))
let counter = FuncCounter()
let hashBefore = parsed.hashValue
counter.visit(parsed)
expectEqual(counter.funcCount, 3)
expectEqual(hashBefore, parsed.hashValue)
})
}
runAllTests()