miller/pkg/bifs/sparkline_test.go
John Kerl a55327c7ef
Add sparkline DSL function and mlr sparkline verb (#166) (#2177)
Adds a sparkline(array|map) built-in that renders numeric values as a
Unicode block-character string (▁▂▃▄▅▆▇█), plus a new `mlr sparkline`
verb that summarizes each field's values in record order -- useful for
eyeballing trends without external plotting tools.

Also adds `-s` to `mlr histogram` to sparkline a field's binned counts
(its distribution shape) rather than emitting one record per bin. This
is a different chart from `mlr sparkline` (order-independent binning
vs. record-order values), and the docs for each cross-reference the
other to avoid conflating them.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-07 15:47:01 -04:00

60 lines
1.5 KiB
Go

package bifs
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/johnkerl/miller/v6/pkg/mlrval"
)
func TestBIF_sparkline(t *testing.T) {
// Needs array or map
input := mlrval.FromInt(3)
output := BIF_sparkline(input)
assert.True(t, output.IsError())
// Non-numeric element
input = mlrval.FromArray([]*mlrval.Mlrval{
mlrval.FromInt(1),
mlrval.FromString("abc"),
})
output = BIF_sparkline(input)
assert.True(t, output.IsError())
// Empty array is void
input = mlrval.FromArray([]*mlrval.Mlrval{})
output = BIF_sparkline(input)
assert.True(t, output.IsVoid())
// Ascending values span the full tick range, low to high
input = mlrval.FromArray([]*mlrval.Mlrval{
mlrval.FromInt(1),
mlrval.FromInt(2),
mlrval.FromInt(3),
mlrval.FromInt(4),
mlrval.FromInt(5),
mlrval.FromInt(6),
mlrval.FromInt(7),
mlrval.FromInt(8),
})
output = BIF_sparkline(input)
assert.True(t, mlrval.Equals(output, mlrval.FromString("▁▂▃▄▅▆▇█")))
// Same value throughout maps to the lowest tick
input = mlrval.FromArray([]*mlrval.Mlrval{
mlrval.FromInt(3),
mlrval.FromInt(3),
mlrval.FromInt(3),
})
output = BIF_sparkline(input)
assert.True(t, mlrval.Equals(output, mlrval.FromString("▁▁▁")))
// Map input follows insertion order
input = array_to_map_for_test(mlrval.FromArray([]*mlrval.Mlrval{
mlrval.FromInt(1),
mlrval.FromInt(8),
}))
output = BIF_sparkline(input)
assert.True(t, mlrval.Equals(output, mlrval.FromString("▁█")))
}