Implemented enum descriptor for bigint enums

This commit is contained in:
Kir_Antipov 2023-01-18 12:03:05 +00:00
parent 6256d33db6
commit 874b950fdf
2 changed files with 88 additions and 0 deletions

View file

@ -0,0 +1,41 @@
import { EnumDescriptor } from "./enum-descriptor";
/**
* This descriptor is used to describe a set of flags stored as a `bigint` value.
*/
export class BigIntDescriptor implements EnumDescriptor<bigint> {
/**
* @inheritdoc
*/
get name(): "bigint" {
return "bigint";
}
/**
* @inheritdoc
*/
get defaultValue(): bigint {
return 0n;
}
/**
* @inheritdoc
*/
hasFlag(value: bigint, flag: bigint): boolean {
return (value & flag) === flag;
}
/**
* @inheritdoc
*/
addFlag(value: bigint, flag: bigint): bigint {
return value | flag;
}
/**
* @inheritdoc
*/
removeFlag(value: bigint, flag: bigint): bigint {
return value & ~flag;
}
}

View file

@ -0,0 +1,47 @@
import { BigIntDescriptor } from "@/utils/enum/descriptors/bigint-descriptor";
describe("BigIntDescriptor", () => {
const descriptor = new BigIntDescriptor();
describe("name", () => {
test("returns 'bigint' as name", () => {
expect(descriptor.name).toBe("bigint");
});
});
describe("defaultValue", () => {
test("returns 0n as default value", () => {
expect(descriptor.defaultValue).toBe(0n);
});
});
describe("hasFlag", () => {
test("returns true if flag is set", () => {
expect(descriptor.hasFlag(3n, 2n)).toBe(true);
});
test("returns false if flag is not set", () => {
expect(descriptor.hasFlag(3n, 4n)).toBe(false);
});
});
describe("addFlag", () => {
test("adds flag to value", () => {
expect(descriptor.addFlag(1n, 2n)).toBe(3n);
});
test("does not add flag if it is already set", () => {
expect(descriptor.addFlag(3n, 2n)).toBe(3n);
});
});
describe("removeFlag", () => {
test("removes flag from value", () => {
expect(descriptor.removeFlag(3n, 2n)).toBe(1n);
});
test("does not remove flag if it does not exist", () => {
expect(descriptor.removeFlag(1n, 2n)).toBe(1n);
});
});
});