{"size": 5002, "ext": "js", "max_stars_repo_path": "projects/adbquery/test/parse_function_expression_addition.test.js", "max_stars_repo_name": "agnesoft/adb", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "projects/adbquery/test/parse_function_expression_addition.test.js", "max_issues_repo_name": "agnesoft/adb", "max_issues_repo_issues_event_min_datetime": "2020-11-03T17:47:17.000Z", "max_issues_repo_issues_event_max_datetime": "2021-03-12T19:12:17.000Z", "max_forks_repo_path": "projects/adbquery/test/parse_function_expression_addition.test.js", "max_forks_repo_name": "agnesoft/adb", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 13, "max_forks_count": null, "avg_line_length": 39.6984126984, "max_line_length": 164, "alphanum_fraction": 0.243902439}
// Copyright 2020 Agnesoft
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import * as parser from "../compiler/parser.js";

describe("parse", () => {
    describe("function", () => {
        describe("expression", () => {
            describe("addition", () => {
                describe("valid", () => {
                    test("type += number", () => {
                        const data = {
                            foo: { body: ["Id += 1"] },
                        };

                        const ast = {
                            foo: {
                                body: [
                                    {
                                        type: "+=",
                                        left: {
                                            type: "identifier",
                                            value: "Id",
                                        },
                                        right: {
                                            type: "number",
                                            value: 1,
                                        },
                                    },
                                ],
                            },
                        };

                        expect(parser.parse(data)).toMatchObject(ast);
                    });

                    test("type += type", () => {
                        const data = {
                            foo: { body: ["Id += From"] },
                        };

                        const ast = {
                            foo: {
                                body: [
                                    {
                                        type: "+=",
                                        left: {
                                            type: "identifier",
                                            value: "Id",
                                        },
                                        right: {
                                            type: "identifier",
                                            value: "From",
                                        },
                                    },
                                ],
                            },
                        };

                        expect(parser.parse(data)).toMatchObject(ast);
                    });

                    test("type.type += type.type", () => {
                        const data = {
                            foo: { body: ["Obj.Id += Arg1.From"] },
                        };

                        const ast = {
                            foo: {
                                body: [
                                    {
                                        type: "+=",
                                        left: {
                                            type: "identifier",
                                            value: "Id",
                                            parent: {
                                                type: "identifier",
                                                value: "Obj",
                                            },
                                        },
                                        right: {
                                            type: "identifier",
                                            value: "From",
                                            parent: {
                                                type: "identifier",
                                                value: "Arg1",
                                            },
                                        },
                                    },
                                ],
                            },
                        };

                        expect(parser.parse(data)).toMatchObject(ast);
                    });
                });

                describe("invalid", () => {
                    test("empty type", () => {
                        const data = {
                            foo: { body: ["Obj. += Arg1.From"] },
                        };

                        const parse = () => {
                            parser.parse(data);
                        };

                        expect(parse).toThrow("Parser: expected an identifier, got '+=' [operator] in function 'foo' when parsing expression 'Obj. += Arg1.From'.");
                    });
                });
            });
        });
    });
});
