Move unit tests from integration test suite (#1170)

This commit is contained in:
Austin Shalit
2019-07-16 01:02:52 -04:00
committed by Peter Johnson
parent dffa1a5cba
commit e488861877
12 changed files with 445 additions and 414 deletions

View File

@@ -1,144 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.logging.Logger;
import org.junit.Before;
import org.junit.Test;
import edu.wpi.first.wpilibj.networktables.NetworkTable;
import edu.wpi.first.wpilibj.test.AbstractComsSetup;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Tests the {@link Preferences}.
*/
public class PreferencesTest extends AbstractComsSetup {
private static final Logger logger = Logger.getLogger(PreferencesTest.class.getName());
private NetworkTable m_prefTable;
private Preferences m_pref;
private long m_check;
@Override
protected Logger getClassLogger() {
return logger;
}
@Before
public void setUp() throws Exception {
NetworkTable.shutdown();
try {
File file = new File("networktables.ini");
file.mkdirs();
if (file.exists()) {
file.delete();
}
file.createNewFile();
OutputStream output = new FileOutputStream(file);
output
.write(("[NetworkTables Storage 3.0]\ndouble \"/Preferences/checkedValueInt\"=2\ndouble "
+ "\"/Preferences/checkedValueDouble\"=.2\ndouble "
+ "\"/Preferences/checkedValueFloat\"=3.14\ndouble "
+ "\"/Preferences/checkedValueLong\"=172\nstring "
+ "\"/Preferences/checkedValueString\"=\"hello \\nHow are you ?\"\nboolean "
+ "\"/Preferences/checkedValueBoolean\"=false\n")
.getBytes());
} catch (IOException ex) {
ex.printStackTrace();
}
NetworkTable.initialize();
m_pref = Preferences.getInstance();
m_prefTable = NetworkTable.getTable("Preferences");
m_check = System.currentTimeMillis();
}
protected void remove() {
m_pref.remove("checkedValueLong");
m_pref.remove("checkedValueDouble");
m_pref.remove("checkedValueString");
m_pref.remove("checkedValueInt");
m_pref.remove("checkedValueFloat");
m_pref.remove("checkedValueBoolean");
}
protected void addCheckedValue() {
m_pref.putLong("checkedValueLong", m_check);
m_pref.putDouble("checkedValueDouble", 1);
m_pref.putString("checkedValueString", "checked");
m_pref.putInt("checkedValueInt", 1);
m_pref.putFloat("checkedValueFloat", 1);
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);
assertEquals(m_pref.getDouble("checkedValueDouble", 0), .2, 0);
assertEquals(m_pref.getString("checkedValueString", ""), "hello \nHow are you ?");
assertEquals(m_pref.getInt("checkedValueInt", 0), 2);
assertEquals(m_pref.getFloat("checkedValueFloat", 0), 3.14, .001);
assertFalse(m_pref.getBoolean("checkedValueBoolean", true));
remove();
assertEquals(m_pref.getLong("checkedValueLong", 0), 0);
assertEquals(m_pref.getDouble("checkedValueDouble", 0), 0, 0);
assertEquals(m_pref.getString("checkedValueString", ""), "");
assertEquals(m_pref.getInt("checkedValueInt", 0), 0);
assertEquals(m_pref.getFloat("checkedValueFloat", 0), 0, 0);
assertFalse(m_pref.getBoolean("checkedValueBoolean", false));
addCheckedValue();
assertEquals(m_check, m_pref.getLong("checkedValueLong", 0));
assertEquals(m_pref.getDouble("checkedValueDouble", 0), 1, 0);
assertEquals(m_pref.getString("checkedValueString", ""), "checked");
assertEquals(m_pref.getInt("checkedValueInt", 0), 1);
assertEquals(m_pref.getFloat("checkedValueFloat", 0), 1, 0);
assertTrue(m_pref.getBoolean("checkedValueBoolean", false));
}
@Test
public void testPreferencesToNetworkTables() {
String networkedNumber = "networkCheckedValue";
int networkNumberValue = 100;
m_pref.putInt(networkedNumber, networkNumberValue);
assertEquals(networkNumberValue, (int) (m_prefTable.getNumber(networkedNumber, 9999999)));
m_pref.remove(networkedNumber);
}
}

View File

