import { getLog } from "console-testing-library" // https://www.npmjs.com/package/console-testing-library import { None, Option, Some } from "../index" import { debug, DebugStyle, _callIfNone, _callIfSome } from "../lang-utils" import { Flag } from "./test-helpers" test("How it is used by a consumer of the library, ergonomics check", () => { let optionalValue: Option optionalValue = Option.create("some_value") // same as Option.some("some_value"). if (optionalValue.isSome()) { expect(optionalValue).toBeInstanceOf(Some) expect(optionalValue.value).toBe("some_value") } optionalValue = Option.create(undefined) // Same as Option.none(). expect(optionalValue.isSome()).toBe(false) if (optionalValue.isNone()) { expect(optionalValue).toBeInstanceOf(None) } }) test("Option Some and None can be created directly", () => { const some: Some = Option.some("some_value") const none: None = Option.none() checkSome(some) checkNone(none) }) test("Option can be created", () => { const some: Option = Option.create("some_value") const none: Option = Option.create(undefined) checkOption(some) checkOption(none) }) function checkOption(option: Option) { if (option.isSome()) return checkSome(option) if (option.isNone()) return checkNone(option) } function checkSome(some: Some) { expect(some).toBeInstanceOf(Some) expect(some.value).toBe("some_value") expect(some.isSome()).toBe(true) expect(some.isNone()).toBe(false) expect(some instanceof Some).toBe(true) expect(some instanceof None).toBe(false) } function checkNone(none: None) { expect(none).toBeInstanceOf(None) } test("callIfSome works", () => { const flag = new Flag() _callIfSome(Option.some("some_value"), flag.set) expect(flag.isSet()).toBe(true) expect(flag.args).toEqual(["some_value"]) }) test("callIfNone works", () => { const flag = new Flag() _callIfNone(Option.none(), flag.set) expect(flag.isSet()).toBe(true) }) test("debug! macro inspired function works", () => { const value = "some_value" expect(debug("msg", value)).toBe("some_value") expect(getLog().log).toEqual( DebugStyle.msgStyle("msg") + " " + DebugStyle.argStyle('"some_value"') ) })