r3bl-ts-utils
Version:
The `r3bl-ts-utils` package is a set of useful TypeScript functions and classes that can be used in Node.js and browser environments. They are inspired by Kotlin stdlib, and Rust to write code as expressions rather than statements, colorized text, powerfu
65 lines • 2.65 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const console_testing_library_1 = require("console-testing-library"); // https://www.npmjs.com/package/console-testing-library
const index_1 = require("../index");
const lang_utils_1 = require("../lang-utils");
const test_helpers_1 = require("./test-helpers");
test("How it is used by a consumer of the library, ergonomics check", () => {
let optionalValue;
optionalValue = index_1.Option.create("some_value"); // same as Option.some("some_value").
if (optionalValue.isSome()) {
expect(optionalValue).toBeInstanceOf(index_1.Some);
expect(optionalValue.value).toBe("some_value");
}
optionalValue = index_1.Option.create(undefined); // Same as Option.none().
expect(optionalValue.isSome()).toBe(false);
if (optionalValue.isNone()) {
expect(optionalValue).toBeInstanceOf(index_1.None);
}
});
test("Option Some and None can be created directly", () => {
const some = index_1.Option.some("some_value");
const none = index_1.Option.none();
checkSome(some);
checkNone(none);
});
test("Option can be created", () => {
const some = index_1.Option.create("some_value");
const none = index_1.Option.create(undefined);
checkOption(some);
checkOption(none);
});
function checkOption(option) {
if (option.isSome())
return checkSome(option);
if (option.isNone())
return checkNone(option);
}
function checkSome(some) {
expect(some).toBeInstanceOf(index_1.Some);
expect(some.value).toBe("some_value");
expect(some.isSome()).toBe(true);
expect(some.isNone()).toBe(false);
expect(some instanceof index_1.Some).toBe(true);
expect(some instanceof index_1.None).toBe(false);
}
function checkNone(none) {
expect(none).toBeInstanceOf(index_1.None);
}
test("callIfSome works", () => {
const flag = new test_helpers_1.Flag();
(0, lang_utils_1._callIfSome)(index_1.Option.some("some_value"), flag.set);
expect(flag.isSet()).toBe(true);
expect(flag.args).toEqual(["some_value"]);
});
test("callIfNone works", () => {
const flag = new test_helpers_1.Flag();
(0, lang_utils_1._callIfNone)(index_1.Option.none(), flag.set);
expect(flag.isSet()).toBe(true);
});
test("debug! macro inspired function works", () => {
const value = "some_value";
expect((0, lang_utils_1.debug)("msg", value)).toBe("some_value");
expect((0, console_testing_library_1.getLog)().log).toEqual(lang_utils_1.DebugStyle.msgStyle("msg") + " " + lang_utils_1.DebugStyle.argStyle('"some_value"'));
});
//# sourceMappingURL=rust-lang-utils.test.js.map
;