effect_list_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "testing"
  6. )
  7. // Every other JSON command emits an object, so a consumer can add fields
  8. // later without breaking. The effect list returned a bare array because the
  9. // EffectList wrapper it built was bypassed in favour of its slice.
  10. func TestEffectListEmitsAnObjectNotABareArray(t *testing.T) {
  11. t.Cleanup(vendoredDefinitions(t))
  12. withJSON(t)
  13. t.Cleanup(impact80Target(t, &fakeZoneProtocol{failAt: -1}))
  14. var out, errOut bytes.Buffer
  15. cmd := NewEffectCmd()
  16. cmd.SetOut(&out)
  17. cmd.SetErr(&errOut)
  18. cmd.SetArgs([]string{zoneAll})
  19. if err := cmd.Execute(); err != nil {
  20. t.Fatalf("effect %s returned error: %v", zoneAll, err)
  21. }
  22. var parsed struct {
  23. Catalog string `json:"catalog"`
  24. Zones []struct {
  25. Zone string `json:"zone"`
  26. Channel int `json:"channel"`
  27. Subsystem string `json:"subsystem"`
  28. Effect string `json:"effect"`
  29. ID int `json:"id"`
  30. } `json:"zones"`
  31. }
  32. if err := json.Unmarshal(out.Bytes(), &parsed); err != nil {
  33. t.Fatalf("effect all output is not a JSON object: %v (output %q)", err, out.String()[:min(80, out.Len())])
  34. }
  35. if parsed.Catalog != "impact80" {
  36. // The catalog is named by the definition file it was read from, so the name
  37. // here is the vendor's own rather than a label the tool invented.
  38. if parsed.Catalog != "Impact 80" {
  39. t.Errorf("catalog = %q, want the definition file's name %q", parsed.Catalog, "Impact 80")
  40. }
  41. }
  42. zones := parsed.Zones
  43. if len(zones) == 0 {
  44. t.Fatal("zones is empty, want the full catalog")
  45. }
  46. if len(zones) == 0 {
  47. t.Fatal("zones is empty, want the full catalog")
  48. }
  49. // The catalog itself must not have lost a field, and gained the channel
  50. // identity a consumer needs to address the entry it finds.
  51. if zones[0].Zone == "" || zones[0].Effect == "" {
  52. t.Errorf("zones[0] = %+v, want zone, effect and id populated", zones[0])
  53. }
  54. if zones[0].Channel == 0 || zones[0].Subsystem == "" {
  55. t.Errorf("zones[0] = %+v, want channel and subsystem populated", zones[0])
  56. }
  57. }
  58. func min(a, b int) int {
  59. if a < b {
  60. return a
  61. }
  62. return b
  63. }