preserves/implementations/javascript/packages/core/test/values.test.ts

92 lines
2.7 KiB
TypeScript

import { Single, Double, fromJS, Dictionary, IDENTITY_FOLD, fold, mapEmbeddeds, Value, embed, preserves } from '../src/index';
import './test-utils';
describe('Single', () => {
it('should print reasonably', () => {
expect(Single(123.45).toString()).toEqual("123.45f");
});
});
describe('Double', () => {
it('should print reasonably', () => {
expect(Double(123.45).toString()).toEqual("123.45");
});
});
describe('fold', () => {
function mkv<T extends object>(t: T): Value<T> {
return fromJS<T>([
1,
2,
new Dictionary([[[3, 4], fromJS([5, 6])],
['a', 1],
['b', true]]),
Single(3.4),
t,
]);
}
it('should support identity', () => {
const w = new Date();
const v = mkv(w);
expect(fold(v, IDENTITY_FOLD)).is(v);
const w1 = new Date();
const v1 = mkv(w1);
expect(fold(v, IDENTITY_FOLD)).not.is(v1);
expect(mapEmbeddeds(v, _t => embed(w1))).is(v1);
});
});
describe('fromJS', () => {
it('should map integers to themselves', () => {
expect(fromJS(1)).toBe(1);
});
it('should map bigints to themselves', () => {
expect(fromJS(BigInt("12345678123456781234567812345678")))
.toBe(BigInt("12345678123456781234567812345678"));;
});
});
describe('is()', () => {
it('should compare small integers sensibly', () => {
expect(3).is(3);
expect(3).not.is(4);
});
it('should compare large integers sensibly', () => {
const a = BigInt("12345678123456781234567812345678");
const b = BigInt("12345678123456781234567812345679");
expect(a).is(a);
expect(a).is(BigInt("12345678123456781234567812345678"));
expect(a).not.is(b);
});
it('should compare mixed integers sensibly', () => {
const a = BigInt("12345678123456781234567812345678");
const b = BigInt("3");
const c = BigInt("4");
expect(3).not.is(a);
expect(a).not.is(3);
expect(3).not.toBe(b);
expect(3).is(b);
expect(b).not.toBe(3);
expect(b).is(3);
expect(3).not.toBe(c);
expect(3).not.is(c);
expect(c).not.toBe(3);
expect(c).not.is(3);
});
});
describe('`preserves` formatter', () => {
it('should format numbers', () => {
expect(preserves`>${3}<`).toBe('>3<');
});
it('should format small bigints', () => {
expect(preserves`>${BigInt("3")}<`).toBe('>3<');
});
it('should format big bigints', () => {
expect(preserves`>${BigInt("12345678123456781234567812345678")}<`)
.toBe('>12345678123456781234567812345678<');
});
});