blob: 67919c782741fd4c7193fcbcaa6528ae98db47f7 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.common.options;
15
lberkibf6ef0f2017-05-29 11:00:40 +020016import static com.google.common.truth.Truth.assertThat;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010017import static org.junit.Assert.fail;
18
19import com.google.devtools.common.options.Converters.LogLevelConverter;
lberkibf6ef0f2017-05-29 11:00:40 +020020import java.util.logging.Level;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010021import org.junit.Test;
22import org.junit.runner.RunWith;
23import org.junit.runners.JUnit4;
24
lberkibf6ef0f2017-05-29 11:00:40 +020025/** A test for {@link LogLevelConverter}. */
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010026@RunWith(JUnit4.class)
27public class LogLevelConverterTest {
28
29 private LogLevelConverter converter = new LogLevelConverter();
30
31 @Test
32 public void convertsIntsToLevels() throws OptionsParsingException {
33 int levelId = 0;
34 for (Level level : LogLevelConverter.LEVELS) {
lberkibf6ef0f2017-05-29 11:00:40 +020035 assertThat(converter.convert(Integer.toString(levelId++))).isEqualTo(level);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010036 }
37 }
38
39 @Test
40 public void throwsExceptionWhenInputIsNotANumber() {
41 try {
42 converter.convert("oops - not a number.");
43 fail();
44 } catch (OptionsParsingException e) {
lberkibf6ef0f2017-05-29 11:00:40 +020045 assertThat(e).hasMessageThat().isEqualTo("Not a log level: oops - not a number.");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010046 }
47 }
48
49 @Test
50 public void throwsExceptionWhenInputIsInvalidInteger() {
51 for (int example : new int[] {-1, 100, 50000}) {
52 try {
53 converter.convert(Integer.toString(example));
54 fail();
55 } catch (OptionsParsingException e) {
56 String expected = "Not a log level: " + Integer.toString(example);
lberkibf6ef0f2017-05-29 11:00:40 +020057 assertThat(e).hasMessageThat().isEqualTo(expected);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010058 }
59 }
60 }
61
62}