Skip to content

Commit

Permalink
Update minibox filter function to support multiple tag separators (#112)
Browse files Browse the repository at this point in the history
* changed MiniBox filter function to accomidate more than 1 TAG_SEPARATOR. Updated unit test

* updated docstring

* formatting fix

* another docstring amendment

* changed MiniBox split to only split on first TAG_Separator. Added test that searches for subcode instead of code and returns empty.

* adding test that tries to use filter to access parameter with no tag separator and returns empty

* Update src/arcade/core/util/MiniBox.java

Co-authored-by: Jessica S. Yu <[email protected]>

* edited tests for clarity

* changed test to make code not first and filter on code, instead of making code first and filtering on subcode

---------

Co-authored-by: Jessica S. Yu <[email protected]>
  • Loading branch information
Jannetty and jessicasyu authored Nov 27, 2024
1 parent 4d546fa commit 600b782
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/arcade/core/util/MiniBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,19 @@ public void put(String id, String val) {
}

/**
* Filters keys by the given code.
* Filters entries for keys starting with given code.
*
* <p>Entries in the form "key = value" where key = code/subkey can be filtered. The returned
* box contains all entries in the form "subkey = value" for all entries where the code matches
* the given code.
* <p>Returns a MiniBox containing entries where the keys start with "code/" followed by any
* number of subkeys. The keys in the returned MiniBox exclude the initial "code/" prefix and
* consist only of the subkeys. The values are the same as the original MiniBox.
*
* @param code the code to filter by
* @return the filtered box
*/
public MiniBox filter(String code) {
MiniBox results = new MiniBox();
for (String key : keys) {
String[] split = key.split(TAG_SEPARATOR);
String[] split = key.split(TAG_SEPARATOR, 2);
if (split.length == 2 && split[0].equals(code)) {
results.put(split[1], contents.get(key));
}
Expand Down
32 changes: 32 additions & 0 deletions test/arcade/core/util/MiniBoxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,28 +333,60 @@ public void filter_multipleCodes_returnsFiltered() {
MiniBox box = new MiniBox();
String code1 = randomString();
String code2 = randomString();
String subcode = randomString();
String key1 = randomString();
String key2 = randomString();
String key3 = randomString();
String key4 = randomString();
String value1 = randomString();
String value2 = randomString();
String value3 = randomString();
String value4 = randomString();

box.put(key1, value1);
box.put(code1 + TAG_SEPARATOR + key2, value2);
box.put(code2 + TAG_SEPARATOR + key3, value3);
box.put(code2 + TAG_SEPARATOR + subcode + TAG_SEPARATOR + key4, value4);

ArrayList<String> filteredKeys = new ArrayList<>();
filteredKeys.add(key3);
filteredKeys.add(subcode + TAG_SEPARATOR + key4);

HashMap<String, String> filteredMap = new HashMap<>();
filteredMap.put(key3, value3);
filteredMap.put(subcode + TAG_SEPARATOR + key4, value4);

MiniBox filtered = box.filter(code2);
assertEquals(filteredKeys, filtered.keys);
assertEquals(filteredMap, filtered.contents);
}

@Test
void filter_codeNotFirst_returnsEmpty() {
MiniBox box = new MiniBox();
String code = randomString();
String subcode = randomString();
String value = randomString();

box.put(subcode + TAG_SEPARATOR + code, value);

MiniBox filtered = box.filter(code);
assertTrue(filtered.keys.isEmpty());
assertTrue(filtered.contents.isEmpty());
}

@Test
void filter_noTagSeparator_returnsEmpty() {
MiniBox box = new MiniBox();
String key = randomString();
String value = randomString();
box.put(key, value);

MiniBox filtered = box.filter(key);
assertTrue(filtered.keys.isEmpty());
assertTrue(filtered.contents.isEmpty());
}

@Test
public void compare_sameContents_returnsTrue() {
MiniBox boxA = new MiniBox();
Expand Down

0 comments on commit 600b782

Please sign in to comment.