Skip to content

Commit

Permalink
Group serializer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bartblast committed Oct 17, 2024
1 parent 93ec588 commit dfcc1b2
Showing 1 changed file with 84 additions and 67 deletions.
151 changes: 84 additions & 67 deletions test/javascript/serializer_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,116 +10,133 @@ import Type from "../../assets/js/type.mjs";

defineGlobalErlangAndElixirModules();

describe("Serializer", () => {
describe.only("Serializer", () => {
describe("serialize()", () => {
const serialize = Serializer.serialize;

describe("boxed terms", () => {
it("atom", () => {
const term = Type.atom('a"bc');
const expected = '[1,"__atom__:a\\"bc"]';
describe("atom", () => {
it("top-level", () => {
const term = Type.atom('a"bc');
const expected = '[1,"__atom__:a\\"bc"]';

assert.equal(serialize(term), expected);
});
assert.equal(serialize(term), expected);
});

it("nested atom", () => {
const term = {a: Type.atom('a"bc'), b: 2};
const expected = '[1,{"a":"__atom__:a\\"bc","b":2}]';
it("nested", () => {
const term = {a: Type.atom('a"bc'), b: 2};
const expected = '[1,{"a":"__atom__:a\\"bc","b":2}]';

assert.equal(serialize(term), expected);
assert.equal(serialize(term), expected);
});
});

describe("bitstring", () => {
it("binary", () => {
const term = Type.bitstring('a"bc');
const expected = '[1,"__binary__:a\\"bc"]';
describe("binary", () => {
it("top-level", () => {
const term = Type.bitstring('a"bc');
const expected = '[1,"__binary__:a\\"bc"]';

assert.equal(serialize(term), expected);
});
assert.equal(serialize(term), expected);
});

it("nested binary", () => {
const term = {a: Type.bitstring('a"bc'), b: 2};
const expected = '[1,{"a":"__binary__:a\\"bc","b":2}]';
it("nested", () => {
const term = {a: Type.bitstring('a"bc'), b: 2};
const expected = '[1,{"a":"__binary__:a\\"bc","b":2}]';

assert.equal(serialize(term), expected);
assert.equal(serialize(term), expected);
});
});

it("non-binary", () => {
const term = Type.bitstring([1, 0, 1, 0]);
const expected = '[1,{"type":"bitstring","bits":[1,0,1,0]}]';
describe("non-binary", () => {
it("top-level", () => {
const term = Type.bitstring([1, 0, 1, 0]);
const expected = '[1,{"type":"bitstring","bits":[1,0,1,0]}]';

assert.equal(serialize(term), expected);
});
assert.equal(serialize(term), expected);
});

it("nested non-binary", () => {
const term = {a: Type.bitstring([1, 0, 1, 0]), b: 2};
const expected =
'[1,{"a":{"type":"bitstring","bits":[1,0,1,0]},"b":2}]';
it("nested", () => {
const term = {a: Type.bitstring([1, 0, 1, 0]), b: 2};
const expected =
'[1,{"a":{"type":"bitstring","bits":[1,0,1,0]},"b":2}]';

assert.equal(serialize(term), expected);
assert.equal(serialize(term), expected);
});
});
});

it("float", () => {
const term = Type.float(1.23);
const expected = '[1,"__float__:1.23"]';
describe("float", () => {
it("top-level", () => {
const term = Type.float(1.23);
const expected = '[1,"__float__:1.23"]';

assert.equal(serialize(term), expected);
});
assert.equal(serialize(term), expected);
});

it("nested float", () => {
const term = {a: Type.float(1.23), b: 2};
const expected = '[1,{"a":"__float__:1.23","b":2}]';
it("nested", () => {
const term = {a: Type.float(1.23), b: 2};
const expected = '[1,{"a":"__float__:1.23","b":2}]';

assert.equal(serialize(term), expected);
assert.equal(serialize(term), expected);
});
});

it("integer", () => {
const term = Type.integer(123);
const expected = '[1,"__integer__:123"]';
describe("integer", () => {
it("top-level", () => {
const term = Type.integer(123);
const expected = '[1,"__integer__:123"]';

assert.equal(serialize(term), expected);
});
assert.equal(serialize(term), expected);
});

it("nested integer", () => {
const term = {a: Type.integer(123), b: 2};
const expected = '[1,{"a":"__integer__:123","b":2}]';
it("nested", () => {
const term = {a: Type.integer(123), b: 2};
const expected = '[1,{"a":"__integer__:123","b":2}]';

assert.equal(serialize(term), expected);
assert.equal(serialize(term), expected);
});
});

it("list", () => {
const term = Type.list([Type.integer(1), Type.float(1.23)]);
describe("list", () => {
it("top-level", () => {
const term = Type.list([Type.integer(1), Type.float(1.23)]);

const expected =
'[1,{"type":"list","data":["__integer__:1","__float__:1.23"],"isProper":true}]';
const expected =
'[1,{"type":"list","data":["__integer__:1","__float__:1.23"],"isProper":true}]';

assert.equal(serialize(term), expected);
});
assert.equal(serialize(term), expected);
});

it("nested list", () => {
const term = {a: Type.list([Type.integer(1), Type.float(1.23)]), b: 2};
it("nested", () => {
const term = {
a: Type.list([Type.integer(1), Type.float(1.23)]),
b: 2,
};

const expected =
'[1,{"a":{"type":"list","data":["__integer__:1","__float__:1.23"],"isProper":true},"b":2}]';
const expected =
'[1,{"a":{"type":"list","data":["__integer__:1","__float__:1.23"],"isProper":true},"b":2}]';

assert.equal(serialize(term), expected);
assert.equal(serialize(term), expected);
});
});
});

describe("JS terms", () => {
it("JS BigInt", () => {
const term = 123n;
const expected = '[1,"__bigint__:123"]';
describe("BigInt", () => {
it("top-level", () => {
const term = 123n;
const expected = '[1,"__bigint__:123"]';

assert.equal(serialize(term), expected);
});
assert.equal(serialize(term), expected);
});

it("nested JS BigInt", () => {
const term = {a: 123n, b: 2};
const expected = '[1,{"a":"__bigint__:123","b":2}]';
it("nested", () => {
const term = {a: 123n, b: 2};
const expected = '[1,{"a":"__bigint__:123","b":2}]';

assert.equal(serialize(term), expected);
assert.equal(serialize(term), expected);
});
});
});
});
Expand Down

0 comments on commit dfcc1b2

Please sign in to comment.