blob: ff92faa8d43e1e5333c43e6bd6c10efad8d7b0dd [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Googler173ee822015-05-19 09:46:33 +00002//
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.
14
15package com.google.devtools.common.options;
16
lberkibf6ef0f2017-05-29 11:00:40 +020017import static com.google.common.truth.Truth.assertThat;
jcater20e631b2019-04-29 13:29:12 -070018import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
Googler173ee822015-05-19 09:46:33 +000019
20import org.junit.Test;
21import org.junit.runner.RunWith;
22import org.junit.runners.JUnit4;
23
24/**
25 * A test for {@link BoolOrEnumConverter}.
26 */
27@RunWith(JUnit4.class)
28public class BoolOrEnumConverterTest {
29
30 private enum CompilationMode {
31 DBG, OPT
32 }
33
34 private static class CompilationModeConverter
35 extends BoolOrEnumConverter<CompilationMode> {
36
37 public CompilationModeConverter() {
38 super(CompilationMode.class, "compilation mode",
39 CompilationMode.DBG, CompilationMode.OPT);
40 }
41 }
42
43 /**
44 * The test options for the CompilationMode hybrid converter.
45 */
46 public static class CompilationModeTestOptions extends OptionsBase {
ccalvarin835e8e32017-06-29 17:05:59 +020047 @Option(
48 name = "compile_mode",
49 converter = CompilationModeConverter.class,
50 documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
51 effectTags = {OptionEffectTag.NO_OP},
52 defaultValue = "dbg"
53 )
Googler173ee822015-05-19 09:46:33 +000054 public CompilationMode compileMode;
55 }
56
57 @Test
58 public void converterFromEnum() throws Exception {
59 CompilationModeConverter converter = new CompilationModeConverter();
lberkibf6ef0f2017-05-29 11:00:40 +020060 assertThat(converter.convert("dbg")).isEqualTo(CompilationMode.DBG);
61 assertThat(converter.convert("opt")).isEqualTo(CompilationMode.OPT);
Googler173ee822015-05-19 09:46:33 +000062
jcater20e631b2019-04-29 13:29:12 -070063 OptionsParsingException e =
64 assertThrows(OptionsParsingException.class, () -> converter.convert("none"));
65 assertThat(e)
66 .hasMessageThat()
67 .isEqualTo("Not a valid compilation mode: 'none' (should be dbg or opt)");
lberkibf6ef0f2017-05-29 11:00:40 +020068 assertThat(converter.getTypeDescription()).isEqualTo("dbg or opt");
Googler173ee822015-05-19 09:46:33 +000069 }
70
71 @Test
72 public void convertFromBooleanValues() throws Exception {
73 String[] falseValues = new String[]{"false", "0"};
74 String[] trueValues = new String[]{"true", "1"};
75 CompilationModeConverter converter = new CompilationModeConverter();
76
77 for (String falseValue : falseValues) {
lberkibf6ef0f2017-05-29 11:00:40 +020078 assertThat(converter.convert(falseValue)).isEqualTo(CompilationMode.OPT);
Googler173ee822015-05-19 09:46:33 +000079 }
80
81 for (String trueValue : trueValues) {
lberkibf6ef0f2017-05-29 11:00:40 +020082 assertThat(converter.convert(trueValue)).isEqualTo(CompilationMode.DBG);
Googler173ee822015-05-19 09:46:33 +000083 }
84 }
85
86 @Test
87 public void prefixedWithNo() throws OptionsParsingException {
jcater706a22e2019-06-17 09:56:03 -070088 OptionsParser parser =
89 OptionsParser.builder().optionsClasses(CompilationModeTestOptions.class).build();
Googler173ee822015-05-19 09:46:33 +000090 parser.parse("--nocompile_mode");
91 CompilationModeTestOptions options =
92 parser.getOptions(CompilationModeTestOptions.class);
lberkibf6ef0f2017-05-29 11:00:40 +020093 assertThat(options.compileMode).isNotNull();
94 assertThat(options.compileMode).isEqualTo(CompilationMode.OPT);
Googler173ee822015-05-19 09:46:33 +000095 }
96
97 @Test
98 public void missingValueAsBoolConversion() throws OptionsParsingException {
jcater706a22e2019-06-17 09:56:03 -070099 OptionsParser parser =
100 OptionsParser.builder().optionsClasses(CompilationModeTestOptions.class).build();
Googler173ee822015-05-19 09:46:33 +0000101 parser.parse("--compile_mode");
102 CompilationModeTestOptions options =
103 parser.getOptions(CompilationModeTestOptions.class);
lberkibf6ef0f2017-05-29 11:00:40 +0200104 assertThat(options.compileMode).isNotNull();
105 assertThat(options.compileMode).isEqualTo(CompilationMode.DBG);
Googler173ee822015-05-19 09:46:33 +0000106 }
107
108}