Skip to content

Commit

Permalink
Get first test working with release of LeafKit
Browse files Browse the repository at this point in the history
  • Loading branch information
0xTim committed Nov 17, 2020
1 parent fb5e063 commit b8c7a4f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Sources/LeafMarkdown/Tag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct Markdown: LeafTag {
public func render(_ ctx: LeafContext) throws -> LeafData {
var markdown = ""

if let markdownArgument = ctx.parameters.first, !markdownArgument.isNull {
if let markdownArgument = ctx.parameters.first, !markdownArgument.isNil {
guard let markdownArgumentValue = markdownArgument.string else {
throw Error.invalidArgument(ctx.parameters.first)
}
Expand Down
127 changes: 62 additions & 65 deletions Tests/LeafMarkdownTests/LeafTests.swift
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
import XCTest
@testable import LeafKit
import LeafMarkdown
import NIO

class MarkdownTests: XCTestCase {
// MARK: - Properties

var renderer: LeafRenderer!
var template: ResolvedDocument!
var ast: [Syntax]!
var markdownTag: Markdown!

// MARK: - Overrides

override func setUp() {
let loop = EmbeddedEventLoop()
let config = LeafConfiguration(rootDirectory: Process().currentDirectoryPath)
var tags = defaultTags
tags["markdown"] = Markdown()
self.renderer = LeafRenderer(
configuration: config,
tags: tags,
files: NIOLeafFiles(fileio: .init(threadPool: .init(numberOfThreads: 1))),
eventLoop: loop
override func setUpWithError() throws {
var lexer = LeafLexer(name: "markdowntest", template: "#markdown(data)")
let tokens = try lexer.lex()
var parser = LeafParser(name: "markdowntest", tokens: tokens)
ast = try parser.parse()
markdownTag = Markdown()
}

// MARK: - Helper
func render(context: [String: LeafData]) throws -> String {
var serializer = LeafSerializer(
ast: ast,
context: context,
tags: ["markdown": markdownTag]
)

do {
var lexer = LeafLexer(name: "markdowntest", template: "#markdown(data)")
let tokens = try lexer.lex()
var parser = LeafParser(name: "markdowntest", tokens: tokens)
let syntax = try parser.parse()
let unresolved = UnresolvedDocument(name: "markdowntest", raw: syntax)
let resolver = ExtendResolver(document: unresolved, dependencies: [])
template = try resolver.resolve(rootDirectory: config.rootDirectory)
} catch {
XCTFail()
}
let view = try serializer.serialize()
return view.getString(at: view.readerIndex, length: view.readableBytes) ?? ""
}

// MARK: - Tests
Expand All @@ -41,49 +37,50 @@ class MarkdownTests: XCTestCase {
let inputMarkdown = "# This is a test\n\nWe have some text in a tag"
let expectedHtml = "<h1>This is a test</h1>\n<p>We have some text in a tag</p>\n"

let result = try renderer.render(template, context: ["data": .string(inputMarkdown)])
let resultString = result.getString(at: 0, length: result.readableBytes)
XCTAssertEqual(resultString, expectedHtml)
}

func testNilParameterDoesNotCrashLeaf() throws {
let expectedHtml = ""
let result = try renderer.render(template, context: ["data": .null])
let resultString = result.getString(at: 0, length: result.readableBytes)
XCTAssertEqual(resultString, expectedHtml)
}

func testStripHtml() throws {
let inputMarkdown = "<br>"
let expectedHtml = "<!-- raw HTML omitted -->\n"

let result = try renderer.render(template, context: ["data": .string(inputMarkdown)])
let resultString = result.getString(at: 0, length: result.readableBytes)
// let result = try renderer.render(path: template, context: ["data": .string(inputMarkdown)])
// let resultString = result.getString(at: 0, length: result.readableBytes)
let resultString = try render(context: ["data": .string(inputMarkdown)])
XCTAssertEqual(resultString, expectedHtml)
}

func testRejectBadData() throws {
let data = LeafData.lazy { .null }
XCTAssertThrowsError(try renderer.render(template, context: ["data": data]))
}

func testDoNotStripHtml() throws {
let loop = EmbeddedEventLoop()
let config = LeafConfiguration(rootDirectory: Process().currentDirectoryPath)
var tags = defaultTags
tags["markdown"] = Markdown(options: [.unsafe])
let renderer = LeafRenderer(
configuration: config,
tags: tags,
files: NIOLeafFiles(fileio: .init(threadPool: .init(numberOfThreads: 1))),
eventLoop: loop
)

let inputMarkdown = "<br>"
let expectedHtml = "<br>\n"

let result = try renderer.render(template, context: ["data": .string(inputMarkdown)])
let resultString = result.getString(at: 0, length: result.readableBytes)
XCTAssertEqual(resultString, expectedHtml)
}
// func testNilParameterDoesNotCrashLeaf() throws {
// let expectedHtml = ""
// let result = try renderer.render(path: template, context: ["data": .nil])
// let resultString = result.getString(at: 0, length: result.readableBytes)
// XCTAssertEqual(resultString, expectedHtml)
// }
//
// func testStripHtml() throws {
// let inputMarkdown = "<br>"
// let expectedHtml = "<!-- raw HTML omitted -->\n"
//
// let result = try renderer.render(template, context: ["data": .string(inputMarkdown)])
// let resultString = result.getString(at: 0, length: result.readableBytes)
// XCTAssertEqual(resultString, expectedHtml)
// }
//
// func testRejectBadData() throws {
// let data = LeafData.lazy { .null }
// XCTAssertThrowsError(try renderer.render(template, context: ["data": data]))
// }
//
// func testDoNotStripHtml() throws {
// let loop = EmbeddedEventLoop()
// let config = LeafConfiguration(rootDirectory: Process().currentDirectoryPath)
// var tags = defaultTags
// tags["markdown"] = Markdown(options: [.unsafe])
// let renderer = LeafRenderer(
// configuration: config,
// tags: tags,
// files: NIOLeafFiles(fileio: .init(threadPool: .init(numberOfThreads: 1))),
// eventLoop: loop
// )
//
// let inputMarkdown = "<br>"
// let expectedHtml = "<br>\n"
//
// let result = try renderer.render(template, context: ["data": .string(inputMarkdown)])
// let resultString = result.getString(at: 0, length: result.readableBytes)
// XCTAssertEqual(resultString, expectedHtml)
// }
}

0 comments on commit b8c7a4f

Please sign in to comment.