Test_SwiftLanguage.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Tests/SwiftProtobufPluginLibraryTests/Test_SwiftLanguage.swift - Test language utilities
  2. //
  3. // Copyright (c) 2014 - 2016 Apple Inc. and the project authors
  4. // Licensed under Apache License v2.0 with Runtime Library Exception
  5. //
  6. // See LICENSE.txt for license information:
  7. // https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
  8. //
  9. // -----------------------------------------------------------------------------
  10. ///
  11. /// Tests for tools to ensure identifiers are valid in Swift or protobuf{2,3}.
  12. ///
  13. // -----------------------------------------------------------------------------
  14. import SwiftProtobufPluginLibrary
  15. import XCTest
  16. final class Test_SwiftLanguage: XCTestCase {
  17. func testIsValidSwiftIdentifier() {
  18. let cases = [
  19. "H9000",
  20. "\u{1f436}\u{1f431}",
  21. ]
  22. for identifier in cases {
  23. XCTAssertTrue(
  24. isValidSwiftIdentifier(identifier, allowQuoted: false),
  25. "Should be valid: \(identifier)"
  26. )
  27. }
  28. let quotedCases = cases.map { "`\($0)`" }
  29. for identifier in quotedCases {
  30. XCTAssertFalse(
  31. isValidSwiftIdentifier(identifier, allowQuoted: false),
  32. "Should NOT be valid: \(identifier)"
  33. )
  34. }
  35. for identifier in cases + quotedCases {
  36. XCTAssertTrue(
  37. isValidSwiftIdentifier(identifier, allowQuoted: true),
  38. "Should be valid: \(identifier)"
  39. )
  40. }
  41. }
  42. func testIsNotValidSwiftIdentifier() {
  43. let cases = [
  44. "_",
  45. "$0",
  46. "$f00",
  47. "12Hour",
  48. "This is bad",
  49. ]
  50. for identifier in cases {
  51. XCTAssertFalse(
  52. isValidSwiftIdentifier(identifier, allowQuoted: false),
  53. "Should NOT be valid: \(identifier)"
  54. )
  55. }
  56. let quotedCases = cases.map { "`\($0)`" }
  57. for identifier in cases + quotedCases {
  58. XCTAssertFalse(
  59. isValidSwiftIdentifier(identifier, allowQuoted: false),
  60. "Should NOT be valid: \(identifier)"
  61. )
  62. }
  63. for identifier in cases + quotedCases {
  64. XCTAssertFalse(
  65. isValidSwiftIdentifier(identifier, allowQuoted: true),
  66. "Should NOT be valid: \(identifier)"
  67. )
  68. }
  69. let badQuotes = [
  70. "`H9000",
  71. "H9000`",
  72. "``H9000",
  73. "H9000``",
  74. "``H9000`",
  75. "``H9000``",
  76. ]
  77. for identifier in badQuotes {
  78. XCTAssertFalse(
  79. isValidSwiftIdentifier(identifier, allowQuoted: true),
  80. "Should NOT be valid: \(identifier)"
  81. )
  82. }
  83. }
  84. }