-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBinaryExpressionTreeTest.java
More file actions
101 lines (80 loc) · 4.04 KB
/
BinaryExpressionTreeTest.java
File metadata and controls
101 lines (80 loc) · 4.04 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
/*
* SonarSource Scala
* Copyright (C) 2018-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonarsource.scala.converter;
import org.junit.jupiter.api.Test;
import org.sonarsource.slang.api.BinaryExpressionTree;
import org.sonarsource.slang.api.NativeTree;
import org.sonarsource.slang.api.Tree;
import static org.sonarsource.slang.testing.TreeAssert.assertTree;
class BinaryExpressionTreeTest extends AbstractScalaConverterTest {
@Test
void comparison_operators() {
Tree binaryExpressionTree = scalaStatement("a == 2");
assertTree(binaryExpressionTree).isBinaryExpression(BinaryExpressionTree.Operator.EQUAL_TO);
binaryExpressionTree = scalaStatement("a != 2");
assertTree(binaryExpressionTree).isBinaryExpression(BinaryExpressionTree.Operator.NOT_EQUAL_TO);
binaryExpressionTree = scalaStatement("a > 2");
assertTree(binaryExpressionTree).isBinaryExpression(BinaryExpressionTree.Operator.GREATER_THAN);
binaryExpressionTree = scalaStatement("a >= 2");
assertTree(binaryExpressionTree).isBinaryExpression(BinaryExpressionTree.Operator.GREATER_THAN_OR_EQUAL_TO);
binaryExpressionTree = scalaStatement("a < 2");
assertTree(binaryExpressionTree).isBinaryExpression(BinaryExpressionTree.Operator.LESS_THAN);
binaryExpressionTree = scalaStatement("a <= 2");
assertTree(binaryExpressionTree).isBinaryExpression(BinaryExpressionTree.Operator.LESS_THAN_OR_EQUAL_TO);
}
@Test
void arithmetic_operators() {
Tree binaryExpressionTree = scalaStatement("a + 2");
assertTree(binaryExpressionTree).isBinaryExpression(BinaryExpressionTree.Operator.PLUS);
binaryExpressionTree = scalaStatement("a - 2");
assertTree(binaryExpressionTree).isBinaryExpression(BinaryExpressionTree.Operator.MINUS);
binaryExpressionTree = scalaStatement("a * 2");
assertTree(binaryExpressionTree).isBinaryExpression(BinaryExpressionTree.Operator.TIMES);
binaryExpressionTree = scalaStatement("a / 2");
assertTree(binaryExpressionTree).isBinaryExpression(BinaryExpressionTree.Operator.DIVIDED_BY);
}
@Test
void logical_operators() {
Tree binaryExpressionTree = scalaStatement("a && 2");
assertTree(binaryExpressionTree).isBinaryExpression(BinaryExpressionTree.Operator.CONDITIONAL_AND);
binaryExpressionTree = scalaStatement("a || 2");
assertTree(binaryExpressionTree).isBinaryExpression(BinaryExpressionTree.Operator.CONDITIONAL_OR);
}
@Test
void mapped_to_native() {
Tree tree = scalaStatement("foo * (bar, baz)");
assertTree(tree).isInstanceOf(NativeTree.class);
tree = scalaStatement("a foo 2");
assertTree(tree).isInstanceOf(NativeTree.class);
}
@Test
void placeholder() {
Tree tree = scalaStatement("_ * _");
// The binary expression is wrapped into an anonymous function declaration
assertTree(tree.children().get(0)).isInstanceOf(BinaryExpressionTree.class);
tree = scalaStatement("_ + 2");
assertTree(tree.children().get(0)).isInstanceOf(BinaryExpressionTree.class);
tree = scalaStatement("_._1 < _._1");
assertTree(tree.children().get(0)).isInstanceOf(BinaryExpressionTree.class);
tree = scalaStatement("_._1 < a._1");
assertTree(tree.children().get(0)).isInstanceOf(BinaryExpressionTree.class);
tree = scalaStatement("_._1._1 < _._1._1");
assertTree(tree.children().get(0)).isInstanceOf(BinaryExpressionTree.class);
tree = scalaStatement("a._1._1 < _._1._1");
assertTree(tree.children().get(0)).isInstanceOf(BinaryExpressionTree.class);
}
}