From 9fbdb92edb819844d2fdcf63f3b61fa657d60eb4 Mon Sep 17 00:00:00 2001 From: John Sundell Date: Fri, 8 Mar 2019 19:14:18 +0100 Subject: [PATCH] Add special case for `XCTAssert` family of functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Normally, we don’t want Splash to contain too many special cases for individual symbol names — but the `XCTAssert` family of functions are so common that it warrants it. The problem is that Splash would currently highlight those functions as types, since they are capitalized, which has now been changed to function calls instead. --- Sources/Splash/Grammar/SwiftGrammar.swift | 7 +++++++ Tests/SplashTests/Tests/FunctionCallTests.swift | 12 +++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index 314ada7..4951fff 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -256,6 +256,13 @@ private extension SwiftGrammar { return false } + // The XCTAssert family of functions is a bit of an edge case, + // since they start with capital letters. Since they are so + // commonly used, we'll add a special case for them here: + guard !segment.tokens.current.starts(with: "XCTAssert") else { + return false + } + // In a generic declaration, only highlight constraints if segment.tokens.previous.isAny(of: "<", ",") { var foundOpeningBracket = false diff --git a/Tests/SplashTests/Tests/FunctionCallTests.swift b/Tests/SplashTests/Tests/FunctionCallTests.swift index 48ef8c4..5d00135 100644 --- a/Tests/SplashTests/Tests/FunctionCallTests.swift +++ b/Tests/SplashTests/Tests/FunctionCallTests.swift @@ -130,6 +130,15 @@ final class FunctionCallTests: SyntaxHighlighterTestCase { ]) } + func testXCTAssertCalls() { + let components = highlighter.highlight("XCTAssertTrue(variable)") + + XCTAssertEqual(components, [ + .token("XCTAssertTrue", .call), + .plainText("(variable)") + ]) + } + func testAllTestsRunOnLinux() { XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests)) } @@ -146,7 +155,8 @@ extension FunctionCallTests { ("testAccessingPropertyAfterFunctionCallWithArguments", testAccessingPropertyAfterFunctionCallWithArguments), ("testCallingStaticMethodOnGenericType", testCallingStaticMethodOnGenericType), ("testPassingTypeToFunction", testPassingTypeToFunction), - ("testPassingBoolToUnnamedArgument", testPassingBoolToUnnamedArgument) + ("testPassingBoolToUnnamedArgument", testPassingBoolToUnnamedArgument), + ("testXCTAssertCalls", testXCTAssertCalls) ] } }