github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/formatter/formatter_test.go (about) 1 package formatter 2 3 import ( 4 "bytes" 5 "fmt" 6 "testing" 7 ) 8 9 func TestPadding(t *testing.T) { 10 t.Run("testMachineFriendlyFormat", testMachineFriendlyFormat) 11 Human = true 12 defer func() { 13 Human = false 14 }() 15 t.Run("testHumanFriendlyFormat", testHumanFriendlyFormat) 16 } 17 18 func testMachineFriendlyFormat(t *testing.T) { 19 if Human { 20 t.Errorf("Expected padding to be machine-friendly by default") 21 } 22 23 var b bytes.Buffer 24 25 var tw = NewTabWriter(&b) 26 var text = "ABC\tDEFGH\tIJKLMNOPQRSTUVWXYZ\n012345\t6789\n" 27 _, err := fmt.Fprint(tw, text) 28 29 if err != nil { 30 t.Errorf("Error printing: %v", err) 31 } 32 33 if err := tw.Flush(); err != nil { 34 t.Errorf("Error flushing: %v", err) 35 } 36 37 var got = b.String() 38 39 if got != text { 40 t.Errorf(`Expected text to be original "%v", got "%v" instead`, text, got) 41 } 42 } 43 44 func testHumanFriendlyFormat(t *testing.T) { 45 if !Human { 46 t.Errorf("Expected padding to be human-friendly") 47 } 48 49 var b bytes.Buffer 50 51 var tw = NewTabWriter(&b) 52 var text = "ABC\tDEFGH\tIJKLMNOPQRSTUVWXYZ\n012345\t6789\n" 53 _, err := fmt.Fprint(tw, text) 54 55 if err != nil { 56 t.Errorf("Error printing: %v", err) 57 } 58 59 if err := tw.Flush(); err != nil { 60 t.Errorf("Error flushing: %v", err) 61 } 62 63 var want = `ABC DEFGH IJKLMNOPQRSTUVWXYZ 64 012345 6789 65 ` 66 var got = b.String() 67 68 if got != want { 69 t.Errorf(`Expected text to be "%v", got "%v" instead`, want, got) 70 } 71 }