target_seam_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package main
  2. import (
  3. "path/filepath"
  4. "testing"
  5. intdevice "netdome.biz/paul/qmk-rgb/internal/device"
  6. "netdome.biz/paul/qmk-rgb/internal/via"
  7. )
  8. // stubOpenTarget points the commands at one protocol, the display names a board
  9. // would supply and the channels that board has. It returns the restore function.
  10. // The zone the command read is the one it resolves, so a test that passes a
  11. // multi-channel zone gets the same channels the real seam would resolve.
  12. func stubOpenTarget(t *testing.T, proto rgbProtocol, display map[uint16]string, channels []via.Channel, vendorID, productID uint16) func() {
  13. t.Helper()
  14. original := openTarget
  15. openTarget = func(zone string) (rgbProtocol, targetDeviceData, []via.Channel, error) {
  16. target := targetDeviceData{
  17. Device: intdevice.Device{VendorID: vendorID, ProductID: productID},
  18. Display: display,
  19. }
  20. requested, err := resolveZoneName(zone, display, nil)
  21. if err != nil {
  22. return nil, target, nil, err
  23. }
  24. target.Requested = requested
  25. resolved, err := resolveChannels(proto, target)
  26. if err != nil {
  27. return nil, target, nil, err
  28. }
  29. return proto, target, resolved, nil
  30. }
  31. return func() { openTarget = original }
  32. }
  33. // impact80Target is the board most command tests describe.
  34. func impact80Target(t *testing.T, proto rgbProtocol) func() {
  35. t.Helper()
  36. return stubOpenTarget(t, proto, impact80Display(), impact80Channels(), 0x36B0, 0x309F)
  37. }
  38. // withJSON turns on the machine shape for a test that asserts the JSON
  39. // commands, which print text unless it is asked for.
  40. func withJSON(t *testing.T) {
  41. t.Helper()
  42. original := jsonOutput
  43. jsonOutput = true
  44. t.Cleanup(func() { jsonOutput = original })
  45. }
  46. // vendoredDefinitions points the data directory at the repository's definitions
  47. // directory, so a test that needs the Impact 80's effect names uses the file that
  48. // ships with the tool rather than the copy built into the binary. The two are the
  49. // same file, so a test that overrides the directory and one that does not both
  50. // resolve the same names, which is the point of a test that has to tell them
  51. // apart.
  52. func vendoredDefinitions(t *testing.T) func() {
  53. t.Helper()
  54. return forceDefinitionsDir(t, filepath.Join("..", "..", "definitions"))
  55. }