mush

git clone git://git.lin.moe/mush.git

 1#include <array>
 2#include <catch2/catch_all.hpp>
 3
 4#include <cassert>
 5#include <catch2/catch_test_macros.hpp>
 6
 7#include "Ast.hpp"
 8#include "Parser.hpp"
 9#include "macros.hpp"
10
11TEST_CASE("Parse dollar parameter", "[parser]")
12{
13	std::map<std::string, ast::Word::ParameterOp> expressions = {
14	    {"$!", ast::Word::ParameterOp::none},
15	    {"$1", ast::Word::ParameterOp::none},
16	    {"$parameter", ast::Word::ParameterOp::none},
17	    {"${parameter}", ast::Word::ParameterOp::none},
18	    {"${parameter-[word]}", ast::Word::ParameterOp::minus},
19	    {"${parameter:-[word]}", ast::Word::ParameterOp::coloned_minus},
20	    {"${parameter=[word]}", ast::Word::ParameterOp::equal},
21	    {"${parameter:=[word]}", ast::Word::ParameterOp::coloned_equal},
22	    {"${parameter?[word]}", ast::Word::ParameterOp::qmark},
23	    {"${parameter:?[word]}", ast::Word::ParameterOp::coloned_qmark},
24	    {"${parameter+[word]}", ast::Word::ParameterOp::plus},
25	    {"${parameter:+[word]}", ast::Word::ParameterOp::coloned_plus},
26	    {"${#parameter}", ast::Word::ParameterOp::leading_hash},
27	    {"${parameter%[word]}", ast::Word::ParameterOp::percent},
28	    {"${parameter%%[word]}", ast::Word::ParameterOp::double_percent},
29	    {"${parameter#[word]}", ast::Word::ParameterOp::hash},
30	    {"${parameter##[word]}", ast::Word::ParameterOp::double_hash},
31	};
32
33	for (const auto &[str, optype] : expressions) {
34		auto in = std::istringstream{str};
35		auto p = parsing::Parser(in);
36		auto s = p.expectDollar();
37		CAPTURE_PARSER_ERROR(p);
38		REQUIRE(s != nullptr);
39		REQUIRE(s->type == ast::WordType::parameter);
40		REQUIRE(dynamic_cast<ast::Word::Parameter *>(s.get())->op ==
41			optype);
42	}
43}
44
45TEST_CASE("Parse dollar command", "[parser]")
46{
47	std::array expressions = {"$(ls)", "$(ls > /dev/null)",
48				  "$(ls 2> /dev/null)"
49				  "$(ls / || ls /tmp )"};
50	for (auto i = expressions.begin(); i != expressions.end(); i++) {
51		INFO("parsing command expression " << *i);
52		// DYNAMIC_SECTION("parsing command expression " << *i)
53		// {
54		// TODO: while dynamic_section case parse failed? data race ?
55		auto in = std::istringstream{*i};
56		auto p = parsing::Parser(in);
57		auto s = p.expectDollar();
58		CAPTURE_PARSER_ERROR(p);
59		REQUIRE(s != nullptr);
60		REQUIRE(s->type == ast::WordType::command);
61		// }
62	}
63}
64
65TEST_CASE("Parse dolloar arithmetic", "[parser]")
66{
67	std::array expressions = {
68	    "$((1 + 1))",
69	    "$((2*(1+1)))",
70	};
71
72	for (auto i = expressions.begin(); i != expressions.end(); i++) {
73		INFO("parsing arithmetic expression " << *i);
74		// DYNAMIC_SECTION("parsing arithmetic expression " << *i)
75		// {
76		auto in = std::istringstream{*i};
77		auto p = parsing::Parser(in);
78		auto s = p.expectDollar();
79		CHECKED_IF(p.error != nullptr)
80		{
81			CAPTURE(p.error->position.line);
82			CAPTURE(p.error->position.column);
83			CAPTURE(p.error->what());
84			REQUIRE(false);
85		}
86
87		REQUIRE(s != nullptr);
88		REQUIRE(s->type == ast::WordType::arithmetic);
89		// }
90	}
91}