@airtasker/form-schema-compiler
Version:
a form schema compiler
319 lines (310 loc) • 10.1 kB
JavaScript
"use strict";
var _createExpressionTokenStream = require("./createExpressionTokenStream");
var _createExpressionTokenStream2 = _interopRequireDefault(_createExpressionTokenStream);
var _createInputStream = require("./createInputStream");
var _createInputStream2 = _interopRequireDefault(_createInputStream);
var _const = require("../const");
var _utils = require("../utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var create = function create(str) {
return (0, _createExpressionTokenStream2["default"])((0, _createInputStream2["default"])(str));
};
describe("createExpressionTokenStream", function () {
it("should peek without move index", function () {
var stream = create("hello");
var peeked = stream.peek();
expect(stream.eof()).toBeFalsy();
expect(stream.next()).toEqual(peeked);
expect(stream.eof()).toBeTruthy();
});
it("should recognize identifier", function () {
var stream = create("hello");
expect(stream.next()).toEqual((0, _utils.createIdentifier)("hello"));
});
it("should recognize null", function () {
var stream = create("null");
expect(stream.next()).toEqual((0, _utils.createValue)(null));
});
it("should recognize boolean", function () {
var stream = create("false true");
expect(stream.next()).toEqual((0, _utils.createValue)(false));
expect(stream.next()).toEqual((0, _utils.createValue)(true));
});
it("should recognize keyword", function () {
var stream = create("if then else");
expect(stream.next()).toEqual({
type: _const.TYPES.Keyword,
value: "if"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Keyword,
value: "then"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Keyword,
value: "else"
});
});
describe("should recognize number", function () {
it("should recognize integer", function () {
var stream = create("0 1 23456 7890");
expect(stream.next()).toEqual((0, _utils.createValue)(0));
expect(stream.next()).toEqual((0, _utils.createValue)(1));
expect(stream.next()).toEqual((0, _utils.createValue)(23456));
expect(stream.next()).toEqual((0, _utils.createValue)(7890));
});
it("should recognize float", function () {
var stream = create("3.14 5.2678");
expect(stream.next()).toEqual((0, _utils.createValue)(3.14));
expect(stream.next()).toEqual((0, _utils.createValue)(5.2678));
});
});
describe("should recognize string", function () {
it("should recognize double quote string", function () {
var stream = create('"asdf"');
expect(stream.next()).toEqual((0, _utils.createValue)("asdf"));
});
it("should recognize single quote string", function () {
var stream = create("'asdf'");
expect(stream.next()).toEqual((0, _utils.createValue)("asdf"));
});
it("should recognize mixed quote string", function () {
var stream = create("'asdf\"'");
expect(stream.next()).toEqual((0, _utils.createValue)('asdf"'));
});
it("should recognize escape", function () {
var stream = create("'\\a\\s\\d\\f\\\\'");
expect(stream.next()).toEqual((0, _utils.createValue)("asdf\\"));
});
it("should throw error when it's not valid", function () {
var stream = create("'asdfasdf");
expect(function () {
return stream.next();
}).toThrow();
stream = create('"asdfasdf');
expect(function () {
return stream.next();
}).toThrow();
});
});
describe("should recognize regexp", function () {
it("should recognize regexp in binary expression", function () {
var stream = create("budget match /\\s\\//gimuy");
expect(stream.next()).toEqual((0, _utils.createIdentifier)("budget"));
expect(stream.next()).toEqual({
type: _const.TYPES.Operator,
value: "match"
});
expect(stream.next()).toEqual((0, _utils.createValue)(new RegExp("\\s\\/", "gimuy")));
});
it("should recognize regexp at start", function () {
var stream = create("/[abc]/iy ");
expect(stream.next()).toEqual((0, _utils.createValue)(new RegExp("[abc]", "iy")));
});
it("should recognize regexp in call expression", function () {
var stream = create("call(/\\s\\//g)");
expect(stream.next()).toEqual((0, _utils.createIdentifier)("call"));
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: "("
});
expect(stream.next()).toEqual((0, _utils.createValue)(/\s\//g));
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: ")"
});
});
});
it("should recognize punctuation", function () {
var stream = create("(){}[],:;");
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: "("
});
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: ")"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: "{"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: "}"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: "["
});
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: "]"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: ","
});
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: ":"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: ";"
});
});
it("should recognize operators", function () {
var stream = create("+-*>=<=match<and>or not is isnt 1 /");
expect(stream.next()).toEqual({
type: _const.TYPES.Operator,
value: "+"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Operator,
value: "-"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Operator,
value: "*"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Operator,
value: ">="
});
expect(stream.next()).toEqual({
type: _const.TYPES.Operator,
value: "<="
});
expect(stream.next()).toEqual({
type: _const.TYPES.Operator,
value: "match"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Operator,
value: "<"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Operator,
value: "and"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Operator,
value: ">"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Operator,
value: "or"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Operator,
value: "not"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Operator,
value: "is"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Operator,
value: "isnt"
});
expect(stream.next()).toEqual((0, _utils.createValue)(1));
expect(stream.next()).toEqual({
type: _const.TYPES.Operator,
value: "/"
});
});
it("should recognize '/' operator after () [FE-1398]", function () {
var stream = create(") /");
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: ")"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Operator,
value: "/"
});
});
describe("string template literal", function () {
it("should support empty template literal", function () {
var stream = create("``");
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: "`"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: "`"
});
expect(stream.eof()).toBeTruthy();
});
it("should support spacing line-break in template literal", function () {
var stream = create("`\n \r \t`");
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: "`"
});
expect(stream.next()).toEqual({
type: _const.TYPES.String,
value: "\n \r \t"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: "`"
});
expect(stream.eof()).toBeTruthy();
});
it("should support complex template literal", function () {
var stream = create("`'\"foo\"'\\{\\`{`1{1}`}bar`");
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: "`"
});
expect(stream.next()).toEqual((0, _utils.createValue)("'\"foo\"'{`"));
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: "{"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: "`"
});
expect(stream.next()).toEqual((0, _utils.createValue)("1"));
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: "{"
});
expect(stream.next()).toEqual((0, _utils.createValue)(1));
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: "}"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: "`"
});
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: "}"
});
expect(stream.next()).toEqual((0, _utils.createValue)("bar"));
expect(stream.next()).toEqual({
type: _const.TYPES.Punctuation,
value: "`"
});
expect(stream.eof()).toBeTruthy();
});
it("should throw error when it's not valid", function () {
var stream = create("`{}`");
stream.next();
expect(function () {
return stream.next();
}).toThrow();
stream = create("`1");
stream.next();
expect(function () {
return stream.next();
}).toThrow();
});
});
});