add test for max int64 validator

This commit is contained in:
2026-02-12 22:40:20 +00:00
parent 08682c35c1
commit cb0d6c1d3a
2 changed files with 44 additions and 5 deletions

View File

@@ -58,12 +58,14 @@ func (t *Int64ComponentInstance) Value() interface{} {
func (t *Int64ComponentInstance) FromJSON(component block.Component, data json.RawMessage) error { func (t *Int64ComponentInstance) FromJSON(component block.Component, data json.RawMessage) error {
var s int64 var s int64
err := json.Unmarshal(data, &s)
if err != nil {
return fmt.Errorf("Error unmarshalling property %s: %w", component.Identifier(), err)
}
t.value = s if data != nil {
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
t.value = s
}
for _, v := range component.Validators() { for _, v := range component.Validators() {
if err := v(s); err != nil { if err := v(s); err != nil {

View File

@@ -0,0 +1,37 @@
package int64_component_validators
import (
"blocky/internal/block/int64_component"
"testing"
)
func TestMax(t *testing.T) {
cfg := &int64_component.Int64ComponentConfig{}
Max(10)(cfg)
if len(cfg.Validators) != 1 {
t.Fatalf("expect 1 validator, got %d", len(cfg.Validators))
}
validate := cfg.Validators[0]
tests := []struct {
name string
value int64
wantErr bool
}{
{"below value", 5, false},
{"equal to value", 10, false},
{"above value", 15, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validate(tt.value)
if (err != nil) != tt.wantErr {
t.Errorf("validate(%v) error = %v, wanterr %v", tt.value, err, tt.wantErr)
}
})
}
}