Data: components 069-080
This commit is contained in:
71 files changed
+5400
-1
No files matched your search
@@ -0,0 +1,81 @@
|
||||
package mujica
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
)
|
||||
|
||||
func TestStatisticFormat(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
v float64
|
||||
p int
|
||||
want string
|
||||
}{
|
||||
{1234567.891, 2, "1,234,567.89"},
|
||||
{999, 0, "999"},
|
||||
{1000, 0, "1,000"},
|
||||
{-42.5, 1, "−42.5"},
|
||||
{-0.001, 1, "0.0"},
|
||||
{0, 3, "0.000"},
|
||||
{math.Inf(1), 0, "+Inf"},
|
||||
} {
|
||||
if got := statisticFormat(tc.v, tc.p); got != tc.want {
|
||||
t.Errorf("format(%v, %d) = %q, want %q", tc.v, tc.p, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatisticChange(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
change float64
|
||||
text string
|
||||
word string
|
||||
}{{3.26, "+3.3%", "up"}, {-1.5, "−1.5%", "down"}, {0, "0.0%", "unchanged"}} {
|
||||
ch := tc.change
|
||||
tt := ui.NewTester(func(c *ui.Context) {
|
||||
Use(c, Settings{})
|
||||
Statistic(c, 12840, StatisticOptions{Title: "Pilgrims", Prefix: "≈", Unit: "souls", Precision: 1, Change: &ch, ChangeUnit: "%", ChangeNote: "vs last feast"})
|
||||
}, 400, 200)
|
||||
for _, s := range []string{"Pilgrims", "≈12,840.0", "souls", tc.text, "vs last feast"} {
|
||||
if !tt.HasText(s) {
|
||||
t.Fatalf("%v: missing %q in %q", tc.change, s, tt.Texts())
|
||||
}
|
||||
}
|
||||
want := "Pilgrims ≈12,840.0 souls, " + tc.word + " " + tc.text
|
||||
if _, ok := tt.Find(want); !ok {
|
||||
t.Fatalf("spoken label %q missing", want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatisticCustomFormatAndBadPrecision(t *testing.T) {
|
||||
tt := ui.NewTester(func(c *ui.Context) {
|
||||
Use(c, Settings{})
|
||||
Statistic(c, 0.5, StatisticOptions{Format: func(v float64) string { return "half" }})
|
||||
}, 200, 100)
|
||||
if !tt.HasText("half") {
|
||||
t.Fatalf("custom format: %q", tt.Texts())
|
||||
}
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("no panic")
|
||||
}
|
||||
}()
|
||||
ui.NewTester(func(c *ui.Context) {
|
||||
Use(c, Settings{})
|
||||
Statistic(c, 1, StatisticOptions{Precision: -1})
|
||||
}, 100, 100)
|
||||
}
|
||||
|
||||
func TestStatisticSnapshot(t *testing.T) {
|
||||
up, down := 12.5, -3.0
|
||||
snapshot(t, "statistic", 560, 140, func(c *ui.Context) {
|
||||
ui.Row(c).Padding(20).Gap(40).AlignItems(ui.Start).Children(func() {
|
||||
Statistic(c, 12840, StatisticOptions{Title: "Pilgrims", Unit: "souls", Change: &up, ChangeUnit: "%", Precision: 1, ChangeNote: "vs last feast"})
|
||||
Statistic(c, 3.75, StatisticOptions{Title: "Tithe", Prefix: "£", Precision: 2, Change: &down, ChangeUnit: "%"})
|
||||
Statistic(c, 7, StatisticOptions{Title: "Bells"})
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user