diff --git a/pkg/eval/vals/assoc_test.go b/pkg/eval/vals/assoc_test.go index e0718a53..5f92f147 100644 --- a/pkg/eval/vals/assoc_test.go +++ b/pkg/eval/vals/assoc_test.go @@ -29,7 +29,7 @@ func TestAssoc(t *testing.T) { Eq(MakeList("foo", "1", "2", "3")), nil), Args(MakeList("0"), MakeList("0"), "1").Rets(nil, errIndexMustBeInteger), Args(MakeList("0"), "1", "x").Rets(nil, errs.OutOfRange{ - What: "index here", ValidLow: "0", ValidHigh: "0", Actual: "1"}), + What: "index", ValidLow: "0", ValidHigh: "0", Actual: "1"}), // TODO: Support list assoc with slice Args(MakeList("0", "1", "2", "3"), "1:3", MakeList("foo")).Rets( nil, errAssocWithSlice), diff --git a/pkg/eval/vals/index_list.go b/pkg/eval/vals/index_list.go index 4eda7673..f75c0166 100644 --- a/pkg/eval/vals/index_list.go +++ b/pkg/eval/vals/index_list.go @@ -84,12 +84,12 @@ func ConvertListIndex(rawIndex interface{}, n int) (*ListIndex, error) { if j < i { if j0 < 0 { return nil, errs.OutOfRange{ - What: "negative slice upper index here", + What: "negative slice upper index", ValidLow: strconv.Itoa(i - n), ValidHigh: "-1", Actual: strconv.Itoa(j0)} } return nil, errs.OutOfRange{ - What: "slice upper index here", + What: "slice upper index", ValidLow: strconv.Itoa(i), ValidHigh: strconv.Itoa(n), Actual: strconv.Itoa(j0)} } @@ -115,7 +115,7 @@ func parseIndexString(s string, n int) (slice bool, i int, j int, err error) { if low == "" { i = 0 } else { - i, err = atoi(low, n) + i, err = atoi(low, n+1) if err != nil { return false, 0, 0, err } @@ -123,11 +123,12 @@ func parseIndexString(s string, n int) (slice bool, i int, j int, err error) { if high == "" { j = n } else { - j, err = atoi(high, n) + j, err = atoi(high, n+1) if err != nil { return false, 0, 0, err } if sep == "..=" { + // TODO: Handle j == MaxInt-1 j++ } } @@ -166,12 +167,12 @@ func atoi(a string, n int) (int, error) { func posIndexOutOfRange(index string, n int) errs.OutOfRange { return errs.OutOfRange{ - What: "index here", + What: "index", ValidLow: "0", ValidHigh: strconv.Itoa(n - 1), Actual: index} } func negIndexOutOfRange(index string, n int) errs.OutOfRange { return errs.OutOfRange{ - What: "negative index here", + What: "negative index", ValidLow: strconv.Itoa(-n), ValidHigh: "-1", Actual: index} } diff --git a/pkg/eval/vals/index_test.go b/pkg/eval/vals/index_test.go index 71d9fc05..db2cf5eb 100644 --- a/pkg/eval/vals/index_test.go +++ b/pkg/eval/vals/index_test.go @@ -46,16 +46,20 @@ func TestIndex(t *testing.T) { Args(li4, "0").Rets("foo", nil), Args(li4, "3").Rets("ipsum", nil), Args(li0, "0").Rets(Any, errs.OutOfRange{ - What: "index here", ValidLow: "0", ValidHigh: "-1", Actual: "0"}), + What: "index", ValidLow: "0", ValidHigh: "-1", Actual: "0"}), Args(li4, "4").Rets(Any, errs.OutOfRange{ - What: "index here", ValidLow: "0", ValidHigh: "3", Actual: "4"}), + What: "index", ValidLow: "0", ValidHigh: "3", Actual: "4"}), Args(li4, "5").Rets(Any, errs.OutOfRange{ - What: "index here", ValidLow: "0", ValidHigh: "3", Actual: "5"}), + What: "index", ValidLow: "0", ValidHigh: "3", Actual: "5"}), + Args(li4, z).Rets(Any, + errs.OutOfRange{What: "index", ValidLow: "0", ValidHigh: "3", Actual: z}), // Negative indices: -n <= i < 0. Args(li4, "-1").Rets("ipsum", nil), Args(li4, "-4").Rets("foo", nil), Args(li4, "-5").Rets(Any, errs.OutOfRange{ - What: "negative index here", ValidLow: "-4", ValidHigh: "-1", Actual: "-5"}), + What: "negative index", ValidLow: "-4", ValidHigh: "-1", Actual: "-5"}), + Args(li4, "-"+z).Rets(Any, + errs.OutOfRange{What: "negative index", ValidLow: "-4", ValidHigh: "-1", Actual: "-" + z}), // Float indices are not allowed even if the value is an integer. Args(li4, 0.0).Rets(Any, errIndexMustBeInteger), @@ -63,10 +67,10 @@ func TestIndex(t *testing.T) { Args(li4, 0).Rets("foo", nil), Args(li4, 3).Rets("ipsum", nil), Args(li4, 5).Rets(nil, errs.OutOfRange{ - What: "index here", ValidLow: "0", ValidHigh: "3", Actual: "5"}), + What: "index", ValidLow: "0", ValidHigh: "3", Actual: "5"}), Args(li4, -1).Rets("ipsum", nil), Args(li4, -5).Rets(nil, errs.OutOfRange{ - What: "negative index here", ValidLow: "-4", ValidHigh: "-1", Actual: "-5"}), + What: "negative index", ValidLow: "-4", ValidHigh: "-1", Actual: "-5"}), // Half-open slices. Args(li4, "1..3").Rets(Eq(MakeList("bar", "lorem")), nil), @@ -102,13 +106,19 @@ func TestIndex(t *testing.T) { Args(li4, "3..=").Rets(Eq(MakeList("ipsum")), nil), Args(li4, "..=").Rets(Eq(li4), nil), - // Index out of range. + // Slice index out of range. Args(li4, "-5:1").Rets(nil, errs.OutOfRange{ - What: "negative index here", ValidLow: "-4", ValidHigh: "-1", Actual: "-5"}), + What: "negative index", ValidLow: "-4", ValidHigh: "-1", Actual: "-5"}), Args(li4, "0:5").Rets(nil, errs.OutOfRange{ - What: "index here", ValidLow: "0", ValidHigh: "4", Actual: "5"}), + What: "index", ValidLow: "0", ValidHigh: "4", Actual: "5"}), + Args(li4, z+":").Rets(nil, + errs.OutOfRange{What: "index", ValidLow: "0", ValidHigh: "4", Actual: z}), + // Slice index upper < lower Args(li4, "3:2").Rets(nil, errs.OutOfRange{ - What: "slice upper index here", ValidLow: "3", ValidHigh: "4", Actual: "2"}), + What: "slice upper index", ValidLow: "3", ValidHigh: "4", Actual: "2"}), + Args(li4, "-1:-2").Rets(nil, + errs.OutOfRange{What: "negative slice upper index", + ValidLow: "-1", ValidHigh: "-1", Actual: "-2"}), // Malformed list indices. Args(li4, "a").Rets(Any, errIndexMustBeInteger),