1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-09-28 23:39:10 +02:00
Files
dotSCAD/src/unittest.scad
2017-05-26 10:13:58 +08:00

100 lines
2.2 KiB
OpenSCAD
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module fail(title, message) {
echo(
str(
"<b>",
"<span style='color: red'>",
"FAIL: ", title,
"</span>",
"</b>"
)
);
if(message != undef) {
echo(
str(
"<b>",
"<span style='color: red'> ",
message,
"</span>",
"</b>"
)
);
}
}
function shift_to_int(point, digits) =
let(
pt = point * digits
)
len(pt) == 2 ?
[round(pt[0]), round(pt[1])] :
[round(pt[0]), round(pt[1]), round(pt[2])];
function all_shift_to_int(points, digits) =
[for(pt = points) shift_to_int(pt, digits) / digits];
module assertEqualPoint(expected, actual) {
leng_expected = len(expected);
leng_actual = len(actual);
if(leng_expected != leng_actual) {
fail(
"Point",
str("expected length: ", leng_expected,
", but: ", leng_actual)
);
} else {
n = 10000;
shifted_expected = shift_to_int(
expected, n
);
shifted_actual = shift_to_int(
actual, n
);
if(shifted_expected != shifted_actual) {
fail(
"Point",
str("expected: ", shifted_expected / n,
", but: ", shifted_actual / n)
);
}
}
}
module assertEqualPoints(expected, actual) {
leng_expected = len(expected);
leng_actual = len(actual);
if(leng_expected != leng_actual) {
fail(
"Points",
str("expected length: ", leng_expected,
", but: ", leng_actual)
);
} else {
for(i = [0:len(actual) - 1]) {
assertEqualPoint(expected[i], actual[i]);
}
}
}
module assertEqual(expected, actual) {
if(expected != actual) {
fail(
"Equality",
str("expected: ", expected,
", but: ", actual)
);
}
}
module assertTrue(truth) {
if(!truth) {
fail(
"Truth",
"expected: true, but: false"
);
}
}