| Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
| Googler | 173ee82 | 2015-05-19 09:46:33 +0000 | [diff] [blame] | 2 | // |
| 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 | |
| 15 | package com.google.devtools.common.options; |
| 16 | |
| lberki | bf6ef0f | 2017-05-29 11:00:40 +0200 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
| jcater | 20e631b | 2019-04-29 13:29:12 -0700 | [diff] [blame] | 18 | import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows; |
| Googler | 173ee82 | 2015-05-19 09:46:33 +0000 | [diff] [blame] | 19 | |
| 20 | import org.junit.Test; |
| 21 | import org.junit.runner.RunWith; |
| 22 | import org.junit.runners.JUnit4; |
| 23 | |
| 24 | /** |
| 25 | * A test for {@link BoolOrEnumConverter}. |
| 26 | */ |
| 27 | @RunWith(JUnit4.class) |
| 28 | public 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 { |
| ccalvarin | 835e8e3 | 2017-06-29 17:05:59 +0200 | [diff] [blame] | 47 | @Option( |
| 48 | name = "compile_mode", |
| 49 | converter = CompilationModeConverter.class, |
| 50 | documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, |
| 51 | effectTags = {OptionEffectTag.NO_OP}, |
| 52 | defaultValue = "dbg" |
| 53 | ) |
| Googler | 173ee82 | 2015-05-19 09:46:33 +0000 | [diff] [blame] | 54 | public CompilationMode compileMode; |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void converterFromEnum() throws Exception { |
| 59 | CompilationModeConverter converter = new CompilationModeConverter(); |
| lberki | bf6ef0f | 2017-05-29 11:00:40 +0200 | [diff] [blame] | 60 | assertThat(converter.convert("dbg")).isEqualTo(CompilationMode.DBG); |
| 61 | assertThat(converter.convert("opt")).isEqualTo(CompilationMode.OPT); |
| Googler | 173ee82 | 2015-05-19 09:46:33 +0000 | [diff] [blame] | 62 | |
| jcater | 20e631b | 2019-04-29 13:29:12 -0700 | [diff] [blame] | 63 | 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)"); |
| lberki | bf6ef0f | 2017-05-29 11:00:40 +0200 | [diff] [blame] | 68 | assertThat(converter.getTypeDescription()).isEqualTo("dbg or opt"); |
| Googler | 173ee82 | 2015-05-19 09:46:33 +0000 | [diff] [blame] | 69 | } |
| 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) { |
| lberki | bf6ef0f | 2017-05-29 11:00:40 +0200 | [diff] [blame] | 78 | assertThat(converter.convert(falseValue)).isEqualTo(CompilationMode.OPT); |
| Googler | 173ee82 | 2015-05-19 09:46:33 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | for (String trueValue : trueValues) { |
| lberki | bf6ef0f | 2017-05-29 11:00:40 +0200 | [diff] [blame] | 82 | assertThat(converter.convert(trueValue)).isEqualTo(CompilationMode.DBG); |
| Googler | 173ee82 | 2015-05-19 09:46:33 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void prefixedWithNo() throws OptionsParsingException { |
| jcater | 706a22e | 2019-06-17 09:56:03 -0700 | [diff] [blame] | 88 | OptionsParser parser = |
| 89 | OptionsParser.builder().optionsClasses(CompilationModeTestOptions.class).build(); |
| Googler | 173ee82 | 2015-05-19 09:46:33 +0000 | [diff] [blame] | 90 | parser.parse("--nocompile_mode"); |
| 91 | CompilationModeTestOptions options = |
| 92 | parser.getOptions(CompilationModeTestOptions.class); |
| lberki | bf6ef0f | 2017-05-29 11:00:40 +0200 | [diff] [blame] | 93 | assertThat(options.compileMode).isNotNull(); |
| 94 | assertThat(options.compileMode).isEqualTo(CompilationMode.OPT); |
| Googler | 173ee82 | 2015-05-19 09:46:33 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Test |
| 98 | public void missingValueAsBoolConversion() throws OptionsParsingException { |
| jcater | 706a22e | 2019-06-17 09:56:03 -0700 | [diff] [blame] | 99 | OptionsParser parser = |
| 100 | OptionsParser.builder().optionsClasses(CompilationModeTestOptions.class).build(); |
| Googler | 173ee82 | 2015-05-19 09:46:33 +0000 | [diff] [blame] | 101 | parser.parse("--compile_mode"); |
| 102 | CompilationModeTestOptions options = |
| 103 | parser.getOptions(CompilationModeTestOptions.class); |
| lberki | bf6ef0f | 2017-05-29 11:00:40 +0200 | [diff] [blame] | 104 | assertThat(options.compileMode).isNotNull(); |
| 105 | assertThat(options.compileMode).isEqualTo(CompilationMode.DBG); |
| Googler | 173ee82 | 2015-05-19 09:46:33 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | } |