|
| 1 | +import { expectBoolean, expectNumber, expectString } from "./parse-utils"; |
| 2 | + |
| 3 | +describe("expectBoolean", () => { |
| 4 | + it("accepts booleans", () => { |
| 5 | + expect(expectBoolean(true)).toEqual(true); |
| 6 | + expect(expectBoolean(false)).toEqual(false); |
| 7 | + }); |
| 8 | + |
| 9 | + it("rejects non-booleans", () => { |
| 10 | + expect(() => expectBoolean("true")).toThrowError(); |
| 11 | + expect(() => expectBoolean("false")).toThrowError(); |
| 12 | + expect(() => expectBoolean(0)).toThrowError(); |
| 13 | + expect(() => expectBoolean(1)).toThrowError(); |
| 14 | + expect(() => expectBoolean(1.1)).toThrowError(); |
| 15 | + expect(() => expectBoolean(Infinity)).toThrowError(); |
| 16 | + expect(() => expectBoolean(-Infinity)).toThrowError(); |
| 17 | + expect(() => expectBoolean(NaN)).toThrowError(); |
| 18 | + expect(() => expectBoolean({})).toThrowError(); |
| 19 | + expect(() => expectBoolean([])).toThrowError(); |
| 20 | + expect(() => expectBoolean(null)).toThrowError(); |
| 21 | + expect(() => expectBoolean(undefined)).toThrowError(); |
| 22 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +describe("expectNumber", () => { |
| 26 | + it("accepts numbers", () => { |
| 27 | + expect(expectNumber(1)).toEqual(1); |
| 28 | + expect(expectNumber(1.1)).toEqual(1.1); |
| 29 | + expect(expectNumber(Infinity)).toEqual(Infinity); |
| 30 | + expect(expectNumber(-Infinity)).toEqual(-Infinity); |
| 31 | + expect(expectNumber(NaN)).toEqual(NaN); |
| 32 | + }); |
| 33 | + |
| 34 | + it("rejects non-numbers", () => { |
| 35 | + expect(() => expectNumber("1")).toThrowError(); |
| 36 | + expect(() => expectNumber("1.1")).toThrowError(); |
| 37 | + expect(() => expectNumber("Infinity")).toThrowError(); |
| 38 | + expect(() => expectNumber("-Infinity")).toThrowError(); |
| 39 | + expect(() => expectNumber("NaN")).toThrowError(); |
| 40 | + expect(() => expectNumber(true)).toThrowError(); |
| 41 | + expect(() => expectNumber(false)).toThrowError(); |
| 42 | + expect(() => expectNumber([])).toThrowError(); |
| 43 | + expect(() => expectNumber({})).toThrowError(); |
| 44 | + expect(() => expectNumber(null)).toThrowError(); |
| 45 | + expect(() => expectNumber(undefined)).toThrowError(); |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +describe("expectString", () => { |
| 50 | + it("accepts strings", () => { |
| 51 | + expect(expectString("foo")).toEqual("foo"); |
| 52 | + }); |
| 53 | + |
| 54 | + it("rejects non-strings", () => { |
| 55 | + expect(() => expectString(1)).toThrowError(); |
| 56 | + expect(() => expectString(NaN)).toThrowError(); |
| 57 | + expect(() => expectString(Infinity)).toThrowError(); |
| 58 | + expect(() => expectString(-Infinity)).toThrowError(); |
| 59 | + expect(() => expectString(true)).toThrowError(); |
| 60 | + expect(() => expectString(false)).toThrowError(); |
| 61 | + expect(() => expectString([])).toThrowError(); |
| 62 | + expect(() => expectString({})).toThrowError(); |
| 63 | + expect(() => expectString(null)).toThrowError(); |
| 64 | + expect(() => expectString(undefined)).toThrowError(); |
| 65 | + }); |
| 66 | +}); |
0 commit comments