github.com/openconfig/goyang@v1.4.5/pkg/indent/indent_test.go (about) 1 // Copyright 2015 Google Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package indent 16 17 import ( 18 "bytes" 19 "errors" 20 "testing" 21 ) 22 23 var tests = []struct { 24 prefix, in, out string 25 }{ 26 { 27 "", "", "", 28 }, { 29 "--", "", "", 30 }, { 31 "", "x\nx", "x\nx", 32 }, { 33 "--", "x", "--x", 34 }, { 35 "--", "\n", "--\n", 36 }, { 37 "--", "\n\n", "--\n--\n", 38 }, { 39 "--", "x\n", "--x\n", 40 }, { 41 "--", "\nx", "--\n--x", 42 }, { 43 "--", "two\nlines\n", "--two\n--lines\n", 44 }, { 45 "--", "\nempty\nfirst\n", "--\n--empty\n--first\n", 46 }, { 47 "--", "empty\nlast\n\n", "--empty\n--last\n--\n", 48 }, { 49 "--", "empty\n\nmiddle\n", "--empty\n--\n--middle\n", 50 }, 51 } 52 53 func TestIndent(t *testing.T) { 54 for x, tt := range tests { 55 out := String(tt.prefix, tt.in) 56 if out != tt.out { 57 t.Errorf("#%d: got %q, want %q", x, out, tt.out) 58 } 59 bout := string(Bytes([]byte(tt.prefix), []byte(tt.in))) 60 if bout != out { 61 t.Errorf("#%d: Bytes got %q\n String got %q", x, bout, out) 62 } 63 } 64 } 65 66 func TestWriter(t *testing.T) { 67 Test: 68 for x, tt := range tests { 69 for size := 1; size < 64; size <<= 1 { 70 var b bytes.Buffer 71 w := NewWriter(&b, tt.prefix) 72 data := []byte(tt.in) 73 for len(data) > size { 74 if _, err := w.Write(data[:size]); err != nil { 75 t.Errorf("#%d: %v", x, err) 76 continue Test 77 } 78 data = data[size:] 79 } 80 if _, err := w.Write(data); err != nil { 81 t.Errorf("#%d/%d: %v", x, size, err) 82 continue Test 83 } 84 85 out := b.String() 86 if out != tt.out { 87 t.Errorf("#%d/%d: got %q, want %q", x, size, out, tt.out) 88 } 89 } 90 } 91 } 92 93 func TestWrittenSize(t *testing.T) { 94 for x, tt := range tests { 95 var b bytes.Buffer 96 w := NewWriter(&b, tt.prefix) 97 data := []byte(tt.in) 98 if n, _ := w.Write(data); n != len(data) { 99 t.Errorf("#%d: got %d, want %d", x, n, len(data)) 100 } 101 } 102 } 103 104 func TestWrittenSizeWithError(t *testing.T) { 105 table := []struct { 106 prefix string 107 input string 108 underlay int 109 expected int 110 }{ 111 {"--", "two\nlines\n", 0, 0}, 112 {"--", "two\nlines\n", 1, 0}, // - 113 {"--", "two\nlines\n", 2, 0}, // - 114 {"--", "two\nlines\n", 3, 1}, // t 115 {"--", "two\nlines\n", 4, 2}, // w 116 {"--", "two\nlines\n", 5, 3}, // o 117 {"--", "two\nlines\n", 6, 4}, // \n 118 {"--", "two\nlines\n", 7, 4}, // - 119 {"--", "two\nlines\n", 8, 4}, // - 120 {"--", "two\nlines\n", 9, 5}, // l 121 {"--", "two\nlines\n", 10, 6}, // i 122 {"--", "two\nlines\n", 11, 7}, // n 123 {"--", "two\nlines\n", 12, 8}, // e 124 {"--", "two\nlines\n", 13, 9}, // s 125 {"--", "two\nlines\n", 14, 10}, // \n 126 {"--", "two\nlines\n", 15, 10}, // - 127 {"--", "two\nlines\n", 16, 10}, // - 128 } 129 130 for _, d := range table { 131 uw := errorWriter{d.underlay} 132 w := NewWriter(uw, d.prefix) 133 data := []byte(d.input) 134 if n, _ := w.Write(data); n != d.expected { 135 t.Errorf("underlay: %d, got %d, want %d, err: ", d.underlay, n, d.expected) 136 } 137 } 138 } 139 140 type errorWriter struct { 141 ret int 142 } 143 144 func (w errorWriter) Write(buf []byte) (int, error) { 145 return w.ret, errors.New("error") 146 }