effect_list_test.go 2.0 KB

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