effect_list_test.go 1.7 KB

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