Skip to content

Commit

Permalink
fixed implicit conversion of integer literals to float
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyapuchka committed Dec 29, 2017
1 parent 74e79d4 commit d75b8c8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

- Fixed rendering `{{ block.super }}` with several levels of inheritance
- Fixed checking dictionary values for nil in `default` filter
- Integer literals now resolve into Int values, not Float


## 0.10.1
Expand Down
5 changes: 4 additions & 1 deletion Sources/Variable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ public struct Variable : Equatable, Resolvable {
return 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
}

Expand Down
14 changes: 13 additions & 1 deletion Tests/StencilTests/FilterSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,19 @@ func testFilter() {
let result = try template.render(Context(dictionary: [:]))
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
Expand Down
2 changes: 1 addition & 1 deletion Tests/StencilTests/VariableSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit d75b8c8

Please sign in to comment.