-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
91 lines (77 loc) · 2.65 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* eslint-disable no-console */
"use strict";
const { test } = require("uvu");
const assert = require("assert");
const parser = require("@babel/parser");
const mast = require(".");
const ast = parser.parse("this.foo(bar, eggs, spam)").program.body[0]
.expression;
test("only checks type with no arguments", () => {
const check = mast.isCallExpression();
assert(check(ast) === true);
assert(check(ast.callee) === false);
});
test("should only check type with an empty object", () => {
const checkCall = mast.isCallExpression({});
const checkIdentifier = mast.isIdentifier({});
assert(checkCall(ast) === true);
assert(checkIdentifier(ast.arguments[0]) === true);
});
test("checks properties if argument is an object", () => {
const check = mast.isCallExpression({
callee: mast.isMemberExpression(),
});
const checkFail = mast.isCallExpression({
callee: mast.isIdentifier(),
});
assert(check(ast) === true);
assert(checkFail(ast) === false);
});
test("fails with foreign properties", () => {
const checkTypeWithOneProp = mast.isIdentifier({
name: "bar",
notName: "bar",
});
assert(checkTypeWithOneProp(ast.arguments[0]) === false);
const checkTypeWithMoreProps = mast.isMemberExpression({
object: mast.isThisExpression(),
arguments: [mast.isIdentifier()],
});
assert(checkTypeWithMoreProps(ast.callee) === false);
});
test("checks array if argument is an array", () => {
const checkFail = mast.isCallExpression({
arguments: [mast.isIdentifier()],
});
const check = mast.isCallExpression({
arguments: [mast.isIdentifier(), mast.isIdentifier(), mast.isIdentifier()],
});
assert(checkFail(ast) === false);
assert(check(ast) === true);
});
test("accepts simple arguments if type has only one property", () => {
const checkIdentifier = mast.isIdentifier("foo");
const checkNumeric = mast.isNumericLiteral(1);
const checkArray = mast.isArrayExpression([
mast.isIdentifier("foo"),
mast.isIdentifier(),
]);
assert(checkIdentifier(parser.parseExpression("foo")) === true);
assert(checkNumeric(parser.parseExpression("1")) === true);
assert(checkArray(parser.parseExpression("[foo, bar]")) === true);
});
test("accepts functions as matcher", () => {
const check = mast.isCallExpression({
arguments: (args) => args.every(mast.isIdentifier()),
});
assert(check(ast) === true);
});
test("either works", () => {
const check = mast.either(mast.isIdentifier(), mast.isNumericLiteral());
const identifier = parser.parseExpression("foo");
const number = parser.parseExpression("1");
assert(check(identifier) === true);
assert(check(number) === true);
assert(check(ast) === false);
});
test.run();