Skip to content

Commit

Permalink
add test examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jbakse committed Feb 1, 2022
1 parent 852cb28 commit 7a474ac
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 1 deletion.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions public/examples/test_types/README.md
@@ -0,0 +1 @@
# Test Types
15 changes: 15 additions & 0 deletions public/examples/test_types/index.html
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<head>
<link rel="stylesheet" href="../sketch.css" />
</head>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.0.0/p5.min.js"></script>

<script src="/dist/p5.party.js"></script>

<script src="index.js"></script>
</body>
</html>
103 changes: 103 additions & 0 deletions public/examples/test_types/index.js
@@ -0,0 +1,103 @@
let shared;

function preload() {
partyConnect(
"wss://deepstream-server-1.herokuapp.com",
"identity_test",
"main"
);
shared = partyLoadShared("shared");
}

function setup() {
createCanvas(400, 400);

if (partyIsHost()) {
// supported types
shared.null = null;
shared.boolean = true;
shared.number = 1;
shared.string = "hello";
shared.array = [1, 2, 3];
shared.object = {
x: 1,
y: 1,
};

// tricky types
shared.date = new Date("January 1, 2001 01:00:00");
shared.color = color("red").toString();
shared.set = new Set([1, 2, 3]);
shared.undefined = "temp";
shared.undefined = undefined;

// unsupported types
shared.function = () => {
console.log("hello");
};

shared.symbol = Symbol("hello");

shared.infinity = Infinity; // -> null
shared.nan = NaN; // -> null
}
console.log("A", shared);

console.log("");
console.log("SUPPORTED");
test("null", shared.null, null);
test("boolean", shared.boolean, true);
test("number", shared.number, 1);
test("string", shared.string, "hello");
test("array", shared.array, [1, 2, 3]);
test("object", shared.object, { x: 1, y: 1 });

console.log("");
console.log("TRICKY");
test(
"date are stringified",
new Date(shared.date),
new Date("January 1, 2001 01:00:00")
);

console.log(
"colors need to be wrapped `shared.color color('red').toString()` and `color(shared.color)`"
);
console.log(shared.color);
const c = color(shared.color);
test("color", red(c), red(color("red")));
test("color", green(c), green(color("red")));
test("color", blue(c), blue(color("red")));

test("undefineds are stripped", shared.undefined, undefined);

console.log("");
console.log("UNSUPPORTED");
test("functions are stripped", undefined);
test("symbols are stripped", shared.symbol, undefined);
test("infinity becomes null", shared.infinity, null);
test("nan becomes null", shared.nan, null);
}

function test(name, value, expected) {
if (same(value, expected)) {
console.log(`%c${name} passed`, "color: green", value, expected);
} else {
console.log(`%c${name} failed`, "color: red", value, expected);
}
}

function same(value, expected) {
if (value === expected) {
return true;
}
if (typeof value === "object" && typeof expected === "object") {
if (Object.keys(expected).length !== Object.keys(value).length) {
return false;
}
return Object.entries(expected).every(([k, v]) => v === value[k]);
}
return false;
}

function draw() {}
5 changes: 4 additions & 1 deletion public/index.html
Expand Up @@ -57,14 +57,17 @@ <h2>Contributed Examples</h2>
<a href="show_example.html?chat_log_no_p5">chat_log_no_p5</a>
</li>

<h2>Tests</h2>
<li><a href="show_example.html?test_identity">test_identity</a></li>
<li><a href="show_example.html?test_types">test_types</a></li>

<h2>Expiremental Examples</h2>
<li><a href="show_example.html?no_p5">no_p5</a></li>
<li><a href="show_example.html?empty_sketch">empty_sketch</a></li>
<li><a href="show_example.html?stickies">stickies</a></li>
<li><a href="show_example.html?ultra_dart_city">ultra_dart_city</a></li>
<li><a href="show_example.html?pong_m">pong</a></li>
<li><a href="show_example.html?coverup_m">coverup</a></li>
<li><a href="show_example.html?identity_test">identity_test</a></li>
</article>
</body>
</html>

0 comments on commit 7a474ac

Please sign in to comment.