@@ -22,7 +22,7 @@ import edu.wpi.first.wpilibj.test.AbstractTestSuite;
BuiltInAccelerometerTest.class, ConstantsPortsTest.class, CounterTest.class,
DigitalGlitchFilterTest.class, DIOCrossConnectTest.class, DriveTest.class,
DriverStationTest.class, EncoderTest.class, GyroTest.class, MotorEncoderTest.class,
MotorInvertingTest.class, PCMTest.class, PDPTest.class, PIDTest.class, PreferencesTest.class,
MotorInvertingTest.class, PCMTest.class, PDPTest.class, PIDTest.class,
RelayCrossConnectTest.class, SampleTest.class, TimerTest.class})
public class WpiLibJTestSuite extends AbstractTestSuite {
}

View File

@@ -1,89 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.smartdashboard;
import java.util.logging.Logger;
import org.junit.Ignore;
import org.junit.Test;
import edu.wpi.first.wpilibj.networktables.NetworkTable;
import edu.wpi.first.wpilibj.test.AbstractComsSetup;
import static org.junit.Assert.assertEquals;
/**
* Test that covers {@link SmartDashboard}.
*/
public class SmartDashboardTest extends AbstractComsSetup {
private static final Logger logger = Logger.getLogger(SmartDashboardTest.class.getName());
private static final NetworkTable table = NetworkTable.getTable("SmartDashboard");
@Override
protected Logger getClassLogger() {
return logger;
}
@Test
public void testGetBadValue() {
assertEquals("", SmartDashboard.getString("_404_STRING_KEY_SHOULD_NOT_BE_FOUND_", ""));
}
@Test
public void testPutString() {
String key = "testPutString";
String value = "thisIsAValue";
SmartDashboard.putString(key, value);
assertEquals(value, SmartDashboard.getString(key, ""));
assertEquals(value, table.getString(key, ""));
}
@Test
public void testPutNumber() {
String key = "testPutNumber";
int value = 2147483647;
SmartDashboard.putNumber(key, value);
assertEquals(value, SmartDashboard.getNumber(key, 0), 0.01);
assertEquals(value, table.getNumber(key, 0), 0.01);
}
@Test
public void testPutBoolean() {
String key = "testPutBoolean";
boolean value = true;
SmartDashboard.putBoolean(key, value);
assertEquals(value, SmartDashboard.getBoolean(key, !value));
assertEquals(value, table.getBoolean(key, false));
}
@Test
public void testReplaceString() {
String key = "testReplaceString";
String valueOld = "oldValue";
SmartDashboard.putString(key, valueOld);
assertEquals(valueOld, SmartDashboard.getString(key, ""));
assertEquals(valueOld, table.getString(key, ""));
String valueNew = "newValue";
SmartDashboard.putString(key, valueNew);
assertEquals(valueNew, SmartDashboard.getString(key, ""));
assertEquals(valueNew, table.getString(key, ""));
}
@Ignore
@Test(expected = IllegalArgumentException.class)
public void testPutStringNullKey() {
SmartDashboard.putString(null, "This should not work");
}
@Ignore
@Test(expected = IllegalArgumentException.class)
public void testPutStringNullValue() {
SmartDashboard.putString("KEY_SHOULD_NOT_BE_STORED", null);
}
}

View File

@@ -1,22 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.smartdashboard;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import edu.wpi.first.wpilibj.test.AbstractTestSuite;
/**
* All tests pertaining to {@link SmartDashboard}.
*/
@RunWith(Suite.class)
@SuiteClasses({SmartDashboardTest.class})
public class SmartDashboardTestSuite extends AbstractTestSuite {
}

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -28,7 +28,6 @@ import junit.framework.JUnit4TestAdapter;
import junit.runner.Version;
import edu.wpi.first.wpilibj.WpiLibJTestSuite;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboardTestSuite;
/**
* The WPILibJ Integeration Test Suite collects all of the tests to be run by junit. In order for a
@@ -36,8 +35,7 @@ import edu.wpi.first.wpilibj.smartdashboard.SmartDashboardTestSuite;
* order they are listed in the suite classes annotation.
*/
@RunWith(Suite.class)
// These are listed on separate lines to prevent merge conflicts
@SuiteClasses({WpiLibJTestSuite.class, SmartDashboardTestSuite.class})
@SuiteClasses(WpiLibJTestSuite.class)
public class TestSuite extends AbstractTestSuite {
static {
// Sets up the logging output