noolite_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright 2016 Denis V. Dedkov (denis.v.dedkov@gmail.com)
  3. This file is part of Noolite Go bindings.
  4. Noolite Go bindings is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Noolite Go bindings is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Noolite Go bindings. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package noolite
  16. import "testing"
  17. func TestDefaultNooliteAdapter(t *testing.T) {
  18. n, err := DefaultNooliteAdapter()
  19. if err != nil {
  20. t.Error(err)
  21. }
  22. defer n.Close()
  23. cmd := n.composeCommand(0x00, 0x00)
  24. if cmd[0] != 0x50 {
  25. t.Error("Adapter mode (first byte): expected 0x50, got", cmd[0])
  26. }
  27. }
  28. func TestNewNooliteAdapter(t *testing.T) {
  29. n, err := NewNooliteAdapter(0, 2, 2)
  30. if err != nil {
  31. t.Error(err)
  32. }
  33. n.Close()
  34. n, err = NewNooliteAdapter(8, 2, 2)
  35. if err == nil {
  36. t.Error("Bad value for mode. Must be in range 0..7")
  37. }
  38. n, err = NewNooliteAdapter(0, 4, 2)
  39. if err == nil {
  40. t.Error("Bad value for bitrate. Must be in range 0..3")
  41. }
  42. n, err = NewNooliteAdapter(0, 2, 8)
  43. if err == nil {
  44. t.Error("Bad value for mode. Must be in range 0..7")
  45. }
  46. }
  47. type teststr struct {
  48. values []byte
  49. cmd command
  50. args []int
  51. }
  52. func TestComposeCommand(t *testing.T) {
  53. n, err := DefaultNooliteAdapter()
  54. if err != nil {
  55. t.Error(err)
  56. }
  57. defer n.Close()
  58. var tests = []teststr{
  59. {[]byte{0x50, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, off, []int{}},
  60. {[]byte{0x50, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, decBrightnes, []int{}},
  61. {[]byte{0x50, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, on, []int{}},
  62. {[]byte{0x50, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, incBrightnes, []int{}},
  63. {[]byte{0x50, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, cSwitch, []int{}},
  64. {[]byte{0x50, 0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, invertBrightnes, []int{}},
  65. {[]byte{0x50, 0x06, 0x01, 0x00, 0x01, 0x01, 0x00, 0x00}, set, []int{1}},
  66. {[]byte{0x50, 0x06, 0x03, 0x00, 0x01, 0x01, 0x01, 0x01}, set, []int{1, 1, 1}},
  67. {[]byte{0x50, 0x07, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, callScenario, []int{}},
  68. {[]byte{0x50, 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, saveScenario, []int{}},
  69. {[]byte{0x50, 0x09, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, unbind, []int{}},
  70. {[]byte{0x50, 0x0a, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, stopColorSelection, []int{}},
  71. {[]byte{0x50, 0x0f, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, bind, []int{}},
  72. {[]byte{0x50, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, colorSelection, []int{}},
  73. {[]byte{0x50, 0x11, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, colorSwitch, []int{}},
  74. {[]byte{0x50, 0x12, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, modeSwitch, []int{}},
  75. {[]byte{0x50, 0x13, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, effectSpeed, []int{}},
  76. }
  77. for _, tstr := range tests {
  78. cmd := n.composeCommand(tstr.cmd, 1, tstr.args...)
  79. for i, v := range cmd {
  80. if v != tstr.values[i] {
  81. t.Error("For command",
  82. tstr.cmd,
  83. "expected",
  84. tstr.values[i],
  85. "got", v,
  86. "in position", i)
  87. }
  88. }
  89. }
  90. }