Gemini/GeminiFormatTests/TableOfContentsTests.swift

149 lines
5.7 KiB
Swift
Raw Normal View History

2020-12-20 18:45:22 +00:00
//
// TableOfContentsTests.swift
// GeminiFormatTests
//
// Created by Shadowfacts on 12/19/20.
//
import XCTest
@testable import GeminiFormat
class TableOfContentsTests: XCTestCase {
func testOneHeading() {
let one = Document(url: URL(string: "gemini://example.com/")!, lines: [
.heading("Heading", level: .h1)
])
XCTAssertEqual(TableOfContents(document: one).entries, [
.init(line: .heading("Heading", level: .h1), lineIndex: 0)
])
let two = Document(url: URL(string: "gemini://example.com/")!, lines: [
.heading("Heading", level: .h2)
])
XCTAssertEqual(TableOfContents(document: two).entries, [
.init(line: .heading("Heading", level: .h2), lineIndex: 0)
])
let three = Document(url: URL(string: "gemini://example.com/")!, lines: [
.heading("Heading", level: .h3)
])
XCTAssertEqual(TableOfContents(document: three).entries, [
.init(line: .heading("Heading", level: .h3), lineIndex: 0)
])
}
func testMultipleTopLevelHeadings() {
let doc = Document(url: URL(string: "gemini://example.com")!, lines: [
.heading("One", level: .h1),
.heading("Two", level: .h1),
])
XCTAssertEqual(TableOfContents(document: doc).entries, [
.init(line: .heading("One", level: .h1), lineIndex: 0),
.init(line: .heading("Two", level: .h1), lineIndex: 1)
])
}
func testNestedHeadings() {
let doc = Document(url: URL(string: "gemini://example.com")!, lines: [
.heading("One", level: .h1),
.heading("Two", level: .h2),
])
let entries = TableOfContents(document: doc).entries
XCTAssertEqual(entries.count, 1)
XCTAssertEqual(entries[0].line, .heading("One", level: .h1))
XCTAssertEqual(entries[0].lineIndex, 0)
XCTAssertEqual(entries[0].children, [
.init(line: .heading("Two", level: .h2), lineIndex: 1)
])
}
func testTriplyNestedHeadings() {
let doc = Document(url: URL(string: "gemini://example.com")!, lines: [
.heading("One", level: .h1),
.heading("Two", level: .h2),
.heading("Three", level: .h3),
])
let entries = TableOfContents(document: doc).entries
XCTAssertEqual(entries.count, 1)
XCTAssertEqual(entries[0].line, .heading("One", level: .h1))
XCTAssertEqual(entries[0].lineIndex, 0)
XCTAssertEqual(entries[0].children.count, 1)
XCTAssertEqual(entries[0].children[0].line, .heading("Two", level: .h2))
XCTAssertEqual(entries[0].children[0].lineIndex, 1)
XCTAssertEqual(entries[0].children[0].children, [
.init(line: .heading("Three", level: .h3), lineIndex: 2)
])
}
func testMultipleTopLevelSections() {
let doc = Document(url: URL(string: "gemini://example.com")!, lines: [
.heading("Top Level One", level: .h1),
.heading("A", level: .h2),
.heading("Top Level Two", level: .h1),
.heading("B", level: .h2),
])
let entries = TableOfContents(document: doc).entries
XCTAssertEqual(entries.count, 2)
let first = entries[0]
XCTAssertEqual(first.line, .heading("Top Level One", level: .h1))
XCTAssertEqual(first.lineIndex, 0)
XCTAssertEqual(first.children, [
.init(line: .heading("A", level: .h2), lineIndex: 1)
])
let second = entries[1]
XCTAssertEqual(second.line, .heading("Top Level Two", level: .h1))
XCTAssertEqual(second.lineIndex, 2)
XCTAssertEqual(second.children, [
.init(line: .heading("B", level: .h2), lineIndex: 3)
])
}
func testMultipleNestedSections() {
let doc = Document(url: URL(string: "gemini://example.com")!, lines: [
.heading("Top Level", level: .h1),
.heading("A", level: .h2),
.heading("Third Level", level: .h3),
.heading("B", level: .h2),
.heading("Third Level 2", level: .h3),
])
let entries = TableOfContents(document: doc).entries
XCTAssertEqual(entries.count, 1)
let topLevel = entries[0]
XCTAssertEqual(topLevel.line, .heading("Top Level", level: .h1))
XCTAssertEqual(topLevel.lineIndex, 0)
let children = topLevel.children
XCTAssertEqual(children.count, 2)
let first = children[0]
XCTAssertEqual(first.line, .heading("A", level: .h2))
XCTAssertEqual(first.lineIndex, 1)
XCTAssertEqual(first.children, [
.init(line: .heading("Third Level", level: .h3), lineIndex: 2)
])
let second = children[1]
XCTAssertEqual(second.line, .heading("B", level: .h2))
XCTAssertEqual(second.lineIndex, 3)
XCTAssertEqual(second.children, [
.init(line: .heading("Third Level 2", level: .h3), lineIndex: 4)
])
}
func testNonH1TopLevelSections() {
let doc = Document(url: URL(string: "gemini://example.com")!, lines: [
.heading("A", level: .h2),
.heading("B", level: .h3),
.heading("C", level: .h1),
])
let entries = TableOfContents(document: doc).entries
XCTAssertEqual(entries.count, 2)
let first = entries[0]
XCTAssertEqual(first.line, .heading("A", level: .h2))
XCTAssertEqual(first.lineIndex, 0)
XCTAssertEqual(first.children, [
.init(line: .heading("B", level: .h3), lineIndex: 1)
])
XCTAssertEqual(entries[1], .init(line: .heading("C", level: .h1), lineIndex: 2))
}
}