1616
1717package com.twosigma.webtau.http.render
1818
19+ import com.twosigma.webtau.console.ConsoleOutput
1920import com.twosigma.webtau.console.ConsoleOutputs
2021import com.twosigma.webtau.console.ansi.AnsiConsoleOutput
22+ import com.twosigma.webtau.console.ansi.Color
23+ import com.twosigma.webtau.console.ansi.FontStyle
2124import com.twosigma.webtau.http.datanode.DataNodeBuilder
2225import com.twosigma.webtau.http.datanode.DataNodeId
2326import org.junit.AfterClass
27+ import org.junit.Assert
2428import org.junit.BeforeClass
2529import org.junit.Test
2630
2731import static com.twosigma.webtau.Ddjt.equal
2832
2933class DataNodeAnsiPrinterTest {
3034 private static def ansiConsoleOutput = new AnsiConsoleOutput ()
35+
3136 @BeforeClass
3237 static void init () {
3338 ConsoleOutputs . add(ansiConsoleOutput)
@@ -40,16 +45,44 @@ class DataNodeAnsiPrinterTest {
4045
4146 @Test
4247 void " should print list data node with indentation and using different colors" () {
43- new DataNodeAnsiPrinter (). print (DataNodeBuilder . fromList(new DataNodeId (" root" ), [1 , 2 , 3 , 4 ]))
48+ def textOnly = withCapturedOutput {
49+ new DataNodeAnsiPrinter (). print (DataNodeBuilder . fromList(new DataNodeId (" root" ), [1 , 2 , 3 , 4 ]))
50+ }
51+
52+ Assert . assertEquals (' [\n ' +
53+ ' 1,\n ' +
54+ ' 2,\n ' +
55+ ' 3,\n ' +
56+ ' 4\n ' +
57+ ' ]' , textOnly)
4458 }
4559
4660 @Test
4761 void " should print object data node" () {
48- new DataNodeAnsiPrinter (). print (DataNodeBuilder . fromMap(new DataNodeId (" root" ), [
49- key1 : ' value1' ,
50- key2 : ' value2' ,
51- key3 : [key31 : ' value31' , key32 : [5 , 6 , 8 ]],
52- key4 : [key41 : ' value41' , key42 : ' value42' ]]))
62+ def textOnly = withCapturedOutput {
63+ new DataNodeAnsiPrinter (). print (DataNodeBuilder . fromMap(new DataNodeId (" root" ), [
64+ key1 : ' value1' ,
65+ key2 : ' value2' ,
66+ key3 : [key31 : ' value31' , key32 : [5 , 6 , 8 ]],
67+ key4 : [key41 : ' value41' , key42 : ' value42' ]]))
68+ }
69+
70+ Assert . assertEquals (' {\n ' +
71+ ' "key1": "value1",\n ' +
72+ ' "key2": "value2",\n ' +
73+ ' "key3": {\n ' +
74+ ' "key31": "value31",\n ' +
75+ ' "key32": [\n ' +
76+ ' 5,\n ' +
77+ ' 6,\n ' +
78+ ' 8\n ' +
79+ ' ]\n ' +
80+ ' },\n ' +
81+ ' "key4": {\n ' +
82+ ' "key41": "value41",\n ' +
83+ ' "key42": "value42"\n ' +
84+ ' }\n ' +
85+ ' }' , textOnly)
5386 }
5487
5588 @Test
@@ -71,6 +104,80 @@ class DataNodeAnsiPrinterTest {
71104 // catch as it is an expected here and serves to check if the value will be rendered as failed
72105 }
73106
74- new DataNodeAnsiPrinter (). print (dataNode)
107+ def textOnly = withCapturedOutput {
108+ new DataNodeAnsiPrinter (). print (dataNode)
109+ }
110+
111+ Assert . assertEquals (' [\n ' +
112+ ' {\n ' +
113+ ' "key1": "value1",\n ' +
114+ ' "key2": __"value2"__,\n ' +
115+ ' "key3": {\n ' +
116+ ' "key31": **"value31"**,\n ' +
117+ ' "key32": [\n ' +
118+ ' 5,\n ' +
119+ ' 6,\n ' +
120+ ' 8\n ' +
121+ ' ]\n ' +
122+ ' }\n ' +
123+ ' },\n ' +
124+ ' {\n ' +
125+ ' "key4": "value4",\n ' +
126+ ' "key5": "value5"\n ' +
127+ ' }\n ' +
128+ ' ]' , textOnly)
129+ }
130+
131+ @Test
132+ void " should collapse empty list and object" () {
133+ def textOnly = withCapturedOutput {
134+ new DataNodeAnsiPrinter (). print (DataNodeBuilder . fromMap(new DataNodeId (" root" ), [
135+ key1 : ' value1' ,
136+ key2 : ' value2' ,
137+ key3 : [],
138+ key4 : [:]]))
139+ }
140+
141+ Assert . assertEquals (' {\n ' +
142+ ' "key1": "value1",\n ' +
143+ ' "key2": "value2",\n ' +
144+ ' "key3": [],\n ' +
145+ ' "key4": {}\n ' +
146+ ' }' , textOnly)
147+ }
148+
149+ private static String withCapturedOutput (Closure code ) {
150+ def capture = new CaptureOutput ()
151+ try {
152+ ConsoleOutputs . add(capture)
153+ code()
154+
155+ return capture. textOnly
156+ } finally {
157+ ConsoleOutputs . remove(capture)
158+ }
159+ }
160+
161+ private static class CaptureOutput implements ConsoleOutput {
162+ private def stylesAndOutput = []
163+ private def textLInes = []
164+
165+ @Override
166+ void out (Object ... styleOrValues ) {
167+ def line = Arrays . asList(styleOrValues). findAll {
168+ ! (it instanceof Color ) && ! (it instanceof FontStyle )
169+ }. join(' ' )
170+
171+ textLInes. add(line)
172+ stylesAndOutput. addAll(Arrays . asList(styleOrValues))
173+ }
174+
175+ String getTextOnly () {
176+ return textLInes. join(' \n ' )
177+ }
178+
179+ @Override
180+ void err (Object ... styleOrValues ) {
181+ }
75182 }
76183}
0 commit comments