summaryrefslogtreecommitdiff
path: root/tst/optional.test.ts
diff options
context:
space:
mode:
authorElizabeth Hunt <me@liz.coffee>2025-12-14 22:39:18 -0800
committerElizabeth Hunt <me@liz.coffee>2025-12-14 22:39:18 -0800
commit666674327f009e9b1013218fc384f193b64c6997 (patch)
treeacebae7b425b469584eb0a5bec396899c2739501 /tst/optional.test.ts
parent594ce452693a71b501d3aff3f35ef3732c06c341 (diff)
downloadpengueno-666674327f009e9b1013218fc384f193b64c6997.tar.gz
pengueno-666674327f009e9b1013218fc384f193b64c6997.zip
Adds unit tests
Diffstat (limited to 'tst/optional.test.ts')
-rw-r--r--tst/optional.test.ts37
1 files changed, 37 insertions, 0 deletions
diff --git a/tst/optional.test.ts b/tst/optional.test.ts
new file mode 100644
index 0000000..ece6b31
--- /dev/null
+++ b/tst/optional.test.ts
@@ -0,0 +1,37 @@
+import { IOptionalEmptyError, Optional, isOptional } from '../lib/index';
+
+describe('types/fn/optional', () => {
+ test('from creates some/none', () => {
+ expect(Optional.from('x').get()).toBe('x');
+ expect(Optional.from(null).present()).toBe(false);
+ expect(Optional.from(undefined).present()).toBe(false);
+ });
+
+ test('get throws on none', () => {
+ expect(() => Optional.none().get()).toThrow(IOptionalEmptyError);
+ });
+
+ test('map/filter/flatMap work', () => {
+ const res = Optional.from(2)
+ .map((n: number) => n * 2)
+ .filter((n: number) => n > 3)
+ .flatMap((n: number) => Optional.from(n.toString()));
+
+ expect(res.get()).toBe('4');
+ });
+
+ test('orSome supplies fallback', () => {
+ const res = Optional.none<number>().orSome(() => 5);
+ expect(res.get()).toBe(5);
+ });
+
+ test('iterator yields only when present', () => {
+ expect(Array.from(Optional.some(1))).toEqual([1]);
+ expect(Array.from(Optional.none<number>())).toEqual([]);
+ });
+
+ test('isOptional detects tagged values', () => {
+ expect(isOptional(Optional.some('x'))).toBe(true);
+ expect(isOptional({})).toBe(false);
+ });
+});