Skip to content

Commit

Permalink
Merge pull request #181 from kylef/fix-float-from-int
Browse files Browse the repository at this point in the history
Fix creating float from int
  • Loading branch information
ilyapuchka authored Jan 1, 2018
2 parents c0e66eb + a6dba67 commit 2e80f70
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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
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 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
}

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
10 changes: 5 additions & 5 deletions Tests/StencilTests/ForNodeSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ func testForNode() {

$0.it("can iterate over dictionary") {
let templateString = "{% for key,value in dict %}" +
"{{ key }}: {{ value }}\n" +
"{% endfor %}\n"
"{{ key }}: {{ value }}," +
"{% endfor %}"

let template = Template(templateString: templateString)
let result = try template.render(context)

let sortedResult = result.split(separator: "\n").sorted(by: <)
let sortedResult = result.characters.split(separator: ",").map(String.init).sorted(by: <)
try expect(sortedResult) == ["one: I", "two: II"]
}

Expand All @@ -148,7 +148,7 @@ func testForNode() {
let node = ForNode(resolvable: Variable("dict"), loopVariables: ["key"], nodes: nodes, emptyNodes: emptyNodes, where: nil)
let result = try node.render(context)

let sortedResult = result.split(separator: ",").sorted(by: <)
let sortedResult = result.characters.split(separator: ",").map(String.init).sorted(by: <)
try expect(sortedResult) == ["one", "two"]
}

Expand All @@ -164,7 +164,7 @@ func testForNode() {

let result = try node.render(context)

let sortedResult = result.split(separator: ",").sorted(by: <)
let sortedResult = result.characters.split(separator: ",").map(String.init).sorted(by: <)
try expect(sortedResult) == ["one=I", "two=II"]
}

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 2e80f70

Please sign in to comment.