|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SwiftRefactor |
| 14 | +import SwiftSyntaxBuilder |
| 15 | +@_spi(RawSyntax) import SwiftSyntax |
| 16 | +@_spi(RawSyntax) import SwiftParser |
| 17 | + |
| 18 | +import XCTest |
| 19 | +import _SwiftSyntaxTestSupport |
| 20 | + |
| 21 | +final class FormatRawStringLiteralTest: XCTestCase { |
| 22 | + func testDelimiterPlacement() throws { |
| 23 | + let tests = [ |
| 24 | + (#line, literal: #" "Hello World" "#, expectation: #" "Hello World" "#), |
| 25 | + (#line, literal: ##" #"Hello World" "##, expectation: #" "Hello World" "#), |
| 26 | + (#line, literal: ##" #"Hello World"# "##, expectation: #" "Hello World" "#), |
| 27 | + (#line, literal: #####" "####" "#####, expectation: #####" "####" "#####), |
| 28 | + (#line, literal: #####" #"####"# "#####, expectation: ######" #####"####"##### "######), |
| 29 | + (#line, literal: #####" #"\####(hello)"# "#####, expectation: ######" #####"\####(hello)"##### "######), |
| 30 | + (#line, literal: #######" #"###### \####(hello) ##"# "#######, expectation: ########" #######"###### \####(hello) ##"####### "########), |
| 31 | + (#line, literal: ########" #######"hello \(world) "####### "########, expectation: #" "hello \(world) " "#), |
| 32 | + ] |
| 33 | + |
| 34 | + for (line, literal, expectation) in tests { |
| 35 | + let literal = try XCTUnwrap(StringLiteralExpr.parseWithoutDiagnostics(from: literal)) |
| 36 | + let expectation = try XCTUnwrap(StringLiteralExpr.parseWithoutDiagnostics(from: expectation)) |
| 37 | + let refactored = try XCTUnwrap(FormatRawStringLiteral.refactor(syntax: literal)) |
| 38 | + AssertStringsEqualWithDiff(refactored.description, expectation.description, line: UInt(line)) |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +extension StringLiteralExpr { |
| 44 | + static func parseWithoutDiagnostics(from string: String) -> StringLiteralExpr? { |
| 45 | + var source = string |
| 46 | + source.makeContiguousUTF8() |
| 47 | + return source.withUTF8 { buffer in |
| 48 | + var parser = Parser(buffer) |
| 49 | + return parser.parseStringLiteral().syntax.as(StringLiteralExpr.self) |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
0 commit comments