swift format

This commit is contained in:
Adam Fowler
2021-03-15 18:24:06 +00:00
parent 955e1eb9e4
commit 66edcba185
18 changed files with 306 additions and 313 deletions

View File

@@ -1,5 +1,5 @@
import XCTest
@testable import HummingbirdMustache
import XCTest
final class LibraryTests: XCTestCase {
func testDirectoryLoad() throws {
@@ -10,7 +10,7 @@ final class LibraryTests: XCTestCase {
defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates")) }
try data.write(to: URL(fileURLWithPath: "templates/test.mustache"))
defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates/test.mustache")) }
let library = HBMustacheLibrary(directory: "./templates")
let object = ["value": ["value1", "value2"]]
XCTAssertEqual(library.render(object, withTemplate: "test"), "<test><value>value1</value><value>value2</value></test>")

View File

@@ -1,117 +1,117 @@
import XCTest
import HummingbirdMustache
import XCTest
final class MethodTests: XCTestCase {
func testLowercased() throws {
let template = try HBMustacheTemplate(string: """
{{ lowercased(name) }}
""")
{{ lowercased(name) }}
""")
let object: [String: Any] = ["name": "Test"]
XCTAssertEqual(template.render(object), "test")
}
func testUppercased() throws {
let template = try HBMustacheTemplate(string: """
{{ uppercased(name) }}
""")
{{ uppercased(name) }}
""")
let object: [String: Any] = ["name": "Test"]
XCTAssertEqual(template.render(object), "TEST")
}
func testFirstLast() throws {
let template = try HBMustacheTemplate(string: """
{{#repo}}
<b>{{#first()}}first: {{/}}{{#last()}}last: {{/}}{{ name }}</b>
{{/repo}}
""")
{{#repo}}
<b>{{#first()}}first: {{/}}{{#last()}}last: {{/}}{{ name }}</b>
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """
<b>first: resque</b>
<b>hub</b>
<b>last: rip</b>
<b>first: resque</b>
<b>hub</b>
<b>last: rip</b>
""")
""")
}
func testIndex() throws {
let template = try HBMustacheTemplate(string: """
{{#repo}}
<b>{{#index()}}{{plusone(.)}}{{/}}) {{ name }}</b>
{{/repo}}
""")
{{#repo}}
<b>{{#index()}}{{plusone(.)}}{{/}}) {{ name }}</b>
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """
<b>1) resque</b>
<b>2) hub</b>
<b>3) rip</b>
<b>1) resque</b>
<b>2) hub</b>
<b>3) rip</b>
""")
""")
}
func testEvenOdd() throws {
let template = try HBMustacheTemplate(string: """
{{#repo}}
<b>{{index()}}) {{#even()}}even {{/}}{{#odd()}}odd {{/}}{{ name }}</b>
{{/repo}}
""")
{{#repo}}
<b>{{index()}}) {{#even()}}even {{/}}{{#odd()}}odd {{/}}{{ name }}</b>
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """
<b>0) even resque</b>
<b>1) odd hub</b>
<b>2) even rip</b>
<b>0) even resque</b>
<b>1) odd hub</b>
<b>2) even rip</b>
""")
""")
}
func testReversed() throws {
let template = try HBMustacheTemplate(string: """
{{#reversed(repo)}}
<b>{{ name }}</b>
{{/repo}}
""")
{{#reversed(repo)}}
<b>{{ name }}</b>
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """
<b>rip</b>
<b>hub</b>
<b>resque</b>
<b>rip</b>
<b>hub</b>
<b>resque</b>
""")
""")
}
func testArrayIndex() throws {
let template = try HBMustacheTemplate(string: """
{{#repo}}
<b>{{ index() }}) {{ name }}</b>
{{/repo}}
""")
{{#repo}}
<b>{{ index() }}) {{ name }}</b>
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """
<b>0) resque</b>
<b>1) hub</b>
<b>2) rip</b>
<b>0) resque</b>
<b>1) hub</b>
<b>2) rip</b>
""")
""")
}
func testArraySorted() throws {
let template = try HBMustacheTemplate(string: """
{{#sorted(repo)}}
<b>{{ index() }}) {{ . }}</b>
{{/repo}}
""")
{{#sorted(repo)}}
<b>{{ index() }}) {{ . }}</b>
{{/repo}}
""")
let object: [String: Any] = ["repo": ["resque", "hub", "rip"]]
XCTAssertEqual(template.render(object), """
<b>0) hub</b>
<b>1) resque</b>
<b>2) rip</b>
<b>0) hub</b>
<b>1) resque</b>
<b>2) rip</b>
""")
""")
}
func testDictionaryEnumerated() throws {
let template = try HBMustacheTemplate(string: """
{{#enumerated(.)}}<b>{{ key }} = {{ value }}</b>{{/.}}
""")
{{#enumerated(.)}}<b>{{ key }} = {{ value }}</b>{{/.}}
""")
let object: [String: Any] = ["one": 1, "two": 2]
let result = template.render(object)
XCTAssertTrue(result == "<b>one = 1</b><b>two = 2</b>" || result == "<b>two = 2</b><b>one = 1</b>")
@@ -119,11 +119,10 @@ final class MethodTests: XCTestCase {
func testDictionarySortedByKey() throws {
let template = try HBMustacheTemplate(string: """
{{#sorted(.)}}<b>{{ key }} = {{ value }}</b>{{/.}}
""")
{{#sorted(.)}}<b>{{ key }} = {{ value }}</b>{{/.}}
""")
let object: [String: Any] = ["one": 1, "two": 2, "three": 3]
let result = template.render(object)
XCTAssertEqual(result, "<b>one = 1</b><b>three = 3</b><b>two = 2</b>")
}
}

View File

@@ -1,54 +1,53 @@
import XCTest
@testable import HummingbirdMustache
import XCTest
final class PartialTests: XCTestCase {
/// Testing partials
func testMustacheManualExample9() throws {
let library = HBMustacheLibrary()
let template = try HBMustacheTemplate(string: """
<h2>Names</h2>
{{#names}}
{{> user}}
{{/names}}
""")
<h2>Names</h2>
{{#names}}
{{> user}}
{{/names}}
""")
let template2 = try HBMustacheTemplate(string: """
<strong>{{.}}</strong>
""")
<strong>{{.}}</strong>
""")
library.register(template, named: "base")
library.register(template2, named: "user")
let object: [String: Any] = ["names": ["john", "adam", "claire"]]
XCTAssertEqual(library.render(object, withTemplate: "base"), """
<h2>Names</h2>
<strong>john</strong>
<strong>adam</strong>
<strong>claire</strong>
<h2>Names</h2>
<strong>john</strong>
<strong>adam</strong>
<strong>claire</strong>
""")
""")
}
/// Testing dynamic partials
func testDynamicPartials() throws {
let library = HBMustacheLibrary()
let template = try HBMustacheTemplate(string: """
<h2>Names</h2>
{{partial}}
""")
<h2>Names</h2>
{{partial}}
""")
let template2 = try HBMustacheTemplate(string: """
{{#names}}
<strong>{{.}}</strong>
{{/names}}
""")
{{#names}}
<strong>{{.}}</strong>
{{/names}}
""")
library.register(template, named: "base")
let object: [String: Any] = ["names": ["john", "adam", "claire"], "partial": template2]
XCTAssertEqual(library.render(object, withTemplate: "base"), """
<h2>Names</h2>
<strong>john</strong>
<strong>adam</strong>
<strong>claire</strong>
<h2>Names</h2>
<strong>john</strong>
<strong>adam</strong>
<strong>claire</strong>
""")
""")
}
}

View File

@@ -1,5 +1,5 @@
import XCTest
@testable import HummingbirdMustache
import XCTest
final class TemplateParserTests: XCTestCase {
func testText() throws {
@@ -75,15 +75,15 @@ extension HBMustacheTemplate: Equatable {
extension HBMustacheTemplate.Token: Equatable {
public static func == (lhs: HBMustacheTemplate.Token, rhs: HBMustacheTemplate.Token) -> Bool {
switch (lhs, rhs) {
case (.text(let lhs), .text(let rhs)):
case let (.text(lhs), .text(rhs)):
return lhs == rhs
case (.variable(let lhs, let lhs2), .variable(let rhs, let rhs2)):
case let (.variable(lhs, lhs2), .variable(rhs, rhs2)):
return lhs == rhs && lhs2 == rhs2
case (.section(let lhs1, let lhs2, let lhs3), .section(let rhs1, let rhs2, let rhs3)):
case let (.section(lhs1, lhs2, lhs3), .section(rhs1, rhs2, rhs3)):
return lhs1 == rhs1 && lhs2 == rhs2 && lhs3 == rhs3
case (.invertedSection(let lhs1, let lhs2, let lhs3), .invertedSection(let rhs1, let rhs2, let rhs3)):
case let (.invertedSection(lhs1, lhs2, lhs3), .invertedSection(rhs1, rhs2, rhs3)):
return lhs1 == rhs1 && lhs2 == rhs2 && lhs3 == rhs3
case (.partial(let name1), .partial(let name2)):
case let (.partial(name1), .partial(name2)):
return name1 == name2
default:
return false

View File

@@ -1,5 +1,5 @@
import XCTest
@testable import HummingbirdMustache
import XCTest
final class TemplateRendererTests: XCTestCase {
func testText() throws {
@@ -67,7 +67,7 @@ final class TemplateRendererTests: XCTestCase {
XCTAssertEqual(template.render(Test(string: "string")), "test string")
XCTAssertEqual(template.render(Test(string: nil)), "test ")
}
func testOptionalSequence() throws {
struct Test {
let string: String?
@@ -79,7 +79,7 @@ final class TemplateRendererTests: XCTestCase {
XCTAssertEqual(template2.render(Test(string: "string")), "test ")
XCTAssertEqual(template2.render(Test(string: nil)), "test *")
}
func testStructureInStructure() throws {
struct SubTest {
let string: String?
@@ -95,134 +95,134 @@ final class TemplateRendererTests: XCTestCase {
/// variables
func testMustacheManualExample1() throws {
let template = try HBMustacheTemplate(string: """
Hello {{name}}
You have just won {{value}} dollars!
{{#in_ca}}
Well, {{taxed_value}} dollars, after taxes.
{{/in_ca}}
""")
Hello {{name}}
You have just won {{value}} dollars!
{{#in_ca}}
Well, {{taxed_value}} dollars, after taxes.
{{/in_ca}}
""")
let object: [String: Any] = ["name": "Chris", "value": 10000, "taxed_value": 10000 - (10000 * 0.4), "in_ca": true]
XCTAssertEqual(template.render(object), """
Hello Chris
You have just won 10000 dollars!
Well, 6000.0 dollars, after taxes.
Hello Chris
You have just won 10000 dollars!
Well, 6000.0 dollars, after taxes.
""")
""")
}
/// test esacped and unescaped text
func testMustacheManualExample2() throws {
let template = try HBMustacheTemplate(string: """
*{{name}}
*{{age}}
*{{company}}
*{{{company}}}
""")
*{{name}}
*{{age}}
*{{company}}
*{{{company}}}
""")
let object: [String: Any] = ["name": "Chris", "company": "<b>GitHub</b>"]
XCTAssertEqual(template.render(object), """
*Chris
*
*&lt;b&gt;GitHub&lt;/b&gt;
*<b>GitHub</b>
""")
*Chris
*
*&lt;b&gt;GitHub&lt;/b&gt;
*<b>GitHub</b>
""")
}
/// test boolean
func testMustacheManualExample3() throws {
let template = try HBMustacheTemplate(string: """
Shown.
{{#person}}
Never shown!
{{/person}}
""")
Shown.
{{#person}}
Never shown!
{{/person}}
""")
let object: [String: Any] = ["person": false]
XCTAssertEqual(template.render(object), """
Shown.
Shown.
""")
""")
}
/// test non-empty lists
func testMustacheManualExample4() throws {
let template = try HBMustacheTemplate(string: """
{{#repo}}
<b>{{name}}</b>
{{/repo}}
""")
{{#repo}}
<b>{{name}}</b>
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """
<b>resque</b>
<b>hub</b>
<b>rip</b>
<b>resque</b>
<b>hub</b>
<b>rip</b>
""")
""")
}
/// test lambdas
func testMustacheManualExample5() throws {
let template = try HBMustacheTemplate(string: """
{{#wrapped}}{{name}} is awesome.{{/wrapped}}
""")
{{#wrapped}}{{name}} is awesome.{{/wrapped}}
""")
func wrapped(object: Any, template: HBMustacheTemplate) -> String {
return "<b>\(template.render(object))</b>"
}
let object: [String: Any] = ["name": "Willy", "wrapped": HBMustacheLambda(wrapped)]
XCTAssertEqual(template.render(object), """
<b>Willy is awesome.</b>
""")
<b>Willy is awesome.</b>
""")
}
/// test setting context object
func testMustacheManualExample6() throws {
let template = try HBMustacheTemplate(string: """
{{#person?}}
Hi {{name}}!
{{/person?}}
""")
{{#person?}}
Hi {{name}}!
{{/person?}}
""")
let object: [String: Any] = ["person?": ["name": "Jon"]]
XCTAssertEqual(template.render(object), """
Hi Jon!
Hi Jon!
""")
""")
}
/// test inverted sections
func testMustacheManualExample7() throws {
let template = try HBMustacheTemplate(string: """
{{#repo}}
<b>{{name}}</b>
{{/repo}}
{{^repo}}
No repos :(
{{/repo}}
""")
{{#repo}}
<b>{{name}}</b>
{{/repo}}
{{^repo}}
No repos :(
{{/repo}}
""")
let object: [String: Any] = ["repo": []]
XCTAssertEqual(template.render(object), """
No repos :(
No repos :(
""")
""")
}
/// test comments
func testMustacheManualExample8() throws {
let template = try HBMustacheTemplate(string: """
<h1>Today{{! ignore me }}.</h1>
""")
<h1>Today{{! ignore me }}.</h1>
""")
let object: [String: Any] = ["repo": []]
XCTAssertEqual(template.render(object), """
<h1>Today.</h1>
""")
<h1>Today.</h1>
""")
}
func testPerformance() throws {
let template = try HBMustacheTemplate(string: """
{{#repo}}
<b>{{name}}</b>
{{/repo}}
""")
{{#repo}}
<b>{{name}}</b>
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
let date = Date()
for _ in 1...10000 {
for _ in 1 ... 10000 {
_ = template.render(object)
}
print(-date.timeIntervalSinceNow)