Skip to content

Commit a7043c6

Browse files
prettyprinter: fallback handler handles decoration (#1368)
1 parent c2e994b commit a7043c6

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

webtau-core/src/main/java/org/testingisdocumenting/webtau/data/render/FallbackPrettyPrintable.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.testingisdocumenting.webtau.data.render;
1818

19+
import org.testingisdocumenting.webtau.data.ValuePath;
20+
1921
import java.util.Objects;
2022

2123
class FallbackPrettyPrintable implements PrettyPrintable {
@@ -25,8 +27,22 @@ public FallbackPrettyPrintable(Object object) {
2527
this.object = object;
2628
}
2729

30+
@Override
31+
public boolean handlesDecoration() {
32+
return true;
33+
}
34+
35+
@Override
36+
public void prettyPrint(PrettyPrinter printer, ValuePath rootPath, PrettyPrinterDecorationToken decorationToken) {
37+
if (decorationToken.isEmpty()) {
38+
printer.print(PrettyPrinter.UNKNOWN_COLOR, Objects.toString(object));
39+
} else {
40+
printer.print(decorationToken.getColor(), decorationToken.getWrapWith(), Objects.toString(object), decorationToken.getWrapWith());
41+
}
42+
}
43+
2844
@Override
2945
public void prettyPrint(PrettyPrinter printer) {
30-
printer.print(PrettyPrinter.UNKNOWN_COLOR, Objects.toString(object));
46+
prettyPrint(printer, ValuePath.UNDEFINED, PrettyPrinterDecorationToken.EMPTY);
3147
}
3248
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2023 webtau maintainers
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.testingisdocumenting.webtau.data.render
18+
19+
import org.junit.Test
20+
import org.testingisdocumenting.webtau.console.ansi.Color
21+
import org.testingisdocumenting.webtau.data.ValuePath
22+
23+
import java.util.regex.Pattern
24+
25+
class FallbackPrettyPrintableTest extends PrettyPrintableTestBase {
26+
@Test
27+
void "without decoration"() {
28+
printer.printObject(null)
29+
30+
expectOutput("null")
31+
}
32+
33+
@Test
34+
void "with decoration"() {
35+
printer.setPathsDecoration(new PrettyPrinterDecorationToken("^^", Color.BLUE), [new ValuePath("root")])
36+
printer.printObject(new ValuePath("root"), null)
37+
38+
expectOutput("^^null^^")
39+
}
40+
}

0 commit comments

Comments
 (0)