Skip to content

Commit

Permalink
Allow to retrieve all variable names of a Scope (#496)
Browse files Browse the repository at this point in the history
* Add a test file to test GetKeys of the Scope class

* Add a GetKeys method

* Change method name to camelCase

* Fix test
  • Loading branch information
racharya authored Feb 6, 2020
1 parent 33e8b43 commit 7e536a8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package com.mitchellbosecke.pebble.template;

import java.util.HashMap;
import java.util.Set;
import java.util.Map;

/**
Expand Down Expand Up @@ -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<String> getKeys(){
return backingMap.keySet();
}
}
28 changes: 28 additions & 0 deletions pebble/src/test/java/com/mitchellbosecke/pebble/ScopeTest.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> map = new HashMap<>();
map.putIfAbsent("key1", new String("value1"));
map.putIfAbsent("key2", new String("value2"));

Scope scope = new Scope(map, false);
Set<String> expected = new HashSet<>();
expected.add("key1");
expected.add("key2");

assertEquals(expected, scope.getKeys());
}
}

0 comments on commit 7e536a8

Please sign in to comment.