github.com/webwurst/docker@v1.7.0/builder/bflag_test.go (about) 1 package builder 2 3 import ( 4 "testing" 5 ) 6 7 func TestBuilderFlags(t *testing.T) { 8 var expected string 9 var err error 10 11 // --- 12 13 bf := NewBuilderFlags() 14 bf.Args = []string{} 15 if err := bf.Parse(); err != nil { 16 t.Fatalf("Test1 of %q was supposed to work: %s", bf.Args, err) 17 } 18 19 // --- 20 21 bf = NewBuilderFlags() 22 bf.Args = []string{"--"} 23 if err := bf.Parse(); err != nil { 24 t.Fatalf("Test2 of %q was supposed to work: %s", bf.Args, err) 25 } 26 27 // --- 28 29 bf = NewBuilderFlags() 30 flStr1 := bf.AddString("str1", "") 31 flBool1 := bf.AddBool("bool1", false) 32 bf.Args = []string{} 33 if err = bf.Parse(); err != nil { 34 t.Fatalf("Test3 of %q was supposed to work: %s", bf.Args, err) 35 } 36 37 if flStr1.IsUsed() == true { 38 t.Fatalf("Test3 - str1 was not used!") 39 } 40 if flBool1.IsUsed() == true { 41 t.Fatalf("Test3 - bool1 was not used!") 42 } 43 44 // --- 45 46 bf = NewBuilderFlags() 47 flStr1 = bf.AddString("str1", "HI") 48 flBool1 = bf.AddBool("bool1", false) 49 bf.Args = []string{} 50 51 if err = bf.Parse(); err != nil { 52 t.Fatalf("Test4 of %q was supposed to work: %s", bf.Args, err) 53 } 54 55 if flStr1.Value != "HI" { 56 t.Fatalf("Str1 was supposed to default to: HI") 57 } 58 if flBool1.IsTrue() { 59 t.Fatalf("Bool1 was supposed to default to: false") 60 } 61 if flStr1.IsUsed() == true { 62 t.Fatalf("Str1 was not used!") 63 } 64 if flBool1.IsUsed() == true { 65 t.Fatalf("Bool1 was not used!") 66 } 67 68 // --- 69 70 bf = NewBuilderFlags() 71 flStr1 = bf.AddString("str1", "HI") 72 bf.Args = []string{"--str1"} 73 74 if err = bf.Parse(); err == nil { 75 t.Fatalf("Test %q was supposed to fail", bf.Args) 76 } 77 78 // --- 79 80 bf = NewBuilderFlags() 81 flStr1 = bf.AddString("str1", "HI") 82 bf.Args = []string{"--str1="} 83 84 if err = bf.Parse(); err != nil { 85 t.Fatalf("Test %q was supposed to work: %s", bf.Args, err) 86 } 87 88 expected = "" 89 if flStr1.Value != expected { 90 t.Fatalf("Str1 (%q) should be: %q", flStr1.Value, expected) 91 } 92 93 // --- 94 95 bf = NewBuilderFlags() 96 flStr1 = bf.AddString("str1", "HI") 97 bf.Args = []string{"--str1=BYE"} 98 99 if err = bf.Parse(); err != nil { 100 t.Fatalf("Test %q was supposed to work: %s", bf.Args, err) 101 } 102 103 expected = "BYE" 104 if flStr1.Value != expected { 105 t.Fatalf("Str1 (%q) should be: %q", flStr1.Value, expected) 106 } 107 108 // --- 109 110 bf = NewBuilderFlags() 111 flBool1 = bf.AddBool("bool1", false) 112 bf.Args = []string{"--bool1"} 113 114 if err = bf.Parse(); err != nil { 115 t.Fatalf("Test %q was supposed to work: %s", bf.Args, err) 116 } 117 118 if !flBool1.IsTrue() { 119 t.Fatalf("Test-b1 Bool1 was supposed to be true") 120 } 121 122 // --- 123 124 bf = NewBuilderFlags() 125 flBool1 = bf.AddBool("bool1", false) 126 bf.Args = []string{"--bool1=true"} 127 128 if err = bf.Parse(); err != nil { 129 t.Fatalf("Test %q was supposed to work: %s", bf.Args, err) 130 } 131 132 if !flBool1.IsTrue() { 133 t.Fatalf("Test-b2 Bool1 was supposed to be true") 134 } 135 136 // --- 137 138 bf = NewBuilderFlags() 139 flBool1 = bf.AddBool("bool1", false) 140 bf.Args = []string{"--bool1=false"} 141 142 if err = bf.Parse(); err != nil { 143 t.Fatalf("Test %q was supposed to work: %s", bf.Args, err) 144 } 145 146 if flBool1.IsTrue() { 147 t.Fatalf("Test-b3 Bool1 was supposed to be false") 148 } 149 150 // --- 151 152 bf = NewBuilderFlags() 153 flBool1 = bf.AddBool("bool1", false) 154 bf.Args = []string{"--bool1=false1"} 155 156 if err = bf.Parse(); err == nil { 157 t.Fatalf("Test %q was supposed to fail", bf.Args) 158 } 159 160 // --- 161 162 bf = NewBuilderFlags() 163 flBool1 = bf.AddBool("bool1", false) 164 bf.Args = []string{"--bool2"} 165 166 if err = bf.Parse(); err == nil { 167 t.Fatalf("Test %q was supposed to fail", bf.Args) 168 } 169 170 // --- 171 172 bf = NewBuilderFlags() 173 flStr1 = bf.AddString("str1", "HI") 174 flBool1 = bf.AddBool("bool1", false) 175 bf.Args = []string{"--bool1", "--str1=BYE"} 176 177 if err = bf.Parse(); err != nil { 178 t.Fatalf("Test %q was supposed to work: %s", bf.Args, err) 179 } 180 181 if flStr1.Value != "BYE" { 182 t.Fatalf("Teset %s, str1 should be BYE", bf.Args) 183 } 184 if !flBool1.IsTrue() { 185 t.Fatalf("Teset %s, bool1 should be true", bf.Args) 186 } 187 }