Skip to content

Commit

Permalink
Merge pull request #9 from swift-tweets/dev-0.1.1
Browse files Browse the repository at this point in the history
Develop 0.1.1
  • Loading branch information
koher authored Nov 3, 2017
2 parents 0420a91 + 19b69ac commit f29d392
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sources/CodeRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class CodeRenderer: NSObject {
zelf = self

DispatchQueue.main.async {
self.webView = WebView(frame: NSRect(x: 0, y: 0, width: 414, height: CodeRenderer.height))
self.webView = WebView(frame: NSRect(x: 0, y: 0, width: 640, height: CodeRenderer.height))
self.webView.frameLoadDelegate = self
self.webView.customUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1"
self.webView.mainFrameURL = url
Expand Down
4 changes: 2 additions & 2 deletions Sources/Tweet.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

public struct Tweet {
internal static let urlPattern = try! NSRegularExpression(pattern: "(^|\\s)(http(s)?://[a-zA-Z0-9~!@#$%&*-_=+\\[\\]|:;',./?]*)($|\\s)")
internal static let urlPattern = try! NSRegularExpression(pattern: "(^|\\s)((http(s)?://)?[a-zA-Z0-9\\-]+(\\.[a-zA-Z0-9\\-]+)+(/[a-zA-Z0-9~!@#$%&*\\-_=+\\[\\]|:;',./?]*)?)($|\\s)")
internal static let urlLength = 23
internal static let maxLength = 140

Expand All @@ -19,7 +19,7 @@ public struct Tweet {

public var length: Int {
let replaced = NSMutableString(string: body)
let numberOfUrls = Tweet.urlPattern.replaceMatches(in: replaced, options: [], range: NSMakeRange(0, replaced.length), withTemplate: "$1$4")
let numberOfUrls = Tweet.urlPattern.replaceMatches(in: replaced, options: [], range: NSMakeRange(0, replaced.length), withTemplate: "$1$7")

let normalized = replaced.precomposedStringWithCanonicalMapping as NSString

Expand Down
28 changes: 28 additions & 0 deletions Tests/TweetupKitTests/TweetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ class TweetTests: XCTestCase {
} catch {
XCTFail()
}

do { // Swift.org as a URL
_ = try Tweet(body: "まず Swift.org の Compiler and Standard Library を見てみました。\n> Semantic analysis includes type inference ...\nhttps://swift.org/compiler-stdlib/#compiler-architecture (6/10) #swtws")
XCTFail()
} catch let TweetInitializationError.tooLong(body, attachment, length) {
XCTAssertEqual(body, "まず Swift.org の Compiler and Standard Library を見てみました。\n> Semantic analysis includes type inference ...\nhttps://swift.org/compiler-stdlib/#compiler-architecture (6/10) #swtws")
XCTAssertNil(attachment)
XCTAssertEqual(length, 153)
} catch {
XCTFail()
}
}

func testDescription() {
Expand Down Expand Up @@ -150,5 +161,22 @@ class TweetTests: XCTestCase {
XCTAssertEqual((string as NSString).substring(with: result.rangeAt(2)), "https://swift-tweets.github.io/?foo=bar&baz=qux#tweeters")
}
}

do {
let string = "まず Swift.org の Compiler and Standard Library を見てみました。\n> Semantic analysis includes type inference ...\nhttps://swift.org/compiler-stdlib/#compiler-architecture (6/10) #swtws"

let results = Tweet.urlPattern.matches(in: string)
XCTAssertEqual(results.count, 2)

do {
let result = results[0]
XCTAssertEqual((string as NSString).substring(with: result.rangeAt(2)), "Swift.org")
}

do {
let result = results[1]
XCTAssertEqual((string as NSString).substring(with: result.rangeAt(2)), "https://swift.org/compiler-stdlib/#compiler-architecture")
}
}
}
}

0 comments on commit f29d392

Please sign in to comment.