mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-05 03:17:50 +08:00
Increase coverage of pkg/eval/vals/index_list.go to 100%.
This addresses #1234.
This commit is contained in:
parent
1fa9451e4b
commit
2093ac680f
|
@ -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),
|
||||
|
|
|
@ -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}
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
|
|
Loading…
Reference in New Issue
Block a user