Loading openscad/libs/libdict.scad +5 −2 Original line number Diff line number Diff line Loading @@ -109,13 +109,16 @@ function valid_dict(dict) = // Key lookup for key value pair "dictionary". // Unlike the built in lookup this works with strings. function key_lookup(key, dict) = function key_lookup(key, dict, default=undef) = assert(is_string(key), "`key` must be a string") assert(valid_dict(dict), "`dict` must be a valid 'dictionary'") let( // key is in [] because otherwise openscad will search for each letter rather than the string. index = search([key], dict, 1, 0)[0] ) assert (index!=[], "Key lookup failed, key not found!") dict[index][1]; // There must be a key match or a default value ) assert (index!=[] || !is_undef(default), "Key lookup failed, key not found and no default given!") // return value from the dict if key matches, else return a default value. index!=[] ? dict[index][1] : default; // Creates a new dictionary with a key value pair replaced. Pair must already // be in dictionary. Loading openscad/libs/test/test_dict.py +32 −0 Original line number Diff line number Diff line Loading @@ -560,6 +560,38 @@ class TestKeyLookup4(BaseTestScadDict): ''' self.run_scad(scad, has_errors=True) class TestKeyLookup5(BaseTestScadDict): """ Check openscad uses fallback when provided. """ def test(self): '''Must be the only test in the class!''' scad = ''' dict = [["a",3], ["ab", 22], ["raisin", 99], ["great", 4]]; val = key_lookup("foo", dict, default=21); assert(val==21); ''' self.run_scad(scad, has_errors=False) class TestKeyLookup6(BaseTestScadDict): """ Check openscad uses ignores fallback if key exists. """ def test(self): '''Must be the only test in the class!''' scad = ''' dict = [["a",3], ["ab", 22], ["raisin", 99], ["great", 4]]; val = key_lookup("raisin", dict, default=21); assert(val==99); ''' self.run_scad(scad, has_errors=False) class TestReplace1(BaseTestScadDict): """ Check that a value can be replaced and then read and that other values are Loading Loading
openscad/libs/libdict.scad +5 −2 Original line number Diff line number Diff line Loading @@ -109,13 +109,16 @@ function valid_dict(dict) = // Key lookup for key value pair "dictionary". // Unlike the built in lookup this works with strings. function key_lookup(key, dict) = function key_lookup(key, dict, default=undef) = assert(is_string(key), "`key` must be a string") assert(valid_dict(dict), "`dict` must be a valid 'dictionary'") let( // key is in [] because otherwise openscad will search for each letter rather than the string. index = search([key], dict, 1, 0)[0] ) assert (index!=[], "Key lookup failed, key not found!") dict[index][1]; // There must be a key match or a default value ) assert (index!=[] || !is_undef(default), "Key lookup failed, key not found and no default given!") // return value from the dict if key matches, else return a default value. index!=[] ? dict[index][1] : default; // Creates a new dictionary with a key value pair replaced. Pair must already // be in dictionary. Loading
openscad/libs/test/test_dict.py +32 −0 Original line number Diff line number Diff line Loading @@ -560,6 +560,38 @@ class TestKeyLookup4(BaseTestScadDict): ''' self.run_scad(scad, has_errors=True) class TestKeyLookup5(BaseTestScadDict): """ Check openscad uses fallback when provided. """ def test(self): '''Must be the only test in the class!''' scad = ''' dict = [["a",3], ["ab", 22], ["raisin", 99], ["great", 4]]; val = key_lookup("foo", dict, default=21); assert(val==21); ''' self.run_scad(scad, has_errors=False) class TestKeyLookup6(BaseTestScadDict): """ Check openscad uses ignores fallback if key exists. """ def test(self): '''Must be the only test in the class!''' scad = ''' dict = [["a",3], ["ab", 22], ["raisin", 99], ["great", 4]]; val = key_lookup("raisin", dict, default=21); assert(val==99); ''' self.run_scad(scad, has_errors=False) class TestReplace1(BaseTestScadDict): """ Check that a value can be replaced and then read and that other values are Loading