Skip to content

Commit

Permalink
[Test] update snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zhgchgli0718 committed Feb 25, 2023
1 parent d8f605a commit 34921a5
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 5 deletions.
3 changes: 1 addition & 2 deletions Sources/ZMarkupParser/HTML/ZHTMLParser+UIExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ public extension UILabel {
}
}
#elseif canImport(AppKit)
import AppKit

public extension NSTextView {
func setHtmlString(_ string: String, with parser: ZHTMLParser) {
self.setHtmlString(NSAttributedString(string: string), with: parser)
Expand Down Expand Up @@ -97,6 +95,7 @@ public extension NSTextField {

func setHtmlString(_ string: NSAttributedString, with parser: ZHTMLParser, completionHandler: ((NSAttributedString) -> Void)? = nil) {
parser.render(string) { attributedString in
self.attributedStringValue = attributedString
completionHandler?(attributedString)
}
}
Expand Down
57 changes: 57 additions & 0 deletions Tests/ZMarkupParserSnapshotTests/ZMarkupParserSnapshotTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,39 @@ final class ZHTMLToNSAttributedStringSnapshotTests: XCTestCase {
self.waitForExpectations(timeout: 5, handler: nil)
}

func testUILabelSetHTMLString() {
let parser = makeSUT()
let label = UILabel()
label.frame.size.width = 390
label.backgroundColor = .white
label.numberOfLines = 0
label.setHtmlString(attributedHTMLString, with: parser)
label.layoutIfNeeded()
assertSnapshot(matching: label, as: .image)
}

func testUILabelSetHTMLStringAsync() {
let parser = makeSUT()
let label = UILabel()
label.frame.size.width = 390
label.backgroundColor = .white
label.numberOfLines = 0
let expectation = self.expectation(description: "testUITextViewSetHTMLStringAsync")
label.setHtmlString(attributedHTMLString, with: parser) { _ in
label.layoutIfNeeded()
assertSnapshot(matching: label, as: .image)
expectation.fulfill()
}
self.waitForExpectations(timeout: 5, handler: nil)
}

#elseif canImport(AppKit)
func testNSTextViewSetHTMLString() {
let parser = makeSUT()

let textView = NSTextView()
textView.frame.size.width = 390
textView.frame.size.height = 500
textView.backgroundColor = .white
textView.setHtmlString(attributedHTMLString, with: parser)
textView.layout()
Expand All @@ -83,6 +110,7 @@ final class ZHTMLToNSAttributedStringSnapshotTests: XCTestCase {
let parser = makeSUT()
let textView = NSTextView()
textView.frame.size.width = 390
textView.frame.size.height = 500
textView.backgroundColor = .white
let expectation = self.expectation(description: "testUITextViewSetHTMLStringAsync")
textView.setHtmlString(attributedHTMLString, with: parser) { _ in
Expand All @@ -92,6 +120,35 @@ final class ZHTMLToNSAttributedStringSnapshotTests: XCTestCase {
}
self.waitForExpectations(timeout: 5, handler: nil)
}

func testNSTextFieldSetHTMLString() {
let parser = makeSUT()

let textField = NSTextField()
textField.frame.size.width = 390
textField.frame.size.height = 500
textField.backgroundColor = .white
textField.textColor = .black
textField.setHtmlString(attributedHTMLString, with: parser)
textField.layout()
assertSnapshot(matching: textField, as: .image)
}

func testNSTextFieldSetHTMLStringAsync() {
let parser = makeSUT()
let textField = NSTextField()
textField.frame.size.width = 390
textField.frame.size.height = 500
textField.backgroundColor = .white
textField.textColor = .black
let expectation = self.expectation(description: "testUITextViewSetHTMLStringAsync")
textField.setHtmlString(attributedHTMLString, with: parser) { _ in
textField.layout()
assertSnapshot(matching: textField, as: .image)
expectation.fulfill()
}
self.waitForExpectations(timeout: 5, handler: nil)
}
#endif
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 19 additions & 3 deletions Tests/ZMarkupParserTests/HTML/ZHTMLParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ final class ZHTMLParserTests: XCTestCase {
XCTAssertEqual(renderResult.attributes(at: 0, effectiveRange: nil)[.kern] as? Int, 999)

XCTAssertEqual(renderResult.attributedSubstring(from: NSString(string: renderResult.string).range(of: "Qoo")).attributes(at: 0, effectiveRange: nil)[.link] as? URL, URL(string: "https://zhgchg.li"))

let expectation = XCTestExpectation(description: #function)
parser.render(string) { renderResult in
XCTAssertEqual(renderResult.string, "TestQooDDD")
expectation.fulfill()
}

wait(for: [expectation], timeout: 3.0)
}

func testSelector() {
Expand All @@ -46,8 +54,16 @@ final class ZHTMLParserTests: XCTestCase {
}

func testStripper() {
let string = "Test<a href=\"https://zhgchg.li\">Qoo</a>DDD"
let stripperResult = parser.stripper(string)
XCTAssertEqual(stripperResult, "TestQooDDD")
let attributedString = NSAttributedString(string: "Test<a href=\"https://zhgchg.li\">Qoo</a>DDD")
let stripperResult = parser.stripper(attributedString)
XCTAssertEqual(stripperResult.string, "TestQooDDD")

let expectation = XCTestExpectation(description: #function)
parser.stripper(attributedString) { stripperResult in
XCTAssertEqual(stripperResult.string, "TestQooDDD")
expectation.fulfill()
}

wait(for: [expectation], timeout: 3.0)
}
}

0 comments on commit 34921a5

Please sign in to comment.