json.nim: delete should raise KeyError, not IndexError, minor cleanup of the tests

This commit is contained in:
Araq 2018-05-20 23:00:25 +02:00
commit 5472574f72

View file

@ -509,7 +509,7 @@ proc delete*(obj: JsonNode, key: string) =
## Deletes ``obj[key]``. ## Deletes ``obj[key]``.
assert(obj.kind == JObject) assert(obj.kind == JObject)
if not obj.fields.hasKey(key): if not obj.fields.hasKey(key):
raise newException(IndexError, "key not in object") raise newException(KeyError, "key not in object")
obj.fields.del(key) obj.fields.del(key)
proc copy*(p: JsonNode): JsonNode = proc copy*(p: JsonNode): JsonNode =
@ -1496,18 +1496,18 @@ when isMainModule:
# Bounds checking # Bounds checking
try: try:
let a = testJson["a"][9] let a = testJson["a"][9]
doAssert(false, "EInvalidIndex not thrown") doAssert(false, "IndexError not thrown")
except IndexError: except IndexError:
discard discard
try: try:
let a = testJson["a"][-1] let a = testJson["a"][-1]
doAssert(false, "EInvalidIndex not thrown") doAssert(false, "IndexError not thrown")
except IndexError: except IndexError:
discard discard
try: try:
doAssert(testJson["a"][0].num == 1, "Index doesn't correspond to its value") doAssert(testJson["a"][0].num == 1, "Index doesn't correspond to its value")
except: except:
doAssert(false, "EInvalidIndex thrown for valid index") doAssert(false, "IndexError thrown for valid index")
doAssert(testJson{"b"}.str=="asd", "Couldn't fetch a singly nested key with {}") doAssert(testJson{"b"}.str=="asd", "Couldn't fetch a singly nested key with {}")
doAssert(isNil(testJson{"nonexistent"}), "Non-existent keys should return nil") doAssert(isNil(testJson{"nonexistent"}), "Non-existent keys should return nil")