StringSnakeCaseTests.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // This source file is part of the Swift Argument Parser open source project
  4. //
  5. // Copyright (c) 2020 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. //
  10. //===----------------------------------------------------------------------===//
  11. import XCTest
  12. @testable import ArgumentParser
  13. final class StringSnakeCaseTests: XCTestCase {}
  14. extension StringSnakeCaseTests {
  15. func testStringSnakeCase() {
  16. let toSnakeCaseTests = [
  17. ("simpleOneTwo", "simple_one_two"),
  18. ("myURL", "my_url"),
  19. ("singleCharacterAtEndX", "single_character_at_end_x"),
  20. ("thisIsAnXMLProperty", "this_is_an_xml_property"),
  21. ("single", "single"), // no underscore
  22. ("", ""), // don't die on empty string
  23. ("a", "a"), // single character
  24. ("aA", "a_a"), // two characters
  25. ("version4Thing", "version4_thing"), // numerics
  26. // only insert underscore before first all caps
  27. ("partCAPS", "part_caps"),
  28. // switch back and forth caps.
  29. ("partCAPSLowerAGAIN", "part_caps_lower_again"),
  30. // simple lowercase + underscore + more
  31. ("manyWordsInThisThing", "many_words_in_this_thing"),
  32. ("asdfĆqer", "asdf_ćqer"),
  33. ("already_snake_case", "already_snake_case"),
  34. ("dataPoint22", "data_point22"),
  35. ("dataPoint22Word", "data_point22_word"),
  36. ("_oneTwoThree", "_one_two_three"),
  37. ("oneTwoThree_", "one_two_three_"),
  38. ("__oneTwoThree", "__one_two_three"),
  39. ("oneTwoThree__", "one_two_three__"),
  40. ("_oneTwoThree_", "_one_two_three_"),
  41. ("__oneTwoThree", "__one_two_three"),
  42. ("__oneTwoThree__", "__one_two_three__"),
  43. ("_test", "_test"),
  44. ("_test_", "_test_"),
  45. ("__test", "__test"),
  46. ("test__", "test__"),
  47. // because Itai wanted to test this
  48. ("m͉̟̹y̦̳G͍͚͎̳r̤͉̤͕ͅea̲͕t͇̥̼͖U͇̝̠R͙̻̥͓̣L̥̖͎͓̪̫ͅR̩͖̩eq͈͓u̞e̱s̙t̤̺ͅ", "m͉̟̹y̦̳_g͍͚͎̳r̤͉̤͕ͅea̲͕t͇̥̼͖_u͇̝̠r͙̻̥͓̣l̥̖͎͓̪̫ͅ_r̩͖̩eq͈͓u̞e̱s̙t̤̺ͅ"),
  49. ("🐧🐟", "🐧🐟"), // fishy emoji example?
  50. ("URLSession", "url_session"),
  51. ("RADAR", "radar"),
  52. ("Sample", "sample"),
  53. ("_Sample", "_sample"),
  54. ("_IAmAnAPPDeveloper", "_i_am_an_app_developer"),
  55. ]
  56. for test in toSnakeCaseTests {
  57. XCTAssertEqual(test.0.convertedToSnakeCase(), test.1)
  58. }
  59. }
  60. func testStringSnakeCaseWithSeparator() {
  61. let toSnakeCaseTests = [
  62. ("simpleOneTwo", "simple-one-two"),
  63. ("myURL", "my-url"),
  64. ("singleCharacterAtEndX", "single-character-at-end-x"),
  65. ("thisIsAnXMLProperty", "this-is-an-xml-property"),
  66. ("single", "single"), // no underscore
  67. ("", ""), // don't die on empty string
  68. ("a", "a"), // single character
  69. ("aA", "a-a"), // two characters
  70. ("version4Thing", "version4-thing"), // numerics
  71. // only insert underscore before first all caps
  72. ("partCAPS", "part-caps"),
  73. // switch back and forth caps.
  74. ("partCAPSLowerAGAIN", "part-caps-lower-again"),
  75. // simple lowercase + underscore + more
  76. ("manyWordsInThisThing", "many-words-in-this-thing"),
  77. ("asdfĆqer", "asdf-ćqer"),
  78. ("already_snake_case", "already_snake_case"),
  79. ("dataPoint22", "data-point22"),
  80. ("dataPoint22Word", "data-point22-word"),
  81. ("_oneTwoThree", "_one-two-three"),
  82. ("oneTwoThree_", "one-two-three_"),
  83. ("__oneTwoThree", "__one-two-three"),
  84. ("oneTwoThree__", "one-two-three__"),
  85. ("_oneTwoThree_", "_one-two-three_"),
  86. ("__oneTwoThree", "__one-two-three"),
  87. ("__oneTwoThree__", "__one-two-three__"),
  88. ("_test", "_test"),
  89. ("_test_", "_test_"),
  90. ("__test", "__test"),
  91. ("test__", "test__"),
  92. // because Itai wanted to test this
  93. ("m͉̟̹y̦̳G͍͚͎̳r̤͉̤͕ͅea̲͕t͇̥̼͖U͇̝̠R͙̻̥͓̣L̥̖͎͓̪̫ͅR̩͖̩eq͈͓u̞e̱s̙t̤̺ͅ", "m͉̟̹y̦̳-g͍͚͎̳r̤͉̤͕ͅea̲͕t͇̥̼͖-u͇̝̠r͙̻̥͓̣l̥̖͎͓̪̫ͅ-r̩͖̩eq͈͓u̞e̱s̙t̤̺ͅ"),
  94. ("🐧🐟", "🐧🐟"), // fishy emoji example?
  95. ("URLSession", "url-session"),
  96. ("RADAR", "radar"),
  97. ("Sample", "sample"),
  98. ("_Sample", "_-sample"),
  99. ("_IAmAnAPPDeveloper", "_-i-am-an-app-developer"),
  100. ]
  101. for test in toSnakeCaseTests {
  102. XCTAssertEqual(test.0.convertedToSnakeCase(separator: "-"), test.1)
  103. }
  104. }
  105. }