github.com/yourbase/yb@v0.7.1/cmd/yb/output_test.go (about) 1 // Copyright 2021 YourBase 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 // https://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 // SPDX-License-Identifier: Apache-2.0 16 17 package main 18 19 import ( 20 "io" 21 "strings" 22 "testing" 23 "time" 24 25 "github.com/google/go-cmp/cmp" 26 ) 27 28 func TestLinePrefixWriter(t *testing.T) { 29 start := time.Date(2021, time.April, 21, 14, 10, 23, 0, time.FixedZone("America/Los_Angeles", -7*60*60)) 30 const defaultPrefix = "default" 31 const defaultPaddedPrefix = "default |" 32 tests := []struct { 33 name string 34 prefix string 35 writes []string 36 want string 37 }{ 38 { 39 name: "Empty", 40 prefix: defaultPrefix, 41 writes: []string{""}, 42 want: "", 43 }, 44 { 45 name: "PartialLine", 46 prefix: defaultPrefix, 47 writes: []string{"foo"}, 48 want: "14:10:23 " + defaultPaddedPrefix + " foo", 49 }, 50 { 51 name: "FullLine", 52 prefix: defaultPrefix, 53 writes: []string{"foo\n"}, 54 want: "14:10:23 " + defaultPaddedPrefix + " foo\n", 55 }, 56 { 57 name: "MultipleLines/OneWrite", 58 prefix: defaultPrefix, 59 writes: []string{"foo\nbar\n"}, 60 want: "14:10:23 " + defaultPaddedPrefix + " foo\n" + 61 "14:10:23 " + defaultPaddedPrefix + " bar\n", 62 }, 63 { 64 name: "MultipleLines/MultipleWrites", 65 prefix: defaultPrefix, 66 writes: []string{"foo\n", "bar\n"}, 67 want: "14:10:23 " + defaultPaddedPrefix + " foo\n" + 68 "14:10:24 " + defaultPaddedPrefix + " bar\n", 69 }, 70 { 71 name: "MultipleLines/Split", 72 prefix: defaultPrefix, 73 writes: []string{"foo\nb", "ar\n"}, 74 want: "14:10:23 " + defaultPaddedPrefix + " foo\n" + 75 "14:10:23 " + defaultPaddedPrefix + " bar\n", 76 }, 77 { 78 name: "RSpecDots", 79 prefix: defaultPrefix, 80 writes: []string{".", ".", ".", ".", "\n"}, 81 want: "14:10:23 " + defaultPaddedPrefix + " ....\n", 82 }, 83 { 84 name: "LongPrefix", 85 prefix: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 86 writes: []string{"foo\n"}, 87 want: "14:10:23 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx| foo\n", 88 }, 89 } 90 for _, test := range tests { 91 t.Run(test.name, func(t *testing.T) { 92 currTime := start 93 out := new(strings.Builder) 94 w := newLinePrefixWriter(out, test.prefix) 95 w.now = func() time.Time { 96 result := currTime 97 currTime = currTime.Add(1 * time.Second) 98 return result 99 } 100 for i, data := range test.writes { 101 if n, err := io.WriteString(w, data); n != len(data) || err != nil { 102 t.Errorf("Write[%d](%q) = %d, %v; want %d, <nil>", i, data, n, err, len(data)) 103 } 104 } 105 if diff := cmp.Diff(test.want, out.String()); diff != "" { 106 t.Errorf("Output (-want +got):\n%s", diff) 107 } 108 }) 109 } 110 }