|
1 | 1 | import { parseBoolean } from "./parse-utils";
|
| 2 | +import { expectBoolean, expectNumber, expectString } from "./parse-utils"; |
2 | 3 |
|
3 | 4 | describe("parseBoolean", () => {
|
4 | 5 | it('Returns true for "true"', () => {
|
@@ -45,3 +46,68 @@ describe("parseBoolean", () => {
|
45 | 46 | expect(() => parseBoolean({} as any)).toThrowError();
|
46 | 47 | });
|
47 | 48 | });
|
| 49 | + |
| 50 | +describe("expectBoolean", () => { |
| 51 | + it("accepts booleans", () => { |
| 52 | + expect(expectBoolean(true)).toEqual(true); |
| 53 | + expect(expectBoolean(false)).toEqual(false); |
| 54 | + }); |
| 55 | + |
| 56 | + it("rejects non-booleans", () => { |
| 57 | + expect(() => expectBoolean("true")).toThrowError(); |
| 58 | + expect(() => expectBoolean("false")).toThrowError(); |
| 59 | + expect(() => expectBoolean(0)).toThrowError(); |
| 60 | + expect(() => expectBoolean(1)).toThrowError(); |
| 61 | + expect(() => expectBoolean(1.1)).toThrowError(); |
| 62 | + expect(() => expectBoolean(Infinity)).toThrowError(); |
| 63 | + expect(() => expectBoolean(-Infinity)).toThrowError(); |
| 64 | + expect(() => expectBoolean(NaN)).toThrowError(); |
| 65 | + expect(() => expectBoolean({})).toThrowError(); |
| 66 | + expect(() => expectBoolean([])).toThrowError(); |
| 67 | + expect(() => expectBoolean(null)).toThrowError(); |
| 68 | + expect(() => expectBoolean(undefined)).toThrowError(); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe("expectNumber", () => { |
| 73 | + it("accepts numbers", () => { |
| 74 | + expect(expectNumber(1)).toEqual(1); |
| 75 | + expect(expectNumber(1.1)).toEqual(1.1); |
| 76 | + expect(expectNumber(Infinity)).toEqual(Infinity); |
| 77 | + expect(expectNumber(-Infinity)).toEqual(-Infinity); |
| 78 | + expect(expectNumber(NaN)).toEqual(NaN); |
| 79 | + }); |
| 80 | + |
| 81 | + it("rejects non-numbers", () => { |
| 82 | + expect(() => expectNumber("1")).toThrowError(); |
| 83 | + expect(() => expectNumber("1.1")).toThrowError(); |
| 84 | + expect(() => expectNumber("Infinity")).toThrowError(); |
| 85 | + expect(() => expectNumber("-Infinity")).toThrowError(); |
| 86 | + expect(() => expectNumber("NaN")).toThrowError(); |
| 87 | + expect(() => expectNumber(true)).toThrowError(); |
| 88 | + expect(() => expectNumber(false)).toThrowError(); |
| 89 | + expect(() => expectNumber([])).toThrowError(); |
| 90 | + expect(() => expectNumber({})).toThrowError(); |
| 91 | + expect(() => expectNumber(null)).toThrowError(); |
| 92 | + expect(() => expectNumber(undefined)).toThrowError(); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +describe("expectString", () => { |
| 97 | + it("accepts strings", () => { |
| 98 | + expect(expectString("foo")).toEqual("foo"); |
| 99 | + }); |
| 100 | + |
| 101 | + it("rejects non-strings", () => { |
| 102 | + expect(() => expectString(1)).toThrowError(); |
| 103 | + expect(() => expectString(NaN)).toThrowError(); |
| 104 | + expect(() => expectString(Infinity)).toThrowError(); |
| 105 | + expect(() => expectString(-Infinity)).toThrowError(); |
| 106 | + expect(() => expectString(true)).toThrowError(); |
| 107 | + expect(() => expectString(false)).toThrowError(); |
| 108 | + expect(() => expectString([])).toThrowError(); |
| 109 | + expect(() => expectString({})).toThrowError(); |
| 110 | + expect(() => expectString(null)).toThrowError(); |
| 111 | + expect(() => expectString(undefined)).toThrowError(); |
| 112 | + }); |
| 113 | +}); |
0 commit comments