//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2021-2021 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Mustache
import XCTest
final class TransformTests: XCTestCase {
func testLowercased() throws {
let template = try MustacheTemplate(string: """
{{ lowercased(name) }}
""")
let object: [String: Any] = ["name": "Test"]
XCTAssertEqual(template.render(object), "test")
}
func testUppercased() throws {
let template = try MustacheTemplate(string: """
{{ uppercased(name) }}
""")
let object: [String: Any] = ["name": "Test"]
XCTAssertEqual(template.render(object), "TEST")
}
func testNewline() throws {
let template = try MustacheTemplate(string: """
{{#repo}}
{{name}}
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """
resque
hub
rip
""")
}
func testFirstLast() throws {
let template = try MustacheTemplate(string: """
{{#repo}}
{{#first()}}first: {{/first()}}{{#last()}}last: {{/last()}}{{ name }}
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """
first: resque
hub
last: rip
""")
}
func testIndex() throws {
let template = try MustacheTemplate(string: """
{{#repo}}
{{#index()}}{{plusone(.)}}{{/index()}}) {{ name }}
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """
1) resque
2) hub
3) rip
""")
}
func testDoubleSequenceTransformWorks() throws {
let template = try MustacheTemplate(string: """
{{#repo}}
{{count(reversed(numbers))}}
{{/repo}}
""")
let object: [String: Any] = ["repo": ["numbers": [1, 2, 3]]]
XCTAssertEqual(template.render(object), """
3
""")
}
func testMultipleTransformWorks() throws {
let template = try MustacheTemplate(string: """
{{#repo}}
{{minusone(plusone(last(reversed(numbers))))}}
{{/repo}}
""")
let object: [String: Any] = ["repo": ["numbers": [5, 4, 3]]]
XCTAssertEqual(template.render(object), """
5
""")
}
func testNestedTransformWorks() throws {
let template = try MustacheTemplate(string: """
{{#repo}}
{{#uppercased(string)}}{{reversed(.)}}{{/uppercased(string)}}
{{/repo}}
""")
let object: [String: Any] = ["repo": ["string": "a123a"]]
XCTAssertEqual(template.render(object), """
A321A
""")
}
func testEvenOdd() throws {
let template = try MustacheTemplate(string: """
{{#repo}}
{{index()}}) {{#even()}}even {{/even()}}{{#odd()}}odd {{/odd()}}{{ name }}
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """
0) even resque
1) odd hub
2) even rip
""")
}
func testReversed() throws {
let template = try MustacheTemplate(string: """
{{#reversed(repo)}}
{{ name }}
{{/reversed(repo)}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """
rip
hub
resque
""")
}
func testArrayIndex() throws {
let template = try MustacheTemplate(string: """
{{#repo}}
{{ index() }}) {{ name }}
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """
0) resque
1) hub
2) rip
""")
}
func testArraySorted() throws {
let template = try MustacheTemplate(string: """
{{#sorted(repo)}}
{{ index() }}) {{ . }}
{{/sorted(repo)}}
""")
let object: [String: Any] = ["repo": ["resque", "hub", "rip"]]
XCTAssertEqual(template.render(object), """
0) hub
1) resque
2) rip
""")
}
func testDictionaryEmpty() throws {
let template = try MustacheTemplate(string: """
{{#empty(array)}}Array{{/empty(array)}}{{#empty(dictionary)}}Dictionary{{/empty(dictionary)}}
""")
let object: [String: Any] = ["array": [], "dictionary": [:]]
XCTAssertEqual(template.render(object), "ArrayDictionary")
}
func testListOutput() throws {
let object = [1, 2, 3, 4]
let template = try MustacheTemplate(string: "{{#.}}{{.}}{{^last()}}, {{/last()}}{{/.}}")
XCTAssertEqual(template.render(object), "1, 2, 3, 4")
}
func testDictionaryEnumerated() throws {
let template = try MustacheTemplate(string: """
{{#enumerated(.)}}{{ key }} = {{ value }}{{/enumerated(.)}}
""")
let object: [String: Any] = ["one": 1, "two": 2]
let result = template.render(object)
XCTAssertTrue(result == "one = 1two = 2" || result == "two = 2one = 1")
}
func testDictionarySortedByKey() throws {
let template = try MustacheTemplate(string: """
{{#sorted(.)}}{{ key }} = {{ value }}{{/sorted(.)}}
""")
let object: [String: Any] = ["one": 1, "two": 2, "three": 3]
let result = template.render(object)
XCTAssertEqual(result, "one = 1three = 3two = 2")
}
}