Skip to content

Commit b99e3f5

Browse files
authored
Merge pull request #25 from f4lco/alignment
Discuss: alignment of fixed fields
2 parents a8ea36e + 0b6e7f3 commit b99e3f5

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ dependencies {
4444

4545
testImplementation 'junit:junit:4.13'
4646
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
47+
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.6.2'
4748
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
4849
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.6.2'
4950

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)