From 9af9cf40052a72e352014df92e92a50fa2c1a2b6 Mon Sep 17 00:00:00 2001 From: Kyle Fuller Date: Mon, 28 Nov 2016 05:54:08 +0000 Subject: [PATCH] fix(context): Allow removing a value at a pushed state --- Sources/Context.swift | 6 ++++-- Tests/StencilTests/ContextSpec.swift | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Sources/Context.swift b/Sources/Context.swift index 53de1a3..a75cfc9 100644 --- a/Sources/Context.swift +++ b/Sources/Context.swift @@ -1,6 +1,6 @@ /// A container for template variables. public class Context { - var dictionaries: [[String: Any]] + var dictionaries: [[String: Any?]] let namespace: Namespace /// Initialise a Context with an optional dictionary and optional namespace @@ -58,7 +58,9 @@ public class Context { for dictionary in dictionaries { for (key, value) in dictionary { - accumulator.updateValue(value, forKey: key) + if let value = value { + accumulator.updateValue(value, forKey: key) + } } } diff --git a/Tests/StencilTests/ContextSpec.swift b/Tests/StencilTests/ContextSpec.swift index 93fbd04..d90c29c 100644 --- a/Tests/StencilTests/ContextSpec.swift +++ b/Tests/StencilTests/ContextSpec.swift @@ -47,6 +47,15 @@ func testContext() { try expect(context["name"] as? String) == "Kyle" } + $0.it("allows you to remove a parent's value in a level") { + try context.push { + context["name"] = nil + try expect(context["name"]).to.beNil() + } + + try expect(context["name"] as? String) == "Kyle" + } + $0.it("allows you to push a dictionary and run a closure then restoring previous state") { var didRun = false