Skip to content

Commit

Permalink
Started adding test cases, but wow, it's a lot of work to port these …
Browse files Browse the repository at this point in the history
…from Golang by hand.
  • Loading branch information
Louis St-Amour committed Jul 1, 2020
1 parent 235ce21 commit 833a730
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 17 deletions.
Empty file added src/builtins/aggregates.test.js
Empty file.
Empty file added src/builtins/arrays.test.js
Empty file.
Empty file.
Empty file added src/builtins/jwt.test.js
Empty file.
Empty file added src/builtins/numbers.test.js
Empty file.
24 changes: 21 additions & 3 deletions src/builtins/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,27 @@ union = function() {
return obj;
}

remove = function(obj, key) {
var newObj = Object.assign({}, obj);
delete newObj[key];
remove = function(object, keys) {
if(!(typeof object === "object" && object && object.constructor === Object)) {
if(object instanceof Set) {
throw 'object.remove: invalid argument(s)';
}
throw `object.remove: operand 1 must be object but got ${object === null || object === undefined ? 'var' : typeof object === 'object' ? object.constructor.name.toLowerCase() : typeof object}`;
}
var newObj = Object.assign({}, object);
if(keys instanceof Set || keys instanceof Array) {
for(var k of keys) {
delete newObj[k];
}
} else if(typeof keys === "object" && keys && keys.constructor === Object) {
for(var k in keys) {
if(keys.hasOwnProperty(k)) {
delete newObj[k];
}
}
} else {
throw `object.remove: operand 2 must be one of {object, string, array} but got ${typeof keys === 'object' ? 'var' : typeof keys}`;
}
return newObj;
}

Expand Down
157 changes: 157 additions & 0 deletions src/builtins/object.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
const object = require('./object');

const testCases = { "object.remove": [
{
note: "base",
object: {"a": 1, "b": {"c": 3}},
keys: new Set(["a"]),
expected: {"b": {"c": 3}},
},
{
note: "multiple keys set",
object: {"a": 1, "b": {"c": 3}, "d": 4},
keys: new Set(["d", "b"]),
expected: {"a": 1},
},
{
note: "multiple keys array",
object: {"a": 1, "b": {"c": 3}, "d": 4},
keys: ["d", "b"],
expected: {"a": 1},
},
{
note: "multiple keys object",
object: {"a": 1, "b": {"c": 3}, "d": 4},
keys: {"d": "", "b": 1},
expected: {"a": 1},
},
{
note: "multiple keys object nested",
object: {"a": {"b": {"c": 2}}, "x": 123},
keys: {"a": {"b": {"foo": "bar"}}},
expected: {"x": 123},
},
{
note: "empty object",
object: {},
keys: new Set(["a", "b"]),
expected: {},
},
{
note: "empty keys set",
object: {"a": 1, "b": {"c": 3}},
keys: new Set(),
expected: {"a": 1, "b": {"c": 3}},
},
{
note: "empty keys array",
object: {"a": 1, "b": {"c": 3}},
keys: [],
expected: {"a": 1, "b": {"c": 3}},
},
{
note: "empty keys obj",
object: {"a": 1, "b": {"c": 3}},
keys: {},
expected: {"a": 1, "b": {"c": 3}},
},
{
note: "key doesnt exist",
object: {"a": 1, "b": {"c": 3}},
keys: new Set(["z"]),
expected: {"a": 1, "b": {"c": 3}},
},
{
note: "error invalid object param type set",
object: new Set(["a"]),
keys: new Set(["a"]),
expected: new Error("object.remove: invalid argument(s)"),
},
// {
// note: "error invalid object param type bool",
// object: false,
// keys: new Set("a"),
// expected: new Error("object.remove: invalid argument(s)"),
// },
{
note: "error invalid object param type array input",
object: ["a"],
keys: new Set(["a"]),
expected: new Error("object.remove: operand 1 must be object but got array"),
},
{
note: "error invalid object param type bool input",
object: false,
keys: new Set(["a"]),
expected: new Error("object.remove: operand 1 must be object but got boolean"),
},
{
note: "error invalid object param type number input",
object: 123,
keys: new Set(["a"]),
expected: new Error("object.remove: operand 1 must be object but got number"),
},
{
note: "error invalid object param type string input",
object: "foo",
keys: new Set(["a"]),
expected: new Error("object.remove: operand 1 must be object but got string"),
},
{
note: "error invalid object param type nil input",
object: null,
keys: new Set(["a"]),
expected: new Error("object.remove: operand 1 must be object but got var"),
},
// {
// note: "error invalid key param type string",
// object: {"a": 1},
// keys: "a",
// expected: new Error("object.remove: invalid argument(s)"),
// },
// {
// note: "error invalid key param type boolean",
// object: {"a": 1},
// keys: false,
// expected: new Error("object.remove: invalid argument(s)"),
// },
{
note: "error invalid key param type string input",
object: {"a": 1},
keys: "foo",
expected: new Error("object.remove: operand 2 must be one of {object, string, array} but got string"),
},
{
note: "error invalid key param type boolean input",
object: {"a": 1},
keys: true,
expected: new Error("object.remove: operand 2 must be one of {object, string, array} but got boolean"),
},
{
note: "error invalid key param type number input",
object: {"a": 1},
keys: 22,
expected: new Error("object.remove: operand 2 must be one of {object, string, array} but got number"),
},
{
note: "error invalid key param type nil input",
object: {"a": 1},
keys: null,
expected: new Error("object.remove: operand 2 must be one of {object, string, array} but got var"),
},
]};

for(const [subject, cases] of Object.entries(testCases)) {
describe(`object.${subject}`, () => {
cases.forEach(c => {
test(c.note, () => {
const test = () => object[subject](c.object, c.keys);
if(c.expected instanceof Error) {
expect(test).toThrow(c.expected);
} else {
expect(test()).toEqual(c.expected);
}
});
});
});
}
Empty file added src/builtins/regex.test.js
Empty file.
Empty file added src/builtins/strings.test.js
Empty file.
Empty file added src/builtins/types.test.js
Empty file.
14 changes: 0 additions & 14 deletions test/numbers.test.js

This file was deleted.

0 comments on commit 833a730

Please sign in to comment.