From 9ca1e9f0d97d176638804c4aeeade95fbe7db0ca Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Sun, 22 Jan 2023 11:03:59 +0000 Subject: [PATCH] Moved `FailMode` to its own file --- src/utils/fail-mode.ts | 48 ++++++++++++++++++++++++++++++ tests/unit/utils/fail-mode.spec.ts | 29 ++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/utils/fail-mode.ts create mode 100644 tests/unit/utils/fail-mode.spec.ts diff --git a/src/utils/fail-mode.ts b/src/utils/fail-mode.ts new file mode 100644 index 0000000..831299d --- /dev/null +++ b/src/utils/fail-mode.ts @@ -0,0 +1,48 @@ +import { Enum, EnumOptions } from "@/utils/enum"; + +/** + * Represents different failure modes for handling errors. + * + * @partial + */ +enum FailModeValues { + /** + * Fail mode, halts the operation on encountering an error. + */ + FAIL, + + /** + * Warn mode, logs a warning and continues operation on encountering an error. + */ + WARN, + + /** + * Skip mode, skips the current operation and continues with the next one on encountering an error. + */ + SKIP, +} + +/** + * Options for configuring the behavior of the `FailMode` enum. + * + * @partial + */ +const FailModeOptions: EnumOptions = { + /** + * The case should be ignored while parsing the fail mode. + */ + ignoreCase: true, +}; + +/** + * Represents different failure modes for handling errors. + */ +export const FailMode = Enum.create( + FailModeValues, + FailModeOptions, +); + +/** + * Represents different failure modes for handling errors. + */ +export type FailMode = Enum; diff --git a/tests/unit/utils/fail-mode.spec.ts b/tests/unit/utils/fail-mode.spec.ts new file mode 100644 index 0000000..5a55e1c --- /dev/null +++ b/tests/unit/utils/fail-mode.spec.ts @@ -0,0 +1,29 @@ +import { FailMode } from "@/utils/fail-mode"; + +describe("FailMode", () => { + describe("parse", () => { + test("parses all its own formatted values", () => { + for (const value of FailMode.values()) { + expect(FailMode.parse(FailMode.format(value))).toBe(value); + } + }); + + test("parses all friendly names of its own values", () => { + for (const value of FailMode.values()) { + expect(FailMode.parse(FailMode.friendlyNameOf(value))).toBe(value); + } + }); + + test("parses all its own formatted values in lowercase", () => { + for (const value of FailMode.values()) { + expect(FailMode.parse(FailMode.format(value).toLowerCase())).toBe(value); + } + }); + + test("parses all its own formatted values in UPPERCASE", () => { + for (const value of FailMode.values()) { + expect(FailMode.parse(FailMode.format(value).toUpperCase())).toBe(value); + } + }); + }); +});