diff options
author | dkf <donal.k.fellows@manchester.ac.uk> | 2019-04-01 10:38:47 (GMT) |
---|---|---|
committer | dkf <donal.k.fellows@manchester.ac.uk> | 2019-04-01 10:38:47 (GMT) |
commit | 8d4509eafc4ae6d4ebc12d6b08180fca038bdf8f (patch) | |
tree | 58763149bab01eaf7f9bce9114c6a59c2becbdfc | |
parent | d2fb211d208690336c1cc183cbd4c8d488411ff2 (diff) | |
download | tcl-8d4509eafc4ae6d4ebc12d6b08180fca038bdf8f.zip tcl-8d4509eafc4ae6d4ebc12d6b08180fca038bdf8f.tar.gz tcl-8d4509eafc4ae6d4ebc12d6b08180fca038bdf8f.tar.bz2 |
Add tests
-rw-r--r-- | generic/tclDictObj.c | 9 | ||||
-rw-r--r-- | tests/dict.test | 28 |
2 files changed, 34 insertions, 3 deletions
diff --git a/generic/tclDictObj.c b/generic/tclDictObj.c index c312242..75dcd09 100644 --- a/generic/tclDictObj.c +++ b/generic/tclDictObj.c @@ -1658,7 +1658,7 @@ DictGetWithDefaultCmd( int numKeys; if (objc < 4) { - Tcl_WrongNumArgs(interp, 1, objv, "dictionary ?key ...? key value"); + Tcl_WrongNumArgs(interp, 1, objv, "dictionary ?key ...? key default"); return TCL_ERROR; } @@ -1668,7 +1668,8 @@ DictGetWithDefaultCmd( dictPtr = objv[1]; keyPath = &objv[2]; - numKeys = objc - 4; + numKeys = objc - 4; /* Number of keys in keyPath; there's always + * one extra key afterwards too. */ keyPtr = objv[objc - 2]; defaultPtr = objv[objc - 1]; @@ -1677,9 +1678,11 @@ DictGetWithDefaultCmd( */ dictPtr = TclTraceDictPath(interp, dictPtr, numKeys, keyPath, - DICT_PATH_READ); + DICT_PATH_EXISTS); if (dictPtr == NULL) { return TCL_ERROR; + } else if (dictPtr == DICT_PATH_NON_EXISTENT) { + Tcl_SetObjResult(interp, defaultPtr); } else if (Tcl_DictObjGet(interp, dictPtr, keyPtr, &valuePtr) != TCL_OK) { return TCL_ERROR; } else if (valuePtr == NULL) { diff --git a/tests/dict.test b/tests/dict.test index 904ec53..50e4db7 100644 --- a/tests/dict.test +++ b/tests/dict.test @@ -2047,6 +2047,34 @@ test dict-25.1 {compiled dict update with low-refcount values [Bug d553228d9f]} dict update item item item two two {} }} } {} + +test dict-26.1 {dict getwithdefault command} -body { + dict getwithdefault {a b} a c +} -result b +test dict-26.2 {dict getwithdefault command} -body { + dict getwithdefault {a b} b c +} -result c +test dict-26.3 {dict getwithdefault command} -body { + dict getwithdefault {a {b c}} a b d +} -result c +test dict-26.4 {dict getwithdefault command} -body { + dict getwithdefault {a {b c}} a c d +} -result d +test dict-26.5 {dict getwithdefault command} -body { + dict getwithdefault {a {b c}} b c d +} -result d +test dict-26.6 {dict getwithdefault command} -returnCodes error -body { + dict getwithdefault {a {b c d}} a b d +} -result {missing value to go with key} +test dict-26.7 {dict getwithdefault command} -returnCodes error -body { + dict getwithdefault +} -result {wrong # args: should be "dict getwithdefault dictionary ?key ...? key default"} +test dict-26.8 {dict getwithdefault command} -returnCodes error -body { + dict getwithdefault {} +} -result {wrong # args: should be "dict getwithdefault dictionary ?key ...? key default"} +test dict-26.9 {dict getwithdefault command} -returnCodes error -body { + dict getwithdefault {} {} +} -result {wrong # args: should be "dict getwithdefault dictionary ?key ...? key default"} # cleanup ::tcltest::cleanupTests |