@@ -1560,6 +1560,120 @@ func TestRepeatBy(t *testing.T) {
15601560 is .Equal ([]int {0 , 1 , 4 , 9 , 16 }, result3 )
15611561}
15621562
1563+ func TestRepeatByErr (t * testing.T ) {
1564+ t .Parallel ()
1565+ is := assert .New (t )
1566+
1567+ testErr := errors .New ("test error" )
1568+
1569+ // Table-driven tests
1570+ tests := []struct {
1571+ name string
1572+ count int
1573+ callback func (index int ) (int , error )
1574+ wantResult []int
1575+ wantErr bool
1576+ expectedCallbackCount int
1577+ }{
1578+ {
1579+ name : "successful completion" ,
1580+ count : 5 ,
1581+ callback : func (i int ) (int , error ) {
1582+ return i * i , nil
1583+ },
1584+ wantResult : []int {0 , 1 , 4 , 9 , 16 },
1585+ wantErr : false ,
1586+ expectedCallbackCount : 5 ,
1587+ },
1588+ {
1589+ name : "error at first iteration" ,
1590+ count : 5 ,
1591+ callback : func (i int ) (int , error ) {
1592+ if i == 0 {
1593+ return 0 , testErr
1594+ }
1595+ return i * i , nil
1596+ },
1597+ wantResult : nil ,
1598+ wantErr : true ,
1599+ expectedCallbackCount : 1 ,
1600+ },
1601+ {
1602+ name : "error at third iteration" ,
1603+ count : 5 ,
1604+ callback : func (i int ) (int , error ) {
1605+ if i == 2 {
1606+ return 0 , testErr
1607+ }
1608+ return i * i , nil
1609+ },
1610+ wantResult : nil ,
1611+ wantErr : true ,
1612+ expectedCallbackCount : 3 ,
1613+ },
1614+ {
1615+ name : "error at last iteration" ,
1616+ count : 5 ,
1617+ callback : func (i int ) (int , error ) {
1618+ if i == 4 {
1619+ return 0 , testErr
1620+ }
1621+ return i * i , nil
1622+ },
1623+ wantResult : nil ,
1624+ wantErr : true ,
1625+ expectedCallbackCount : 5 ,
1626+ },
1627+ {
1628+ name : "zero count - empty result" ,
1629+ count : 0 ,
1630+ callback : func (i int ) (int , error ) {
1631+ return i * i , nil
1632+ },
1633+ wantResult : []int {},
1634+ wantErr : false ,
1635+ expectedCallbackCount : 0 ,
1636+ },
1637+ {
1638+ name : "single item success" ,
1639+ count : 1 ,
1640+ callback : func (i int ) (int , error ) {
1641+ return 42 , nil
1642+ },
1643+ wantResult : []int {42 },
1644+ wantErr : false ,
1645+ expectedCallbackCount : 1 ,
1646+ },
1647+ }
1648+
1649+ for _ , tt := range tests {
1650+ tt := tt
1651+ t .Run (tt .name , func (t * testing.T ) {
1652+ t .Parallel ()
1653+
1654+ // Track callback count to verify early return
1655+ callbackCount := 0
1656+ wrappedCallback := func (i int ) (int , error ) {
1657+ callbackCount ++
1658+ return tt .callback (i )
1659+ }
1660+
1661+ result , err := RepeatByErr (tt .count , wrappedCallback )
1662+
1663+ if tt .wantErr {
1664+ is .ErrorIs (err , testErr )
1665+ is .Nil (result )
1666+ } else {
1667+ is .NoError (err )
1668+ is .Equal (tt .wantResult , result )
1669+ }
1670+
1671+ // Verify callback count matches expected (tests early return)
1672+ is .Equal (tt .expectedCallbackCount , callbackCount , "callback count should match expected" )
1673+ })
1674+ }
1675+ }
1676+
15631677func TestKeyBy (t * testing.T ) {
15641678 t .Parallel ()
15651679 is := assert .New (t )
0 commit comments