UNPKG

reign

Version:

A persistent, typed-objects implementation.

226 lines (186 loc) 6.21 kB
"use strict"; var _2 = require("../.."); var _3 = require("./"); var _symbols = require("../../symbols"); function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } describeRealm('EnumType', function (options) { var realm = void 0; var StructType = void 0; var EnumType = void 0; var T = void 0; before(function () { realm = options.realm; StructType = realm.StructType; EnumType = realm.EnumType; T = realm.T; }); describe('Tiny Enums', function () { var Status = void 0; var Field = void 0; var field = void 0; it('should create a tiny enum when given less than 256 values', function () { Status = new EnumType("valid", "invalid"); }); it('should have the right byte length and alignment', function () { Status.byteLength.should.equal(1); Status.byteAlignment.should.equal(1); }); it('should create a struct containing the enum', function () { Field = new StructType({ name: T.InternedString, value: T.Any, status: Status }); }); it('should create a field instance', function () { field = new Field(); }); it('should have set the right initial value', function () { field.status.should.equal("valid"); }); it('should not set the status to a bad value', function () { (function () { return field.status = "nope"; }).should.throw(TypeError); }); it('should set the status to a new value', function () { field.status = "invalid"; }); it('should have updated the status', function () { field.status.should.equal("invalid"); }); describe('.randomValue()', function () { it('should generate a random value', function () { var value = Status.randomValue(); var seen = value; (value === "valid" || value === "invalid").should.equal(true); for (var i = 0; i < 100; i++) { value = Status.randomValue(); if (value !== seen) { if (seen === "invalid") { value.should.equal("valid"); } else { value.should.equal("invalid"); } return; } } throw new Error("Should have seen the other value in 100 attempts!"); }); }); describe('EnumArray', function () { var arr = void 0; it('should create a Status.Array instance', function () { arr = new Status.Array(3); }); it('should have a length of 3', function () { arr.length.should.equal(3); }); it('should be full of "valid" values', function () { arr.forEach(function (item) { return item.should.equal("valid"); }); }); it('should write some values', function () { arr[0] = "invalid"; arr[1] = "invalid"; arr[2] = "invalid"; }); it('should have the right values', function () { arr[0].should.equal("invalid"); arr[1].should.equal("invalid"); arr[2].should.equal("invalid"); }); it('should overwrite a value', function () { arr[1] = "valid"; }); it('should have overwritten the value', function () { arr.toJSON().should.eql(["invalid", "valid", "invalid"]); }); it('should not overwrite with an invalid value', function () { (function () { return arr[0] = "no"; }).should.throw(TypeError); }); }); }); describe('Large Enums', function () { var Status = void 0; var Field = void 0; var field = void 0; it('should create a large enum when given more than 256 values', function () { Status = new (Function.prototype.bind.apply(EnumType, [null].concat(_toConsumableArray(Array.from({ length: 512 }, function (_, index) { return index; })))))(); }); it('should have the right byte length and alignment', function () { Status.byteLength.should.equal(2); Status.byteAlignment.should.equal(2); }); it('should create a struct containing the enum', function () { Field = new StructType({ name: T.InternedString, value: T.Any, status: Status }); }); it('should create a field instance', function () { field = new Field(); }); it('should have set the right initial value', function () { field.status.should.equal(0); }); it('should not set the status to a bad value', function () { (function () { return field.status = "nope"; }).should.throw(TypeError); (function () { return field.status = -123; }).should.throw(TypeError); (function () { return field.status = 1024; }).should.throw(TypeError); }); it('should set the status to a new value', function () { field.status = 123; }); it('should have updated the status', function () { field.status.should.equal(123); }); describe('EnumArray', function () { var arr = void 0; it('should create a Status.Array instance', function () { arr = new Status.Array(3); }); it('should have a length of 3', function () { arr.length.should.equal(3); }); it('should be full of 0 values', function () { arr.forEach(function (item) { return item.should.equal(0); }); }); it('should write some values', function () { arr[0] = 1; arr[1] = 2; arr[2] = 3; }); it('should have the right values', function () { arr[0].should.equal(1); arr[1].should.equal(2); arr[2].should.equal(3); }); it('should overwrite a value', function () { arr[1] = 66; }); it('should have overwritten the value', function () { arr.toJSON().should.eql([1, 66, 3]); }); it('should not overwrite with an invalid value', function () { (function () { return arr[0] = "no"; }).should.throw(TypeError); }); }); }); });