1/*
2 * Copyright 2016 Google Inc. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.turbine.parse;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import com.google.common.base.Joiner;
22import com.google.turbine.diag.SourceFile;
23import java.util.Arrays;
24import java.util.List;
25import org.junit.Test;
26import org.junit.runner.RunWith;
27import org.junit.runners.Parameterized;
28import org.junit.runners.Parameterized.Parameters;
29
30@RunWith(Parameterized.class)
31public class VariableInitializerTest {
32
33  @Parameters
34  public static Iterable<Object[]> parameters() {
35    return Arrays.asList(
36        new Object[][] {
37          {
38            "a = List<A, B<C, D>, G>::asd, b = 2;",
39            "[IDENT(a), ASSIGN, IDENT(List), COLONCOLON, IDENT(asd), EOF],"
40                + " [IDENT(b), ASSIGN, INT_LITERAL(2), EOF]",
41          },
42          {
43            "a = new List<A, B<C, D>, G>(), b = 2;",
44            "[IDENT(a), ASSIGN, NEW, IDENT(List), LPAREN, RPAREN, EOF],"
45                + " [IDENT(b), ASSIGN, INT_LITERAL(2), EOF]",
46          },
47          {
48            "a = x < y, b = y > x;",
49            "[IDENT(a), ASSIGN, IDENT(x), LT, IDENT(y), EOF],"
50                + " [IDENT(b), ASSIGN, IDENT(y), GT, IDENT(x), EOF]",
51          },
52          {
53            "a = List<A, Q<B<C, D<E, F>>>, G>::asd, b = 2;",
54            "[IDENT(a), ASSIGN, IDENT(List), COLONCOLON, IDENT(asd), EOF],"
55                + " [IDENT(b), ASSIGN, INT_LITERAL(2), EOF]"
56          },
57          {
58            "a = 1, b[], c = 2, d[] = 3, f",
59            "[IDENT(a), ASSIGN, INT_LITERAL(1), EOF],"
60                + " [IDENT(b), LBRACK, RBRACK, EOF],"
61                + " [IDENT(c), ASSIGN, INT_LITERAL(2), EOF],"
62                + " [IDENT(d), LBRACK, RBRACK, ASSIGN, INT_LITERAL(3), EOF],"
63                + " [IDENT(f), EOF]"
64          },
65          {
66            "a = (x) -> {{return asd<asd>::asd;}}, b = 2;",
67            "[IDENT(a), ASSIGN, LPAREN, IDENT(x), RPAREN, ARROW, LBRACE, LBRACE, RETURN,"
68                + " IDENT(asd), LT, IDENT(asd),"
69                + " GT, COLONCOLON, IDENT(asd), SEMI, RBRACE, RBRACE, EOF],"
70                + " [IDENT(b), ASSIGN, INT_LITERAL(2), EOF]",
71          },
72          {
73            "a = List<A, B<C, D>>::asd, b = 2;",
74            "[IDENT(a), ASSIGN, IDENT(List), COLONCOLON, IDENT(asd), EOF],"
75                + " [IDENT(b), ASSIGN, INT_LITERAL(2), EOF]",
76          },
77          {
78            "a = ((int) 1) + 2, b = 2;",
79            "[IDENT(a), ASSIGN, LPAREN, LPAREN, INT, RPAREN, INT_LITERAL(1), RPAREN, PLUS,"
80                + " INT_LITERAL(2), EOF],"
81                + " [IDENT(b), ASSIGN, INT_LITERAL(2), EOF]",
82          },
83          {
84            "a = ImmutableList.<List<String>>of(), b = 2;",
85            "[IDENT(a), ASSIGN, IDENT(ImmutableList), DOT, IDENT(of), LPAREN, RPAREN, EOF],"
86                + " [IDENT(b), ASSIGN, INT_LITERAL(2), EOF]",
87          },
88          {
89            "a = new <X,Y> F<U,V>.G<M,N<X<?>>>(), b = 2;",
90            "[IDENT(a), ASSIGN, NEW, IDENT(F), IDENT(G), LPAREN, RPAREN, EOF],"
91                + " [IDENT(b), ASSIGN, INT_LITERAL(2), EOF]",
92          },
93          {
94            "a = new <X,Y> F<U,V>.G<M,N<X<?>>>(), b = 2;",
95            "[IDENT(a), ASSIGN, NEW, IDENT(F), IDENT(G), LPAREN, RPAREN, EOF],"
96                + " [IDENT(b), ASSIGN, INT_LITERAL(2), EOF]",
97          },
98          {
99            "a = Foo::new<X>, b = 2;",
100            "[IDENT(a), ASSIGN, IDENT(Foo), COLONCOLON, LT, IDENT(X), GT, EOF],"
101                + " [IDENT(b), ASSIGN, INT_LITERAL(2), EOF]",
102          },
103          {
104            "a = (x) -> { return (int) null; }, b = 2;",
105            "[IDENT(a), ASSIGN, LPAREN, IDENT(x), RPAREN, ARROW, LBRACE, RETURN, LPAREN, INT,"
106                + " RPAREN, NULL, SEMI, RBRACE, EOF],"
107                + " [IDENT(b), ASSIGN, INT_LITERAL(2), EOF]",
108          },
109          {
110            "a = a<b, c = c<d, e, f>::new;",
111            "[IDENT(a), ASSIGN, IDENT(a), LT, IDENT(b), EOF],"
112                + " [IDENT(c), ASSIGN, IDENT(c), COLONCOLON, EOF]",
113          },
114          {
115            "a = a < b ? x -> g(ArrayList<String>::new) : null, c = c<d, e, f>::new;",
116            "[IDENT(a), ASSIGN, IDENT(a), LT, IDENT(b), COND, IDENT(x), ARROW, IDENT(g), LPAREN,"
117                + " IDENT(ArrayList), LT, IDENT(String), GT, COLONCOLON, NEW, RPAREN, COLON, NULL,"
118                + " EOF],"
119                + " [IDENT(c), ASSIGN, IDENT(c), COLONCOLON, EOF]",
120          },
121        });
122  }
123
124  final String input;
125  final String expected;
126
127  public VariableInitializerTest(String input, String expected) {
128    this.input = input;
129    this.expected = expected;
130  }
131
132  @Test
133  public void test() {
134    Lexer lexer = new StreamLexer(new UnicodeEscapePreprocessor(new SourceFile(null, input)));
135    List<List<SavedToken>> initializers =
136        new VariableInitializerParser(lexer.next(), lexer).parseInitializers();
137    assertThat(Joiner.on(", ").join(initializers)).isEqualTo(expected);
138  }
139}
140