Skip to content

Commit eca81f1

Browse files
committed
add more tests for input values
1 parent 1ed0684 commit eca81f1

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ const blobs = (opt: BlobOptions): string => {
1212
// Random number generator.
1313
const rgen = rand(opt.seed || String(Date.now()));
1414

15+
if (!opt.size) {
16+
throw new Error("no size specified");
17+
}
18+
1519
if (!opt.stroke && !opt.color) {
1620
throw new Error("no color or stroke specified");
1721
}

testing/blobs.test.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import blobs, {BlobOptions} from "..";
22

3+
const genMinimalOptions = (): BlobOptions => ({
4+
size: 1000 * Math.random(),
5+
complexity: 1 - Math.random(),
6+
contrast: 1 - Math.random(),
7+
color: "#fff",
8+
});
9+
310
describe("blobs", () => {
411
it("should return a different result when seed is not provided", () => {
5-
const options: BlobOptions = {size: 100, complexity: 1, contrast: 1, color: "red"};
12+
const options = genMinimalOptions();
613

714
const a = blobs(options);
815
const b = blobs(options);
@@ -11,12 +18,48 @@ describe("blobs", () => {
1118
});
1219

1320
it("should return the same result when the seed is provided", () => {
14-
const options: BlobOptions = {size: 200, complexity: 0.5, contrast: 0.5, color: "blue"};
15-
options.seed = "abcde";
21+
const options = genMinimalOptions();
1622

23+
options.seed = "abcde";
1724
const a = blobs(options);
1825
const b = blobs(options);
1926

2027
expect(a).toEqual(b);
2128
});
29+
30+
it("should require a size be provided", () => {
31+
const options = genMinimalOptions();
32+
33+
delete options.size;
34+
expect(() => blobs(options)).toThrow("size");
35+
});
36+
37+
it("should reject out of range complexity values", () => {
38+
const options = genMinimalOptions();
39+
40+
options.complexity = 1234;
41+
expect(() => blobs(options)).toThrow("complexity");
42+
43+
options.complexity = 0;
44+
expect(() => blobs(options)).toThrow("complexity");
45+
});
46+
47+
it("should reject out of range contrast values", () => {
48+
const options = genMinimalOptions();
49+
50+
options.contrast = 999;
51+
expect(() => blobs(options)).toThrow("contrast");
52+
53+
options.contrast = -1;
54+
expect(() => blobs(options)).toThrow("contrast");
55+
});
56+
57+
it("should reject options without stroke or color", () => {
58+
const options = genMinimalOptions();
59+
60+
delete options.stroke;
61+
delete options.color;
62+
expect(() => blobs(options)).toThrow("stroke");
63+
expect(() => blobs(options)).toThrow("color");
64+
});
2265
});

0 commit comments

Comments
 (0)