Files
dqs/internal/dqs/category/category_test.go
T
2026-10-26 15:14:43 +00:00

61 lines
1.4 KiB
Go

package category
import (
"testing"
"code.chimeric.al/dwrz/dqs/internal/dqs/portion"
)
func testDairy() Category {
var c = Dairy
c.Portions = make([]portion.Portion, len(Dairy.Portions))
copy(c.Portions, Dairy.Portions)
return c
}
func TestAddRemoveScore(t *testing.T) {
c := testDairy()
// Dairy points: +2, +1, +1, 0, -1, -2.
if err := c.Add(1.5); err != nil {
t.Fatalf("Add(1.5) error = %v", err)
}
if got := c.Score(); got != 2.5 {
t.Errorf("Score() = %v, want 2.5", got)
}
// Add 0.5 to complete the second slot (+1) and 1.0 to fill the third (+1).
if err := c.Add(1.5); err != nil {
t.Fatalf("Add(1.5) error = %v", err)
}
if got := c.Score(); got != 4.0 {
t.Errorf("Score() = %v, want 4.0", got)
}
// Fill remaining 3 slots (0, -1, -2).
if err := c.Add(3.0); err != nil {
t.Fatalf("Add(3.0) error = %v", err)
}
if got := c.Score(); got != 1.0 {
t.Errorf("Score() = %v, want 1.0", got)
}
// Adding even 0.5 more must fail when full.
if err := c.Add(0.5); err == nil {
t.Errorf("Add(0.5) on full category succeeded, want error")
}
// Remove 2.5 from the right (-2, -1, and half of 0).
if err := c.Remove(2.5); err != nil {
t.Fatalf("Remove(2.5) error = %v", err)
}
if got := c.Score(); got != 4.0 {
t.Errorf("Score() = %v, want 4.0", got)
}
// Removing more than remaining (3.5) must fail.
if err := c.Remove(4.0); err == nil {
t.Errorf("Remove(4.0) with 3.5 remaining succeeded, want error")
}
}