target_seam_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. func stubOpenTarget(t *testing.T, proto rgbProtocol, display map[uint16]string, channels []via.Channel, vendorID, productID uint16) func() {
  11. t.Helper()
  12. original := openTarget
  13. openTarget = func() (rgbProtocol, targetDeviceData, []via.Channel, error) {
  14. target := targetDeviceData{
  15. Device: intdevice.Device{VendorID: vendorID, ProductID: productID},
  16. Display: display,
  17. }
  18. requested, err := resolveZoneName(targetZone, display, nil)
  19. if err != nil {
  20. return nil, target, nil, err
  21. }
  22. target.Requested = requested
  23. resolved, err := resolveChannels(proto, target)
  24. if err != nil {
  25. return nil, target, nil, err
  26. }
  27. return proto, target, resolved, nil
  28. }
  29. return func() { openTarget = original }
  30. }
  31. // impact80Target is the board most command tests describe.
  32. func impact80Target(t *testing.T, proto rgbProtocol) func() {
  33. t.Helper()
  34. return stubOpenTarget(t, proto, impact80Display(), impact80Channels(), 0x36B0, 0x309F)
  35. }
  36. // withJSON turns on the machine shape for a test that asserts the JSON
  37. // commands, which print text unless it is asked for.
  38. func withJSON(t *testing.T) {
  39. t.Helper()
  40. original := jsonOutput
  41. jsonOutput = true
  42. t.Cleanup(func() { jsonOutput = original })
  43. }
  44. // vendoredDefinitions points the data directory at the repository's definitions,
  45. // so a test that needs the Impact 80's effect names uses the file that ships with
  46. // the tool rather than a catalog compiled in beside it. There is no such catalog
  47. // any more: the file is the only source of a name.
  48. func vendoredDefinitions(t *testing.T) func() {
  49. t.Helper()
  50. return forceDefinitionsDir(t, filepath.Join("..", "..", "definitions"))
  51. }