// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.util;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assert_;
import static com.google.gerrit.server.util.LabelVote.parse;
import static com.google.gerrit.server.util.LabelVote.parseWithEquals;

import org.junit.Test;

public class LabelVoteTest {
  @Test
  public void labelVoteParse() {
    assertLabelVoteEquals(parse("Code-Review-2"), "Code-Review", -2);
    assertLabelVoteEquals(parse("Code-Review-1"), "Code-Review", -1);
    assertLabelVoteEquals(parse("-Code-Review"), "Code-Review", 0);
    assertLabelVoteEquals(parse("Code-Review"), "Code-Review", 1);
    assertLabelVoteEquals(parse("Code-Review+1"), "Code-Review", 1);
    assertLabelVoteEquals(parse("Code-Review+2"), "Code-Review", 2);
  }

  @Test
  public void labelVoteFormat() {
    assertThat(parse("Code-Review-2").format()).isEqualTo("Code-Review-2");
    assertThat(parse("Code-Review-1").format()).isEqualTo("Code-Review-1");
    assertThat(parse("-Code-Review").format()).isEqualTo("-Code-Review");
    assertThat(parse("Code-Review+1").format()).isEqualTo("Code-Review+1");
    assertThat(parse("Code-Review+2").format()).isEqualTo("Code-Review+2");
  }

  @Test
  public void labelVoteParseWithEquals() {
    assertLabelVoteEquals(parseWithEquals("Code-Review=-2"), "Code-Review", -2);
    assertLabelVoteEquals(parseWithEquals("Code-Review=-1"), "Code-Review", -1);
    assertLabelVoteEquals(parseWithEquals("Code-Review=0"), "Code-Review", 0);
    assertLabelVoteEquals(parseWithEquals("Code-Review=1"), "Code-Review", 1);
    assertLabelVoteEquals(parseWithEquals("Code-Review=+1"), "Code-Review", 1);
    assertLabelVoteEquals(parseWithEquals("Code-Review=2"), "Code-Review", 2);
    assertLabelVoteEquals(parseWithEquals("Code-Review=+2"), "Code-Review", 2);
    assertLabelVoteEquals(parseWithEquals("R=0"), "R", 0);

    String longName = "A-loooooooooooooooooooooooooooooooooooooooooooooooooong-label";
    // Regression test: an old bug passed the string length as a radix to Short#parseShort.
    assertThat(longName.length()).isGreaterThan(Character.MAX_RADIX);
    assertLabelVoteEquals(parseWithEquals(longName + "=+1"), longName, 1);

    assertParseWithEqualsFails(null);
    assertParseWithEqualsFails("");
    assertParseWithEqualsFails("Code-Review");
    assertParseWithEqualsFails("=1");
    assertParseWithEqualsFails("=.1");
    assertParseWithEqualsFails("=a1");
    assertParseWithEqualsFails("=1a");
    assertParseWithEqualsFails("=.");
  }

  @Test
  public void labelVoteFormatWithEquals() {
    assertThat(parseWithEquals("Code-Review=-2").formatWithEquals()).isEqualTo("Code-Review=-2");
    assertThat(parseWithEquals("Code-Review=-1").formatWithEquals()).isEqualTo("Code-Review=-1");
    assertThat(parseWithEquals("Code-Review=0").formatWithEquals()).isEqualTo("Code-Review=0");
    assertThat(parseWithEquals("Code-Review=+1").formatWithEquals()).isEqualTo("Code-Review=+1");
    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) {
    try {
      parseWithEquals(value);
      assert_().fail("expected IllegalArgumentException when parsing \"%s\"", value);
    } catch (IllegalArgumentException e) {
      // Expected.
    }
  }
}

@@ -702,61 +702,8 @@
 at;%0A
-import static com.google.common.truth.Truth.assert_;%0A
 impo
@@ -829,16 +829,82 @@
 hEquals;
+%0Aimport static com.google.gerrit.testing.GerritJUnit.assertThrows;
 %0A%0Aimport
@@ -3982,197 +3982,83 @@
     
-try %7B%0A      parseWithEquals(value);%0A      assert_().fail(%22expected IllegalArgumentException when parsing %5C%22%25s%5C%22%22, value);%0A    %7D catch (IllegalArgumentException e) %7B%0A      // Expected.%0A    %7D
+assertThrows(IllegalArgumentException.class, () -%3E parseWithEquals(value));
 %0A  %7D

Migrate from try-catch-fail to assertThrows