mirror of
https://github.com/Kir-Antipov/mc-publish.git
synced 2024-11-22 00:11:02 -05:00
Implemented enum descriptor for boolean enums
This commit is contained in:
parent
874b950fdf
commit
e16de98226
2 changed files with 88 additions and 0 deletions
41
src/utils/enum/descriptors/boolean-descriptor.ts
Normal file
41
src/utils/enum/descriptors/boolean-descriptor.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
import { EnumDescriptor } from "./enum-descriptor";
|
||||
|
||||
/**
|
||||
* This descriptor is used to describe a set of flags stored as a `boolean` value.
|
||||
*/
|
||||
export class BooleanDescriptor implements EnumDescriptor<boolean> {
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
get name(): "boolean" {
|
||||
return "boolean";
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
get defaultValue(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
hasFlag(value: boolean, flag: boolean): boolean {
|
||||
return !flag || value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
addFlag(value: boolean, flag: boolean): boolean {
|
||||
return value || flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
removeFlag(value: boolean, flag: boolean): boolean {
|
||||
return value && !flag;
|
||||
}
|
||||
}
|
47
tests/unit/utils/enum/descriptors/boolean-descriptor.spec.ts
Normal file
47
tests/unit/utils/enum/descriptors/boolean-descriptor.spec.ts
Normal file
|
@ -0,0 +1,47 @@
|
|||
import { BooleanDescriptor } from "@/utils/enum/descriptors/boolean-descriptor";
|
||||
|
||||
describe("BooleanDescriptor", () => {
|
||||
const descriptor = new BooleanDescriptor();
|
||||
|
||||
describe("name", () => {
|
||||
test("returns 'boolean' as name", () => {
|
||||
expect(descriptor.name).toBe("boolean");
|
||||
});
|
||||
});
|
||||
|
||||
describe("defaultValue", () => {
|
||||
test("returns false as default value", () => {
|
||||
expect(descriptor.defaultValue).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("hasFlag", () => {
|
||||
test("returns true if flag is set", () => {
|
||||
expect(descriptor.hasFlag(true, true)).toBe(true);
|
||||
});
|
||||
|
||||
test("returns false if flag is not set", () => {
|
||||
expect(descriptor.hasFlag(false, true)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("addFlag", () => {
|
||||
test("adds flag to value", () => {
|
||||
expect(descriptor.addFlag(false, true)).toBe(true);
|
||||
});
|
||||
|
||||
test("does not add flag if it is already set", () => {
|
||||
expect(descriptor.addFlag(true, true)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("removeFlag", () => {
|
||||
test("removes flag from value", () => {
|
||||
expect(descriptor.removeFlag(true, true)).toBe(false);
|
||||
});
|
||||
|
||||
test("does not remove flag if it does not exist", () => {
|
||||
expect(descriptor.removeFlag(false, true)).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue