Skip to content

Commit bf37676

Browse files
committed
fix: Corrected Cursor.prevIf() and .nextIf() feedback
They would not correctly indicate if they were pointing to a valid element or not. This is now fixed. See #17 for more information
1 parent 72591e6 commit bf37676

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

src/main/java/org/codejive/properties/Cursor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ public boolean nextIf(PropertiesParser.Type type) {
9595

9696
public boolean nextIf(Predicate<PropertiesParser.Token> accept) {
9797
if (hasToken() && accept.test(token())) {
98-
return next().hasToken();
98+
next();
99+
return true;
99100
} else {
100101
return false;
101102
}
@@ -120,7 +121,8 @@ public boolean prevIf(PropertiesParser.Type type) {
120121

121122
public boolean prevIf(Predicate<PropertiesParser.Token> accept) {
122123
if (hasToken() && accept.test(token())) {
123-
return prev().hasToken();
124+
prev();
125+
return true;
124126
} else {
125127
return false;
126128
}

src/main/java/org/codejive/properties/Properties.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -969,15 +969,15 @@ private Cursor skipHeaderCommentLines() {
969969
}
970970
}
971971

972-
private Cursor index(int index) {
972+
Cursor index(int index) {
973973
return Cursor.index(tokens, index);
974974
}
975975

976-
private Cursor first() {
976+
Cursor first() {
977977
return Cursor.first(tokens);
978978
}
979979

980-
private Cursor last() {
980+
Cursor last() {
981981
return Cursor.last(tokens);
982982
}
983983

src/test/java/org/codejive/properties/TestProperties.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,15 @@ void testGetComment() throws IOException, URISyntaxException {
206206
.containsExactly("# another comment", "! and a comment", "! block");
207207
}
208208

209+
@Test
210+
void testCommentFirstLine() throws IOException, URISyntaxException {
211+
Properties p = Properties.loadProperties(getResource("/test-commentfirstline.properties"));
212+
assertThat(p.getComment("one")).containsExactly("#comment1", "# comment2");
213+
StringWriter sw = new StringWriter();
214+
p.store(sw);
215+
assertThat(sw.toString()).isEqualTo(readAll(getResource("/test-commentfirstline.properties")));
216+
}
217+
209218
@Test
210219
void testSetComment() throws IOException, URISyntaxException {
211220
Properties p = Properties.loadProperties(getResource("/test.properties"));
@@ -611,6 +620,15 @@ void testUnescaped() throws IOException, URISyntaxException {
611620
assertThat(sw.toString()).isEqualTo(readAll(getResource("/test-unescaped.properties")));
612621
}
613622

623+
@Test
624+
void testCursor() throws IOException, URISyntaxException {
625+
Properties p = Properties.loadProperties(getResource("/test.properties"));
626+
Cursor c = p.first();
627+
assertThat(c.nextCount(t -> true)).isEqualTo(p.last().position() + 1);
628+
c = p.last();
629+
assertThat(c.prevCount(t -> true)).isEqualTo(p.last().position() + 1);
630+
}
631+
614632
private Path getResource(String name) throws URISyntaxException {
615633
return Paths.get(getClass().getResource(name).toURI());
616634
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#comment1
2+
# comment2
3+
one=simple

0 commit comments

Comments
 (0)