From 806149ec1b53713a591eaee617d672c22c9f8da9 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 1 Oct 2021 19:27:45 -0400 Subject: [PATCH] GeminiRenderer: Add HTML renderer tests --- .../GeminiHTMLRendererTests.swift | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 GeminiRendererTests/GeminiHTMLRendererTests.swift diff --git a/GeminiRendererTests/GeminiHTMLRendererTests.swift b/GeminiRendererTests/GeminiHTMLRendererTests.swift new file mode 100644 index 0000000..42be4e7 --- /dev/null +++ b/GeminiRendererTests/GeminiHTMLRendererTests.swift @@ -0,0 +1,101 @@ +// +// GeminiHTMLRendererTests.swift +// GeminiRendererTests +// +// Created by Shadowfacts on 10/1/21. +// + +import XCTest +import GeminiFormat +@testable import GeminiRenderer + +class GeminiHTMLRendererTests: XCTestCase { + + private var doc: Document! + + override func setUp() { + doc = Document(url: URL(string: "gemini://example.com/")!) + } + + func testEscapeToEntities() { + doc.lines = [.text("hello")] + let html = GeminiHTMLRenderer().renderDocumentToHTML(doc) + XCTAssertEqual(html, "

<b>hello</b>

") + } + + func testParagraph() { + doc.lines = [.text("Hello, world!")] + let html = GeminiHTMLRenderer().renderDocumentToHTML(doc) + XCTAssertEqual(html, "

Hello, world!

") + } + + func testLink() { + doc.lines = [.link(URL(string: "gemini://example.com/")!, text: "text")] + let html = GeminiHTMLRenderer().renderDocumentToHTML(doc) + XCTAssertEqual(html, "

text

") + + doc.lines = [.link(URL(string: "gemini://example.com/")!, text: nil)] + let noText = GeminiHTMLRenderer().renderDocumentToHTML(doc) + XCTAssertEqual(noText, "

gemini://example.com/

") + } + + func testDisableLinkClass() { + doc.lines = [.link(URL(string: "gemini://example.com/")!, text: "test")] + let renderer = GeminiHTMLRenderer() + renderer.addLinkClass = false + let html = renderer.renderDocumentToHTML(doc) + XCTAssertEqual(html, "

test

") + } + + func testPreformatting() { + doc.lines = [ + .preformattedToggle(alt: nil), + .preformattedText("foo"), + .preformattedText("* bar"), + .preformattedToggle(alt: nil), + ] + let html = GeminiHTMLRenderer().renderDocumentToHTML(doc) + XCTAssertEqual(html, "
foo\n* bar\n
") + } + + func testHeading() { + doc.lines = [ + .heading("One", level: .h1), + .heading("Two", level: .h2), + ] + let html = GeminiHTMLRenderer().renderDocumentToHTML(doc) + XCTAssertEqual(html, "

One

Two

") + } + + func testDisableHeadingIDs() { + doc.lines = [ + .heading("One", level: .h1), + .heading("Two", level: .h2), + ] + let renderer = GeminiHTMLRenderer() + renderer.addHeadingLineIDs = false + let html = renderer.renderDocumentToHTML(doc) + XCTAssertEqual(html, "

One

Two

") + } + + func testUnorderedList() { + doc.lines = [ + .text("before"), + .unorderedListItem("a"), + .unorderedListItem("b"), + .unorderedListItem("c"), + .text("after"), + ] + let html = GeminiHTMLRenderer().renderDocumentToHTML(doc) + XCTAssertEqual(html, "

before

after

") + } + + func testQuote() { + doc.lines = [ + .quote("quoted") + ] + let html = GeminiHTMLRenderer().renderDocumentToHTML(doc) + XCTAssertEqual(html, "
quoted
") + } + +}