rgb_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package rgb
  2. import (
  3. "testing"
  4. "github.com/spf13/cobra"
  5. )
  6. func TestLightingEnabledRequiresActiveEffectAndBrightness(t *testing.T) {
  7. cases := []struct {
  8. name string
  9. mode uint8
  10. brightness uint8
  11. want bool
  12. }{
  13. {"active", 1, 160, true},
  14. {"zero brightness", 1, 0, false},
  15. {"disabled effect", 0, 160, false},
  16. {"both zero", 0, 0, false},
  17. }
  18. for _, tc := range cases {
  19. t.Run(tc.name, func(t *testing.T) {
  20. if got := lightingEnabled(tc.mode, tc.brightness); got != tc.want {
  21. t.Errorf("lightingEnabled(%d, %d) = %t, want %t", tc.mode, tc.brightness, got, tc.want)
  22. }
  23. })
  24. }
  25. }
  26. func TestDeviceFlagIsInheritedByRGBCommands(t *testing.T) {
  27. originalTargetDevice := targetDevice
  28. t.Cleanup(func() { targetDevice = originalTargetDevice })
  29. const wantPath = "/dev/hidraw5"
  30. ran := false
  31. cmd := Init()
  32. cmd.AddCommand(&cobra.Command{
  33. Use: "probe",
  34. Run: func(*cobra.Command, []string) { ran = true },
  35. })
  36. cmd.SetArgs([]string{"--device", wantPath, "probe"})
  37. if err := cmd.Execute(); err != nil {
  38. t.Fatalf("Execute() error = %v", err)
  39. }
  40. if !ran {
  41. t.Fatal("child command did not run")
  42. }
  43. if targetDevice != wantPath {
  44. t.Errorf("targetDevice = %q, want %q", targetDevice, wantPath)
  45. }
  46. }