mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-03 03:01:44 +00:00
Add removeAll to preferences (#987)
This removes all keys except for .type.
This commit is contained in:
committed by
Peter Johnson
parent
2e0709f05b
commit
f90e429bf9
@@ -222,3 +222,14 @@ bool Preferences::ContainsKey(wpi::StringRef key) {
|
||||
* @param key the key
|
||||
*/
|
||||
void Preferences::Remove(wpi::StringRef key) { m_table->Delete(key); }
|
||||
|
||||
/**
|
||||
* Remove all preferences.
|
||||
*/
|
||||
void Preferences::RemoveAll() {
|
||||
for (auto preference : GetKeys()) {
|
||||
if (preference != ".type") {
|
||||
Remove(preference);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,6 +52,7 @@ class Preferences : public ErrorBase {
|
||||
void PutLong(wpi::StringRef key, int64_t value);
|
||||
bool ContainsKey(wpi::StringRef key);
|
||||
void Remove(wpi::StringRef key);
|
||||
void RemoveAll();
|
||||
|
||||
protected:
|
||||
Preferences();
|
||||
|
||||
@@ -173,6 +173,17 @@ public class Preferences {
|
||||
m_table.delete(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all preferences.
|
||||
*/
|
||||
public void removeAll() {
|
||||
for (String key : m_table.getKeys()) {
|
||||
if (!".type".equals(key)) {
|
||||
remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string at the given key. If this table does not have a value for that position,
|
||||
* then the given backup value will be returned.
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
package edu.wpi.first.wpilibj;
|
||||
|
||||
import java.util.Arrays;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -88,6 +89,25 @@ public class PreferencesTest extends AbstractComsSetup {
|
||||
m_pref.putBoolean("checkedValueBoolean", true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Just checking to make sure our helper method works.
|
||||
*/
|
||||
@Test
|
||||
public void testRemove() {
|
||||
remove();
|
||||
assertEquals("Preferences was not empty! Preferences in table: "
|
||||
+ Arrays.toString(m_pref.getKeys().toArray()),
|
||||
1, m_pref.getKeys().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveAll() {
|
||||
m_pref.removeAll();
|
||||
assertEquals("Preferences was not empty! Preferences in table: "
|
||||
+ Arrays.toString(m_pref.getKeys().toArray()),
|
||||
1, m_pref.getKeys().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddRemoveSave() {
|
||||
assertEquals(m_pref.getLong("checkedValueLong", 0), 172L);
|
||||
|
||||
Reference in New Issue
Block a user