|
| 1 | +import SourceKittenFramework |
| 2 | +import SwiftIDEUtils |
| 3 | +@testable import SwiftLintCore |
| 4 | +import SwiftSyntax |
| 5 | +import TestHelpers |
| 6 | +import XCTest |
| 7 | + |
| 8 | +final class SwiftSyntaxKindBridgeTests: SwiftLintTestCase { |
| 9 | + func testBasicKeywordMapping() { |
| 10 | + // Test basic keyword mappings |
| 11 | + XCTAssertEqual(SwiftSyntaxKindBridge.mapClassification(.keyword), .keyword) |
| 12 | + } |
| 13 | + |
| 14 | + func testIdentifierMapping() { |
| 15 | + // Test identifier mappings |
| 16 | + XCTAssertEqual(SwiftSyntaxKindBridge.mapClassification(.identifier), .identifier) |
| 17 | + XCTAssertEqual(SwiftSyntaxKindBridge.mapClassification(.dollarIdentifier), .identifier) |
| 18 | + } |
| 19 | + |
| 20 | + func testCommentMapping() { |
| 21 | + // Test comment mappings |
| 22 | + XCTAssertEqual(SwiftSyntaxKindBridge.mapClassification(.lineComment), .comment) |
| 23 | + XCTAssertEqual(SwiftSyntaxKindBridge.mapClassification(.blockComment), .comment) |
| 24 | + XCTAssertEqual(SwiftSyntaxKindBridge.mapClassification(.docLineComment), .docComment) |
| 25 | + XCTAssertEqual(SwiftSyntaxKindBridge.mapClassification(.docBlockComment), .docComment) |
| 26 | + } |
| 27 | + |
| 28 | + func testLiteralMapping() { |
| 29 | + // Test literal mappings |
| 30 | + XCTAssertEqual(SwiftSyntaxKindBridge.mapClassification(.stringLiteral), .string) |
| 31 | + XCTAssertEqual(SwiftSyntaxKindBridge.mapClassification(.integerLiteral), .number) |
| 32 | + XCTAssertEqual(SwiftSyntaxKindBridge.mapClassification(.floatLiteral), .number) |
| 33 | + } |
| 34 | + |
| 35 | + func testOperatorAndTypeMapping() { |
| 36 | + // Test operator and type mappings |
| 37 | + XCTAssertEqual(SwiftSyntaxKindBridge.mapClassification(.operator), .operator) |
| 38 | + XCTAssertEqual(SwiftSyntaxKindBridge.mapClassification(.type), .typeidentifier) |
| 39 | + } |
| 40 | + |
| 41 | + func testSpecialCaseMapping() { |
| 42 | + // Test special case mappings |
| 43 | + XCTAssertEqual(SwiftSyntaxKindBridge.mapClassification(.attribute), .attributeID) |
| 44 | + XCTAssertEqual(SwiftSyntaxKindBridge.mapClassification(.editorPlaceholder), .placeholder) |
| 45 | + XCTAssertEqual(SwiftSyntaxKindBridge.mapClassification(.ifConfigDirective), .poundDirectiveKeyword) |
| 46 | + XCTAssertEqual(SwiftSyntaxKindBridge.mapClassification(.argumentLabel), .argument) |
| 47 | + } |
| 48 | + |
| 49 | + func testUnmappedClassifications() { |
| 50 | + // Test classifications that have no mapping |
| 51 | + XCTAssertNil(SwiftSyntaxKindBridge.mapClassification(.none)) |
| 52 | + XCTAssertNil(SwiftSyntaxKindBridge.mapClassification(.regexLiteral)) |
| 53 | + } |
| 54 | + |
| 55 | + func testSourceKittenSyntaxKindsGeneration() { |
| 56 | + // Test that we can generate SourceKitten-compatible tokens from a simple Swift file |
| 57 | + let contents = """ |
| 58 | + // This is a comment |
| 59 | + let x = 42 |
| 60 | + """ |
| 61 | + let file = SwiftLintFile(contents: contents) |
| 62 | + |
| 63 | + // Get the tokens from the bridge |
| 64 | + let tokens = SwiftSyntaxKindBridge.sourceKittenSyntaxKinds(for: file) |
| 65 | + |
| 66 | + // Verify we got some tokens |
| 67 | + XCTAssertFalse(tokens.isEmpty) |
| 68 | + |
| 69 | + // Check that we have expected token types |
| 70 | + let tokenTypes = Set(tokens.map { $0.value.type }) |
| 71 | + XCTAssertTrue(tokenTypes.contains(SyntaxKind.comment.rawValue)) |
| 72 | + XCTAssertTrue(tokenTypes.contains(SyntaxKind.keyword.rawValue)) |
| 73 | + XCTAssertTrue(tokenTypes.contains(SyntaxKind.identifier.rawValue)) |
| 74 | + XCTAssertTrue(tokenTypes.contains(SyntaxKind.number.rawValue)) |
| 75 | + } |
| 76 | + |
| 77 | + func testTokenOffsetAndLength() { |
| 78 | + // Test that token offsets and lengths are correct |
| 79 | + let contents = "let x = 42" |
| 80 | + let file = SwiftLintFile(contents: contents) |
| 81 | + |
| 82 | + let tokens = SwiftSyntaxKindBridge.sourceKittenSyntaxKinds(for: file) |
| 83 | + |
| 84 | + // Find the "let" keyword token |
| 85 | + let letToken = tokens.first { token in |
| 86 | + if token.value.type == SyntaxKind.keyword.rawValue { |
| 87 | + let start = token.value.offset.value |
| 88 | + let end = token.value.offset.value + token.value.length.value |
| 89 | + let startIndex = contents.index(contents.startIndex, offsetBy: start) |
| 90 | + let endIndex = contents.index(contents.startIndex, offsetBy: end) |
| 91 | + let substring = String(contents[startIndex..<endIndex]) |
| 92 | + return substring == "let" |
| 93 | + } |
| 94 | + return false |
| 95 | + } |
| 96 | + XCTAssertNotNil(letToken) |
| 97 | + XCTAssertEqual(letToken?.value.offset.value, 0) |
| 98 | + XCTAssertEqual(letToken?.value.length.value, 3) |
| 99 | + |
| 100 | + // Find the number token |
| 101 | + let numberToken = tokens.first { $0.value.type == SyntaxKind.number.rawValue } |
| 102 | + XCTAssertNotNil(numberToken) |
| 103 | + // "42" starts at offset 8 and has length 2 |
| 104 | + XCTAssertEqual(numberToken?.value.offset.value, 8) |
| 105 | + XCTAssertEqual(numberToken?.value.length.value, 2) |
| 106 | + } |
| 107 | + |
| 108 | + func testComplexCodeStructure() { |
| 109 | + // Test with more complex Swift code |
| 110 | + let contents = """ |
| 111 | + import Foundation |
| 112 | +
|
| 113 | + /// A sample class |
| 114 | + @objc |
| 115 | + class MyClass { |
| 116 | + // Properties |
| 117 | + var name: String = "test" |
| 118 | + let id = UUID() |
| 119 | +
|
| 120 | + func doSomething() { |
| 121 | + print("Hello, \\(name)!") |
| 122 | + } |
| 123 | + } |
| 124 | + """ |
| 125 | + let file = SwiftLintFile(contents: contents) |
| 126 | + |
| 127 | + let tokens = SwiftSyntaxKindBridge.sourceKittenSyntaxKinds(for: file) |
| 128 | + |
| 129 | + // Verify we have various token types |
| 130 | + let tokenTypes = Set(tokens.map { $0.value.type }) |
| 131 | + XCTAssertTrue(tokenTypes.contains(SyntaxKind.keyword.rawValue)) // import, class, var, let, func |
| 132 | + XCTAssertTrue(tokenTypes.contains(SyntaxKind.identifier.rawValue)) // Foundation, MyClass, name, etc. |
| 133 | + XCTAssertTrue(tokenTypes.contains(SyntaxKind.docComment.rawValue)) // /// A sample class |
| 134 | + XCTAssertTrue(tokenTypes.contains(SyntaxKind.comment.rawValue)) // // Properties |
| 135 | + XCTAssertTrue(tokenTypes.contains(SyntaxKind.attributeID.rawValue)) // @objc |
| 136 | + XCTAssertTrue(tokenTypes.contains(SyntaxKind.typeidentifier.rawValue)) // String, UUID |
| 137 | + XCTAssertTrue(tokenTypes.contains(SyntaxKind.string.rawValue)) // "test", "Hello, \\(name)!" |
| 138 | + } |
| 139 | + |
| 140 | + func testNoSourceKitCallsAreMade() { |
| 141 | + // This test verifies that the bridge doesn't make any SourceKit calls |
| 142 | + // If it did, the validation system would fatal error in test mode |
| 143 | + |
| 144 | + let contents = """ |
| 145 | + struct Test { |
| 146 | + let value = 123 |
| 147 | + func method() -> Int { return value } |
| 148 | + } |
| 149 | + """ |
| 150 | + let file = SwiftLintFile(contents: contents) |
| 151 | + |
| 152 | + // This should succeed without any fatal errors from the validation system |
| 153 | + let tokens = SwiftSyntaxKindBridge.sourceKittenSyntaxKinds(for: file) |
| 154 | + XCTAssertFalse(tokens.isEmpty) |
| 155 | + } |
| 156 | + |
| 157 | + func testEmptyFileHandling() { |
| 158 | + // Test that empty files are handled gracefully |
| 159 | + let file = SwiftLintFile(contents: "") |
| 160 | + let tokens = SwiftSyntaxKindBridge.sourceKittenSyntaxKinds(for: file) |
| 161 | + XCTAssertTrue(tokens.isEmpty) |
| 162 | + } |
| 163 | + |
| 164 | + func testWhitespaceOnlyFile() { |
| 165 | + // Test files with only whitespace |
| 166 | + let file = SwiftLintFile(contents: " \n\n \t \n") |
| 167 | + let tokens = SwiftSyntaxKindBridge.sourceKittenSyntaxKinds(for: file) |
| 168 | + // Whitespace is not classified, so we should get no tokens |
| 169 | + XCTAssertTrue(tokens.isEmpty) |
| 170 | + } |
| 171 | +} |
0 commit comments