| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- // Tests/SwiftProtobufPluginLibraryTests/Test_SwiftLanguage.swift - Test language utilities
- //
- // Copyright (c) 2014 - 2016 Apple Inc. and the project authors
- // Licensed under Apache License v2.0 with Runtime Library Exception
- //
- // See LICENSE.txt for license information:
- // https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
- //
- // -----------------------------------------------------------------------------
- ///
- /// Tests for tools to ensure identifiers are valid in Swift or protobuf{2,3}.
- ///
- // -----------------------------------------------------------------------------
- import SwiftProtobufPluginLibrary
- import XCTest
- final class Test_SwiftLanguage: XCTestCase {
- func testIsValidSwiftIdentifier() {
- let cases = [
- "H9000",
- "\u{1f436}\u{1f431}",
- ]
- for identifier in cases {
- XCTAssertTrue(
- isValidSwiftIdentifier(identifier, allowQuoted: false),
- "Should be valid: \(identifier)"
- )
- }
- let quotedCases = cases.map { "`\($0)`" }
- for identifier in quotedCases {
- XCTAssertFalse(
- isValidSwiftIdentifier(identifier, allowQuoted: false),
- "Should NOT be valid: \(identifier)"
- )
- }
- for identifier in cases + quotedCases {
- XCTAssertTrue(
- isValidSwiftIdentifier(identifier, allowQuoted: true),
- "Should be valid: \(identifier)"
- )
- }
- }
- func testIsNotValidSwiftIdentifier() {
- let cases = [
- "_",
- "$0",
- "$f00",
- "12Hour",
- "This is bad",
- ]
- for identifier in cases {
- XCTAssertFalse(
- isValidSwiftIdentifier(identifier, allowQuoted: false),
- "Should NOT be valid: \(identifier)"
- )
- }
- let quotedCases = cases.map { "`\($0)`" }
- for identifier in cases + quotedCases {
- XCTAssertFalse(
- isValidSwiftIdentifier(identifier, allowQuoted: false),
- "Should NOT be valid: \(identifier)"
- )
- }
- for identifier in cases + quotedCases {
- XCTAssertFalse(
- isValidSwiftIdentifier(identifier, allowQuoted: true),
- "Should NOT be valid: \(identifier)"
- )
- }
- let badQuotes = [
- "`H9000",
- "H9000`",
- "``H9000",
- "H9000``",
- "``H9000`",
- "``H9000``",
- ]
- for identifier in badQuotes {
- XCTAssertFalse(
- isValidSwiftIdentifier(identifier, allowQuoted: true),
- "Should NOT be valid: \(identifier)"
- )
- }
- }
- }
|