| 968 | } |
| 969 | |
| 970 | func TestGetrange(t *testing.T) { |
| 971 | server.Clear() |
| 972 | c, err := proto.Dial(server.Addr()) |
| 973 | ok(t, err) |
| 974 | defer c.Close() |
| 975 | s := server.Direct() |
| 976 | |
| 977 | { |
| 978 | s.Set("foo", "abcdefg") |
| 979 | test := func(s, e int, res string) { |
| 980 | t.Helper() |
| 981 | mustDo(t, c, |
| 982 | "GETRANGE", "foo", strconv.Itoa(s), strconv.Itoa(e), |
| 983 | proto.String(res), |
| 984 | ) |
| 985 | } |
| 986 | test(0, 0, "a") |
| 987 | test(0, 3, "abcd") |
| 988 | test(0, 7, "abcdefg") |
| 989 | test(0, 100, "abcdefg") |
| 990 | test(1, 2, "bc") |
| 991 | test(1, 100, "bcdefg") |
| 992 | test(-4, -2, "def") |
| 993 | test(0, -1, "abcdefg") |
| 994 | test(0, -2, "abcdef") |
| 995 | test(0, -100, "a") // Redis is funny |
| 996 | test(-2, 2, "") |
| 997 | } |
| 998 | |
| 999 | // New key |
| 1000 | mustDo(t, c, |
| 1001 | "GETRANGE", "bar", "0", "4", |
| 1002 | proto.String(""), |
| 1003 | ) |
| 1004 | |
| 1005 | // Wrong type of existing key |
| 1006 | { |
| 1007 | s.HSet("wrong", "aap", "noot") |
| 1008 | mustDo(t, c, |
| 1009 | "GETRANGE", "wrong", "0", "0", |
| 1010 | proto.Error(msgWrongType), |
| 1011 | ) |
| 1012 | } |
| 1013 | |
| 1014 | // Wrong usage |
| 1015 | { |
| 1016 | mustDo(t, c, |
| 1017 | "GETRANGE", |
| 1018 | proto.Error(errWrongNumber("getrange")), |
| 1019 | ) |
| 1020 | mustDo(t, c, |
| 1021 | "GETRANGE", "missing", |
| 1022 | proto.Error(errWrongNumber("getrange")), |
| 1023 | ) |
| 1024 | mustDo(t, c, |
| 1025 | "GETRANGE", "many", "spurious", "arguments", "!", |
| 1026 | proto.Error(errWrongNumber("getrange")), |
| 1027 | ) |