forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidatorTest.cpp
More file actions
150 lines (127 loc) · 4.06 KB
/
ValidatorTest.cpp
File metadata and controls
150 lines (127 loc) · 4.06 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "../Parser/DiagContext.h"
#include "hermes/AST/SemValidate.h"
#include "hermes/Parser/JSParser.h"
#include "gtest/gtest.h"
using namespace hermes;
using namespace hermes::parser;
namespace {
/// Left side of assignment must be an LValue.
TEST(ValidatorTest, TestBadAssignmentLValue) {
Context ctx;
sem::SemContext semCtx{};
DiagContext diag(ctx);
JSParser parser(ctx, "a + 1 = 10;");
auto parsed = parser.parse();
ASSERT_TRUE(parsed.hasValue());
ASSERT_FALSE(validateAST(ctx, semCtx, *parsed));
EXPECT_EQ(1, diag.getErrCount());
}
/// For-in control expression must be an LValue.
TEST(ValidatorTest, TestBadForLValue) {
Context ctx;
sem::SemContext semCtx{};
DiagContext diag(ctx);
JSParser parser(ctx, "for(a + 1 in x);");
auto parsed = parser.parse();
ASSERT_TRUE(parsed.hasValue());
ASSERT_FALSE(validateAST(ctx, semCtx, *parsed));
EXPECT_EQ(1, diag.getErrCount());
}
/// Test an anonymous break outside of a loop.
TEST(ValidatorTest, UnnamedBreakLabelTest) {
Context ctx;
sem::SemContext semCtx{};
DiagContext diag(ctx);
JSParser parser(ctx, "break; for(;;) break; break;");
auto parsed = parser.parse();
ASSERT_TRUE(parsed.hasValue());
ASSERT_FALSE(validateAST(ctx, semCtx, *parsed));
ASSERT_EQ(2, diag.getErrCountClear());
}
/// Test an anonymous continue outside of a loop.
TEST(ValidatorTest, UnnamedContinueLabelTest) {
Context ctx;
sem::SemContext semCtx{};
DiagContext diag(ctx);
JSParser parser(ctx, "continue;");
auto parsed = parser.parse();
ASSERT_TRUE(parsed.hasValue());
ASSERT_FALSE(validateAST(ctx, semCtx, *parsed));
ASSERT_EQ(1, diag.getErrCountClear());
}
/// Test a continue with a block label.
TEST(ValidatorTest, ContinueWithBlockLabelTest) {
Context ctx;
sem::SemContext semCtx{};
DiagContext diag(ctx);
JSParser parser(ctx, "label1: { continue label1; }");
auto parsed = parser.parse();
ASSERT_TRUE(parsed.hasValue());
ASSERT_FALSE(validateAST(ctx, semCtx, *parsed));
ASSERT_EQ(1, diag.getErrCountClear());
}
/// Test that multiple labels are correctly attached to the same statement.
TEST(ValidatorTest, ChainedNamedLabelsTest) {
Context ctx;
sem::SemContext semCtx{};
DiagContext diag(ctx);
JSParser parser(
ctx,
"label1: label2: label3: for(;;) { continue label1; continue "
"label2; continue label3; }");
auto parsed = parser.parse();
ASSERT_TRUE(parsed.hasValue());
ASSERT_TRUE(validateAST(ctx, semCtx, *parsed));
}
/// Duplicated label in the scope of the previous one.
TEST(ValidatorTest, DuplicateNamedLabelTest) {
Context ctx;
sem::SemContext semCtx{};
DiagContext diag(ctx);
JSParser parser(
ctx,
"label1: { label1: ; }\n"
"label2: label2: ;");
auto parsed = parser.parse();
ASSERT_TRUE(parsed.hasValue());
ASSERT_FALSE(validateAST(ctx, semCtx, *parsed));
ASSERT_EQ(2, diag.getErrCountClear());
}
TEST(ValidatorTest, CorrectDuplicateNamedLabelTest) {
Context ctx;
sem::SemContext semCtx{};
DiagContext diag(ctx);
JSParser parser(
ctx, "label1: { break label1; } label1: for(;;) break label1;");
auto parsed = parser.parse();
ASSERT_TRUE(parsed.hasValue());
ASSERT_TRUE(validateAST(ctx, semCtx, *parsed));
}
TEST(ValidatorTest, ScopeNamedLabelTest) {
Context ctx;
sem::SemContext semCtx{};
DiagContext diag(ctx);
JSParser parser(ctx, "label1: ; for(;;) break label1;");
auto parsed = parser.parse();
ASSERT_TRUE(parsed.hasValue());
ASSERT_FALSE(validateAST(ctx, semCtx, *parsed));
ASSERT_EQ(1, diag.getErrCountClear());
}
TEST(ValidatorTest, NamedBreakLabelTest) {
Context ctx;
sem::SemContext semCtx{};
DiagContext diag(ctx);
JSParser parser(
ctx, "break exitLoop; exitLoop: for(;;) break exitLoop; break exitLoop;");
auto parsed = parser.parse();
ASSERT_TRUE(parsed.hasValue());
ASSERT_FALSE(validateAST(ctx, semCtx, *parsed));
ASSERT_EQ(2, diag.getErrCountClear());
}
} // anonymous namespace