Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to retrieve all variable names of a Scope #496

Merged
merged 4 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}