Skip to content

Commit 17c6727

Browse files
committed
Use a file replacement visitor to test mutation
1 parent 2343d7a commit 17c6727

File tree

2 files changed

+17
-88
lines changed

2 files changed

+17
-88
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ let package = Package(
5959
"PackageKit",
6060
"SyntaxMutation",
6161
.product(name: "SwiftSyntax", package: "swift-syntax"),
62+
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
6263
],
6364
),
6465

Lines changed: 16 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,40 @@
11
import CoreMutation
22
@testable import PackageKit
33
import SwiftSyntax
4+
import SwiftSyntaxBuilder
45
import SyntaxMutation
56
import Testing
67

78
@Suite("SwiftSyntax Mutation")
89
struct SyntaxMutationTests {
910

10-
@Test("empty")
11-
func empty() {
12-
let file = Source.File(name: "name", path: "path", code: "")
13-
let mutants = Mutation.reverseString.mutants(for: file)
14-
#expect(mutants.isEmpty)
15-
}
16-
1711
@Test("single")
1812
func single() throws {
1913

20-
let file = Source.File(name: "name", path: "path", code: """
21-
func foo() {
22-
print("hello!")
23-
}
24-
""")
14+
final class Visitor: MutationVisitor {
2515

26-
let mutants = Mutation.reverseString.mutants(for: file)
27-
28-
try #require(mutants.count == 1)
29-
#expect(mutants[0].original == file.code)
30-
#expect(mutants[0].mutation == "Reverse String")
31-
#expect(mutants[0].location.name == file.name)
32-
#expect(mutants[0].location.path == file.path)
33-
#expect(mutants[0].location.start == Source.Position(line: 2, column: 10, offset: 22))
34-
#expect(mutants[0].location.end == Source.Position(line: 2, column: 16, offset: 28))
35-
#expect(mutants[0].replacement == """
36-
func foo() {
37-
print("!olleh")
16+
override func visit(_ node: CodeBlockItemListSyntax) -> SyntaxVisitorContinueKind {
17+
record(before: node, after: ExprSyntax("""
18+
let name = "Daniel"
19+
"""))
20+
return super.visit(node)
3821
}
39-
""")
40-
}
41-
42-
@Test("multiple")
43-
func multiple() throws {
22+
}
4423

45-
let file = Source.File(name: "name", path: "path", code: """
46-
func foo() {
47-
print("hello!")
48-
}
49-
50-
var bar: String { "baz" }
51-
""")
24+
let mutation = Mutation(name: "Replace File", visitor: Visitor.self)
25+
let file = Source.File(name: "name", path: "path", code: "")
5226

53-
let mutants = Mutation.reverseString.mutants(for: file)
27+
let mutants = mutation.mutants(for: file)
5428

55-
try #require(mutants.count == 2)
29+
try #require(mutants.count == 1)
5630
#expect(mutants[0].original == file.code)
57-
#expect(mutants[0].mutation == "Reverse String")
31+
#expect(mutants[0].mutation == mutation.name)
5832
#expect(mutants[0].location.name == file.name)
5933
#expect(mutants[0].location.path == file.path)
60-
#expect(mutants[0].location.start == Source.Position(line: 2, column: 10, offset: 22))
61-
#expect(mutants[0].location.end == Source.Position(line: 2, column: 16, offset: 28))
34+
#expect(mutants[0].location.start == Source.Position(line: 1, column: 1, offset: 0))
35+
#expect(mutants[0].location.end == Source.Position(line: 1, column: 1, offset: 0))
6236
#expect(mutants[0].replacement == """
63-
func foo() {
64-
print("!olleh")
65-
}
66-
67-
var bar: String { "baz" }
68-
""")
69-
70-
#expect(mutants[1].original == file.code)
71-
#expect(mutants[1].mutation == "Reverse String")
72-
#expect(mutants[1].location.name == file.name)
73-
#expect(mutants[1].location.path == file.path)
74-
#expect(mutants[1].location.start == Source.Position(line: 5, column: 20, offset: 53))
75-
#expect(mutants[1].location.end == Source.Position(line: 5, column: 23, offset: 56))
76-
#expect(mutants[1].replacement == """
77-
func foo() {
78-
print("hello!")
79-
}
80-
81-
var bar: String { "zab" }
37+
let name = "Daniel"
8238
""")
8339
}
8440
}
85-
86-
extension Mutation {
87-
88-
fileprivate static let reverseString = Mutation(
89-
name: "Reverse String",
90-
visitor: ReverseString.self,
91-
)
92-
}
93-
94-
final private class ReverseString: MutationVisitor {
95-
96-
override func visit(_ node: StringLiteralExprSyntax) -> SyntaxVisitorContinueKind {
97-
98-
for segment in node.segments {
99-
switch segment {
100-
case .expressionSegment:
101-
continue
102-
103-
case .stringSegment(let before):
104-
let new = String(before.content.text.reversed())
105-
let after = StringSegmentSyntax(content: .stringSegment(new))
106-
record(before: before, after: after)
107-
}
108-
}
109-
110-
return super.visit(node)
111-
}
112-
}

0 commit comments

Comments
 (0)