diff options
author | michajlo <michajlo@google.com> | 2017-10-19 21:04:09 +0200 |
---|---|---|
committer | Damien Martin-Guillerez <dmarting@google.com> | 2017-10-20 14:04:05 +0200 |
commit | 1e65fee5b3f306db2cc3babc370a0b9dd2bb800d (patch) | |
tree | f9dc187fe950347d83e1dc8f54bd0e7be9a3963c /src/test/java/com/google/devtools/build/lib | |
parent | 8b5bf1f440ef8d140eb3843861fbce65709438ea (diff) |
Add ObjectCodecTester, migrating a few tests to use it
This provides a composition-based alternative to the existing inheritance-based
testing style. The inheritance style has been showing its age/has become quite
cumbersome when multiple codecs are in the same class or codecs are colocated
with their encoded class. This will hopefully get rid of some friction when
adding new codecs.
RELNOTES: None
PiperOrigin-RevId: 172778555
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib')
2 files changed, 33 insertions, 31 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/strings/FastStringCodecTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/strings/FastStringCodecTest.java index 30b9a1ded2..c0ae7d02ee 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/strings/FastStringCodecTest.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/strings/FastStringCodecTest.java @@ -15,41 +15,39 @@ package com.google.devtools.build.lib.skyframe.serialization.strings; import com.google.common.testing.EqualsTester; -import com.google.devtools.build.lib.skyframe.serialization.testutils.AbstractObjectCodecTest; +import com.google.devtools.build.lib.skyframe.serialization.testutils.ObjectCodecTester; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; /** Tests for {@link FastStringCodec}. */ @RunWith(JUnit4.class) -public class FastStringCodecTest extends AbstractObjectCodecTest<String> { +public class FastStringCodecTest { - public FastStringCodecTest() { - super( - // TODO(michajlo): Don't bother running this test if FastStringCodec isn't available. - FastStringCodec.isAvailable() ? new FastStringCodec() : new StringCodec(), - "ow now brown cow. ow now brown cow", - "(╯°□°)╯︵┻━┻ string with utf8/ascii", - "string with ascii/utf8 (╯°□°)╯︵┻━┻", - "last character utf8 ╯", - "last char only non-ascii ƒ", - "ƒ", - "╯", - "", - Character.toString((char) 0xc3));; - } - - // hashCode is stored in String. Because we're using Unsafe to bypass standard String - // constructors, make sure it still works. @Test - public void testEqualsAndHashCodePreserved() throws Exception { - String original1 = "hello world"; - String original2 = "dlrow olleh"; + public void testCodec() throws Exception { + if (!FastStringCodec.isAvailable()) { + // Not available on this platform, skip test. + return; + } - // Equals tester tests equals and hash code. - new EqualsTester() - .addEqualityGroup(original1, fromBytes(toBytes(original1))) - .addEqualityGroup(original2, fromBytes(toBytes(original2))) - .testEquals(); + ObjectCodecTester.newBuilder(new FastStringCodec()) + .verificationFunction( + (original, deserialized) -> { + // hashCode is stored in String. Because we're using Unsafe to bypass standard String + // constructors, make sure it still works. + new EqualsTester().addEqualityGroup(original, deserialized).testEquals(); + }) + .addSubjects( + "ow now brown cow. ow now brown cow", + "(╯°□°)╯︵┻━┻ string with utf8/ascii", + "string with ascii/utf8 (╯°□°)╯︵┻━┻", + "last character utf8 ╯", + "last char only non-ascii ƒ", + "ƒ", + "╯", + "", + Character.toString((char) 0xc3)) + .buildAndRunTests(); } } diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecTest.java index c1b6f5dbe3..8e8b80ea0b 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecTest.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/strings/StringCodecTest.java @@ -14,15 +14,19 @@ package com.google.devtools.build.lib.skyframe.serialization.strings; -import com.google.devtools.build.lib.skyframe.serialization.testutils.AbstractObjectCodecTest; +import com.google.devtools.build.lib.skyframe.serialization.testutils.ObjectCodecTester; +import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; /** Basic tests for {@link StringCodec}. */ @RunWith(JUnit4.class) -public class StringCodecTest extends AbstractObjectCodecTest<String> { +public class StringCodecTest { - public StringCodecTest() { - super(new StringCodec(), "usually precomputed and supports weird unicodes: (╯°□°)╯︵┻━┻ "); + @Test + public void testCodec() throws Exception { + ObjectCodecTester.newBuilder(new StringCodec()) + .addSubjects("usually precomputed and supports weird unicodes: (╯°□°)╯︵┻━┻ ") + .buildAndRunTests(); } } |