From 7e536a8894c13c1a6a3912651312030330c89a84 Mon Sep 17 00:00:00 2001 From: Rachana Acharya Date: Thu, 6 Feb 2020 13:32:59 -0700 Subject: [PATCH] Allow to retrieve all variable names of a Scope (#496) * Add a test file to test GetKeys of the Scope class * Add a GetKeys method * Change method name to camelCase * Fix test --- .../pebble/template/Scope.java | 10 +++++++ .../com/mitchellbosecke/pebble/ScopeTest.java | 28 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 pebble/src/test/java/com/mitchellbosecke/pebble/ScopeTest.java diff --git a/pebble/src/main/java/com/mitchellbosecke/pebble/template/Scope.java b/pebble/src/main/java/com/mitchellbosecke/pebble/template/Scope.java index 8b09a5d99..b16001753 100644 --- a/pebble/src/main/java/com/mitchellbosecke/pebble/template/Scope.java +++ b/pebble/src/main/java/com/mitchellbosecke/pebble/template/Scope.java @@ -9,6 +9,7 @@ package com.mitchellbosecke.pebble.template; import java.util.HashMap; +import java.util.Set; import java.util.Map; /** @@ -96,4 +97,13 @@ public boolean containsKey(String key) { public boolean isLocal() { return this.local; } + + /** + * Returns keys of all the variables at this scope. + * + * @return A set of keys + */ + public Set getKeys(){ + return backingMap.keySet(); + } } diff --git a/pebble/src/test/java/com/mitchellbosecke/pebble/ScopeTest.java b/pebble/src/test/java/com/mitchellbosecke/pebble/ScopeTest.java new file mode 100644 index 000000000..8ef59acb9 --- /dev/null +++ b/pebble/src/test/java/com/mitchellbosecke/pebble/ScopeTest.java @@ -0,0 +1,28 @@ +package com.mitchellbosecke.pebble; + +import com.mitchellbosecke.pebble.template.Scope; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class ScopeTest { + + @Test + void testGetKeys() { + Map map = new HashMap<>(); + map.putIfAbsent("key1", new String("value1")); + map.putIfAbsent("key2", new String("value2")); + + Scope scope = new Scope(map, false); + Set expected = new HashSet<>(); + expected.add("key1"); + expected.add("key2"); + + assertEquals(expected, scope.getKeys()); + } +}