github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/pretty/pretty_test.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package pretty_test
    12  
    13  import (
    14  	"fmt"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/util/pretty"
    17  )
    18  
    19  // Example_align demonstrates alignment.
    20  func Example_align() {
    21  	testData := []pretty.Doc{
    22  		pretty.JoinGroupAligned("SELECT", ",",
    23  			pretty.Text("aaa"),
    24  			pretty.Text("bbb"),
    25  			pretty.Text("ccc")),
    26  		pretty.Table(pretty.TableRightAlignFirstColumn, pretty.Text,
    27  			pretty.TableRow{Label: "SELECT",
    28  				Doc: pretty.Join(",",
    29  					pretty.Text("aaa"),
    30  					pretty.Text("bbb"),
    31  					pretty.Text("ccc")),
    32  			},
    33  			pretty.TableRow{Label: "FROM",
    34  				Doc: pretty.Join(",",
    35  					pretty.Text("t"),
    36  					pretty.Text("u"),
    37  					pretty.Text("v")),
    38  			}),
    39  		pretty.Table(pretty.TableLeftAlignFirstColumn, pretty.Text,
    40  			pretty.TableRow{Label: "SELECT",
    41  				Doc: pretty.Join(",",
    42  					pretty.Text("aaa"),
    43  					pretty.Text("bbb"),
    44  					pretty.Text("ccc")),
    45  			},
    46  			pretty.TableRow{Label: "FROM",
    47  				Doc: pretty.Join(",",
    48  					pretty.Text("t"),
    49  					pretty.Text("u"),
    50  					pretty.Text("v")),
    51  			}),
    52  		pretty.Table(pretty.TableRightAlignFirstColumn, pretty.Text,
    53  			pretty.TableRow{Label: "woo", Doc: nil}, // check nil rows are omitted
    54  			pretty.TableRow{Label: "", Doc: pretty.Nil},
    55  			pretty.TableRow{Label: "KEY", Doc: pretty.Text("VALUE")},
    56  			pretty.TableRow{Label: "", Doc: pretty.Text("OTHERVALUE")},
    57  			pretty.TableRow{Label: "AAA", Doc: pretty.Nil}, // check no extra space is added
    58  		),
    59  	}
    60  	for _, n := range []int{1, 15, 30, 80} {
    61  		fmt.Printf("%d:\n", n)
    62  		for _, doc := range testData {
    63  			p := pretty.Pretty(doc, n, true /*useTabs*/, 4 /*tabWidth*/, nil /*keywordTransform*/)
    64  			fmt.Printf("%s\n\n", p)
    65  		}
    66  	}
    67  
    68  	// Output:
    69  	// 1:
    70  	// SELECT
    71  	// 	aaa,
    72  	// 	bbb,
    73  	// 	ccc
    74  	//
    75  	// SELECT
    76  	// 	aaa,
    77  	// 	bbb,
    78  	// 	ccc
    79  	// FROM
    80  	// 	t,
    81  	// 	u,
    82  	// 	v
    83  	//
    84  	// SELECT
    85  	// 	aaa,
    86  	// 	bbb,
    87  	// 	ccc
    88  	// FROM
    89  	// 	t,
    90  	// 	u,
    91  	// 	v
    92  	//
    93  	// KEY
    94  	// 	VALUE
    95  	// OTHERVALUE
    96  	// AAA
    97  	//
    98  	// 15:
    99  	// SELECT aaa,
   100  	//        bbb,
   101  	//        ccc
   102  	//
   103  	// SELECT aaa,
   104  	//        bbb,
   105  	//        ccc
   106  	//   FROM t, u, v
   107  	//
   108  	// SELECT aaa,
   109  	//        bbb,
   110  	//        ccc
   111  	// FROM   t, u, v
   112  	//
   113  	// KEY VALUE
   114  	//     OTHERVALUE
   115  	// AAA
   116  	//
   117  	// 30:
   118  	// SELECT aaa, bbb, ccc
   119  	//
   120  	// SELECT aaa, bbb, ccc
   121  	//   FROM t, u, v
   122  	//
   123  	// SELECT aaa, bbb, ccc
   124  	// FROM   t, u, v
   125  	//
   126  	// KEY VALUE OTHERVALUE AAA
   127  	//
   128  	// 80:
   129  	// SELECT aaa, bbb, ccc
   130  	//
   131  	// SELECT aaa, bbb, ccc FROM t, u, v
   132  	//
   133  	// SELECT aaa, bbb, ccc FROM t, u, v
   134  	//
   135  	// KEY VALUE OTHERVALUE AAA
   136  }
   137  
   138  // ExampleTree demonstrates the Tree example from the paper.
   139  func Example_tree() {
   140  	type Tree struct {
   141  		s  string
   142  		n  []Tree
   143  		op string
   144  	}
   145  	tree := Tree{
   146  		s: "aaa",
   147  		n: []Tree{
   148  			{
   149  				s: "bbbbb",
   150  				n: []Tree{
   151  					{s: "ccc"},
   152  					{s: "dd"},
   153  					{s: "ee", op: "*", n: []Tree{
   154  						{s: "some"},
   155  						{s: "another", n: []Tree{{s: "2a"}, {s: "2b"}}},
   156  						{s: "final"},
   157  					}},
   158  				},
   159  			},
   160  			{s: "eee"},
   161  			{
   162  				s: "ffff",
   163  				n: []Tree{
   164  					{s: "gg"},
   165  					{s: "hhh"},
   166  					{s: "ii"},
   167  				},
   168  			},
   169  		},
   170  	}
   171  	var (
   172  		showTree    func(Tree) pretty.Doc
   173  		showTrees   func([]Tree) pretty.Doc
   174  		showBracket func([]Tree) pretty.Doc
   175  	)
   176  	showTrees = func(ts []Tree) pretty.Doc {
   177  		if len(ts) == 1 {
   178  			return showTree(ts[0])
   179  		}
   180  		return pretty.Fold(pretty.Concat,
   181  			showTree(ts[0]),
   182  			pretty.Text(","),
   183  			pretty.Line,
   184  			showTrees(ts[1:]),
   185  		)
   186  	}
   187  	showBracket = func(ts []Tree) pretty.Doc {
   188  		if len(ts) == 0 {
   189  			return pretty.Nil
   190  		}
   191  		return pretty.Fold(pretty.Concat,
   192  			pretty.Text("["),
   193  			pretty.NestT(showTrees(ts)),
   194  			pretty.Text("]"),
   195  		)
   196  	}
   197  	showTree = func(t Tree) pretty.Doc {
   198  		var doc pretty.Doc
   199  		if t.op != "" {
   200  			var operands []pretty.Doc
   201  			for _, o := range t.n {
   202  				operands = append(operands, showTree(o))
   203  			}
   204  			doc = pretty.Fold(pretty.Concat,
   205  				pretty.Text("("),
   206  				pretty.JoinNestedRight(
   207  					pretty.Text(t.op), operands...),
   208  				pretty.Text(")"),
   209  			)
   210  		} else {
   211  			doc = showBracket(t.n)
   212  		}
   213  		return pretty.Group(pretty.Concat(
   214  			pretty.Text(t.s),
   215  			pretty.NestS(int16(len(t.s)), doc),
   216  		))
   217  	}
   218  	for _, n := range []int{1, 30, 80} {
   219  		p := pretty.Pretty(showTree(tree), n, false /*useTabs*/, 4 /*tabWidth*/, nil /*keywordTransform*/)
   220  		fmt.Printf("%d:\n%s\n\n", n, p)
   221  	}
   222  	// Output:
   223  	// 1:
   224  	// aaa[bbbbb[ccc,
   225  	//             dd,
   226  	//             ee(some
   227  	//               * another[2a,
   228  	//                         2b]
   229  	//               * final)],
   230  	//     eee,
   231  	//     ffff[gg,
   232  	//             hhh,
   233  	//             ii]]
   234  	//
   235  	// 30:
   236  	// aaa[bbbbb[ccc,
   237  	//             dd,
   238  	//             ee(some
   239  	//               * another[2a,
   240  	//                         2b]
   241  	//               * final)],
   242  	//     eee,
   243  	//     ffff[gg, hhh, ii]]
   244  	//
   245  	// 80:
   246  	// aaa[bbbbb[ccc, dd, ee(some * another[2a, 2b] * final)], eee, ffff[gg, hhh, ii]]
   247  }