github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/cmds/core/seq/seq_test.go (about) 1 // Copyright 2013 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // created by Manoel Vilela < manoel_vilela@engineer.com > 6 7 package main 8 9 import ( 10 "bytes" 11 "io" 12 "reflect" 13 "testing" 14 ) 15 16 type test struct { 17 args []string 18 expect string 19 } 20 21 func resetFlags() { 22 flags.format = "%v" 23 flags.separator = "\n" 24 flags.widthEqual = false 25 } 26 27 func testseq(tests []test, t *testing.T) { 28 for _, tst := range tests { 29 b := bytes.Buffer{} 30 w := io.Writer(&b) 31 if err := seq(w, tst.args); err != nil { 32 t.Error(err) 33 } 34 35 got := b.Bytes() 36 want := []byte(tst.expect) 37 38 if !reflect.DeepEqual(got, want) { 39 t.Logf("Got: \n%v\n", string(got)) 40 t.Logf("Expect: \n%v\n", tst.expect) 41 t.Error("Mismatching output") 42 } 43 } 44 } 45 46 // test default behavior without flags 47 func TestSeqDefault(t *testing.T) { 48 var tests = []test{ 49 { 50 []string{"1", "3"}, 51 "1\n2\n3\n", 52 }, 53 { 54 []string{"1", "0.5", "3"}, 55 "1.0\n1.5\n2.0\n2.5\n3.0\n", 56 }, 57 } 58 59 testseq(tests, t) 60 } 61 62 // test seq fixed width with leading zeros 63 func TestSeqWidthEqual(t *testing.T) { 64 flags.widthEqual = true 65 defer resetFlags() 66 var tests = []test{ 67 { 68 []string{"8", "10"}, 69 "08\n09\n10\n", 70 }, 71 { 72 []string{"8", "0.5", "10"}, 73 "08.0\n08.5\n09.0\n09.5\n10.0\n", 74 }, 75 } 76 77 testseq(tests, t) 78 } 79 80 func TestSeqCustomFormat(t *testing.T) { 81 flags.format = "%.2f" 82 flags.widthEqual = true 83 defer resetFlags() 84 var tests = []test{ 85 { 86 []string{"8", "10"}, 87 "08.00\n09.00\n10.00\n", 88 }, 89 { 90 []string{"8", "0.5", "10"}, 91 "08.00\n08.50\n09.00\n09.50\n10.00\n", 92 }, 93 } 94 95 testseq(tests, t) 96 } 97 98 func TestSeqSeparator(t *testing.T) { 99 flags.separator = "->" 100 defer resetFlags() 101 var tests = []test{ 102 { 103 []string{"8", "10"}, 104 "8->9->10\n", 105 }, 106 { 107 []string{"8", "0.5", "10"}, 108 "8.0->8.5->9.0->9.5->10.0\n", 109 }, 110 } 111 112 testseq(tests, t) 113 114 }