|
| 1 | +package main_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + godog "github.com/DATA-DOG/godog" |
| 13 | + gherkin "github.com/DATA-DOG/godog/gherkin" |
| 14 | + assistdog "github.com/hellomd/assistdog" |
| 15 | + |
| 16 | + _ "github.com/multiformats/go-multiaddr" |
| 17 | +) |
| 18 | + |
| 19 | +var multiaddrCmd = []string{"go", "run", "github.com/multiformats/go-multiaddr/multiaddr"} |
| 20 | +var assist *assistdog.Assist |
| 21 | + |
| 22 | +func init() { |
| 23 | + bin := os.Getenv("MULTIADDR_BIN") |
| 24 | + if len(bin) > 0 { |
| 25 | + multiaddrCmd = strings.Split(bin, " ") |
| 26 | + } |
| 27 | + |
| 28 | + assist = assistdog.NewDefault() |
| 29 | +} |
| 30 | + |
| 31 | +func TestGodog(t *testing.T) { |
| 32 | + fmt.Printf(`MULTIADDR_BIN="%s"`+"\n", strings.Join(multiaddrCmd, " ")) |
| 33 | + |
| 34 | + status := godog.RunWithOptions("godog", func(s *godog.Suite) { |
| 35 | + FeatureContext(s) |
| 36 | + }, godog.Options{ |
| 37 | + Format: "pretty", |
| 38 | + Paths: []string{"."}, |
| 39 | + StopOnFailure: true, |
| 40 | + Strict: true, |
| 41 | + }) |
| 42 | + |
| 43 | + if status != 0 { |
| 44 | + t.Errorf("exit status: %d", status) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func FeatureContext(s *godog.Suite) { |
| 49 | + s.Step(`^the multiaddr (.+)$`, theMultiaddr) |
| 50 | + s.Step(`^the packed form is (.+)$`, thePackedFormIs) |
| 51 | + s.Step(`^the packed size is (.+) bytes$`, thePackedSizeIs) |
| 52 | + s.Step(`^the string form is (.+)$`, theStringFormIs) |
| 53 | + s.Step(`^the string size is (.+) bytes$`, theStringSizeIs) |
| 54 | + s.Step(`^the components are:$`, theComponentsAre) |
| 55 | +} |
| 56 | + |
| 57 | +var theMaddr multiaddr |
| 58 | + |
| 59 | +type multiaddr struct { |
| 60 | + String string |
| 61 | + StringSize string |
| 62 | + Packed string |
| 63 | + PackedSize string |
| 64 | + Components []component |
| 65 | +} |
| 66 | + |
| 67 | +type component struct { |
| 68 | + String string |
| 69 | + StringSize string |
| 70 | + Packed string |
| 71 | + PackedSize string |
| 72 | + Value string |
| 73 | + RawValue string |
| 74 | + ValueSize string |
| 75 | + Protocol string |
| 76 | + Codec string |
| 77 | + Uvarint string |
| 78 | + LengthPrefix string |
| 79 | +} |
| 80 | + |
| 81 | +func theMultiaddr(addr string) error { |
| 82 | + theMaddr = multiaddr{} |
| 83 | + |
| 84 | + cmd := exec.Command(multiaddrCmd[0], append(multiaddrCmd[1:], addr)...) |
| 85 | + cmd.Stdout = &bytes.Buffer{} |
| 86 | + cmd.Stderr = &bytes.Buffer{} |
| 87 | + if err := cmd.Run(); err != nil { |
| 88 | + return fmt.Errorf("command error: %s - stderr: %s", err, cmd.Stderr.(*bytes.Buffer).String()) |
| 89 | + } |
| 90 | + |
| 91 | + stdout := cmd.Stdout.(*bytes.Buffer) |
| 92 | + if err := json.Unmarshal(stdout.Bytes(), &theMaddr); err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +func thePackedFormIs(packed string) error { |
| 99 | + if theMaddr.Packed != packed { |
| 100 | + return fmt.Errorf("expected %s, got %s", packed, theMaddr.Packed) |
| 101 | + } |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +func thePackedSizeIs(size string) error { |
| 106 | + if theMaddr.PackedSize != size { |
| 107 | + return fmt.Errorf("expected %s, got %s", size, theMaddr.PackedSize) |
| 108 | + } |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +func theStringFormIs(str string) error { |
| 113 | + if theMaddr.String != str { |
| 114 | + return fmt.Errorf("expected %s, got %s", str, theMaddr.String) |
| 115 | + } |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +func theStringSizeIs(size string) error { |
| 120 | + if theMaddr.StringSize != size { |
| 121 | + return fmt.Errorf("expected %s, got %s", size, theMaddr.StringSize) |
| 122 | + } |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +func theComponentsAre(tbl *gherkin.DataTable) error { |
| 127 | + table, err := assist.ParseSlice(tbl) |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + |
| 132 | + actual := len(theMaddr.Components) |
| 133 | + expected := len(table) |
| 134 | + if actual != expected { |
| 135 | + return fmt.Errorf("expected %d row, got %d", expected, actual) |
| 136 | + } |
| 137 | + |
| 138 | + for i, _ := range table { |
| 139 | + actual, ok := table[i]["string"] |
| 140 | + if ok { |
| 141 | + expected := theMaddr.Components[i].String |
| 142 | + if actual != expected { |
| 143 | + return fmt.Errorf("expected string '%s', got '%s'", expected, actual) |
| 144 | + } |
| 145 | + } |
| 146 | + actual, ok = table[i]["stringSize"] |
| 147 | + if ok { |
| 148 | + expected := theMaddr.Components[i].StringSize |
| 149 | + if actual != expected { |
| 150 | + return fmt.Errorf("expected stringSize '%s', got '%s'", expected, actual) |
| 151 | + } |
| 152 | + } |
| 153 | + actual, ok = table[i]["packed"] |
| 154 | + if ok { |
| 155 | + expected := theMaddr.Components[i].Packed |
| 156 | + if actual != expected { |
| 157 | + return fmt.Errorf("expected packed '%s', got '%s'", expected, actual) |
| 158 | + } |
| 159 | + } |
| 160 | + actual, ok = table[i]["packedSize"] |
| 161 | + if ok { |
| 162 | + expected := theMaddr.Components[i].PackedSize |
| 163 | + if actual != expected { |
| 164 | + return fmt.Errorf("expected packedSize '%s', got '%s'", expected, actual) |
| 165 | + } |
| 166 | + } |
| 167 | + actual, ok = table[i]["value"] |
| 168 | + if ok { |
| 169 | + expected := theMaddr.Components[i].Value |
| 170 | + if actual != expected { |
| 171 | + return fmt.Errorf("expected value '%s', got '%s'", expected, actual) |
| 172 | + } |
| 173 | + } |
| 174 | + actual, ok = table[i]["rawValue"] |
| 175 | + if ok { |
| 176 | + expected := theMaddr.Components[i].RawValue |
| 177 | + if actual != expected { |
| 178 | + return fmt.Errorf("expected rawValue '%s', got '%s'", expected, actual) |
| 179 | + } |
| 180 | + } |
| 181 | + actual, ok = table[i]["valueSize"] |
| 182 | + if ok { |
| 183 | + expected := theMaddr.Components[i].ValueSize |
| 184 | + if actual != expected { |
| 185 | + return fmt.Errorf("expected valueSize '%s', got '%s'", expected, actual) |
| 186 | + } |
| 187 | + } |
| 188 | + actual, ok = table[i]["protocol"] |
| 189 | + if ok { |
| 190 | + expected := theMaddr.Components[i].Protocol |
| 191 | + if actual != expected { |
| 192 | + return fmt.Errorf("expected protocol '%s', got '%s'", expected, actual) |
| 193 | + } |
| 194 | + } |
| 195 | + actual, ok = table[i]["codec"] |
| 196 | + if ok { |
| 197 | + expected := theMaddr.Components[i].Codec |
| 198 | + if actual != expected { |
| 199 | + return fmt.Errorf("expected codec '%s', got '%s'", expected, actual) |
| 200 | + } |
| 201 | + } |
| 202 | + actual, ok = table[i]["uvarint"] |
| 203 | + if ok { |
| 204 | + expected := theMaddr.Components[i].Uvarint |
| 205 | + if actual != expected { |
| 206 | + return fmt.Errorf("expected uvarint '%s', got '%s'", expected, actual) |
| 207 | + } |
| 208 | + } |
| 209 | + actual, ok = table[i]["lengthPrefix"] |
| 210 | + if ok { |
| 211 | + expected := theMaddr.Components[i].LengthPrefix |
| 212 | + if actual != expected { |
| 213 | + return fmt.Errorf("expected lengthPrefix '%s', got '%s'", expected, actual) |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + return nil |
| 219 | +} |
0 commit comments