fixed implicit conversion of integer literals to float

This commit is contained in:
Ilya Puchka
2017-12-29 17:26:38 +01:00
parent a4b75f3c89
commit 79a16854e7
4 changed files with 19 additions and 3 deletions

View File

@@ -11,6 +11,7 @@
- Fixed rendering `{{ block.super }}` with several levels of inheritance
- Fixed checking dictionary values for nil in `default` filter
- Fixed comparing string variables with string literals, in Swift 4 string literals became `Substring` and thus couldn't be directly compared to strings.
- Integer literals now resolve into Int values, not Float
## 0.10.1

View File

@@ -63,8 +63,11 @@ public struct Variable : Equatable, Resolvable {
return String(variable[variable.characters.index(after: variable.startIndex) ..< variable.characters.index(before: variable.endIndex)])
}
// Number literal
if let int = Int(variable) {
return int
}
if let number = Number(variable) {
// Number literal
return number
}

View File

@@ -137,6 +137,18 @@ func testFilter() {
try expect(result) == "Hello World"
}
$0.it("can use int as default") {
let template = Template(templateString: "{{ value|default:1 }}")
let result = try template.render(Context(dictionary: [:]))
try expect(result) == "1"
}
$0.it("can use float as default") {
let template = Template(templateString: "{{ value|default:1.5 }}")
let result = try template.render(Context(dictionary: [:]))
try expect(result) == "1.5"
}
$0.it("checks for underlying nil value correctly") {
let template = Template(templateString: "Hello {{ user.name|default:\"anonymous\" }}")
let nilName: String? = nil

View File

@@ -61,7 +61,7 @@ func testVariable() {
$0.it("can resolve an integer literal") {
let variable = Variable("5")
let result = try variable.resolve(context) as? Number
let result = try variable.resolve(context) as? Int
try expect(result) == 5
}