import "mocha"; import { expect } from "chai"; import { is, objects, funcs } from "./helpers"; describe('Thunk factory "is.string"', () => { const valid = [ "hello", "world", "", String(42), String(true) ]; const invalid: any[] = [ ...valid.map(Object), ...objects, ...funcs ]; it("recognizes primitive strings", () => { const thunk = is.string(); for (const value of valid) expect(thunk(value)).to.be.true; for (const value of invalid) expect(thunk(value)).to.be.false; }); it('recognizes strings with a given prefix', () => { const thunk = is.string({ startsWith: "hi" }); const _valid = [ "hi there", "hipster", "hilarious" ]; const _invalid = [ ..._valid.map((value) => value.slice(2)), ...invalid ]; for (const value of _valid) expect(thunk(value)).to.be.true; for (const value of _invalid) expect(thunk(value)).to.be.false; }); it('recognizes strings with a given ending', () => { const thunk = is.string({ endsWith: "ing" }); const _valid = [ "sitting", "doing", "nothing" ]; const _invalid = [ ..._valid.map((value) => value.slice(0, -3)), ...invalid ]; for (const value of _valid) expect(thunk(value)).to.be.true; for (const value of _invalid) expect(thunk(value)).to.be.false; }); it('recognizes strings, which include a given substring', () => { const thunk = is.string({ includes: "te" }); const _valid = [ "tear", "step", "byte" ]; const _invalid = [ "bear", "stop", "bit" ]; for (const value of _valid) expect(thunk(value)).to.be.true; for (const value of _invalid) expect(thunk(value)).to.be.false; }); it('recognizes strings, that match given regular expression', () => { const thunk = is.string({ matches: /^ab+c$/ }); const _valid = [ "abc", "abbc", "abbbc", `a${ "b".repeat(Math.round(Math.random() * 1e3)) }c` ]; const _invalid = [ "a", "b", "c", ..._valid.map((value) => value.toUpperCase()), ...invalid ]; for (const value of _valid) expect(thunk(value)).to.be.true; for (const value of _invalid) expect(thunk(value)).to.be.false; }); it('recognizes strings, that match given string exactly', () => { const sample = "hello"; const thunk = is.string({ matches: sample }); expect(thunk(sample)).to.be.true; expect(thunk(sample.toUpperCase())).to.be.false; }); });