Skip to content

Commit

Permalink
Convert typed dictionary tests
Browse files Browse the repository at this point in the history
  • Loading branch information
limzykenneth committed Nov 26, 2024
1 parent b7f970c commit de256c3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 75 deletions.
5 changes: 3 additions & 2 deletions test/js/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ const httpMocks = [
export const httpMock = setupWorker(...httpMocks);

// p5.js module mocks
export const mockP5 = {
export const mockP5 = vi.fn();
Object.assign(mockP5, {
_validateParameters: vi.fn(),
_friendlyFileLoadError: vi.fn(),
_friendlyError: vi.fn()
};
});

const mockCanvas = document.createElement('canvas');
export const mockP5Prototype = {
Expand Down
102 changes: 52 additions & 50 deletions test/unit/data/local_storage.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,86 @@
import p5 from '../../../src/app.js';
import { mockP5, mockP5Prototype } from '../../js/mocks';
import storage from '../../../src/data/local_storage';
import p5Color from '../../../src/color/p5.Color';
import creatingReading from '../../../src/color/creating_reading';
import p5Vector from '../../../src/math/p5.Vector';
import math from '../../../src/math/math';

suite('local storage', function() {
var myp5;
var myBoolean = false;
var myObject = { one: 1, two: { nested: true } };
var myNumber = 46;
var myString = 'coolio';
var myColor;
var myVector;
const myBoolean = false;
const myObject = { one: 1, two: { nested: true } };
const myNumber = 46;
const myString = 'coolio';
let myColor;
let myVector;

var hardCodedTypeID = 'p5TypeID';
const hardCodedTypeID = 'p5TypeID';

beforeAll(function() {
new p5(function(p) {
p.setup = function() {
myp5 = p;
myColor = myp5.color(40, 100, 70);
myVector = myp5.createVector(10, 20, 30);
myp5.storeItem('myBoolean', myBoolean);
myp5.storeItem('myObject', myObject);
myp5.storeItem('myNumber', myNumber);
myp5.storeItem('myString', myString);
myp5.storeItem('myColor', myColor);
myp5.storeItem('myVector', myVector);
};
});
});
storage(mockP5, mockP5Prototype);
p5Color(mockP5, mockP5Prototype);
creatingReading(mockP5, mockP5Prototype);
p5Vector(mockP5, mockP5Prototype);
math(mockP5, mockP5Prototype);

mockP5Prototype.storeItem('myBoolean', myBoolean);
mockP5Prototype.storeItem('myObject', myObject);
mockP5Prototype.storeItem('myNumber', myNumber);
mockP5Prototype.storeItem('myString', myString);

afterAll(function() {
myp5.remove();
myColor = mockP5Prototype.color(40, 100, 70);
myVector = mockP5Prototype.createVector(10, 20, 30);
mockP5Prototype.storeItem('myColor', myColor);
mockP5Prototype.storeItem('myVector', myVector);
});

suite('all keys and type keys should exist in local storage', function() {
test('boolean storage retrieval should work', function() {
assert.isTrue(myp5.getItem('myBoolean') === false);
assert.equal(mockP5Prototype.getItem('myBoolean'), false);
});
test('boolean storage should store the correct type ID', function() {
assert.isTrue(
localStorage.getItem('myBoolean' + hardCodedTypeID) === 'boolean'
assert.equal(
localStorage.getItem('myBoolean' + hardCodedTypeID), 'boolean'
);
});
test('object storage should work', function() {
assert.deepEqual(myp5.getItem('myObject'), {
assert.deepEqual(mockP5Prototype.getItem('myObject'), {
one: 1,
two: { nested: true }
});
});
test('object storage retrieval should store the correct type ID', function() {
assert.isTrue(
localStorage.getItem('myObject' + hardCodedTypeID) === 'object'
assert.equal(
localStorage.getItem('myObject' + hardCodedTypeID), 'object'
);
});
test('number storage retrieval should work', function() {
assert.isTrue(myp5.getItem('myNumber') === 46);
assert.equal(mockP5Prototype.getItem('myNumber'), 46);
});
test('number storage should store the correct type ID', function() {
assert.isTrue(
localStorage.getItem('myNumber' + hardCodedTypeID) === 'number'
assert.equal(
localStorage.getItem('myNumber' + hardCodedTypeID), 'number'
);
});
test('string storage retrieval should work', function() {
assert.isTrue(myp5.getItem('myString') === 'coolio');
assert.equal(mockP5Prototype.getItem('myString'), 'coolio');
});
test('string storage should store the correct type ID', function() {
assert.isTrue(
localStorage.getItem('myString' + hardCodedTypeID) === 'string'
assert.equal(
localStorage.getItem('myString' + hardCodedTypeID), 'string'
);
});
test('p5 Color should retrieve as p5 Color', function() {
assert.isTrue(myp5.getItem('myColor') instanceof p5.Color);
assert.instanceOf(mockP5Prototype.getItem('myColor'), mockP5.Color);
});
test('p5 Vector should retrieve as p5 Vector', function() {
assert.isTrue(myp5.getItem('myVector') instanceof p5.Vector);
assert.instanceOf(mockP5Prototype.getItem('myVector'), mockP5.Vector);
});
});

var checkRemoval = function(key) {
myp5.removeItem(key);
assert.deepEqual(myp5.getItem(key), null);
assert.deepEqual(myp5.getItem(key + hardCodedTypeID), null);
mockP5Prototype.removeItem(key);
assert.deepEqual(mockP5Prototype.getItem(key), null);
assert.deepEqual(mockP5Prototype.getItem(key + hardCodedTypeID), null);
};

suite('should be able to remove all items', function() {
Expand Down Expand Up @@ -110,14 +112,14 @@ suite('local storage', function() {
suite('should be able to clear all items at once', function () {
test('should remove all items set by storeItem()', function () {
localStorage.setItem('extra', 'stuff');
myp5.clearStorage();
assert.deepEqual(myp5.getItem('myBoolean'), null);
assert.deepEqual(myp5.getItem('myNumber'), null);
assert.deepEqual(myp5.getItem('myObject'), null);
assert.deepEqual(myp5.getItem('myString'), null);
assert.deepEqual(myp5.getItem('myColor'), null);
assert.deepEqual(myp5.getItem('myVector'), null);
assert.deepEqual(myp5.getItem('extra'), 'stuff');
mockP5Prototype.clearStorage();
assert.deepEqual(mockP5Prototype.getItem('myBoolean'), null);
assert.deepEqual(mockP5Prototype.getItem('myNumber'), null);
assert.deepEqual(mockP5Prototype.getItem('myObject'), null);
assert.deepEqual(mockP5Prototype.getItem('myString'), null);
assert.deepEqual(mockP5Prototype.getItem('myColor'), null);
assert.deepEqual(mockP5Prototype.getItem('myVector'), null);
assert.deepEqual(mockP5Prototype.getItem('extra'), 'stuff');
});
});
});
38 changes: 15 additions & 23 deletions test/unit/data/p5.TypedDict.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
import p5 from '../../../src/app.js';
import { mockP5, mockP5Prototype } from '../../js/mocks';
import typeDict from '../../../src/data/p5.TypedDict';

suite('Dictionary Objects', function() {
var myp5;
var stringDict;
var numberDict;
let stringDict;
let numberDict;

beforeAll(function() {
new p5(function(p) {
p.setup = function() {
myp5 = p;
stringDict = myp5.createStringDict('happy', 'coding');
numberDict = myp5.createNumberDict(1, 2);
};
});
});

afterAll(function() {
myp5.remove();
typeDict(mockP5, mockP5Prototype);
stringDict = mockP5Prototype.createStringDict('happy', 'coding');
numberDict = mockP5Prototype.createNumberDict(1, 2);
});

suite('p5.prototype.stringDict', function() {
test('should be created', function() {
assert.isTrue(stringDict instanceof p5.StringDict);
assert.instanceOf(stringDict, mockP5.StringDict);
});

test('has correct structure', function() {
assert.deepEqual(
JSON.stringify(stringDict),
JSON.stringify({ data: { happy: 'coding' } })
stringDict,
{ data: { happy: 'coding' } }
);
});

test('should have correct size', function() {
var amt = stringDict.size();
assert.isTrue(amt === Object.keys(stringDict.data).length);
assert.lengthOf(Object.keys(stringDict.data), amt);
});

test('should add new key-value pairs', function() {
Expand All @@ -59,19 +51,19 @@ suite('Dictionary Objects', function() {

suite('p5.prototype.numberDict', function() {
test('should be created', function() {
assert.isTrue(numberDict instanceof p5.NumberDict);
assert.instanceOf(numberDict, mockP5.NumberDict);
});

test('has correct structure', function() {
assert.deepEqual(
JSON.stringify(numberDict),
JSON.stringify({ data: { 1: 2 } })
numberDict,
{ data: { 1: 2 } }
);
});

test('should have correct size', function() {
var amt = numberDict.size();
assert.isTrue(amt === Object.keys(numberDict.data).length);
assert.lengthOf(Object.keys(numberDict.data), amt);
});

test('should add new key-value pairs', function() {
Expand Down

0 comments on commit de256c3

Please sign in to comment.