Added regressions and bug fixes for structs.scad

This commit is contained in:
Revar Desmera 2019-11-08 18:31:37 -08:00
parent 1a0b026da5
commit a26bb92978
3 changed files with 65 additions and 4 deletions

View file

@ -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
);

59
tests/test_structs.scad Normal file
View file

@ -0,0 +1,59 @@
include <BOSL2/std.scad>
include <BOSL2/structs.scad>
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

View file

@ -8,7 +8,7 @@
//////////////////////////////////////////////////////////////////////
BOSL_VERSION = [2,0,20];
BOSL_VERSION = [2,0,21];
// Section: BOSL Library Version Functions