LabelVoteTest: Factor out some methods & use Truth

Change-Id: I90d0ee1cd5d75429244d06ff6487d6d3d7b97428
This commit is contained in:
Dave Borowitz
2017-08-24 07:42:40 -04:00
parent 5af68039fd
commit d9a5bf3936

View File

@@ -14,79 +14,48 @@
package com.google.gerrit.server.util; package com.google.gerrit.server.util;
import static org.junit.Assert.assertEquals; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertTrue; import static com.google.common.truth.Truth.assert_;
import static org.junit.Assert.fail; import static com.google.gerrit.server.util.LabelVote.parse;
import static com.google.gerrit.server.util.LabelVote.parseWithEquals;
import org.junit.Test; import org.junit.Test;
public class LabelVoteTest { public class LabelVoteTest {
@Test @Test
public void parse() { public void labelVoteParse() {
LabelVote l; assertLabelVoteEquals(parse("Code-Review-2"), "Code-Review", -2);
l = LabelVote.parse("Code-Review-2"); assertLabelVoteEquals(parse("Code-Review-1"), "Code-Review", -1);
assertEquals("Code-Review", l.label()); assertLabelVoteEquals(parse("-Code-Review"), "Code-Review", 0);
assertEquals((short) -2, l.value()); assertLabelVoteEquals(parse("Code-Review"), "Code-Review", 1);
l = LabelVote.parse("Code-Review-1"); assertLabelVoteEquals(parse("Code-Review+1"), "Code-Review", 1);
assertEquals("Code-Review", l.label()); assertLabelVoteEquals(parse("Code-Review+2"), "Code-Review", 2);
assertEquals((short) -1, l.value());
l = LabelVote.parse("-Code-Review");
assertEquals("Code-Review", l.label());
assertEquals((short) 0, l.value());
l = LabelVote.parse("Code-Review");
assertEquals("Code-Review", l.label());
assertEquals((short) 1, l.value());
l = LabelVote.parse("Code-Review+1");
assertEquals("Code-Review", l.label());
assertEquals((short) 1, l.value());
l = LabelVote.parse("Code-Review+2");
assertEquals("Code-Review", l.label());
assertEquals((short) 2, l.value());
} }
@Test @Test
public void format() { public void labelVoteFormat() {
assertEquals("Code-Review-2", LabelVote.parse("Code-Review-2").format()); assertThat(parse("Code-Review-2").format()).isEqualTo("Code-Review-2");
assertEquals("Code-Review-1", LabelVote.parse("Code-Review-1").format()); assertThat(parse("Code-Review-1").format()).isEqualTo("Code-Review-1");
assertEquals("-Code-Review", LabelVote.parse("-Code-Review").format()); assertThat(parse("-Code-Review").format()).isEqualTo("-Code-Review");
assertEquals("Code-Review+1", LabelVote.parse("Code-Review+1").format()); assertThat(parse("Code-Review+1").format()).isEqualTo("Code-Review+1");
assertEquals("Code-Review+2", LabelVote.parse("Code-Review+2").format()); assertThat(parse("Code-Review+2").format()).isEqualTo("Code-Review+2");
} }
@Test @Test
public void parseWithEquals() { public void labelVoteParseWithEquals() {
LabelVote l; assertLabelVoteEquals(parseWithEquals("Code-Review=-2"), "Code-Review", -2);
l = LabelVote.parseWithEquals("Code-Review=-2"); assertLabelVoteEquals(parseWithEquals("Code-Review=-1"), "Code-Review", -1);
assertEquals("Code-Review", l.label()); assertLabelVoteEquals(parseWithEquals("Code-Review=0"), "Code-Review", 0);
assertEquals((short) -2, l.value()); assertLabelVoteEquals(parseWithEquals("Code-Review=1"), "Code-Review", 1);
l = LabelVote.parseWithEquals("Code-Review=-1"); assertLabelVoteEquals(parseWithEquals("Code-Review=+1"), "Code-Review", 1);
assertEquals("Code-Review", l.label()); assertLabelVoteEquals(parseWithEquals("Code-Review=2"), "Code-Review", 2);
assertEquals((short) -1, l.value()); assertLabelVoteEquals(parseWithEquals("Code-Review=+2"), "Code-Review", 2);
l = LabelVote.parseWithEquals("Code-Review=0"); assertLabelVoteEquals(parseWithEquals("R=0"), "R", 0);
assertEquals("Code-Review", l.label());
assertEquals((short) 0, l.value());
l = LabelVote.parseWithEquals("Code-Review=1");
assertEquals("Code-Review", l.label());
assertEquals((short) 1, l.value());
l = LabelVote.parseWithEquals("Code-Review=+1");
assertEquals("Code-Review", l.label());
assertEquals((short) 1, l.value());
l = LabelVote.parseWithEquals("Code-Review=2");
assertEquals("Code-Review", l.label());
assertEquals((short) 2, l.value());
l = LabelVote.parseWithEquals("Code-Review=+2");
assertEquals("Code-Review", l.label());
assertEquals((short) 2, l.value());
l = LabelVote.parseWithEquals("R=0");
assertEquals("R", l.label());
assertEquals((short) 0, l.value());
String longName = "A-loooooooooooooooooooooooooooooooooooooooooooooooooong-label"; String longName = "A-loooooooooooooooooooooooooooooooooooooooooooooooooong-label";
// Regression test: an old bug passed the string length as a radix to Short#parseShort. // Regression test: an old bug passed the string length as a radix to Short#parseShort.
assertTrue(longName.length() > Character.MAX_RADIX); assertThat(longName.length()).isGreaterThan(Character.MAX_RADIX);
l = LabelVote.parseWithEquals(longName + "=+1"); assertLabelVoteEquals(parseWithEquals(longName + "=+1"), longName, 1);
assertEquals(longName, l.label());
assertEquals((short) 1, l.value());
assertParseWithEqualsFails(null); assertParseWithEqualsFails(null);
assertParseWithEqualsFails(""); assertParseWithEqualsFails("");
@@ -99,18 +68,23 @@ public class LabelVoteTest {
} }
@Test @Test
public void formatWithEquals() { public void labelVoteFormatWithEquals() {
assertEquals("Code-Review=-2", LabelVote.parseWithEquals("Code-Review=-2").formatWithEquals()); assertThat(parseWithEquals("Code-Review=-2").formatWithEquals()).isEqualTo("Code-Review=-2");
assertEquals("Code-Review=-1", LabelVote.parseWithEquals("Code-Review=-1").formatWithEquals()); assertThat(parseWithEquals("Code-Review=-1").formatWithEquals()).isEqualTo("Code-Review=-1");
assertEquals("Code-Review=0", LabelVote.parseWithEquals("Code-Review=0").formatWithEquals()); assertThat(parseWithEquals("Code-Review=0").formatWithEquals()).isEqualTo("Code-Review=0");
assertEquals("Code-Review=+1", LabelVote.parseWithEquals("Code-Review=+1").formatWithEquals()); assertThat(parseWithEquals("Code-Review=+1").formatWithEquals()).isEqualTo("Code-Review=+1");
assertEquals("Code-Review=+2", LabelVote.parseWithEquals("Code-Review=+2").formatWithEquals()); assertThat(parseWithEquals("Code-Review=+2").formatWithEquals()).isEqualTo("Code-Review=+2");
}
private void assertLabelVoteEquals(LabelVote actual, String expectedLabel, int expectedValue) {
assertThat(actual.label()).isEqualTo(expectedLabel);
assertThat((int) actual.value()).isEqualTo(expectedValue);
} }
private void assertParseWithEqualsFails(String value) { private void assertParseWithEqualsFails(String value) {
try { try {
LabelVote.parseWithEquals(value); parseWithEquals(value);
fail("expected IllegalArgumentException when parsing \"" + value + "\""); assert_().fail("expected IllegalArgumentException when parsing \"%s\"", value);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// Expected. // Expected.
} }