From a26bb929782c41f49ef2c4b2e71cf2b60f09de6c Mon Sep 17 00:00:00 2001 From: Revar Desmera Date: Fri, 8 Nov 2019 18:31:37 -0800 Subject: [PATCH] Added regressions and bug fixes for structs.scad --- structs.scad | 8 +++--- tests/test_structs.scad | 59 +++++++++++++++++++++++++++++++++++++++++ version.scad | 2 +- 3 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 tests/test_structs.scad diff --git a/structs.scad b/structs.scad index d8f7d49..bb0986c 100644 --- a/structs.scad +++ b/structs.scad @@ -103,6 +103,8 @@ module struct_echo(struct,name="") { // is_struct(struct) // Description: Returns true if the input has the form of a structure, false otherwise. function is_struct(struct, ind=0) = - struct == [] || - let(dim = array_dim(struct)) - len(dim)==2 && dim[1]==2; + is_list(struct) && ( + struct == [] || + let(dim = array_dim(struct)) + len(dim)==2 && dim[1]==2 + ); diff --git a/tests/test_structs.scad b/tests/test_structs.scad new file mode 100644 index 0000000..147990a --- /dev/null +++ b/tests/test_structs.scad @@ -0,0 +1,59 @@ +include +include + + +module test_struct_set() { + st = struct_set([], "Foo", 42); + assert(st == [["Foo",42]]); + st2 = struct_set(st, "Bar", 28); + assert(st2 == [["Foo",42],["Bar",28]]); + st3 = struct_set(st2, "Foo", 91); + assert(st3 == [["Foo",91],["Bar",28]]); +} +test_struct_set(); + + +module test_struct_remove() { + st = [["Foo",91],["Bar",28],["Baz",9]]; + assert(struct_remove(st, "Foo") == [["Bar",28],["Baz",9]]); + assert(struct_remove(st, "Bar") == [["Foo",91],["Baz",9]]); + assert(struct_remove(st, "Baz") == [["Foo",91],["Bar",28]]); +} +test_struct_remove(); + + +module test_struct_val() { + st = [["Foo",91],["Bar",28],["Baz",9]]; + assert(struct_val(st,"Foo") == 91); + assert(struct_val(st,"Bar") == 28); + assert(struct_val(st,"Baz") == 9); +} +test_struct_val(); + + +module test_struct_keys() { + assert(struct_keys([["Foo",3],["Bar",2],["Baz",1]]) == ["Foo","Bar","Baz"]); + assert(struct_keys([["Zee",1],["Why",2],["Exx",3]]) == ["Zee","Why","Exx"]); +} +test_struct_keys(); + + +module test_struct_echo() { + // Can't yet test echo output +} +test_struct_echo(); + + +module test_is_struct() { + assert(is_struct([["Foo",1],["Bar",2],["Baz",3]])); + assert(!is_struct([["Foo"],["Bar"],["Baz"]])); + assert(!is_struct(["Foo","Bar","Baz"])); + assert(!is_struct([3,4,5])); + assert(!is_struct(3)); + assert(!is_struct(true)); + assert(!is_struct("foo")); +} +test_is_struct(); + + +// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap diff --git a/version.scad b/version.scad index 1be5831..f4b4a95 100644 --- a/version.scad +++ b/version.scad @@ -8,7 +8,7 @@ ////////////////////////////////////////////////////////////////////// -BOSL_VERSION = [2,0,20]; +BOSL_VERSION = [2,0,21]; // Section: BOSL Library Version Functions