mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
conver midi.message.create into separate processors
This commit is contained in:
161
internal/processor/test/midi-control_change-create_test.go
Normal file
161
internal/processor/test/midi-control_change-create_test.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
|
||||
func TestMIDIControlChangeCreateFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["midi.control_change.create"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.control_change.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "midi.control_change.create",
|
||||
Params: map[string]any{
|
||||
"channel": "1",
|
||||
"control": "60",
|
||||
"value": "100",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create midi.control_change.create processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "midi.control_change.create" {
|
||||
t.Fatalf("midi.control_change.create processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodMIDIControlChangeCreate(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
payload any
|
||||
expected any
|
||||
}{
|
||||
{
|
||||
name: "control_change message",
|
||||
params: map[string]any{
|
||||
"type": "control_change",
|
||||
"channel": "1",
|
||||
"control": "64",
|
||||
"value": "127",
|
||||
},
|
||||
payload: "test",
|
||||
expected: midi.ControlChange(1, 64, 127),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["midi.control_change.create"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.control_change.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "midi.control_change.create",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("midi.control_change.create failed to create processor: %s", err)
|
||||
}
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload))
|
||||
if err != nil {
|
||||
t.Fatalf("midi.control_change.create processing failed: %s", err)
|
||||
}
|
||||
|
||||
gotMessage, ok := got.Payload.(midi.Message)
|
||||
if !ok {
|
||||
t.Fatalf("midi.control_change.create returned a %T payload: %+v", got, got)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(gotMessage, test.expected) {
|
||||
t.Fatalf("midi.control_change.create got %v, expected %v", gotMessage, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadMIDIControlChangeCreate(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
payload any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "control_change no channel",
|
||||
params: map[string]any{
|
||||
"type": "control_change",
|
||||
"control": "64",
|
||||
"value": "127",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.control_change.create channel error: not found",
|
||||
},
|
||||
{
|
||||
name: "control_change no control",
|
||||
params: map[string]any{
|
||||
"type": "control_change",
|
||||
"channel": "1",
|
||||
"value": "127",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.control_change.create control error: not found",
|
||||
},
|
||||
{
|
||||
name: "control_change no value",
|
||||
params: map[string]any{
|
||||
"type": "control_change",
|
||||
"channel": "1",
|
||||
"control": "64",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.control_change.create value error: not found",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["midi.control_change.create"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.control_change.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "midi.control_change.create",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("midi.control_change.create got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload))
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("midi.control_change.create expected to fail but succeeded, got: %v", got)
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("midi.control_change.create got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,294 +0,0 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
|
||||
func TestMIDIMessageCreateFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["midi.message.create"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.message.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "midi.message.create",
|
||||
Params: map[string]any{
|
||||
"type": "note_on",
|
||||
"channel": "1",
|
||||
"note": "60",
|
||||
"velocity": "100",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create midi.message.create processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "midi.message.create" {
|
||||
t.Fatalf("midi.message.create processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodMIDIMessageCreate(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
payload any
|
||||
expected any
|
||||
}{
|
||||
{
|
||||
name: "note_on message",
|
||||
params: map[string]any{
|
||||
"type": "note_on",
|
||||
"channel": "1",
|
||||
"note": "60",
|
||||
"velocity": "100",
|
||||
},
|
||||
payload: "test",
|
||||
expected: midi.NoteOn(1, 60, 100),
|
||||
},
|
||||
{
|
||||
name: "note_off message",
|
||||
params: map[string]any{
|
||||
"type": "note_off",
|
||||
"channel": "1",
|
||||
"note": "60",
|
||||
"velocity": "100",
|
||||
},
|
||||
payload: "test",
|
||||
expected: midi.NoteOffVelocity(1, 60, 100),
|
||||
},
|
||||
{
|
||||
name: "control_change message",
|
||||
params: map[string]any{
|
||||
"type": "control_change",
|
||||
"channel": "1",
|
||||
"control": "64",
|
||||
"value": "127",
|
||||
},
|
||||
payload: "test",
|
||||
expected: midi.ControlChange(1, 64, 127),
|
||||
},
|
||||
{
|
||||
name: "program_change message",
|
||||
params: map[string]any{
|
||||
"type": "program_change",
|
||||
"channel": "1",
|
||||
"program": "10",
|
||||
},
|
||||
payload: "test",
|
||||
expected: midi.ProgramChange(1, 10),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["midi.message.create"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.message.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "midi.message.create",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("midi.message.create failed to create processor: %s", err)
|
||||
}
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload))
|
||||
if err != nil {
|
||||
t.Fatalf("midi.message.create processing failed: %s", err)
|
||||
}
|
||||
|
||||
gotMessage, ok := got.Payload.(midi.Message)
|
||||
if !ok {
|
||||
t.Fatalf("midi.message.create returned a %T payload: %+v", got, got)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(gotMessage, test.expected) {
|
||||
t.Fatalf("midi.message.create got %v, expected %v", gotMessage, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadMIDIMessageCreate(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
payload any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "no type parameter",
|
||||
params: map[string]any{},
|
||||
payload: "test",
|
||||
errorString: "midi.message.create type error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-string type parameter",
|
||||
params: map[string]any{
|
||||
"type": 1,
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.message.create type error: not a string",
|
||||
},
|
||||
{
|
||||
name: "unknown type parameter",
|
||||
params: map[string]any{
|
||||
"type": "asdf",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.message.create does not support type asdf",
|
||||
},
|
||||
{
|
||||
name: "note_on message no channel",
|
||||
params: map[string]any{
|
||||
"type": "note_on",
|
||||
"note": "60",
|
||||
"velocity": "100",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.message.create channel error: not found",
|
||||
},
|
||||
{
|
||||
name: "note_on message no note",
|
||||
params: map[string]any{
|
||||
"type": "note_on",
|
||||
"channel": "1",
|
||||
"velocity": "100",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.message.create note error: not found",
|
||||
},
|
||||
{
|
||||
name: "note_on message no velocity",
|
||||
params: map[string]any{
|
||||
"type": "note_on",
|
||||
"channel": "1",
|
||||
"note": "60",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.message.create velocity error: not found",
|
||||
},
|
||||
{
|
||||
name: "note_off message no channel",
|
||||
params: map[string]any{
|
||||
"type": "note_off",
|
||||
"note": "60",
|
||||
"velocity": "100",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.message.create channel error: not found",
|
||||
},
|
||||
{
|
||||
name: "note_off message no note",
|
||||
params: map[string]any{
|
||||
"type": "note_off",
|
||||
"channel": "1",
|
||||
"velocity": "100",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.message.create note error: not found",
|
||||
},
|
||||
{
|
||||
name: "note_off message no velocity",
|
||||
params: map[string]any{
|
||||
"type": "note_off",
|
||||
"channel": "1",
|
||||
"note": "60",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.message.create velocity error: not found",
|
||||
},
|
||||
{
|
||||
name: "control_change no channel",
|
||||
params: map[string]any{
|
||||
"type": "control_change",
|
||||
"control": "64",
|
||||
"value": "127",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.message.create channel error: not found",
|
||||
},
|
||||
{
|
||||
name: "control_change no control",
|
||||
params: map[string]any{
|
||||
"type": "control_change",
|
||||
"channel": "1",
|
||||
"value": "127",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.message.create control error: not found",
|
||||
},
|
||||
{
|
||||
name: "control_change no value",
|
||||
params: map[string]any{
|
||||
"type": "control_change",
|
||||
"channel": "1",
|
||||
"control": "64",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.message.create value error: not found",
|
||||
},
|
||||
{
|
||||
name: "program_change no channel",
|
||||
params: map[string]any{
|
||||
"type": "program_change",
|
||||
"program": "64",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.message.create channel error: not found",
|
||||
},
|
||||
{
|
||||
name: "program_change no program",
|
||||
params: map[string]any{
|
||||
"type": "program_change",
|
||||
"channel": "1",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.message.create program error: not found",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["midi.message.create"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.message.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "midi.message.create",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("midi.message.create got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload))
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("midi.message.create expected to fail but succeeded, got: %v", got)
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("midi.message.create got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
160
internal/processor/test/midi-note_off-create_test.go
Normal file
160
internal/processor/test/midi-note_off-create_test.go
Normal file
@@ -0,0 +1,160 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
|
||||
func TestMIDINoteOffCreteaFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["midi.note_off.create"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.note_off.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "midi.note_off.create",
|
||||
Params: map[string]any{
|
||||
"channel": "1",
|
||||
"note": "60",
|
||||
"velocity": "100",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create midi.note_off.create processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "midi.note_off.create" {
|
||||
t.Fatalf("midi.note_off.create processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodMIDINoteOffCretea(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
payload any
|
||||
expected any
|
||||
}{
|
||||
{
|
||||
name: "note_off message",
|
||||
params: map[string]any{
|
||||
"channel": "1",
|
||||
"note": "60",
|
||||
"velocity": "100",
|
||||
},
|
||||
payload: "test",
|
||||
expected: midi.NoteOffVelocity(1, 60, 100),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["midi.note_off.create"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.note_off.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "midi.note_off.create",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("midi.note_off.create failed to create processor: %s", err)
|
||||
}
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload))
|
||||
if err != nil {
|
||||
t.Fatalf("midi.note_off.create processing failed: %s", err)
|
||||
}
|
||||
|
||||
gotMessage, ok := got.Payload.(midi.Message)
|
||||
if !ok {
|
||||
t.Fatalf("midi.note_off.create returned a %T payload: %+v", got, got)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(gotMessage, test.expected) {
|
||||
t.Fatalf("midi.note_off.create got %v, expected %v", gotMessage, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadMIDINoteOffCretea(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
payload any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "note_off message no channel",
|
||||
params: map[string]any{
|
||||
"type": "note_off",
|
||||
"note": "60",
|
||||
"velocity": "100",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.note_off.create channel error: not found",
|
||||
},
|
||||
{
|
||||
name: "note_off message no note",
|
||||
params: map[string]any{
|
||||
"type": "note_off",
|
||||
"channel": "1",
|
||||
"velocity": "100",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.note_off.create note error: not found",
|
||||
},
|
||||
{
|
||||
name: "note_off message no velocity",
|
||||
params: map[string]any{
|
||||
"type": "note_off",
|
||||
"channel": "1",
|
||||
"note": "60",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.note_off.create velocity error: not found",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["midi.note_off.create"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.note_off.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "midi.note_off.create",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("midi.note_off.create got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload))
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("midi.note_off.create expected to fail but succeeded, got: %v", got)
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("midi.note_off.create got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
157
internal/processor/test/midi-note_on-create_test.go
Normal file
157
internal/processor/test/midi-note_on-create_test.go
Normal file
@@ -0,0 +1,157 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
|
||||
func TestMIDINoteOnCreateFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["midi.note_on.create"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.note_on.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "midi.note_on.create",
|
||||
Params: map[string]any{
|
||||
"channel": "1",
|
||||
"note": "60",
|
||||
"velocity": "100",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create midi.note_on.create processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "midi.note_on.create" {
|
||||
t.Fatalf("midi.note_on.create processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodMIDINoteOnCreate(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
payload any
|
||||
expected any
|
||||
}{
|
||||
{
|
||||
name: "note_on message",
|
||||
params: map[string]any{
|
||||
"channel": "1",
|
||||
"note": "60",
|
||||
"velocity": "100",
|
||||
},
|
||||
payload: "test",
|
||||
expected: midi.NoteOn(1, 60, 100),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["midi.note_on.create"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.note_on.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "midi.note_on.create",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("midi.note_on.create failed to create processor: %s", err)
|
||||
}
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload))
|
||||
if err != nil {
|
||||
t.Fatalf("midi.note_on.create processing failed: %s", err)
|
||||
}
|
||||
|
||||
gotMessage, ok := got.Payload.(midi.Message)
|
||||
if !ok {
|
||||
t.Fatalf("midi.note_on.create returned a %T payload: %+v", got, got)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(gotMessage, test.expected) {
|
||||
t.Fatalf("midi.note_on.create got %v, expected %v", gotMessage, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadMIDINoteOnCreate(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
payload any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "note_on message no channel",
|
||||
params: map[string]any{
|
||||
"note": "60",
|
||||
"velocity": "100",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.note_on.create channel error: not found",
|
||||
},
|
||||
{
|
||||
name: "note_on message no note",
|
||||
params: map[string]any{
|
||||
"channel": "1",
|
||||
"velocity": "100",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.note_on.create note error: not found",
|
||||
},
|
||||
{
|
||||
name: "note_on message no velocity",
|
||||
params: map[string]any{
|
||||
"channel": "1",
|
||||
"note": "60",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.note_on.create velocity error: not found",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["midi.note_on.create"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.note_on.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "midi.note_on.create",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("midi.note_on.create got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload))
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("midi.note_on.create expected to fail but succeeded, got: %v", got)
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("midi.note_on.create got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
147
internal/processor/test/midi-program_change-create_test.go
Normal file
147
internal/processor/test/midi-program_change-create_test.go
Normal file
@@ -0,0 +1,147 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
|
||||
func TestMIDIProgramChangeCreateFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["midi.program_change.create"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.program_change.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "midi.program_change.create",
|
||||
Params: map[string]any{
|
||||
"channel": "1",
|
||||
"program": "60",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create midi.program_change.create processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "midi.program_change.create" {
|
||||
t.Fatalf("midi.program_change.create processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodMIDIProgramChangeCreate(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
payload any
|
||||
expected any
|
||||
}{
|
||||
{
|
||||
name: "program_change message",
|
||||
params: map[string]any{
|
||||
"type": "program_change",
|
||||
"channel": "1",
|
||||
"program": "10",
|
||||
},
|
||||
payload: "test",
|
||||
expected: midi.ProgramChange(1, 10),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["midi.program_change.create"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.program_change.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "midi.program_change.create",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("midi.program_change.create failed to create processor: %s", err)
|
||||
}
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload))
|
||||
if err != nil {
|
||||
t.Fatalf("midi.program_change.create processing failed: %s", err)
|
||||
}
|
||||
|
||||
gotMessage, ok := got.Payload.(midi.Message)
|
||||
if !ok {
|
||||
t.Fatalf("midi.program_change.create returned a %T payload: %+v", got, got)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(gotMessage, test.expected) {
|
||||
t.Fatalf("midi.program_change.create got %v, expected %v", gotMessage, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadMIDIProgramChangeCreate(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
payload any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "program_change no channel",
|
||||
params: map[string]any{
|
||||
"type": "program_change",
|
||||
"program": "64",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.program_change.create channel error: not found",
|
||||
},
|
||||
{
|
||||
name: "program_change no program",
|
||||
params: map[string]any{
|
||||
"type": "program_change",
|
||||
"channel": "1",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "midi.program_change.create program error: not found",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["midi.program_change.create"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.program_change.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "midi.program_change.create",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("midi.program_change.create got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload))
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("midi.program_change.create expected to fail but succeeded, got: %v", got)
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("midi.program_change.create got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user