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

     1  // Copyright 2017 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 treeprinter
    12  
    13  import (
    14  	"strings"
    15  	"testing"
    16  )
    17  
    18  func TestTreePrinter(t *testing.T) {
    19  	n := New()
    20  
    21  	r := n.Child("root")
    22  	r.AddEmptyLine()
    23  	n1 := r.Childf("%d", 1)
    24  	n1.Child("1.1")
    25  	n12 := n1.Child("1.2")
    26  	r.AddEmptyLine()
    27  	r.AddEmptyLine()
    28  	n12.Child("1.2.1")
    29  	r.AddEmptyLine()
    30  	n12.Child("1.2.2")
    31  	n13 := n1.Child("1.3")
    32  	n13.AddEmptyLine()
    33  	n131 := n13.Child("1.3.1\n1.3.1a")
    34  	n13.Child("1.3.2\n1.3.2a")
    35  	n13.AddEmptyLine()
    36  	n131.Child("1.3.1.1\n1.3.1.1a")
    37  	n1.Child("1.4")
    38  	r.Child("2")
    39  
    40  	res := n.String()
    41  	exp := `
    42  root
    43   │
    44   ├── 1
    45   │    ├── 1.1
    46   │    ├── 1.2
    47   │    │    │
    48   │    │    │
    49   │    │    ├── 1.2.1
    50   │    │    │
    51   │    │    └── 1.2.2
    52   │    ├── 1.3
    53   │    │    │
    54   │    │    ├── 1.3.1
    55   │    │    │   1.3.1a
    56   │    │    └── 1.3.2
    57   │    │        1.3.2a
    58   │    │         │
    59   │    │         └── 1.3.1.1
    60   │    │             1.3.1.1a
    61   │    └── 1.4
    62   └── 2
    63  `
    64  	exp = strings.TrimLeft(exp, "\n")
    65  	if res != exp {
    66  		t.Errorf("incorrect result:\n%s", res)
    67  	}
    68  }
    69  
    70  func TestTreePrinterUTF(t *testing.T) {
    71  	n := New()
    72  
    73  	r := n.Child("root")
    74  	r1 := r.Child("日本語\n本語\n本語")
    75  	r1.Child("日本語\n本語\n本語")
    76  	r.Child("日本語\n本語\n本語")
    77  	res := n.String()
    78  	exp := `
    79  root
    80   ├── 日本語
    81   │   本語
    82   │   本語
    83   │    └── 日本語
    84   │        本語
    85   │        本語
    86   └── 日本語
    87       本語
    88       本語
    89  `
    90  
    91  	exp = strings.TrimLeft(exp, "\n")
    92  	if res != exp {
    93  		t.Errorf("incorrect result:\n%s", res)
    94  	}
    95  }
    96  
    97  func TestTreePrinterNested(t *testing.T) {
    98  	// The output of a treeprinter can be used as a node inside a larger
    99  	// treeprinter. This is useful when formatting routines use treeprinter
   100  	// internally.
   101  	tp1 := New()
   102  	r1 := tp1.Child("root1")
   103  	r11 := r1.Child("1.1")
   104  	r11.Child("1.1.1")
   105  	r11.Child("1.1.2")
   106  	r1.Child("1.2")
   107  	tree1 := strings.TrimRight(tp1.String(), "\n")
   108  
   109  	tp2 := New()
   110  	r2 := tp2.Child("root2")
   111  	r2.Child("2.1")
   112  	r22 := r2.Child("2.2")
   113  	r22.Child("2.2.1")
   114  	tree2 := strings.TrimRight(tp2.String(), "\n")
   115  
   116  	tp := New()
   117  	r := tp.Child("tree of trees")
   118  	r.Child(tree1)
   119  	r.Child(tree2)
   120  	res := tp.String()
   121  	exp := `
   122  tree of trees
   123   ├── root1
   124   │    ├── 1.1
   125   │    │    ├── 1.1.1
   126   │    │    └── 1.1.2
   127   │    └── 1.2
   128   └── root2
   129        ├── 2.1
   130        └── 2.2
   131             └── 2.2.1
   132  `
   133  
   134  	exp = strings.TrimLeft(exp, "\n")
   135  	if res != exp {
   136  		t.Errorf("incorrect result:\n%s", res)
   137  	}
   138  }