Skip to content

Commit

Permalink
Fix #550
Browse files Browse the repository at this point in the history
  • Loading branch information
CaryCatZ authored and mkarg committed Jul 6, 2024
1 parent d53cdce commit 3bc0804
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/main/java/com/beust/jcommander/converters/EnumConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@ public EnumConverter(String optionName, Class<T> clazz) {

@Override
public T convert(String value) {
try {
try {
return Enum.valueOf(clazz, value);
} catch (IllegalArgumentException e) {
return Enum.valueOf(clazz, value.toUpperCase());
for (T constant : EnumSet.allOf(clazz)) {
// the toString method may be overridden, causing what is printed (or what user types) is different from it's declared name
if (constant.name().equals(value) || constant.name().equals(value.toUpperCase())
|| constant.toString().equals(value) || constant.toString().equals(value.toUpperCase())) {
return constant;
}
} catch (Exception e) {
throw new ParameterException("Invalid value for " + optionName + " parameter. Allowed values:" +
EnumSet.allOf(clazz));

}
throw new ParameterException("Invalid value for " + optionName + " parameter. Allowed values:" +
EnumSet.allOf(clazz));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright (C) 2024 the original author or authors.
* See the notice.md file distributed with this work for additional
* information regarding copyright ownership.
*
* 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.beust.jcommander.converters;

import org.testng.Assert;
import org.testng.annotations.Test;

@Test
public class EnumConverterTest {
private static final EnumConverter<Season> SEASON_ENUM_CONVERTER = new EnumConverter<>("", Season.class);

private static final EnumConverter<Resolution> RESOLUTION_ENUM_CONVERTER = new EnumConverter<>("", Resolution.class);

@Test
public void testMatch() {
Assert.assertEquals(Season.AUTUMN, SEASON_ENUM_CONVERTER.convert("AUTUMN"));
}

@Test
public void testMatchLowerCase() {
Assert.assertEquals(Season.AUTUMN, SEASON_ENUM_CONVERTER.convert("autumn"));
}

@Test
public void testMatchWithToString() {
Assert.assertEquals(Resolution.R_4K, RESOLUTION_ENUM_CONVERTER.convert("4k"));
Assert.assertEquals(Resolution.R_1080P, RESOLUTION_ENUM_CONVERTER.convert("1080P"));
}

@Test
public void testNoMatch() {
Assert.assertThrows(() -> SEASON_ENUM_CONVERTER.convert("XXX"));
Assert.assertThrows(() -> RESOLUTION_ENUM_CONVERTER.convert("XXX"));
}

private enum Season {
SPRING,
SUMMER,
AUTUMN,
WINTER
}

private enum Resolution {
R_4K, // can not start with a number
R_1080P,
R_480P;

@Override
public String toString() {
return name().substring(2); // removes the prefix for user-friendly reading
}
}
}

0 comments on commit 3bc0804

Please sign in to comment.