misc.js 1.7 KB

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