Line data Source code
1 : #include "../../inc/parsingAnalysis/builder.h"
2 : #include <catch2/catch_test_macros.hpp>
3 : #include <memory>
4 :
5 : using namespace nicole;
6 :
7 9 : TEST_CASE("AST_DO_WHILE class methods", "[AST_DO_WHILE]") {
8 9 : Location loc{"file", 0, 0};
9 9 : Token token{TokenType::OPERATOR_ADD, "+", loc};
10 9 : auto condition = *Builder::createCondition(
11 9 : SourceLocation{token, token},
12 9 : *Builder::createBool(SourceLocation{token, token}, true));
13 9 : auto statement =
14 9 : *Builder::createStatement(SourceLocation{token, token}, condition);
15 9 : auto body = *Builder::createBody(
16 9 : SourceLocation{token, token},
17 9 : std::vector<std::shared_ptr<AST_STATEMENT>>{statement});
18 9 : AST_DO_WHILE astDoWhile{0, SourceLocation{token, token}, body, condition};
19 :
20 9 : REQUIRE(astDoWhile.condition() == condition);
21 9 : REQUIRE(astDoWhile.body() == body);
22 9 : }
23 :
24 9 : TEST_CASE("AST_FOR class methods", "[AST_FOR]") {
25 9 : Location loc{"file", 0, 0};
26 9 : Token token{TokenType::OPERATOR_ADD, "+", loc};
27 9 : auto init = std::vector<std::shared_ptr<AST>>{*Builder::createStatement(
28 9 : SourceLocation{token, token},
29 9 : *Builder::createBool(SourceLocation{token, token}, true))};
30 9 : auto condition = *Builder::createCondition(
31 9 : SourceLocation{token, token},
32 9 : *Builder::createBool(SourceLocation{token, token}, true));
33 9 : auto update = std::vector<std::shared_ptr<AST>>{*Builder::createStatement(
34 9 : SourceLocation{token, token},
35 9 : *Builder::createBool(SourceLocation{token, token}, false))};
36 9 : auto statement =
37 9 : *Builder::createStatement(SourceLocation{token, token}, condition);
38 9 : auto body = *Builder::createBody(
39 9 : SourceLocation{token, token},
40 9 : std::vector<std::shared_ptr<AST_STATEMENT>>{statement});
41 :
42 9 : AST_FOR astFor{0, SourceLocation{token, token}, init, condition, update,
43 9 : body};
44 :
45 9 : REQUIRE(astFor.init()[0] == init[0]);
46 9 : REQUIRE(astFor.condition() == condition);
47 9 : REQUIRE(astFor.update()[0] == update[0]);
48 9 : REQUIRE(astFor.body() == body);
49 9 : }
50 :
51 9 : TEST_CASE("AST_WHILE class methods", "[AST_WHILE]") {
52 9 : Location loc{"file", 0, 0};
53 9 : Token token{TokenType::OPERATOR_ADD, "+", loc};
54 9 : auto condition = *Builder::createCondition(
55 9 : SourceLocation{token, token},
56 9 : *Builder::createBool(SourceLocation{token, token}, true));
57 9 : auto statement =
58 9 : *Builder::createStatement(SourceLocation{token, token}, condition);
59 9 : auto body = *Builder::createBody(
60 9 : SourceLocation{token, token},
61 9 : std::vector<std::shared_ptr<AST_STATEMENT>>{statement});
62 9 : AST_WHILE astWhile{0, SourceLocation{token, token}, condition, body};
63 :
64 9 : REQUIRE(astWhile.condition() == condition);
65 9 : REQUIRE(astWhile.body() == body);
66 9 : }
67 :
68 27 : TEST_CASE("AST_STOP within loops", "[AST_STOP]") {
69 27 : Location loc{"file", 0, 0};
70 27 : Token token{TokenType::OPERATOR_ADD, "+", loc};
71 27 : auto condition = *Builder::createCondition(
72 27 : SourceLocation{token, token},
73 27 : *Builder::createBool(SourceLocation{token, token}, true));
74 27 : auto body =
75 27 : *Builder::createBody(SourceLocation{token, token},
76 27 : std::vector<std::shared_ptr<AST_STATEMENT>>{});
77 :
78 27 : SECTION("AST_STOP in AST_FOR") {
79 9 : auto loop = *Builder::createFor(
80 9 : SourceLocation{token, token}, std::vector<std::shared_ptr<AST>>{},
81 9 : condition, std::vector<std::shared_ptr<AST>>{}, body);
82 9 : AST_STOP astStop{0, SourceLocation{token, token}, loop};
83 9 : REQUIRE(astStop.fatherLoop() == loop);
84 9 : }
85 :
86 27 : SECTION("AST_STOP in AST_WHILE") {
87 9 : auto loop =
88 9 : *Builder::createWhile(SourceLocation{token, token}, condition, body);
89 9 : AST_STOP astStop{0, SourceLocation{token, token}, loop};
90 9 : REQUIRE(astStop.fatherLoop() == loop);
91 9 : }
92 :
93 27 : SECTION("AST_STOP in AST_DO_WHILE") {
94 9 : auto loop =
95 9 : *Builder::createDoWhile(SourceLocation{token, token}, body, condition);
96 9 : AST_STOP astStop{0, SourceLocation{token, token}, loop};
97 9 : REQUIRE(astStop.fatherLoop() == loop);
98 9 : }
99 27 : }
100 :
101 27 : TEST_CASE("AST_PASS within loops", "[AST_PASS]") {
102 27 : Location loc{"file", 0, 0};
103 27 : Token token{TokenType::OPERATOR_ADD, "+", loc};
104 27 : auto condition = *Builder::createCondition(
105 27 : SourceLocation{token, token},
106 27 : *Builder::createBool(SourceLocation{token, token}, true));
107 27 : auto body =
108 27 : *Builder::createBody(SourceLocation{token, token},
109 27 : std::vector<std::shared_ptr<AST_STATEMENT>>{});
110 :
111 27 : SECTION("AST_PASS in AST_FOR") {
112 9 : auto loop = *Builder::createFor(
113 9 : SourceLocation{token, token}, std::vector<std::shared_ptr<AST>>{},
114 9 : condition, std::vector<std::shared_ptr<AST>>{}, body);
115 9 : AST_PASS astPass{0, SourceLocation{token, token}, loop};
116 9 : REQUIRE(astPass.fatherLoop() == loop);
117 9 : }
118 :
119 27 : SECTION("AST_PASS in AST_WHILE") {
120 9 : auto loop =
121 9 : *Builder::createWhile(SourceLocation{token, token}, condition, body);
122 9 : AST_PASS astPass{0, SourceLocation{token, token}, loop};
123 9 : REQUIRE(astPass.fatherLoop() == loop);
124 9 : }
125 :
126 27 : SECTION("AST_PASS in AST_DO_WHILE") {
127 9 : auto loop =
128 9 : *Builder::createDoWhile(SourceLocation{token, token}, body, condition);
129 9 : AST_PASS astPass{0, SourceLocation{token, token}, loop};
130 9 : REQUIRE(astPass.fatherLoop() == loop);
131 9 : }
132 27 : }
|