swift format

This commit is contained in:
Adam Fowler
2021-03-18 10:34:52 +00:00
parent 16a2c54be6
commit c560bd0fd9
3 changed files with 34 additions and 53 deletions

View File

@@ -284,7 +284,7 @@ extension HBParser {
unsafeAdvance() unsafeAdvance()
} }
if startIndex == index { if startIndex == index {
return subParser(startIndex..<startIndex) return subParser(startIndex ..< startIndex)
} }
return subParser(startIndex ..< index) return subParser(startIndex ..< index)
} }
@@ -404,6 +404,7 @@ extension HBParser {
func getPosition() -> Int { func getPosition() -> Int {
return index return index
} }
mutating func setPosition(_ index: Int) throws { mutating func setPosition(_ index: Int) throws {
guard range.contains(index) else { throw Error.invalidPosition } guard range.contains(index) else { throw Error.invalidPosition }
guard validateUTF8Character(at: index).0 != nil else { throw Error.invalidPosition } guard validateUTF8Character(at: index).0 != nil else { throw Error.invalidPosition }

View File

@@ -61,7 +61,7 @@ extension HBMustacheTemplate {
// section // section
parser.unsafeAdvance() parser.unsafeAdvance()
let (name, method) = try parseName(&parser) let (name, method) = try parseName(&parser)
if newLine && hasLineFinished(&parser) { if newLine, hasLineFinished(&parser) {
setNewLine = true setNewLine = true
if parser.current() == "\n" { if parser.current() == "\n" {
parser.unsafeAdvance() parser.unsafeAdvance()
@@ -77,7 +77,7 @@ extension HBMustacheTemplate {
// inverted section // inverted section
parser.unsafeAdvance() parser.unsafeAdvance()
let (name, method) = try parseName(&parser) let (name, method) = try parseName(&parser)
if newLine && hasLineFinished(&parser) { if newLine, hasLineFinished(&parser) {
setNewLine = true setNewLine = true
if parser.current() == "\n" { if parser.current() == "\n" {
parser.unsafeAdvance() parser.unsafeAdvance()
@@ -96,7 +96,7 @@ extension HBMustacheTemplate {
guard name == sectionName else { guard name == sectionName else {
throw Error.sectionCloseNameIncorrect throw Error.sectionCloseNameIncorrect
} }
if newLine && hasLineFinished(&parser) { if newLine, hasLineFinished(&parser) {
setNewLine = true setNewLine = true
if parser.current() == "\n" { if parser.current() == "\n" {
parser.unsafeAdvance() parser.unsafeAdvance()
@@ -111,7 +111,7 @@ extension HBMustacheTemplate {
// comment // comment
parser.unsafeAdvance() parser.unsafeAdvance()
_ = try parseComment(&parser) _ = try parseComment(&parser)
if newLine && hasLineFinished(&parser) { if newLine, hasLineFinished(&parser) {
setNewLine = true setNewLine = true
if !parser.reachedEnd() { if !parser.reachedEnd() {
parser.unsafeAdvance() parser.unsafeAdvance()
@@ -143,16 +143,16 @@ extension HBMustacheTemplate {
// partial // partial
parser.unsafeAdvance() parser.unsafeAdvance()
let (name, _) = try parseName(&parser) let (name, _) = try parseName(&parser)
/*if newLine && hasLineFinished(&parser) { /* if newLine && hasLineFinished(&parser) {
setNewLine = true setNewLine = true
if parser.current() == "\n" { if parser.current() == "\n" {
parser.unsafeAdvance() parser.unsafeAdvance()
} }
}*/ } */
if whiteSpaceBefore.count > 0 { if whiteSpaceBefore.count > 0 {
tokens.append(.text(whiteSpaceBefore)) tokens.append(.text(whiteSpaceBefore))
} }
if newLine && hasLineFinished(&parser) { if newLine, hasLineFinished(&parser) {
setNewLine = true setNewLine = true
if parser.current() == "\n" { if parser.current() == "\n" {
parser.unsafeAdvance() parser.unsafeAdvance()

View File

@@ -10,10 +10,9 @@ func test(_ object: Any, _ template: String, _ expected: String) throws {
XCTAssertEqual(result, expected) XCTAssertEqual(result, expected)
} }
//MARK: Comments // MARK: Comments
final class SpecCommentsTests: XCTestCase { final class SpecCommentsTests: XCTestCase {
func testInline() throws { func testInline() throws {
let object = {} let object = {}
let template = "12345{{! Comment Block! }}67890" let template = "12345{{! Comment Block! }}67890"
@@ -113,7 +112,7 @@ final class SpecCommentsTests: XCTestCase {
} }
} }
//MARK: Interpolation // MARK: Interpolation
final class SpecInterpolationTests: XCTestCase { final class SpecInterpolationTests: XCTestCase {
func testNoInterpolation() throws { func testNoInterpolation() throws {
@@ -121,71 +120,66 @@ final class SpecInterpolationTests: XCTestCase {
let template = "Hello from {Mustache}!" let template = "Hello from {Mustache}!"
let expected = "Hello from {Mustache}!" let expected = "Hello from {Mustache}!"
try test(object, template, expected) try test(object, template, expected)
} }
func testBasicInterpolation() throws { func testBasicInterpolation() throws {
let object = [ "subject": "world" ] let object = ["subject": "world"]
let template = "Hello, {{subject}}!" let template = "Hello, {{subject}}!"
let expected = "Hello, world!" let expected = "Hello, world!"
try test(object, template, expected) try test(object, template, expected)
} }
func testHTMLEscaping() throws { func testHTMLEscaping() throws {
let object = [ "forbidden": #"& " < >"# ] let object = ["forbidden": #"& " < >"#]
let template = "These characters should be HTML escaped: {{forbidden}}" let template = "These characters should be HTML escaped: {{forbidden}}"
let expected = #"These characters should be HTML escaped: &amp; &quot; &lt; &gt;"# let expected = #"These characters should be HTML escaped: &amp; &quot; &lt; &gt;"#
try test(object, template, expected) try test(object, template, expected)
} }
func testTripleMustache() throws { func testTripleMustache() throws {
let object = [ "forbidden": #"& " < >"# ] let object = ["forbidden": #"& " < >"#]
let template = "These characters should not be HTML escaped: {{{forbidden}}}" let template = "These characters should not be HTML escaped: {{{forbidden}}}"
let expected = #"These characters should not be HTML escaped: & " < >"# let expected = #"These characters should not be HTML escaped: & " < >"#
try test(object, template, expected) try test(object, template, expected)
} }
func testAmpersand() throws { func testAmpersand() throws {
let object = [ "forbidden": #"& " < >"# ] let object = ["forbidden": #"& " < >"#]
let template = "These characters should not be HTML escaped: {{&forbidden}}" let template = "These characters should not be HTML escaped: {{&forbidden}}"
let expected = #"These characters should not be HTML escaped: & " < >"# let expected = #"These characters should not be HTML escaped: & " < >"#
try test(object, template, expected) try test(object, template, expected)
} }
func testBasicInteger() throws { func testBasicInteger() throws {
let object = [ "mph": 85 ] let object = ["mph": 85]
let template = #""{{mph}} miles an hour!""# let template = #""{{mph}} miles an hour!""#
let expected = #""85 miles an hour!""# let expected = #""85 miles an hour!""#
try test(object, template, expected) try test(object, template, expected)
} }
func testTripleMustacheInteger() throws { func testTripleMustacheInteger() throws {
let object = [ "mph": 85 ] let object = ["mph": 85]
let template = #""{{{mph}}} miles an hour!""# let template = #""{{{mph}}} miles an hour!""#
let expected = #""85 miles an hour!""# let expected = #""85 miles an hour!""#
try test(object, template, expected) try test(object, template, expected)
} }
func testBasicDecimal() throws { func testBasicDecimal() throws {
let object = [ "power": 1.210 ] let object = ["power": 1.210]
let template = #""{{power}} jiggawatts!""# let template = #""{{power}} jiggawatts!""#
let expected = #""1.21 jiggawatts!""# let expected = #""1.21 jiggawatts!""#
try test(object, template, expected) try test(object, template, expected)
} }
func testTripleMustacheDecimal() throws { func testTripleMustacheDecimal() throws {
let object = [ "power": 1.210 ] let object = ["power": 1.210]
let template = #""{{{power}}} jiggawatts!""# let template = #""{{{power}}} jiggawatts!""#
let expected = #""1.21 jiggawatts!""# let expected = #""1.21 jiggawatts!""#
try test(object, template, expected) try test(object, template, expected)
} }
func testAmpersandDecimal() throws { func testAmpersandDecimal() throws {
let object = [ "power": 1.210 ] let object = ["power": 1.210]
let template = #""{{&power}} jiggawatts!""# let template = #""{{&power}} jiggawatts!""#
let expected = #""1.21 jiggawatts!""# let expected = #""1.21 jiggawatts!""#
try test(object, template, expected) try test(object, template, expected)
@@ -250,7 +244,7 @@ final class SpecInterpolationTests: XCTestCase {
func testInitialResolutionDottedName() throws { func testInitialResolutionDottedName() throws {
let object = [ let object = [
"a": ["b": ["c": ["d": ["e": ["name": "Phil"]]]]], "a": ["b": ["c": ["d": ["e": ["name": "Phil"]]]]],
"b": ["c": ["d": ["e": ["name": "Wrong"]]]] "b": ["c": ["d": ["e": ["name": "Wrong"]]]],
] ]
let template = #""{{#a}}{{b.c.d.e.name}}{{/a}}" == "Phil""# let template = #""{{#a}}{{b.c.d.e.name}}{{/a}}" == "Phil""#
let expected = #""Phil" == "Phil""# let expected = #""Phil" == "Phil""#
@@ -260,7 +254,7 @@ final class SpecInterpolationTests: XCTestCase {
func testContextPrecedenceDottedName() throws { func testContextPrecedenceDottedName() throws {
let object = [ let object = [
"a": ["b": []], "a": ["b": []],
"b": ["c": "Error"] "b": ["c": "Error"],
] ]
let template = #"{{#a}}{{b.c}}{{/a}}"# let template = #"{{#a}}{{b.c}}{{/a}}"#
let expected = "" let expected = ""
@@ -314,7 +308,6 @@ final class SpecInterpolationTests: XCTestCase {
let template = "|{{ string }}|" let template = "|{{ string }}|"
let expected = "|---|" let expected = "|---|"
try test(object, template, expected) try test(object, template, expected)
} }
func testTripleMustacheWithPadding() throws { func testTripleMustacheWithPadding() throws {
@@ -322,7 +315,6 @@ final class SpecInterpolationTests: XCTestCase {
let template = "|{{{ string }}}|" let template = "|{{{ string }}}|"
let expected = "|---|" let expected = "|---|"
try test(object, template, expected) try test(object, template, expected)
} }
func testAmpersandWithPadding() throws { func testAmpersandWithPadding() throws {
@@ -341,7 +333,6 @@ final class SpecInvertedTests: XCTestCase {
let template = #""{{^boolean}}This should be rendered.{{/boolean}}""# let template = #""{{^boolean}}This should be rendered.{{/boolean}}""#
let expected = #""This should be rendered.""# let expected = #""This should be rendered.""#
try test(object, template, expected) try test(object, template, expected)
} }
func testTrue() throws { func testTrue() throws {
@@ -349,7 +340,6 @@ final class SpecInvertedTests: XCTestCase {
let template = #""{{^boolean}}This should not be rendered.{{/boolean}}""# let template = #""{{^boolean}}This should not be rendered.{{/boolean}}""#
let expected = "\"\"" let expected = "\"\""
try test(object, template, expected) try test(object, template, expected)
} }
func testContext() throws { func testContext() throws {
@@ -357,7 +347,6 @@ final class SpecInvertedTests: XCTestCase {
let template = #""{{^context}}Hi {{name}}.{{/context}}""# let template = #""{{^context}}Hi {{name}}.{{/context}}""#
let expected = "\"\"" let expected = "\"\""
try test(object, template, expected) try test(object, template, expected)
} }
func testList() throws { func testList() throws {
@@ -365,7 +354,6 @@ final class SpecInvertedTests: XCTestCase {
let template = #""{{^list}}{{n}}{{/list}}""# let template = #""{{^list}}{{n}}{{/list}}""#
let expected = "\"\"" let expected = "\"\""
try test(object, template, expected) try test(object, template, expected)
} }
func testEmptyList() throws { func testEmptyList() throws {
@@ -442,7 +430,6 @@ final class SpecInvertedTests: XCTestCase {
let template = " | {{^boolean}}\t|\t{{/boolean}} | \n" let template = " | {{^boolean}}\t|\t{{/boolean}} | \n"
let expected = " | \t|\t | \n" let expected = " | \t|\t | \n"
try test(object, template, expected) try test(object, template, expected)
} }
func testInternalWhitespace() throws { func testInternalWhitespace() throws {
@@ -450,7 +437,6 @@ final class SpecInvertedTests: XCTestCase {
let template = " | {{^boolean}} {{! Important Whitespace }}\n {{/boolean}} | \n" let template = " | {{^boolean}} {{! Important Whitespace }}\n {{/boolean}} | \n"
let expected = " | \n | \n" let expected = " | \n | \n"
try test(object, template, expected) try test(object, template, expected)
} }
func testIndentedInline() throws { func testIndentedInline() throws {
@@ -458,7 +444,6 @@ final class SpecInvertedTests: XCTestCase {
let template = " {{^boolean}}NO{{/boolean}}\n {{^boolean}}WAY{{/boolean}}\n" let template = " {{^boolean}}NO{{/boolean}}\n {{^boolean}}WAY{{/boolean}}\n"
let expected = " NO\n WAY\n" let expected = " NO\n WAY\n"
try test(object, template, expected) try test(object, template, expected)
} }
func testStandaloneLines() throws { func testStandaloneLines() throws {
@@ -476,7 +461,6 @@ final class SpecInvertedTests: XCTestCase {
| A Line | A Line
""" """
try test(object, template, expected) try test(object, template, expected)
} }
func testStandaloneIndentedLines() throws { func testStandaloneIndentedLines() throws {
@@ -501,7 +485,6 @@ final class SpecInvertedTests: XCTestCase {
let template = "|\r\n{{^boolean}}\r\n{{/boolean}}\r\n|" let template = "|\r\n{{^boolean}}\r\n{{/boolean}}\r\n|"
let expected = "|\r\n|" let expected = "|\r\n|"
try test(object, template, expected) try test(object, template, expected)
} }
func testStandaloneWithoutPreviousLine() throws { func testStandaloneWithoutPreviousLine() throws {
@@ -637,7 +620,7 @@ final class SpecPartialsTests: XCTestCase {
} }
func testPaddingWhitespace() throws { func testPaddingWhitespace() throws {
let object = ["boolean": true ] let object = ["boolean": true]
let template = "|{{> partial }}|" let template = "|{{> partial }}|"
let partial = "[]" let partial = "[]"
let expected = "|[]|" let expected = "|[]|"
@@ -653,7 +636,6 @@ final class SpecSectionTests: XCTestCase {
let template = #""{{#boolean}}This should be rendered.{{/boolean}}""# let template = #""{{#boolean}}This should be rendered.{{/boolean}}""#
let expected = #""This should be rendered.""# let expected = #""This should be rendered.""#
try test(object, template, expected) try test(object, template, expected)
} }
func testFalse() throws { func testFalse() throws {
@@ -661,7 +643,6 @@ final class SpecSectionTests: XCTestCase {
let template = #""{{#boolean}}This should not be rendered.{{/boolean}}""# let template = #""{{#boolean}}This should not be rendered.{{/boolean}}""#
let expected = "\"\"" let expected = "\"\""
try test(object, template, expected) try test(object, template, expected)
} }
func testContext() throws { func testContext() throws {
@@ -792,34 +773,33 @@ final class SpecSectionTests: XCTestCase {
} }
func testImplicitIteratorString() throws { func testImplicitIteratorString() throws {
let object = ["list": [ "a", "b", "c", "d", "e" ]] let object = ["list": ["a", "b", "c", "d", "e"]]
let template = #""{{#list}}({{.}}){{/list}}""# let template = #""{{#list}}({{.}}){{/list}}""#
let expected = #""(a)(b)(c)(d)(e)""# let expected = #""(a)(b)(c)(d)(e)""#
try test(object, template, expected) try test(object, template, expected)
} }
func testImplicitIteratorInteger() throws { func testImplicitIteratorInteger() throws {
let object = ["list": [ 1, 2, 3, 4, 5 ]] let object = ["list": [1, 2, 3, 4, 5]]
let template = #""{{#list}}({{.}}){{/list}}""# let template = #""{{#list}}({{.}}){{/list}}""#
let expected = #""(1)(2)(3)(4)(5)""# let expected = #""(1)(2)(3)(4)(5)""#
try test(object, template, expected) try test(object, template, expected)
} }
func testImplicitIteratorDecimal() throws { func testImplicitIteratorDecimal() throws {
let object = ["list": [ 1.1, 2.2, 3.3, 4.4, 5.5 ]] let object = ["list": [1.1, 2.2, 3.3, 4.4, 5.5]]
let template = #""{{#list}}({{.}}){{/list}}""# let template = #""{{#list}}({{.}}){{/list}}""#
let expected = #""(1.1)(2.2)(3.3)(4.4)(5.5)""# let expected = #""(1.1)(2.2)(3.3)(4.4)(5.5)""#
try test(object, template, expected) try test(object, template, expected)
} }
func testImplicitIteratorArray() throws { func testImplicitIteratorArray() throws {
let object: [String: Any] = ["list": [[ 1, 2, 3], [ "a", "b", "c"]]] let object: [String: Any] = ["list": [[1, 2, 3], ["a", "b", "c"]]]
let template = #""{{#list}}({{#.}}{{.}}{{/.}}){{/list}}""# let template = #""{{#list}}({{#.}}{{.}}{{/.}}){{/list}}""#
let expected = #""(123)(abc)""# let expected = #""(123)(abc)""#
try test(object, template, expected) try test(object, template, expected)
} }
func testDottedNameTrue() throws { func testDottedNameTrue() throws {
let object = ["a": ["b": ["c": true]]] let object = ["a": ["b": ["c": true]]]
let template = #""{{#a.b.c}}Here{{/a.b.c}}" == "Here""# let template = #""{{#a.b.c}}Here{{/a.b.c}}" == "Here""#