|
| 1 | +package com.joutvhu.fixedwidth.parser; |
| 2 | + |
| 3 | +import com.joutvhu.fixedwidth.parser.annotation.FixedField; |
| 4 | +import com.joutvhu.fixedwidth.parser.annotation.FixedObject; |
| 5 | +import com.joutvhu.fixedwidth.parser.domain.Alignment; |
| 6 | +import lombok.AllArgsConstructor; |
| 7 | +import lombok.Data; |
| 8 | +import org.junit.jupiter.api.BeforeAll; |
| 9 | +import org.junit.jupiter.api.TestInstance; |
| 10 | +import org.junit.jupiter.params.ParameterizedTest; |
| 11 | +import org.junit.jupiter.params.provider.MethodSource; |
| 12 | + |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | + |
| 18 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 19 | +class AlignmentTests { |
| 20 | + |
| 21 | + private FixedParser fixedParser; |
| 22 | + |
| 23 | + @BeforeAll |
| 24 | + public void beforeTest() { |
| 25 | + this.fixedParser = FixedParser.parser(); |
| 26 | + } |
| 27 | + |
| 28 | + @ParameterizedTest |
| 29 | + @MethodSource("samples") |
| 30 | + void validateFormattedFieldWithPadding(Sample sample) { |
| 31 | + String output = fixedParser.export(sample.person); |
| 32 | + |
| 33 | + assertEquals(sample.expected, output); |
| 34 | + } |
| 35 | + |
| 36 | + @SuppressWarnings("unused") |
| 37 | + static List<Sample> samples() { |
| 38 | + return Arrays.asList( |
| 39 | + new Sample(new AutoPerson("Bob"), "Bob "), |
| 40 | + |
| 41 | + // FIXME "left" alignment means padding to the right to me |
| 42 | + new Sample(new LeftPerson("Bob"), "Bob "), |
| 43 | + |
| 44 | + // FIXME "right" alignment means padding to the left for me |
| 45 | + new Sample(new RightPerson("Bob"), " Bob"), |
| 46 | + |
| 47 | + new Sample(new CenterPerson("Bob"), " Bob "), |
| 48 | + |
| 49 | + // FIXME fixed length = 10, "Emma".length = 4, this means 3 chars to the left, and 3 chars to the right |
| 50 | + // But the padding routine constructs a string of length 11, 4 spaces to the left, 3 spaces to the right |
| 51 | + new Sample(new CenterPerson("Emma"), " Emma ") |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + @Data |
| 56 | + @AllArgsConstructor |
| 57 | + static class Sample { |
| 58 | + private Object person; |
| 59 | + private String expected; |
| 60 | + } |
| 61 | + |
| 62 | + @FixedObject |
| 63 | + @Data |
| 64 | + @AllArgsConstructor |
| 65 | + public static class AutoPerson { |
| 66 | + @FixedField(length = 10, alignment = Alignment.AUTO) |
| 67 | + private String name; |
| 68 | + } |
| 69 | + |
| 70 | + @FixedObject |
| 71 | + @Data |
| 72 | + @AllArgsConstructor |
| 73 | + public static class LeftPerson { |
| 74 | + @FixedField(length = 10, alignment = Alignment.LEFT) |
| 75 | + private String name; |
| 76 | + } |
| 77 | + |
| 78 | + @FixedObject |
| 79 | + @Data |
| 80 | + @AllArgsConstructor |
| 81 | + public static class RightPerson { |
| 82 | + @FixedField(length = 10, alignment = Alignment.RIGHT) |
| 83 | + private String name; |
| 84 | + } |
| 85 | + |
| 86 | + @FixedObject |
| 87 | + @Data |
| 88 | + @AllArgsConstructor |
| 89 | + public static class CenterPerson { |
| 90 | + @FixedField(length = 10, alignment = Alignment.CENTER) |
| 91 | + private String name; |
| 92 | + } |
| 93 | +} |
0 commit comments