Add Comment spec

This commit is contained in:
Adam Fowler
2021-03-15 18:57:55 +00:00
parent b1b65525e8
commit a76a02a8dc
2 changed files with 120 additions and 0 deletions

View File

@@ -62,11 +62,17 @@ extension HBMustacheTemplate {
case "!": case "!":
parser.unsafeAdvance() parser.unsafeAdvance()
_ = try parseComment(&parser) _ = try parseComment(&parser)
if parser.current() == "\n" {
parser.unsafeAdvance()
}
case ">": case ">":
parser.unsafeAdvance() parser.unsafeAdvance()
let (name, _) = try parseName(&parser) let (name, _) = try parseName(&parser)
tokens.append(.partial(name)) tokens.append(.partial(name))
if parser.current() == "\n" {
parser.unsafeAdvance()
}
default: default:
let (name, method) = try parseName(&parser) let (name, method) = try parseName(&parser)

View File

@@ -0,0 +1,114 @@
import HummingbirdMustache
import XCTest
/// Mustache spec tests. These are the formal standard for Mustache. More details
/// can be found at https://github.com/mustache/spec
func test(_ object: Any, _ template: String, _ expected: String) throws {
let template = try HBMustacheTemplate(string: template)
let result = template.render(object)
XCTAssertEqual(result, expected)
}
//MARK: Comments
final class SpecCommentsTests: XCTestCase {
func testInline() throws {
let object = {}
let template = "12345{{! Comment Block! }}67890"
let expected = "1234567890"
try test(object, template, expected)
}
func testMultiline() throws {
let object = {}
let template = """
12345{{!
This is a
multi-line comment...
}}67890
"""
let expected = "1234567890"
try test(object, template, expected)
}
func testStandalone() throws {
let object = {}
let template = """
Begin.
{{! Comment Block! }}
End.
"""
let expected = """
Begin.
End.
"""
try test(object, template, expected)
}
func testIndentedStandalone() throws {
let object = {}
let template = """
Begin.
{{! Comment Block! }}
End.
"""
let expected = """
Begin.
End.
"""
try test(object, template, expected)
}
func testStandaloneLineEndings() throws {
let object = {}
let template = "\r\n{{! Standalone Comment }}\r\n"
let expected = "\r\n"
try test(object, template, expected)
}
func testStandaloneWithoutPreviousLine() throws {
let object = {}
let template = " {{! I'm Still Standalone }}\n!"
let expected = "!"
try test(object, template, expected)
}
func testStandaloneWithoutNewLine() throws {
let object = {}
let template = "!\n {{! I'm Still Standalone }}"
let expected = "!\n"
try test(object, template, expected)
}
func testStandaloneMultiLine() throws {
let object = {}
let template = """
Begin.
{{!
Something's going on here...
}}
End.
"""
let expected = """
Begin.
End.
"""
try test(object, template, expected)
}
func testIndentedInline() throws {
let object = {}
let template = " 12 {{! 34 }}\n"
let expected = " 12 \n"
try test(object, template, expected)
}
func testSurroundingWhitespace() throws {
let object = {}
let template = "12345 {{! Comment Block! }} 67890"
let expected = "12345 67890"
try test(object, template, expected)
}
}