github.com/quantumghost/awgo@v0.15.0/util/formatting_test.go (about) 1 // 2 // Copyright (c) 2018 Dean Jackson <deanishe@deanishe.net> 3 // 4 // MIT Licence. See http://opensource.org/licenses/MIT 5 // 6 // Created on 2018-02-10 7 // 8 9 package util 10 11 import ( 12 "testing" 13 ) 14 15 type padTest struct { 16 str string // input string 17 pad string // pad character 18 n int // size to pad to 19 x string // expected result 20 } 21 22 // TestPadLeft tests PadLeft 23 func TestPadLeft(t *testing.T) { 24 var padLeftTests = []padTest{ 25 // Simple cases 26 padTest{"wow", "-", 5, "--wow"}, 27 padTest{"pow", " ", 4, " pow"}, 28 // Input same length as n 29 padTest{"pow", " ", 3, "pow"}, 30 // Input longer than n 31 padTest{"powwow", " ", 3, "powwow"}, 32 } 33 for _, td := range padLeftTests { 34 if out := PadLeft(td.str, td.pad, td.n); out != td.x { 35 t.Fatalf("PadLeft output incorrect. Expected=%v, Got=%v", td.x, out) 36 } 37 } 38 } 39 40 // TestPadRight tests PadRight 41 func TestPadRight(t *testing.T) { 42 43 var padRightTests = []padTest{ 44 // Simple cases 45 padTest{"wow", "-", 5, "wow--"}, 46 padTest{"pow", " ", 4, "pow "}, 47 // Input same length as n 48 padTest{"pow", " ", 3, "pow"}, 49 // Input longer than n 50 padTest{"powwow", " ", 3, "powwow"}, 51 } 52 for _, td := range padRightTests { 53 if out := PadRight(td.str, td.pad, td.n); out != td.x { 54 t.Fatalf("PadRight output incorrect. Expected=%v, Got=%v", td.x, out) 55 } 56 } 57 } 58 59 // TestPad tests Pad 60 func TestPad(t *testing.T) { 61 62 var padTests = []padTest{ 63 // Simple cases 64 padTest{"wow", "-", 5, "-wow-"}, 65 padTest{"pow", " ", 4, "pow "}, 66 // Even-length str 67 padTest{"wow", "-", 10, "---wow----"}, 68 // Input same length as n 69 padTest{"pow", " ", 3, "pow"}, 70 // Input longer than n 71 padTest{"powwow", " ", 3, "powwow"}, 72 } 73 for _, td := range padTests { 74 if out := Pad(td.str, td.pad, td.n); out != td.x { 75 t.Fatalf("Pad output incorrect. Expected=%v, Got=%v", td.x, out) 76 } 77 } 78 }