|
| 1 | +package pl.mperor.lab.java.lang.element; |
| 2 | + |
| 3 | + |
| 4 | +import org.junit.jupiter.api.Assertions; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import java.util.Arrays; |
| 8 | + |
| 9 | +public class EnumTest { |
| 10 | + |
| 11 | + @Test |
| 12 | + public void testEnumeration() { |
| 13 | + Assertions.assertEquals(0, Season.SPRING.ordinal()); |
| 14 | + var hottestSeasons = Season.SUMMER; |
| 15 | + Assertions.assertTrue(hottestSeasons == Season.SUMMER); |
| 16 | + Assertions.assertEquals("FALL", Season.FALL.name()); |
| 17 | + Assertions.assertEquals(Season.WINTER, Season.valueOf("WINTER")); |
| 18 | + Assertions.assertArrayEquals(new Month[]{Month.DECEMBER, Month.JANUARY, Month.FEBRUARY}, |
| 19 | + Season.WINTER.getMonths()); |
| 20 | + Assertions.assertEquals(4, Season.values().length); |
| 21 | + } |
| 22 | + |
| 23 | + enum Season { |
| 24 | + SPRING(Month.MARCH, Month.APRIL, Month.MAY), |
| 25 | + SUMMER(Month.JUNE, Month.JULY, Month.AUGUST), |
| 26 | + FALL(Month.SEPTEMBER, Month.OCTOBER, Month.NOVEMBER), |
| 27 | + WINTER(Month.DECEMBER, Month.JANUARY, Month.FEBRUARY); |
| 28 | + |
| 29 | + private final Month[] months; |
| 30 | + |
| 31 | + Season(Month... months) { |
| 32 | + this.months = months; |
| 33 | + } |
| 34 | + |
| 35 | + Month[] getMonths() { |
| 36 | + return Arrays.copyOf(months, months.length); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + enum Month { |
| 41 | + JANUARY, |
| 42 | + FEBRUARY, |
| 43 | + MARCH, |
| 44 | + APRIL, |
| 45 | + MAY, |
| 46 | + JUNE, |
| 47 | + JULY, |
| 48 | + AUGUST, |
| 49 | + SEPTEMBER, |
| 50 | + OCTOBER, |
| 51 | + NOVEMBER, |
| 52 | + DECEMBER |
| 53 | + } |
| 54 | + |
| 55 | +} |
0 commit comments