Gemini/GeminiRendererTests/GeminiMarkdownRendererTests...

104 lines
3.3 KiB
Swift

//
// GeminiMarkdownRendererTests.swift
// GeminiRendererTests
//
// Created by Shadowfacts on 10/1/21.
//
import XCTest
import GeminiFormat
@testable import GeminiRenderer
class GeminiMarkdownRendererTests: XCTestCase {
private var doc: Document!
override func setUp() {
doc = Document(url: URL(string: "gemini://example.com/")!)
}
func testEscapeToEntities() {
doc.lines = [.text("<b>hello</b>")]
let markdown = GeminiMarkdownRenderer().renderDocumentToMarkdown(doc)
XCTAssertEqual(markdown, "&#x3C;b&#x3E;hello&#x3C;/b&#x3E;\n\n")
}
func testParagraph() {
doc.lines = [.text("Hello, world!")]
let markdown = GeminiMarkdownRenderer().renderDocumentToMarkdown(doc)
XCTAssertEqual(markdown, "Hello, world!\n\n")
}
func testLink() {
doc.lines = [.link(URL(string: "gemini://example.com/")!, text: "text")]
let markdown = GeminiMarkdownRenderer().renderDocumentToMarkdown(doc)
XCTAssertEqual(markdown, "[text](gemini://example.com/)\n\n")
doc.lines = [.link(URL(string: "gemini://example.com/")!, text: nil)]
let noText = GeminiMarkdownRenderer().renderDocumentToMarkdown(doc)
XCTAssertEqual(noText, "[gemini://example.com/](gemini://example.com/)\n\n")
}
func testLinksAfterList() {
doc.lines = [
.unorderedListItem("a"),
.unorderedListItem("b"),
.link(URL(string: "gemini://example.com")!, text: "one"),
.link(URL(string: "gemini://example.com")!, text: "two"),
]
let markdown = GeminiMarkdownRenderer().renderDocumentToMarkdown(doc)
XCTAssertEqual(markdown, "* a\n* b\n\n[one](gemini://example.com)\n\n[two](gemini://example.com)\n\n")
}
func testPreformatting() {
doc.lines = [
.preformattedToggle(alt: nil),
.preformattedText("foo"),
.preformattedText("* bar"),
.preformattedToggle(alt: nil),
]
let markdown = GeminiMarkdownRenderer().renderDocumentToMarkdown(doc)
XCTAssertEqual(markdown, "```\nfoo\n* bar\n```\n\n")
}
func testHeading() {
doc.lines = [
.heading("One", level: .h1),
.heading("Two", level: .h2),
]
let markdown = GeminiMarkdownRenderer().renderDocumentToMarkdown(doc)
XCTAssertEqual(markdown, "# One\n\n## Two\n\n")
}
func testUnorderedList() {
doc.lines = [
.text("before"),
.unorderedListItem("a"),
.unorderedListItem("b"),
.unorderedListItem("c"),
.text("after"),
]
let markdown = GeminiMarkdownRenderer().renderDocumentToMarkdown(doc)
XCTAssertEqual(markdown, "before\n\n* a\n* b\n* c\n\nafter\n\n")
}
func testQuote() {
doc.lines = [
.quote("quoted")
]
let markdown = GeminiMarkdownRenderer().renderDocumentToMarkdown(doc)
XCTAssertEqual(markdown, "> quoted\n\n")
}
func testSkipBlankLines() {
doc.lines = [
.heading("Hello", level: .h1),
.text(""),
.text("World"),
]
let markdown = GeminiMarkdownRenderer().renderDocumentToMarkdown(doc)
XCTAssertEqual(markdown, "# Hello\n\nWorld\n\n")
}
}