UrlTests.swift (9612B)
1 // 2 // UrlTests.swift 3 // damusTests 4 // 5 // Created by William Casarin on 2023-08-06. 6 // 7 8 import XCTest 9 @testable import damus 10 11 final class UrlTests: XCTestCase { 12 13 override func setUpWithError() throws { 14 // Put setup code here. This method is called before the invocation of each test method in the class. 15 } 16 17 override func tearDownWithError() throws { 18 // Put teardown code here. This method is called after the invocation of each test method in the class. 19 } 20 21 func testPurpleUrls() { 22 let landing_staging = DamusPurpleURL(is_staging: true, variant: .landing) 23 let welcome_staging = DamusPurpleURL(is_staging: true, variant: .welcome(checkout_id: "abc")) 24 let verify_staging = DamusPurpleURL(is_staging: true, variant: .verify_npub(checkout_id: "abc")) 25 26 let landing = DamusPurpleURL(is_staging: false, variant: .landing) 27 let welcome = DamusPurpleURL(is_staging: false, variant: .welcome(checkout_id: "abc")) 28 let verify = DamusPurpleURL(is_staging: false, variant: .verify_npub(checkout_id: "abc")) 29 30 XCTAssertEqual(landing_staging, .init(url: URL(string: landing_staging.url_string())!)!) 31 XCTAssertEqual(welcome_staging, .init(url: URL(string: welcome_staging.url_string())!)!) 32 XCTAssertEqual(verify_staging, .init(url: URL(string: verify_staging.url_string())!)!) 33 34 XCTAssertEqual(landing, .init(url: URL(string: landing.url_string())!)!) 35 XCTAssertEqual(welcome, .init(url: URL(string: welcome.url_string())!)!) 36 XCTAssertEqual(verify, .init(url: URL(string: verify.url_string())!)!) 37 } 38 39 func testParseUrlTrailingParenthesis() { 40 let testURL = URL(string: "https://en.m.wikipedia.org/wiki/Delicious_(website)") 41 XCTAssertNotNil(testURL) 42 43 let testString = "https://en.m.wikipedia.org/wiki/Delicious_(website)" 44 45 let parsed = parse_note_content(content: .content(testString, nil)).blocks 46 47 XCTAssertNotNil(parsed) 48 XCTAssertEqual(parsed[0].asURL, testURL) 49 } 50 51 func testParseUrlTrailingParenthesisAndInitialParenthesis() { 52 let testURL = URL(string: "https://en.m.wikipedia.org/wiki/Delicious_(website)") 53 XCTAssertNotNil(testURL) 54 55 let testString = "( https://en.m.wikipedia.org/wiki/Delicious_(website)" 56 let parsed = parse_note_content(content: .content(testString, nil)).blocks 57 58 XCTAssertNotNil(parsed) 59 XCTAssertEqual(parsed[1].asURL, testURL) 60 } 61 62 func testParseUrlTrailingParenthesisShouldntParse() { 63 let testURL = URL(string: "https://jb55.com") 64 XCTAssertNotNil(testURL) 65 66 let testString = "(https://jb55.com)" 67 let parsed = parse_note_content(content: .content(testString, nil)).blocks 68 69 XCTAssertNotNil(parsed) 70 XCTAssertEqual(parsed[1].asURL, testURL) 71 } 72 73 func testParseSmartParens() { 74 let testURL = URL(string: "https://nostr-con.com/simplex") 75 XCTAssertNotNil(testURL) 76 77 let testString = "(https://nostr-con.com/simplex)" 78 let parsed = parse_note_content(content: .content(testString, nil)).blocks 79 80 XCTAssertNotNil(parsed) 81 XCTAssertEqual(parsed[1].asURL, testURL) 82 } 83 84 func testLinkIsNotAHashtag() { 85 let link = "https://github.com/damus-io/damus/blob/b7513f28fa1d31c2747865067256ad1d7cf43aac/damus/Nostr/NostrEvent.swift#L560" 86 let testURL = URL(string: link) 87 XCTAssertNotNil(testURL) 88 89 let content = "my \(link) link" 90 let blocks = parse_post_blocks(content: content) 91 92 XCTAssertEqual(blocks.count, 3) 93 XCTAssertEqual(blocks[0].asText, "my ") 94 XCTAssertEqual(blocks[1].asURL, testURL) 95 XCTAssertEqual(blocks[2].asText, " link") 96 } 97 98 func testParseUrlUpper() { 99 let testURL = URL(string: "HTTPS://jb55.COM") 100 XCTAssertNotNil(testURL) 101 102 let parsed = parse_note_content(content: .content("a HTTPS://jb55.COM b", nil)).blocks 103 104 XCTAssertNotNil(parsed) 105 XCTAssertEqual(parsed.count, 3) 106 XCTAssertEqual(parsed[1].asURL, testURL) 107 } 108 109 func testUrlAnchorsAreNotHashtags() { 110 let testURL = URL(string: "https://jb55.com/index.html#buybitcoin") 111 XCTAssertNotNil(testURL) 112 113 let content = "this is my link: https://jb55.com/index.html#buybitcoin this is not a hashtag!" 114 let blocks = parse_post_blocks(content: content) 115 116 XCTAssertEqual(blocks.count, 3) 117 XCTAssertEqual(blocks[0].asText, "this is my link: ") 118 XCTAssertEqual(blocks[1].asURL, testURL) 119 XCTAssertEqual(blocks[2].asText, " this is not a hashtag!") 120 } 121 122 func testParseURL_OneURLEndPeriodSimple_RemovesPeriod(){ 123 testParseURL(inputURLString: "http://example.com.", expectedURLs: "http://example.com") 124 } 125 126 func testParseURL_OneURL_RemovesPeriod(){ 127 testParseURL(inputURLString: "http://example.com/.test", expectedURLs: "http://example.com/.test") 128 } 129 130 func testParseURL_OneURLEndPeriodAndSpaceSimple_RemovesPeriod(){ 131 testParseURL(inputURLString: "http://example.com. ", expectedURLs: "http://example.com") 132 } 133 134 func testParseURL_OneURLEndPeriodComplex_RemovesPeriod(){ 135 testParseURL(inputURLString: "http://example.com/test.", expectedURLs: "http://example.com/test") 136 } 137 138 func testParseURL_TwoURLEndPeriodSimple_RemovesPeriods(){ 139 testParseURL(inputURLString: "http://example.com. http://example.com.", expectedURLs: "http://example.com", "http://example.com") 140 } 141 142 func testParseURL_ThreeURLEndPeriodSimple_RemovesPeriods(){ 143 testParseURL(inputURLString: "http://example.com. http://example.com. http://example.com.", expectedURLs: "http://example.com", "http://example.com", "http://example.com") 144 } 145 146 func testParseURL_TwoURLEndPeriodFirstComplexSecondSimple_RemovesPeriods(){ 147 testParseURL(inputURLString: "http://example.com/test. http://example.com.", expectedURLs: "http://example.com/test", "http://example.com") 148 } 149 150 func testParseURL_TwoURLEndPeriodFirstSimpleSecondComplex_RemovesPeriods(){ 151 testParseURL(inputURLString: "http://example.com. http://example.com/test.", expectedURLs: "http://example.com", "http://example.com/test") 152 } 153 154 func testParseURL_TwoURLEndPeriodFirstComplexSecondComplex_RemovesPeriods(){ 155 testParseURL(inputURLString: "http://example.com/test. http://example.com/test.", expectedURLs: "http://example.com/test", "http://example.com/test") 156 } 157 158 func testParseURL_OneURLEndPeriodSerachQuery_RemovesPeriod(){ 159 testParseURL(inputURLString: "https://www.example.com/search?q=test+query.", expectedURLs: "https://www.example.com/search?q=test+query") 160 } 161 162 func testParseURL_OneURLEndComma_RemovesComma(){ 163 testParseURL(inputURLString: "http://example.com,", expectedURLs: "http://example.com") 164 } 165 166 func testParseURL_OneURL_RemovesComma(){ 167 testParseURL(inputURLString: "http://example.com/,test", expectedURLs: "http://example.com/,test") 168 } 169 170 func testParseURL_OneURLEndCommaAndSpaceSimple_RemovesComma(){ 171 testParseURL(inputURLString: "http://example.com, ", expectedURLs: "http://example.com") 172 } 173 174 func testParseURL_OneURLEndCommaComplex_RemovesComma(){ 175 testParseURL(inputURLString: "http://example.com/test,", expectedURLs: "http://example.com/test") 176 } 177 178 func testParseURL_TwoURLEndCommaSimple_RemovesCommas(){ 179 testParseURL(inputURLString: "http://example.com, http://example.com,", expectedURLs: "http://example.com", "http://example.com") 180 } 181 182 func testParseURL_ThreeURLEndCommaSimple_RemovesCommas(){ 183 testParseURL(inputURLString: "http://example.com, http://example.com, http://example.com,", expectedURLs: "http://example.com", "http://example.com", "http://example.com") 184 } 185 186 func testParseURL_TwoURLEndCommaFirstComplexSecondSimple_RemovesCommas(){ 187 testParseURL(inputURLString: "http://example.com/test, http://example.com,", expectedURLs: "http://example.com/test", "http://example.com") 188 } 189 190 func testParseURL_TwoURLEndCommaFirstSimpleSecondComplex_RemovesCommas(){ 191 testParseURL(inputURLString: "http://example.com, http://example.com/test,", expectedURLs: "http://example.com", "http://example.com/test") 192 } 193 194 func testParseURL_TwoURLEndCommaFirstComplexSecondComplex_RemovesCommas(){ 195 testParseURL(inputURLString: "http://example.com/test, http://example.com/test,", expectedURLs: "http://example.com/test", "http://example.com/test") 196 } 197 198 func testParseURL_OneURLEndCommaSerachQuery_RemovesComma(){ 199 testParseURL(inputURLString: "https://www.example.com/search?q=test+query,", expectedURLs: "https://www.example.com/search?q=test+query") 200 } 201 202 func testParseURL_TwoURLFirstSimpleSecondSimpleNoSpace_RemovesCommas(){ 203 testParseURL(inputURLString: "http://example.com,http://example.com,", 204 expectedURLs: "http://example.com", "http://example.com") 205 } 206 } 207 208 func testParseURL(inputURLString: String, expectedURLs: String...) { 209 let parsedURL: [Block] = parse_note_content(content: .content(inputURLString, nil)).blocks.filter { 210 $0.isURL 211 } 212 213 if(expectedURLs.count != parsedURL.count) { 214 XCTFail() 215 } 216 217 for i in 0..<parsedURL.count { 218 guard let expectedURL = URL(string: expectedURLs[i]) else { 219 XCTFail() 220 return 221 } 222 223 XCTAssertEqual(parsedURL[i].asURL, expectedURL) 224 } 225 }