| 1 |
import * as t from "../lib/index.js"; |
| 2 |
import { parse } from "@babel/parser"; |
| 3 |
|
| 4 |
function parseCode(string) { |
| 5 |
return parse(string, { |
| 6 |
allowReturnOutsideFunction: true, |
| 7 |
}).program.body[0]; |
| 8 |
} |
| 9 |
|
| 10 |
describe("misc helpers", function () { |
| 11 |
describe("matchesPattern", function () { |
| 12 |
it("matches explicitly", function () { |
| 13 |
const ast = parseCode("a.b.c.d").expression; |
| 14 |
expect(t.matchesPattern(ast, "a.b.c.d")).toBeTruthy(); |
| 15 |
expect(t.matchesPattern(ast, "a.b.c")).toBe(false); |
| 16 |
expect(t.matchesPattern(ast, "b.c.d")).toBe(false); |
| 17 |
expect(t.matchesPattern(ast, "a.b.c.d.e")).toBe(false); |
| 18 |
}); |
| 19 |
|
| 20 |
it("matches partially", function () { |
| 21 |
const ast = parseCode("a.b.c.d").expression; |
| 22 |
expect(t.matchesPattern(ast, "a.b.c.d", true)).toBeTruthy(); |
| 23 |
expect(t.matchesPattern(ast, "a.b.c", true)).toBeTruthy(); |
| 24 |
expect(t.matchesPattern(ast, "b.c.d", true)).toBe(false); |
| 25 |
expect(t.matchesPattern(ast, "a.b.c.d.e", true)).toBe(false); |
| 26 |
}); |
| 27 |
|
| 28 |
it("matches string literal expressions", function () { |
| 29 |
const ast = parseCode("a['b'].c.d").expression; |
| 30 |
expect(t.matchesPattern(ast, "a.b.c.d")).toBeTruthy(); |
| 31 |
expect(t.matchesPattern(ast, "a.b.c")).toBe(false); |
| 32 |
expect(t.matchesPattern(ast, "b.c.d")).toBe(false); |
| 33 |
expect(t.matchesPattern(ast, "a.b.c.d.e")).toBe(false); |
| 34 |
}); |
| 35 |
|
| 36 |
it("matches string literal expressions partially", function () { |
| 37 |
const ast = parseCode("a['b'].c.d").expression; |
| 38 |
expect(t.matchesPattern(ast, "a.b.c.d", true)).toBeTruthy(); |
| 39 |
expect(t.matchesPattern(ast, "a.b.c", true)).toBeTruthy(); |
| 40 |
expect(t.matchesPattern(ast, "b.c.d", true)).toBe(false); |
| 41 |
expect(t.matchesPattern(ast, "a.b.c.d.e", true)).toBe(false); |
| 42 |
}); |
| 43 |
|
| 44 |
it("matches this expressions", function () { |
| 45 |
const ast = parseCode("this.a.b.c.d").expression; |
| 46 |
expect(t.matchesPattern(ast, "this.a.b.c.d")).toBeTruthy(); |
| 47 |
expect(t.matchesPattern(ast, "this.a.b.c")).toBe(false); |
| 48 |
expect(t.matchesPattern(ast, "this.b.c.d")).toBe(false); |
| 49 |
expect(t.matchesPattern(ast, "this.a.b.c.d.e")).toBe(false); |
| 50 |
}); |
| 51 |
}); |
| 52 |
}); |