profile_load_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package main
  2. import (
  3. "testing"
  4. intrgb "netdome.biz/paul/qmk-rgb/internal/rgb"
  5. )
  6. func sameZones(a, b []intrgb.Zone) bool {
  7. if len(a) != len(b) {
  8. return false
  9. }
  10. for i := range a {
  11. if a[i] != b[i] {
  12. return false
  13. }
  14. }
  15. return true
  16. }
  17. // README.md: "With `--zone`, commands target exactly the selected zone."
  18. // `save` already honours it, so `load` must not rewrite zones the user did
  19. // not select — otherwise a single-zone restore silently touches all three.
  20. func TestFilterZonesKeepsOnlyTheSelection(t *testing.T) {
  21. allZones := []intrgb.Zone{intrgb.ZoneLogo, intrgb.ZoneBacklight, intrgb.ZoneSide}
  22. tests := []struct {
  23. name string
  24. zoneFlag string
  25. want []intrgb.Zone
  26. }{
  27. {"no flag keeps every zone", "", allZones},
  28. {"logo keeps logo", "logo", []intrgb.Zone{intrgb.ZoneLogo}},
  29. {"backlight keeps backlight", "backlight", []intrgb.Zone{intrgb.ZoneBacklight}},
  30. {"side keeps side", "side", []intrgb.Zone{intrgb.ZoneSide}},
  31. }
  32. for _, tt := range tests {
  33. t.Run(tt.name, func(t *testing.T) {
  34. original := targetZone
  35. t.Cleanup(func() { targetZone = original })
  36. targetZone = tt.zoneFlag
  37. selected, err := selectedZones()
  38. if err != nil {
  39. t.Fatalf("selectedZones() returned error: %v", err)
  40. }
  41. got := filterZones(allZones, selected)
  42. if !sameZones(got, tt.want) {
  43. t.Errorf("filterZones(all, %v) = %v, want %v", selected, got, tt.want)
  44. }
  45. })
  46. }
  47. }
  48. // A profile may omit zones the selection names. Those must not be invented.
  49. func TestFilterZonesDoesNotAddMissingZones(t *testing.T) {
  50. original := targetZone
  51. t.Cleanup(func() { targetZone = original })
  52. targetZone = "backlight"
  53. profileZones := []intrgb.Zone{intrgb.ZoneSide}
  54. selected, err := selectedZones()
  55. if err != nil {
  56. t.Fatalf("selectedZones() returned error: %v", err)
  57. }
  58. got := filterZones(profileZones, selected)
  59. if len(got) != 0 {
  60. t.Errorf("filterZones(side-only profile, backlight) = %v, want empty", got)
  61. }
  62. }
  63. // filterZones must not reorder or mutate its input.
  64. func TestFilterZonesDoesNotMutateInput(t *testing.T) {
  65. original := targetZone
  66. t.Cleanup(func() { targetZone = original })
  67. targetZone = "logo"
  68. input := []intrgb.Zone{intrgb.ZoneSide, intrgb.ZoneLogo}
  69. selected, err := selectedZones()
  70. if err != nil {
  71. t.Fatalf("selectedZones() returned error: %v", err)
  72. }
  73. filterZones(input, selected)
  74. if !sameZones(input, []intrgb.Zone{intrgb.ZoneSide, intrgb.ZoneLogo}) {
  75. t.Errorf("filterZones() mutated its input: %v", input)
  76. }
  77. }