120 lines
4.8 KiB
Diff
120 lines
4.8 KiB
Diff
From 634c75e521140bc33af968f8b5249123eee5aebc Mon Sep 17 00:00:00 2001
|
|
From: crast <contact@jamescrasta.com>
|
|
Date: Wed, 20 Mar 2013 15:59:03 -0600
|
|
Subject: [PATCH] Substantially more comprehensive unit tests.
|
|
|
|
Check all the interesting implementation details in metadatavalues
|
|
which were never tested before, as well as making sure we document
|
|
things thoroughly.
|
|
---
|
|
.../org/bukkit/metadata/MetadataValueAdapter.java | 2 +-
|
|
.../bukkit/metadata/MetadataValueAdapterTest.java | 73 +++++++++++++++++++---
|
|
2 files changed, 64 insertions(+), 11 deletions(-)
|
|
|
|
diff --git a/src/main/java/org/bukkit/metadata/MetadataValueAdapter.java b/src/main/java/org/bukkit/metadata/MetadataValueAdapter.java
|
|
index 354b6dc..c4b8b39 100644
|
|
--- a/src/main/java/org/bukkit/metadata/MetadataValueAdapter.java
|
|
+++ b/src/main/java/org/bukkit/metadata/MetadataValueAdapter.java
|
|
@@ -9,7 +9,7 @@ import org.bukkit.util.NumberConversions;
|
|
*
|
|
* This provides all the conversion functions for MetadataValue
|
|
* so that writing an implementation of MetadataValue is as simple
|
|
- * as implementing value() and invalidate()
|
|
+ * as implementing value() and invalidate().
|
|
*
|
|
*/
|
|
public abstract class MetadataValueAdapter implements MetadataValue {
|
|
diff --git a/src/test/java/org/bukkit/metadata/MetadataValueAdapterTest.java b/src/test/java/org/bukkit/metadata/MetadataValueAdapterTest.java
|
|
index 5ae7df4..7d8a17f 100644
|
|
--- a/src/test/java/org/bukkit/metadata/MetadataValueAdapterTest.java
|
|
+++ b/src/test/java/org/bukkit/metadata/MetadataValueAdapterTest.java
|
|
@@ -10,22 +10,75 @@ public class MetadataValueAdapterTest {
|
|
private TestPlugin plugin = new TestPlugin("x");
|
|
|
|
@Test
|
|
- public void testIncrementingAdapter() {
|
|
+ public void testAdapterBasics() {
|
|
IncrementingMetaValue mv = new IncrementingMetaValue(plugin);
|
|
// check getOwningPlugin
|
|
assertEquals(mv.getOwningPlugin(), this.plugin);
|
|
|
|
- // check the various value-making methods
|
|
- assertEquals(mv.asInt(), 1);
|
|
- assertEquals(mv.asLong(), 2L);
|
|
- assertEquals(mv.asFloat(), 3.0, 0.001);
|
|
- assertEquals(mv.asByte(), 4);
|
|
- assertEquals(mv.asDouble(), 5.0, 0.001);
|
|
- assertEquals(mv.asShort(), 6);
|
|
- assertEquals(mv.asString(), "7");
|
|
+ // Check value-getting and invalidation.
|
|
+ assertEquals(new Integer(1), mv.value());
|
|
+ assertEquals(new Integer(2), mv.value());
|
|
+ mv.invalidate();
|
|
+ assertEquals(new Integer(1), mv.value());
|
|
}
|
|
|
|
- /** Silly Metadata implementation that increments every time value() is called */
|
|
+ @Test
|
|
+ public void testAdapterConversions() {
|
|
+ IncrementingMetaValue mv = new IncrementingMetaValue(plugin);
|
|
+
|
|
+ assertEquals(1, mv.asInt());
|
|
+ assertEquals(2L, mv.asLong());
|
|
+ assertEquals(3.0, mv.asFloat(), 0.001);
|
|
+ assertEquals(4, mv.asByte());
|
|
+ assertEquals(5.0, mv.asDouble(), 0.001);
|
|
+ assertEquals(6, mv.asShort());
|
|
+ assertEquals("7", mv.asString());
|
|
+ }
|
|
+
|
|
+ /** Boolean conversion is non-trivial, we want to test it thoroughly. */
|
|
+ @Test
|
|
+ public void testBooleanConversion() {
|
|
+ // null is False.
|
|
+ assertEquals(false, simpleValue(null).asBoolean());
|
|
+
|
|
+ // String to boolean.
|
|
+ assertEquals(true, simpleValue("True").asBoolean());
|
|
+ assertEquals(true, simpleValue("TRUE").asBoolean());
|
|
+ assertEquals(false, simpleValue("false").asBoolean());
|
|
+
|
|
+ // Number to boolean.
|
|
+ assertEquals(true, simpleValue(1).asBoolean());
|
|
+ assertEquals(true, simpleValue(5.0).asBoolean());
|
|
+ assertEquals(false, simpleValue(0).asBoolean());
|
|
+ assertEquals(false, simpleValue(0.1).asBoolean());
|
|
+
|
|
+ // Boolean as boolean, of course.
|
|
+ assertEquals(true, simpleValue(Boolean.TRUE).asBoolean());
|
|
+ assertEquals(false, simpleValue(Boolean.FALSE).asBoolean());
|
|
+
|
|
+ // any object that is not null and not a Boolean, String, or Number is true.
|
|
+ assertEquals(true, simpleValue(new Object()).asBoolean());
|
|
+ }
|
|
+
|
|
+ /** Test String conversions return an empty string when given null. */
|
|
+ @Test
|
|
+ public void testStringConversionNull() {
|
|
+ assertEquals("", simpleValue(null).asString());
|
|
+ }
|
|
+
|
|
+ /** Get a fixed value MetadataValue. */
|
|
+ private MetadataValue simpleValue(Object value) {
|
|
+ return new FixedMetadataValue(plugin, value);
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * A sample non-trivial MetadataValueAdapter implementation.
|
|
+ *
|
|
+ * The rationale for implementing an incrementing value is to have a value
|
|
+ * which changes with every call to value(). This is important for testing
|
|
+ * because we want to make sure all the tested conversions are calling the
|
|
+ * value() method exactly once and no caching is going on.
|
|
+ */
|
|
class IncrementingMetaValue extends MetadataValueAdapter {
|
|
private int internalValue = 0;
|
|
|
|
--
|
|
1.8.1-rc2
|
|
|