Skip to content

Commit

Permalink
toString improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen committed Dec 16, 2024
1 parent 9266f9e commit 6eccf0c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ private static String joinPreconditions(Collection<PreCondition> rules, String o
} else if (rules.size() == 1) {
return rules.iterator().next().toString();
}
char[] indentChars = new char[indent];
Arrays.fill(indentChars, ' ');
String indentStr = new String(indentChars);
String whitespace = String.format("%" + indent + "s", " ");
Set<String> preconditions = rules.stream().map(Object::toString).collect(toSet());
return "Preconditions." + op + "(\n" + indentStr + String.join(",\n" + indentStr, preconditions) + "\n" + indentStr.substring(0, indent - 4) + ')';
return "Preconditions." + op + "(\n"
+ whitespace + String.join(",\n" + whitespace, preconditions) + "\n"
+ whitespace.substring(0, indent - 4) + ')';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,40 @@
import static com.google.common.truth.Truth.assertThat;

class PreConditionTest {
@Test
void toStringWithInden() {
String result = new PreCondition.Or(
setOf(
new PreCondition.And(
setOf(
new PreCondition.Rule("A"),
new PreCondition.Rule("B"),
new PreCondition.Rule("C")),
4
),
new PreCondition.And(
setOf(
new PreCondition.Rule("X"),
new PreCondition.Rule("Y"),
new PreCondition.Rule("Z")),
4
)
), 4).toString();

assertThat(result).isEqualTo("Preconditions.or(\n" +
" Preconditions.and(\n" +
" X,\n" +
" Y,\n" +
" Z\n" +
"),\n" +
" Preconditions.and(\n" +
" A,\n" +
" B,\n" +
" C\n" +
")\n" +
")");
}

@Test
void sameRulesArePrunedAutomatically() {
Set<PreCondition> result = setOf(new PreCondition.Rule("A"), new PreCondition.Rule("A"));
Expand Down

0 comments on commit 6eccf0c

Please sign in to comment.