|
| 1 | +package dev.objz.commandbridge.scripting.bind.adapters; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.BeforeAll; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import dev.objz.commandbridge.scripting.bind.BindContext; |
| 11 | +import dev.objz.commandbridge.scripting.bind.ConvertContext; |
| 12 | +import dev.objz.commandbridge.scripting.model.enums.Location; |
| 13 | +import dev.objz.commandbridge.scripting.platform.PlatformFeatures; |
| 14 | +import dev.objz.commandbridge.scripting.validation.ProblemSink; |
| 15 | +import dev.objz.commandbridge.scripting.yaml.YamlNode; |
| 16 | + |
| 17 | +class EnumAdapterTest { |
| 18 | + |
| 19 | + @BeforeAll |
| 20 | + static void installLog() { |
| 21 | + try { |
| 22 | + dev.objz.commandbridge.logging.Log.install(java.util.logging.Logger.getLogger("test")); |
| 23 | + } catch (IllegalStateException ignored) { |
| 24 | + // expected |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + private static ConvertContext ctx() { |
| 29 | + return new BindContext(null, new ProblemSink(), PlatformFeatures.none()); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + @SuppressWarnings("unchecked") |
| 34 | + void parsesExactMatch() { |
| 35 | + EnumAdapter adapter = new EnumAdapter(); |
| 36 | + YamlNode node = YamlNode.scalar("BACKEND"); |
| 37 | + Location result = (Location) adapter.fromYaml(node, Location.class, ctx()); |
| 38 | + assertEquals(Location.BACKEND, result); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + @SuppressWarnings("unchecked") |
| 43 | + void parsesLowercase() { |
| 44 | + EnumAdapter adapter = new EnumAdapter(); |
| 45 | + YamlNode node = YamlNode.scalar("backend"); |
| 46 | + Location result = (Location) adapter.fromYaml(node, Location.class, ctx()); |
| 47 | + assertEquals(Location.BACKEND, result); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + @SuppressWarnings("unchecked") |
| 52 | + void parsesUppercase() { |
| 53 | + EnumAdapter adapter = new EnumAdapter(); |
| 54 | + YamlNode node = YamlNode.scalar("VELOCITY"); |
| 55 | + Location result = (Location) adapter.fromYaml(node, Location.class, ctx()); |
| 56 | + assertEquals(Location.VELOCITY, result); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + @SuppressWarnings("unchecked") |
| 61 | + void parsesMixedCase() { |
| 62 | + EnumAdapter adapter = new EnumAdapter(); |
| 63 | + YamlNode node = YamlNode.scalar("VeLoCiTy"); |
| 64 | + Location result = (Location) adapter.fromYaml(node, Location.class, ctx()); |
| 65 | + assertEquals(Location.VELOCITY, result); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + @SuppressWarnings("unchecked") |
| 70 | + void returnsNullForNullValue() { |
| 71 | + EnumAdapter adapter = new EnumAdapter(); |
| 72 | + YamlNode node = YamlNode.scalar(null); |
| 73 | + Location result = (Location) adapter.fromYaml(node, Location.class, ctx()); |
| 74 | + assertNull(result); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void throwsForNonScalarNode() { |
| 79 | + EnumAdapter adapter = new EnumAdapter(); |
| 80 | + YamlNode node = YamlNode.mapping(java.util.Map.of()); |
| 81 | + assertThrows(IllegalArgumentException.class, () -> adapter.fromYaml(node, Location.class, ctx())); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void throwsForUnknownEnumValue() { |
| 86 | + EnumAdapter adapter = new EnumAdapter(); |
| 87 | + YamlNode node = YamlNode.scalar("UNKNOWN"); |
| 88 | + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
| 89 | + () -> adapter.fromYaml(node, Location.class, ctx())); |
| 90 | + assertEquals("Unknown enum constant: UNKNOWN for Location", ex.getMessage()); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void throwsForNonEnumTargetType() { |
| 95 | + EnumAdapter adapter = new EnumAdapter(); |
| 96 | + YamlNode node = YamlNode.scalar("BACKEND"); |
| 97 | + assertThrows(IllegalArgumentException.class, () -> adapter.fromYaml(node, String.class, ctx())); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void convertsToYamlName() { |
| 102 | + EnumAdapter adapter = new EnumAdapter(); |
| 103 | + YamlNode result = adapter.toYaml(Location.BACKEND, Location.class, ctx()); |
| 104 | + assertEquals(YamlNode.scalar("BACKEND"), result); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void convertsNullToYamlNull() { |
| 109 | + EnumAdapter adapter = new EnumAdapter(); |
| 110 | + YamlNode result = adapter.toYaml(null, Location.class, ctx()); |
| 111 | + assertEquals(YamlNode.scalar(null), result); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void supportsEnumTypes() { |
| 116 | + EnumAdapter adapter = new EnumAdapter(); |
| 117 | + assertEquals(true, adapter.supports(Location.class)); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void doesNotSupportNonEnumTypes() { |
| 122 | + EnumAdapter adapter = new EnumAdapter(); |
| 123 | + assertEquals(false, adapter.supports(String.class)); |
| 124 | + assertEquals(false, adapter.supports(Integer.class)); |
| 125 | + } |
| 126 | +} |
0 commit comments