From 5c241d916ae246cb9b008d1dcde22a0e15daa3f3 Mon Sep 17 00:00:00 2001 From: David Jennes Date: Wed, 22 Feb 2017 15:18:03 +0100 Subject: [PATCH] Add tests --- Tests/StencilTests/VariableSpec.swift | 68 +++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/Tests/StencilTests/VariableSpec.swift b/Tests/StencilTests/VariableSpec.swift index d66dc1a4..1a680b82 100644 --- a/Tests/StencilTests/VariableSpec.swift +++ b/Tests/StencilTests/VariableSpec.swift @@ -115,4 +115,72 @@ func testVariable() { } #endif } + + describe("CompoundVariable") { + let context = Context(dictionary: [ + "name": "Kyle", + "a": 3, + "x": 20 + ]) + + $0.it("Falls back to default behaviour for strings") { + let variable = CompoundVariable("\"name\"") + let result = try variable.resolve(context) as? String + try expect(result) == "name" + } + + $0.it("Falls back to default behaviour for numbers") { + let variable = CompoundVariable("5") + let result = try variable.resolve(context) as? Number + try expect(result) == 5 + } + + $0.it("Falls back to default behaviour for resolvables") { + let variable = CompoundVariable("name") + let result = try variable.resolve(context) as? String + try expect(result) == "Kyle" + } + + $0.it("Unresolvables still produce nil") { + let variable = CompoundVariable("something") + let result = try variable.resolve(context) + try expect(result).to.beNil() + } + + $0.it("Can add two numbers") { + let variable = CompoundVariable("1 + 2") + let result = try variable.resolve(context) as? Number + try expect(result) == 3 + } + + $0.it("Can substract two numbers") { + let variable = CompoundVariable("2 - 4") + let result = try variable.resolve(context) as? Number + try expect(result) == -2 + } + + $0.it("Can multiply two numbers") { + let variable = CompoundVariable("-2 * -4") + let result = try variable.resolve(context) as? Number + try expect(result) == 8 + } + + $0.it("Can divide two numbers") { + let variable = CompoundVariable("4 / 2") + let result = try variable.resolve(context) as? Number + try expect(result) == 2 + } + + $0.it("Can resolve variables") { + let variable = CompoundVariable("a * x") + let result = try variable.resolve(context) as? Number + try expect(result) == 60 + } + + $0.it("Can process a complex expression") { + let variable = CompoundVariable("1 + 2 * 3 - (4 + 6) / 5") + let result = try variable.resolve(context) as? Number + try expect(result) == 5 + } + } }