miller/pkg/transformers/step_test.go
John Kerl e415682e61
Allow more step -a options to specify the number of records back (#1002) (#2190)
* Allow step -a shift_lag/shift_lead/delta/ratio to specify records back/forward (#1002)

Extends the slwin_m_n naming convention to more steppers: shift_lag_12
refers 12 records back, shift_lead_4 refers 4 records forward, and
likewise delta_N, ratio_N, and shift_N. The unsuffixed forms keep their
existing 1-record-back/forward behavior and output field names.

Backward-looking steppers cache copies of previous values in a
fixed-size ring buffer (tValueRing) in their own state, following the
existing pattern of not reading from the window keeper's already-emitted
records, to avoid races with downstream transformers. shift_lead_N uses
the existing forward-window machinery shared with slwin.

Also adds nil guards to all steppers' process functions for the case
where the record at the window center lacks the stepped field, which
previously panicked (pre-existing on main, e.g.
"mlr step -a delta,shift_lead -f x" or "-a slwin_1_1" over
heterogeneous data), and which the new parameterized combinations make
easier to reach.

Includes unit tests, regression cases 0024-0027, and regenerated help
text / docs / man page.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Clarify step -a help text: both plain and _{n} stepper forms are valid (#1002)

Per review on #2190: descriptions like "E.g. delta_7 for 7 records
back" read as if the suffixed form were the only syntax. Reword the
shift, shift_lag, shift_lead, delta, and ratio descriptions to state
both forms explicitly -- e.g. "Use delta or equivalently delta_1 for
the previous record, or delta_{n} for n records back" -- using the {n}
placeholder style, and update the matching usage paragraph and
reference-verbs prose. Regenerate help-derived docs, man page, and
cli-help regression expectation.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Give a specific error for invalid stepper count suffixes (#1002)

Per review on #2190: "mlr step -a delta_0" (or delta_-1, delta_x, etc.)
previously reported the generic 'stepper "delta_0" not found'. Now,
when the name prefixes a known count-suffix stepper (shift, shift_lag,
shift_lead, delta, ratio) but the suffix is not a positive integer, the
error is:

    mlr step: stepper "delta_0": count must be a positive integer

Truly unknown stepper names keep the existing "not found" message.
Adds should-fail regression cases for delta_0 and delta_-1, and unit
tests for the detection helper.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-14 16:39:48 -04:00

210 lines
5.6 KiB
Go

package transformers
import (
"testing"
"github.com/johnkerl/miller/v6/pkg/mlrval"
)
func TestParseStepperCount(t *testing.T) {
cases := []struct {
stepperName string
baseName string
wantCount int
wantOK bool
}{
{"shift_lag", "shift_lag", 1, true},
{"shift_lag_1", "shift_lag", 1, true},
{"shift_lag_12", "shift_lag", 12, true},
{"shift_lag_007", "shift_lag", 7, true},
{"shift_lag_0", "shift_lag", 0, false},
{"shift_lag_-3", "shift_lag", 0, false},
{"shift_lag_+3", "shift_lag", 0, false},
{"shift_lag_", "shift_lag", 0, false},
{"shift_lag_x", "shift_lag", 0, false},
{"shift_lag_3_4", "shift_lag", 0, false},
{"shift_lead", "shift_lag", 0, false},
{"shift_lag", "shift", 0, false}, // "lag" is not a count
{"shift_12", "shift", 12, true},
{"delta_4", "delta", 4, true},
{"ratio_4", "ratio", 4, true},
}
for _, tc := range cases {
count, ok := parseStepperCount(tc.stepperName, tc.baseName)
if ok != tc.wantOK || (ok && count != tc.wantCount) {
t.Errorf(
"parseStepperCount(%q, %q) = (%d, %v); want (%d, %v)",
tc.stepperName, tc.baseName, count, ok, tc.wantCount, tc.wantOK,
)
}
}
}
func TestStepperNameHasBadCount(t *testing.T) {
cases := []struct {
stepperName string
want bool
}{
{"delta_0", true},
{"delta_-1", true},
{"delta_x", true},
{"delta_3_4", true},
{"shift_lag_0", true},
{"shift_lead_-2", true},
{"ratio_", true},
{"shift_", true},
{"delta", false}, // valid; never reaches the error path anyway
{"delta_7", false}, // valid; never reaches the error path anyway
{"foo", false},
{"foo_0", false},
{"slwin_x_y", false}, // slwin has its own handling
}
for _, tc := range cases {
got := stepperNameHasBadCount(tc.stepperName)
if got != tc.want {
t.Errorf("stepperNameHasBadCount(%q) = %v; want %v", tc.stepperName, got, tc.want)
}
}
}
func TestStepperInputFromNameWithCounts(t *testing.T) {
cases := []struct {
stepperName string
wantFound bool
wantBackward int
wantForward int
}{
{"shift_lag", true, 0, 0},
{"shift_lag_12", true, 0, 0},
{"shift_lead", true, 0, 1},
{"shift_lead_4", true, 0, 4},
{"shift", true, 0, 0},
{"shift_3", true, 0, 0},
{"delta", true, 0, 0},
{"delta_2", true, 0, 0},
{"ratio_2", true, 0, 0},
{"slwin_2_3", true, 2, 3},
{"shift_lag_0", false, 0, 0},
{"delta_x", false, 0, 0},
{"nonesuch", false, 0, 0},
}
for _, tc := range cases {
stepperInput := stepperInputFromName(tc.stepperName)
if tc.wantFound {
if stepperInput == nil {
t.Errorf("stepperInputFromName(%q) = nil; want non-nil", tc.stepperName)
continue
}
if stepperInput.numRecordsBackward != tc.wantBackward ||
stepperInput.numRecordsForward != tc.wantForward {
t.Errorf(
"stepperInputFromName(%q) = {backward:%d, forward:%d}; want {backward:%d, forward:%d}",
tc.stepperName,
stepperInput.numRecordsBackward, stepperInput.numRecordsForward,
tc.wantBackward, tc.wantForward,
)
}
} else {
if stepperInput != nil {
t.Errorf("stepperInputFromName(%q) = %v; want nil", tc.stepperName, stepperInput)
}
}
}
}
func TestAllocateStepperOutputFieldNames(t *testing.T) {
cases := []struct {
stepperName string
wantOutputFieldName string
}{
{"shift", "x_shift"},
{"shift_lag", "x_shift_lag"},
{"shift_lag_12", "x_shift_lag_12"},
{"shift_lead", "x_shift_lead"},
{"shift_lead_4", "x_shift_lead_4"},
{"delta", "x_delta"},
{"delta_2", "x_delta_2"},
{"ratio", "x_ratio"},
{"ratio_2", "x_ratio_2"},
}
for _, tc := range cases {
stepperInput := stepperInputFromName(tc.stepperName)
if stepperInput == nil {
t.Errorf("stepperInputFromName(%q) = nil; want non-nil", tc.stepperName)
continue
}
stepper := allocateStepper(stepperInput, "x", nil, nil)
if stepper == nil {
t.Errorf("allocateStepper for %q = nil; want non-nil", tc.stepperName)
continue
}
var got string
switch s := stepper.(type) {
case *tStepperShiftLag:
got = s.outputFieldName
case *tStepperShiftLead:
got = s.outputFieldName
case *tStepperDelta:
got = s.outputFieldName
case *tStepperRatio:
got = s.outputFieldName
default:
t.Errorf("allocateStepper for %q: unexpected stepper type %T", tc.stepperName, stepper)
continue
}
if got != tc.wantOutputFieldName {
t.Errorf(
"allocateStepper for %q: output field name %q; want %q",
tc.stepperName, got, tc.wantOutputFieldName,
)
}
}
}
func TestValueRing(t *testing.T) {
ring := newValueRing(3)
// Fewer than 3 values seen: has must be false.
for i := 1; i <= 3; i++ {
nBack, has := ring.push(mlrval.FromInt(int64(i)))
if has {
t.Errorf("push %d: has = true; want false", i)
}
if nBack != nil {
t.Errorf("push %d: nBack = %v; want nil", i, nBack)
}
}
// From the 4th value on, the value from 3 records back is returned.
for i := 4; i <= 8; i++ {
nBack, has := ring.push(mlrval.FromInt(int64(i)))
if !has {
t.Errorf("push %d: has = false; want true", i)
}
if nBack == nil {
t.Errorf("push %d: nBack = nil; want non-nil", i)
continue
}
want := int64(i - 3)
got, ok := nBack.GetIntValue()
if !ok || got != want {
t.Errorf("push %d: nBack = %v; want %d", i, nBack, want)
}
}
// A nil push (absent field) occupies a slot.
ring.push(nil) // 9th slot: nil
ring.push(mlrval.FromInt(10)) // 10th
ring.push(mlrval.FromInt(11)) // 11th
nBack, has := ring.push(mlrval.FromInt(12)) // 12th: 3 back is the nil slot
if !has {
t.Errorf("push 12: has = false; want true")
}
if nBack != nil {
t.Errorf("push 12: nBack = %v; want nil", nBack)
}
}