ShaarliOSTests.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // ShaarliOSTests.swift
  3. // ShaarliOSTests
  4. //
  5. // Created by Marcus Rohrmoser on 09.06.19.
  6. // Copyright © 2019-2022 Marcus Rohrmoser mobile Software http://mro.name/me. All rights reserved.
  7. //
  8. // This program is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. import XCTest
  22. @testable import ShaarliOS
  23. func dataWithContentsOfFixture(me mu:AnyObject, fileName: String, extensio:String) -> Data {
  24. precondition(true, "Never happens")
  25. let ty:AnyObject.Type = type(of: mu)
  26. let bu = Bundle(for: ty)
  27. let sub = "testdata/\(String(describing: ty))"
  28. guard let ur = bu.url(forResource: fileName, withExtension: extensio, subdirectory:sub)
  29. else { return Data() }
  30. do {
  31. return try Data(contentsOf: ur)
  32. } catch {
  33. return Data()
  34. }
  35. }
  36. class ShaarliOSTests: XCTestCase {
  37. private let df1123 = DateFormatter()
  38. internal func RFC1123(_ date:Date) -> String {
  39. return df1123.string(from: date)
  40. }
  41. override func setUp() {
  42. // https://mro.name/blog/2009/08/nsdateformatter-http-header/
  43. df1123.locale = Locale(identifier: "en_US")
  44. df1123.timeZone = TimeZone(abbreviation: "GMT")
  45. df1123.dateFormat = "EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'"
  46. }
  47. override func tearDown() {
  48. // Put teardown code here. This method is called after the invocation of each test method in the class.
  49. }
  50. func testRfc1123() {
  51. XCTAssertEqual("Sat, 30 Dec 0001 00:00:00 GMT", RFC1123(Date.distantPast))
  52. }
  53. func testUrl () {
  54. let url = URL(string: "https://uid:pwd@example.com/foo")!
  55. XCTAssertEqual("https://uid:pwd@example.com/foo", url.description)
  56. XCTAssertEqual("https", url.scheme)
  57. XCTAssertEqual("example.com", url.host)
  58. XCTAssertEqual("uid", url.user)
  59. XCTAssertEqual("pwd", url.password)
  60. XCTAssertEqual("/foo", url.path)
  61. }
  62. func testEnumerateSubstrings() {
  63. var a:[String] = []
  64. let str = "#You shouldn’t do that"
  65. // https://medium.com/@sorenlind/three-ways-to-enumerate-the-words-in-a-string-using-swift-7da5504f0062
  66. str.enumerateSubstrings(in: str.startIndex..<str.endIndex, options:.byWords) { sub, _, _, _ in
  67. a.append(sub ?? "")
  68. }
  69. XCTAssertEqual(["You", "shouldn’t", "do", "that"], a)
  70. }
  71. func testScanUpToCharacters() {
  72. let sc = Scanner(string:"#You shouldn’t do that")
  73. let set = CharacterSet.whitespacesAndNewlines
  74. var value: NSString?
  75. guard sc.scanUpToCharacters(from: set, into: &value) else { return }
  76. let dst = value as String?
  77. XCTAssertEqual("#You", dst)
  78. }
  79. func testTagsFromString() {
  80. // -(NSString *)stringByStrippingTags:(NSMutableArray *)tags
  81. // https://code.mro.name/mro/ShaarliOS/src/e9009ef466582e806b97d723e5acea885eaa4c7d/ios/ShaarliOS/ShaarliM.m#L33
  82. // tagsFromString
  83. // https://code.mro.name/mro/ShaarliGo/src/c65e142dda32bac7cec02deedc345b8f32a2cf8e/atom.go#L495
  84. // https://code.mro.name/mro/ShaarliGo/src/c65e142dda32bac7cec02deedc345b8f32a2cf8e/atom_test.go#L44
  85. // api0LinkForMap
  86. // https://code.mro.name/mro/ShaarliGo/src/c65e142dda32bac7cec02deedc345b8f32a2cf8e/api0.go#L432
  87. XCTAssertEqual("ha", isTag(word:"#ha"))
  88. XCTAssertEqual("🐳", isTag(word:"🐳"))
  89. XCTAssertEqual("", isTag(word:"foo#nein"))
  90. XCTAssertEqual("><(((°>", isTag(word:"#><(((°>"))
  91. XCTAssertEqual("F#", isTag(word:"#F#"))
  92. XCTAssertEqual("#F#", isTag(word:"##F#"))
  93. XCTAssertEqual(["ha"], tagsFrom(string:"#ha, 1.2 foo#nein"), "aha")
  94. XCTAssertEqual(["🐳"], tagsFrom(string:"🐳, foo#nein"), "aha")
  95. XCTAssertEqual(["$", "§", "†"], tagsFrom(string:"#§, #$ #† foo#nein"), "aha")
  96. XCTAssertEqual(["🐳"], tagsFrom(string:"#🐳, foo#nein #"), "aha")
  97. XCTAssertEqual(["ipsum", "opensource", "🐳"], tagsFrom(string:"""
  98. Lorem #ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
  99. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
  100. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. #opensource #🐳
  101. """), "ja, genau")
  102. XCTAssertEqual(["⭐️"], tagsFrom(string:"a single ⭐️ is also a tag"), "aha")
  103. }
  104. func testFold() {
  105. XCTAssertEqual("hallo wyrld!", fold(lbl:" Hälló wÿrld! "))
  106. XCTAssertEqual("demaiziere", fold(lbl:" DeMaizière \n"))
  107. XCTAssertEqual("cegłowski", fold(lbl:"\tCegłowski"))
  108. }
  109. func testTagsNormalise() {
  110. let n0 = tagsNormalise(description:"#A", extended:"#B #C", tags:["a", "c", "D"], known:["c"])
  111. XCTAssertEqual("#A", n0.description)
  112. XCTAssertEqual("#B #C\n#D", n0.extended)
  113. XCTAssertEqual(["B", "D", "a", "c"], n0.tags.sorted())
  114. let n1 = tagsNormalise(description:"", extended:"📱 #Shaarli💫", tags:[], known:[])
  115. XCTAssertEqual("", n1.description)
  116. XCTAssertEqual("📱 #Shaarli💫", n1.extended)
  117. XCTAssertEqual(["Shaarli💫", "📱"], n1.tags)
  118. let n2 = tagsNormalise(description:"", extended:"", tags:n1.tags, known:[])
  119. XCTAssertEqual("", n2.description)
  120. XCTAssertEqual("#Shaarli💫 📱", n2.extended)
  121. XCTAssertEqual(["Shaarli💫", "📱"], n2.tags)
  122. }
  123. func testPerformanceExample() {
  124. // This is an example of a performance test case.
  125. self.measure {
  126. // Put the code you want to measure the time of here.
  127. }
  128. }
  129. }