mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
add more tests for int/uint parsers
This commit is contained in:
@@ -3,9 +3,101 @@ package processor_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestIntParseFromRegistry(t *testing.T) {
|
||||||
|
registration, ok := processor.ProcessorRegistry["int.parse"]
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("int.parse processor not registered")
|
||||||
|
}
|
||||||
|
|
||||||
|
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||||
|
Type: "int.parse",
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to create int.parse processor: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if processorInstance.Type() != "int.parse" {
|
||||||
|
t.Fatalf("int.parse processor has wrong type: %s", processorInstance.Type())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIntParseBadConfigBaseString(t *testing.T) {
|
||||||
|
registration, ok := processor.ProcessorRegistry["int.parse"]
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("int.parse processor not registered")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := registration.New(config.ProcessorConfig{
|
||||||
|
Type: "int.parse",
|
||||||
|
Params: map[string]any{
|
||||||
|
"base": "10",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("int.parse should have returned an error for bad base config")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIntParseBadConfigBitSizeString(t *testing.T) {
|
||||||
|
registration, ok := processor.ProcessorRegistry["int.parse"]
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("int.parse processor not registered")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := registration.New(config.ProcessorConfig{
|
||||||
|
Type: "int.parse",
|
||||||
|
Params: map[string]any{
|
||||||
|
"bitSize": "64",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("int.parse should have returned an error for bad bitSize config")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIntParseGoodConfig(t *testing.T) {
|
||||||
|
registration, ok := processor.ProcessorRegistry["int.parse"]
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("int.parse processor not registered")
|
||||||
|
}
|
||||||
|
|
||||||
|
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||||
|
Type: "int.parse",
|
||||||
|
Params: map[string]any{
|
||||||
|
"base": 10.0,
|
||||||
|
"bitSize": 64.0,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("int.parse should have created processor but got error: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
payload := "12345"
|
||||||
|
expected := int64(12345)
|
||||||
|
|
||||||
|
got, err := processorInstance.Process(t.Context(), payload)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("int.parse processing failed: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
gotInt, ok := got.(int64)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("int.parse returned a %T payload: %s", got, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
if gotInt != expected {
|
||||||
|
t.Fatalf("int.parse got %d, expected %d", gotInt, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGoodIntParse(t *testing.T) {
|
func TestGoodIntParse(t *testing.T) {
|
||||||
intParser := processor.IntParse{}
|
intParser := processor.IntParse{}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
|||||||
@@ -3,31 +3,147 @@ package processor_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestUintParseFromRegistry(t *testing.T) {
|
||||||
|
registration, ok := processor.ProcessorRegistry["uint.parse"]
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("uint.parse processor not registered")
|
||||||
|
}
|
||||||
|
|
||||||
|
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||||
|
Type: "uint.parse",
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to create uint.parse processor: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if processorInstance.Type() != "uint.parse" {
|
||||||
|
t.Fatalf("uint.parse processor has wrong type: %s", processorInstance.Type())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUintParseBadConfigBaseString(t *testing.T) {
|
||||||
|
registration, ok := processor.ProcessorRegistry["uint.parse"]
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("uint.parse processor not registered")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := registration.New(config.ProcessorConfig{
|
||||||
|
Type: "uint.parse",
|
||||||
|
Params: map[string]any{
|
||||||
|
"base": "10",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("uint.parse should have returned an error for bad base config")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUintParseBadConfigBitSizeString(t *testing.T) {
|
||||||
|
registration, ok := processor.ProcessorRegistry["uint.parse"]
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("uint.parse processor not registered")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := registration.New(config.ProcessorConfig{
|
||||||
|
Type: "uint.parse",
|
||||||
|
Params: map[string]any{
|
||||||
|
"bitSize": "64",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("uint.parse should have returned an error for bad bitSize config")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUintParseGoodConfig(t *testing.T) {
|
||||||
|
registration, ok := processor.ProcessorRegistry["uint.parse"]
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("uint.parse processor not registered")
|
||||||
|
}
|
||||||
|
|
||||||
|
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||||
|
Type: "uint.parse",
|
||||||
|
Params: map[string]any{
|
||||||
|
"base": 10.0,
|
||||||
|
"bitSize": 64.0,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("uint.parse should have created processor but got error: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
payload := "12345"
|
||||||
|
expected := uint64(12345)
|
||||||
|
|
||||||
|
got, err := processorInstance.Process(t.Context(), payload)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("uint.parse processing failed: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
gotUint, ok := got.(uint64)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("uint.parse returned a %T payload: %s", got, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
if gotUint != expected {
|
||||||
|
t.Fatalf("uint.parse got %d, expected %d", gotUint, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGoodUintParse(t *testing.T) {
|
func TestGoodUintParse(t *testing.T) {
|
||||||
uintParser := processor.UintParse{}
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
processor processor.Processor
|
processor processor.Processor
|
||||||
name string
|
name string
|
||||||
payload any
|
payload any
|
||||||
expected uint64
|
expected uint64
|
||||||
|
base int
|
||||||
|
bitSize int
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "positive number",
|
name: "positive number",
|
||||||
payload: "12345",
|
payload: "12345",
|
||||||
expected: 12345,
|
expected: 12345,
|
||||||
|
base: 10,
|
||||||
|
bitSize: 64,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "zero",
|
name: "zero",
|
||||||
payload: "0",
|
payload: "0",
|
||||||
expected: 0,
|
expected: 0,
|
||||||
|
base: 10,
|
||||||
|
bitSize: 64,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "binary",
|
||||||
|
payload: "1010101",
|
||||||
|
expected: 85,
|
||||||
|
base: 2,
|
||||||
|
bitSize: 64,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "hex",
|
||||||
|
payload: "15F",
|
||||||
|
expected: 351,
|
||||||
|
base: 16,
|
||||||
|
bitSize: 64,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
uintParser := processor.UintParse{
|
||||||
|
Base: test.base,
|
||||||
|
BitSize: test.bitSize,
|
||||||
|
}
|
||||||
got, err := uintParser.Process(t.Context(), test.payload)
|
got, err := uintParser.Process(t.Context(), test.payload)
|
||||||
|
|
||||||
gotUint, ok := got.(uint64)
|
gotUint, ok := got.(uint64)
|
||||||
|
|||||||
Reference in New Issue
Block a user