Skip to content

Commit

Permalink
Add configuration-based unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxattax97 committed Mar 15, 2019
1 parent ffb14b8 commit 4d03002
Show file tree
Hide file tree
Showing 13 changed files with 1,019 additions and 37 deletions.
14 changes: 8 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,14 @@ async function main(params) {
}
}

if (argv.input && argv.input.length > 1) {
await fs.ensureDir(argv.output);
} else if (argv.input && argv.input.length === 1) {
await fs.ensureFile(argv.output);
} else if (!argv.output && argv.input && argv.input.length === 1) {
argv.output = argv.input;
try {
if (argv.output && argv.input && argv.input.length > 1) {
await fs.ensureDir(argv.output);
} else if (argv.output && argv.input && argv.input.length === 1) {
await fs.ensureFile(argv.output);
}
} catch (err) {
console.error('Failure while ensuring proper output pathing', err);
}

const resultList = [];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint": "eslint .",
"fix": "eslint . --fix",
"test": "mocha",
"reset-clean": "./index.js -i ./test/dirty/*.scad -o ./test/clean/",
"reset-clean": "./index.js -i './test/dirty/*.scad' -o ./test/clean/",
"all": "eslint . --fix && mocha"
},
"repository": {
Expand Down
26 changes: 26 additions & 0 deletions test/clean/constant.scad
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
E = 2.71828182845904523536028747135266249775724709369995; // Natural number.

// Ratio of a circle's circumference to it's diameter.
PI = 3.14159265358979323846264338327950288419716939937510;

// Golden ratio.
PHI = 1.61803398874989484820458683436563811772030917980576;

// A set of common square roots.
SQRT_2 = 1.41421356237309504880168872420969807856967187537694;
SQRT_3 = 1.73205080756887729352744634150587236694280525381038;
SQRT_5 = 2.23606797749978969640917366873127623544061835961152;
SQRT_7 = 2.64575131106459059050161575363926042571025918308245;

IN = 25.4 * MM;
FT = 304.8 * MM;
YD = 914.4 * MM;
MI = 1609344.0 * MM;
THOU = 0.0254 * MM;
MIL = THOU;

INCH = IN;
FOOT = FT;
FEET = FT;
YARD = YD;
MILE = MI;
42 changes: 42 additions & 0 deletions test/clean/function.scad
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@


/**
* Computes the exponent of a base and a power.
*
* @param base The number to be multiplied power times.
* @param power The number of times to multiply the base together.
* @return The base risen the the power.
*/
function MTH_power(base, power) = pow(base, power); // exp(ln(base) * power);

/**
* Measures the distance between two 3D vectors.
*
* @param vector_a The first 3D vector to compare.
* @param vector_b The second 3D vector to compare.
* @return The distance between vector_a and vector_b.
*/
function MTH_distance3D(vector_a, vector_b) =
sqrt((vector_a[0] - vector_b[0]) * (vector_a[0] - vector_b[0]) +
(vector_a[1] - vector_b[1]) * (vector_a[1] - vector_b[1]) +
(vector_a[2] - vector_b[2]) * (vector_a[2] - vector_b[2]));

/**
* Measures the distance between two 2D vectors.
*
* @param vector_a The first 2D vector to compare.
* @param vector_b The second 2D vector to compare.
* @return The distance between vector_a and vector_b.
*/
function MTH_distance2D(vector_a, vector_b) =
sqrt((vector_a[0] - vector_b[0]) * (vector_a[0] - vector_b[0]) +
(vector_a[1] - vector_b[1]) * (vector_a[1] - vector_b[1]));

function MTH_distance1D(vector_a, vector_b) = abs(vector_a - vector_b);
function MTH_normalize(vector) =
norm(vector); // vector / (max(MTH_distance3D(ORIGIN, vector), EPSILON));
function MTH_normalVectorAngle(vector) = [
0,
-1 * atan2(vector[2], MTH_distance1D([ vector[0], vector[1] ])),
atan2(vector[1], vector[0])
];
21 changes: 21 additions & 0 deletions test/clean/include.scad
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
include <cornucopia/util/math.scad>
include <cornucopia/util/measures/imperial.scad>
use <cornucopia/util/vector.scad>

include <cornucopia/util/constants.scad>

use <cornucopia/util/constants.scad>

module
testUnitTest()
{
include <cornucopia/util/measures/standard.scad>
include <cornucopia/util/util.scad>
echo(TST_equal("Equality", [ 1, 2, 4, 8 ], [ 1, 2, 4, 8 ]));
echo(TST_notEqual("Non-equality", [ 1, 2, 4, 8 ], [ 0, 1, 1, 2 ]));
echo(TST_true("Truthiness", 1 + 1 == 2));
echo(TST_false("Falseness", 1 + 1 == 3));
echo(TST_in("Presence", 4, [ 1, 2, 4, 8 ]));
echo(TST_notIn("Absence", 16, [ 1, 2, 4, 8 ]));
echo(TST_approximately("Approximately Equal", 15 + (EPSILON / 2), 15));
}
166 changes: 166 additions & 0 deletions test/clean/integration.scad
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@

echo(TST_true("Iterable", UTL_iterable([ 1, 2, 3 ])));
echo(TST_false("Not iterable", UTL_iterable(1)));

echo(TST_true("Empty", UTL_empty([])));
echo(TST_false("Not empty", UTL_empty([ 1, 2, 3 ])));

echo(TST_equal("Head", UTL_head([ 1, 2, 3 ]), 1));

echo(TST_equal("Tail some", UTL_tail([ 1, 2, 3 ]), [ 2, 3 ]));
echo(TST_equal("Tail one", UTL_tail([1]), []));
echo(TST_equal("Tail zero", UTL_tail([]), undef));

echo(TST_equal("Last some", UTL_last([ 1, 2, 3 ]), 3));
echo(TST_equal("Last one", UTL_last([1]), 1));
echo(TST_equal("Last zero", UTL_last([]), undef));

echo(TST_equal("Reverse some", UTL_reverse([ 1, 2, 3 ]), [ 3, 2, 1 ]));
echo(TST_equal("Reverse zero", UTL_reverse([]), []));

echo(TST_true("Equal number", UTL_equal(0, 0), true));
echo(TST_false("Not equal number", UTL_equal(0, 5)));
echo(TST_true("Equal empty list", UTL_equal([], [])));
echo(TST_true("Equal list", UTL_equal([ 1, 2, 4 ], [ 1, 2, 4 ])));
echo(TST_false("Not equal list", UTL_equal([ 1, 2, 3 ], [ 1, 2, 4 ])));
echo(TST_true("Equal nested list",
UTL_equal([ [ 1, 2, 3 ], [ 4, 5, 6 ] ],
[ [ 1, 2, 3 ], [ 4, 5, 6 ] ])));
echo(TST_false("Not equal nested list",
UTL_equal([ [ 1, 2, 3 ], [ 4, 5, 6 ] ],
[ [ 1, 2, 4 ], [ 4, 5, 6 ] ])));
echo(TST_false("Equal unbalanced list",
UTL_equal([ [ 1, 2, 3 ], [ 4, 5, 6 ] ], [ 7, [ 4, 5, 6 ] ])));

echo(TST_true("All", UTL_all([ true, true, true ])));
echo(TST_false("Not all", UTL_all([ true, true, false ])));

echo(TST_true("Any", UTL_any([ false, false, true ])));
echo(TST_false("Not any", UTL_any([ false, false, false ])));

echo(TST_true("Contains", UTL_contains([ 1, 2, 3 ], 2)));
echo(TST_false("Doesn't contain", UTL_contains([ 1, 2, 3 ], 6)));

echo(TST_equal("Zip zero", UTL_zip([]), []));
echo(TST_equal("Zip zero 2", UTL_zip([ [], [], [] ]), []));
echo(TST_equal("Zip zero 3", UTL_zip([ [], [1], [2] ]), []));
echo(TST_equal("Zip equal length",
UTL_zip([ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]),
[ [ 1, 4, 7 ], [ 2, 5, 8 ], [ 3, 6, 9 ] ]));
echo(TST_equal("Zip different length",
UTL_zip([ [ 1, 2, 3 ], [ 4, 5 ], [ 7, 8, 9 ] ]),
[ [ 1, 4, 7 ], [ 2, 5, 8 ] ]));

echo(TST_equal("Sort zero", UTL_sort([]), []));
echo(TST_equal("Sort some", UTL_sort([ 4, 2, 8, 16, 1 ]), [ 1, 2, 4, 8, 16 ]));

echo(TST_equal("One Pole Filter Zero", UTL_onePoleFilter([], 0), []));
echo(TST_equal("One Pole Filter Some",
UTL_onePoleFilter([ 1, 2, 3 ], 0),
[ 1, 2, 3 ]));
echo(TST_equal("One Pole Filter Positive",
UTL_onePoleFilter([ 4, 2, 5 ], 0.5),
[ 4, 3, 4 ]));
echo(TST_equal("One Pole Filter Negative",
UTL_onePoleFilter([ 4, 2, 5 ], -0.5),
[ 4, 1, 7 ]));

rod(20);
translate([ rodsize * 2.5, 0, 0 ]) rod(20, true);
translate([ rodsize * 5, 0, 0 ]) screw(10, true);
translate([ rodsize * 7.5, 0, 0 ]) bearing();
translate([ rodsize * 10, 0, 0 ]) rodnut();
translate([ rodsize * 12.5, 0, 0 ]) rodwasher();
translate([ rodsize * 15, 0, 0 ]) nut();
translate([ rodsize * 17.5, 0, 0 ]) washer();

// examples
linearBearing(model = "LM8UU");
translate([ 20, 0, 0 ]) linearBearing(model = "LM10UU");

module metric_ruler(millimeters)
{
difference()
{
// Body of ruler
color("Beige")
cube(size = [ length_mm(millimeters), length_cm(3), length_mm(1) ]);
// Centimeter markings
for (i = [0:length_cm(1):length_mm(millimeters) + epsilon]) {
translate([ i, length_cm(2.5), length_mm(0.75) ]) color("Red")
cube(size =
[
length_mm(0.5),
length_cm(1) + epsilon,
length_mm(0.5) +
epsilon
],
center = true);
}
// Half centimeter markings
for (i = [length_cm(0.5):length_cm(1):length_mm(millimeters) +
epsilon]) {
tran,slate([ i, length_cm(2.7), length_mm(0.875) ]) color("Red")
cube(size =
[
length_mm(0.5),
length_cm(0.6) + epsilon,
length_mm(0.25) +
epsilon
],
center = true);
}
// Millimeter markings
for (i = [length_mm(1):length_mm(1):length_mm(millimeters) + epsilon]) {
translate([ i, length_cm(2.85), length_mm(0.9375) ]) color("Red")
cube(size =
[
length_mm(0.5),
length_cm(0.3) + epsilon,
length_mm(0.125) +
epsilon
],
center = true);
}
}
}

metric_ruler(100);

include <MCAD/shapes/polyhole.scad>

module
polyhole_demo()
{
difference()
{
cube(size = [ 100, 27, 3 ]);
union()
{
for (i = [1:10]) {
translate([ (i * i + i) / 2 + 3 * i, 8, -1 ])
mcad_polyhole(h = 5, d = i);

assign(d = i + 0.5)
translate([ (d * d + d) / 2 + 3 * d, 19, -1 ])
mcad_polyhole(h = 5, d = d);
}
}
}
}

polyhole_demo();

include <MCAD/gears/rack_and_pinion.scad>

// examples of usage
// include this in your code:
// use <rack_and_pinion.scad>
// then:
// a simple rack
rack(4,
20,
10,
1); // CP (mm/tooth), width (mm), thickness(of base) (mm), # teeth
// a simple pinion and translation / rotation to make it mesh the rack
translate([ 0, -8.5, 0 ]) rotate([ 0, 0, 360 / 10 / 2 ]) pinion(4, 10, 10, 5);
Loading

0 comments on commit 4d03002

Please sign in to comment.