effect_list_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. `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. var out, errOut bytes.Buffer
  12. cmd := NewEffectCmd()
  13. cmd.SetOut(&out)
  14. cmd.SetErr(&errOut)
  15. cmd.SetArgs([]string{"--list"})
  16. if err := cmd.Execute(); err != nil {
  17. t.Fatalf("effect --list returned error: %v", err)
  18. }
  19. var parsed struct {
  20. Catalog string `json:"catalog"`
  21. Zones []struct {
  22. Zone string `json:"zone"`
  23. Channel int `json:"channel"`
  24. Subsystem string `json:"subsystem"`
  25. Effect string `json:"effect"`
  26. ID int `json:"id"`
  27. } `json:"zones"`
  28. }
  29. if err := json.Unmarshal(out.Bytes(), &parsed); err != nil {
  30. t.Fatalf("effect --list output is not a JSON object: %v (output %q)", err, out.String()[:min(80, out.Len())])
  31. }
  32. if parsed.Catalog != "impact80" {
  33. t.Errorf("catalog = %q, want %q", parsed.Catalog, "impact80")
  34. }
  35. zones := parsed.Zones
  36. if len(zones) == 0 {
  37. t.Fatal("zones is empty, want the full catalog")
  38. }
  39. if len(zones) == 0 {
  40. t.Fatal("zones is empty, want the full catalog")
  41. }
  42. // The catalog itself must not have lost a field, and gained the channel
  43. // identity a consumer needs to address the entry it finds.
  44. if zones[0].Zone == "" || zones[0].Effect == "" {
  45. t.Errorf("zones[0] = %+v, want zone, effect and id populated", zones[0])
  46. }
  47. if zones[0].Channel == 0 || zones[0].Subsystem == "" {
  48. t.Errorf("zones[0] = %+v, want channel and subsystem populated", zones[0])
  49. }
  50. }
  51. func min(a, b int) int {
  52. if a < b {
  53. return a
  54. }
  55. return b
  56. }