target_seam_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. // directory, so a test that needs the Impact 80's effect names uses the file that
  46. // ships with the tool rather than the copy built into the binary. The two are the
  47. // same file, so a test that overrides the directory and one that does not both
  48. // resolve the same names, which is the point of a test that has to tell them
  49. // apart.
  50. func vendoredDefinitions(t *testing.T) func() {
  51. t.Helper()
  52. return forceDefinitionsDir(t, filepath.Join("..", "..", "definitions"))
  53. }