github.com/seeker-insurance/kit@v0.0.13/pretty/text/indent_test.go (about)

     1  package text
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  type T struct {
     9  	inp, exp, pre string
    10  }
    11  
    12  var tests = []T{
    13  	{
    14  		"The quick brown fox\njumps over the lazy\ndog.\nBut not quickly.\n",
    15  		"xxxThe quick brown fox\nxxxjumps over the lazy\nxxxdog.\nxxxBut not quickly.\n",
    16  		"xxx",
    17  	},
    18  	{
    19  		"The quick brown fox\njumps over the lazy\ndog.\n\nBut not quickly.",
    20  		"xxxThe quick brown fox\nxxxjumps over the lazy\nxxxdog.\n\nxxxBut not quickly.",
    21  		"xxx",
    22  	},
    23  }
    24  
    25  func TestIndent(t *testing.T) {
    26  	for _, test := range tests {
    27  		got := Indent(test.inp, test.pre)
    28  		if got != test.exp {
    29  			t.Errorf("mismatch %q != %q", got, test.exp)
    30  		}
    31  	}
    32  }
    33  
    34  type IndentWriterTest struct {
    35  	inp, exp string
    36  	pre      []string
    37  }
    38  
    39  var ts = []IndentWriterTest{
    40  	{
    41  		`
    42  The quick brown fox
    43  jumps over the lazy
    44  dog.
    45  But not quickly.
    46  `[1:],
    47  		`
    48  xxxThe quick brown fox
    49  xxxjumps over the lazy
    50  xxxdog.
    51  xxxBut not quickly.
    52  `[1:],
    53  		[]string{"xxx"},
    54  	},
    55  	{
    56  		`
    57  The quick brown fox
    58  jumps over the lazy
    59  dog.
    60  But not quickly.
    61  `[1:],
    62  		`
    63  xxaThe quick brown fox
    64  xxxjumps over the lazy
    65  xxxdog.
    66  xxxBut not quickly.
    67  `[1:],
    68  		[]string{"xxa", "xxx"},
    69  	},
    70  	{
    71  		`
    72  The quick brown fox
    73  jumps over the lazy
    74  dog.
    75  But not quickly.
    76  `[1:],
    77  		`
    78  xxaThe quick brown fox
    79  xxbjumps over the lazy
    80  xxcdog.
    81  xxxBut not quickly.
    82  `[1:],
    83  		[]string{"xxa", "xxb", "xxc", "xxx"},
    84  	},
    85  	{
    86  		`
    87  The quick brown fox
    88  jumps over the lazy
    89  dog.
    90  
    91  But not quickly.`[1:],
    92  		`
    93  xxaThe quick brown fox
    94  xxxjumps over the lazy
    95  xxxdog.
    96  xxx
    97  xxxBut not quickly.`[1:],
    98  		[]string{"xxa", "xxx"},
    99  	},
   100  }
   101  
   102  func TestIndentWriter(t *testing.T) {
   103  	for _, test := range ts {
   104  		b := new(bytes.Buffer)
   105  		pre := make([][]byte, len(test.pre))
   106  		for i := range test.pre {
   107  			pre[i] = []byte(test.pre[i])
   108  		}
   109  		w := NewIndentWriter(b, pre...)
   110  		if _, err := w.Write([]byte(test.inp)); err != nil {
   111  			t.Error(err)
   112  		}
   113  		if got := b.String(); got != test.exp {
   114  			t.Errorf("mismatch %q != %q", got, test.exp)
   115  			t.Log(got)
   116  			t.Log(test.exp)
   117  		}
   118  	}
   119  }