• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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 
17 package com.google.turbine.options;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static com.google.common.truth.Truth8.assertThat;
21 import static org.junit.Assert.assertThrows;
22 
23 import com.google.common.collect.ImmutableList;
24 import javax.lang.model.SourceVersion;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.JUnit4;
28 
29 @RunWith(JUnit4.class)
30 public class LanguageVersionTest {
31   @Test
parseSourceVersion()32   public void parseSourceVersion() {
33     assertThat(LanguageVersion.fromJavacopts(ImmutableList.of()).sourceVersion())
34         .isEqualTo(SourceVersion.RELEASE_8);
35     assertThat(
36             LanguageVersion.fromJavacopts(ImmutableList.of("-source", "8", "-target", "11"))
37                 .sourceVersion())
38         .isEqualTo(SourceVersion.RELEASE_8);
39     assertThat(
40             LanguageVersion.fromJavacopts(ImmutableList.of("-source", "8", "-source", "7"))
41                 .sourceVersion())
42         .isEqualTo(SourceVersion.RELEASE_7);
43   }
44 
45   @Test
withPrefix()46   public void withPrefix() {
47     assertThat(LanguageVersion.fromJavacopts(ImmutableList.of("-source", "1.7")).sourceVersion())
48         .isEqualTo(SourceVersion.RELEASE_7);
49     assertThat(LanguageVersion.fromJavacopts(ImmutableList.of("-source", "1.8")).sourceVersion())
50         .isEqualTo(SourceVersion.RELEASE_8);
51   }
52 
53   @Test
invalidPrefix()54   public void invalidPrefix() {
55     assertThat(LanguageVersion.fromJavacopts(ImmutableList.of("-source", "1.10")).source())
56         .isEqualTo(10);
57     IllegalArgumentException expected =
58         assertThrows(
59             IllegalArgumentException.class,
60             () -> LanguageVersion.fromJavacopts(ImmutableList.of("-source", "1.11")));
61     assertThat(expected).hasMessageThat().contains("invalid -source version: 1.11");
62   }
63 
64   @Test
latestSupported()65   public void latestSupported() {
66     String latest = SourceVersion.latestSupported().toString();
67     assertThat(latest).startsWith("RELEASE_");
68     latest = latest.substring("RELEASE_".length());
69     assertThat(LanguageVersion.fromJavacopts(ImmutableList.of("-source", latest)).sourceVersion())
70         .isEqualTo(SourceVersion.latestSupported());
71   }
72 
73   @Test
missingArgument()74   public void missingArgument() {
75     for (String flag :
76         ImmutableList.of("-source", "--source", "-target", "--target", "--release")) {
77       IllegalArgumentException expected =
78           assertThrows(
79               IllegalArgumentException.class,
80               () -> LanguageVersion.fromJavacopts(ImmutableList.of(flag)));
81       assertThat(expected).hasMessageThat().contains(flag + " requires an argument");
82     }
83   }
84 
85   @Test
invalidSourceVersion()86   public void invalidSourceVersion() {
87     IllegalArgumentException expected =
88         assertThrows(
89             IllegalArgumentException.class,
90             () -> LanguageVersion.fromJavacopts(ImmutableList.of("-source", "NOSUCH")));
91     assertThat(expected).hasMessageThat().contains("invalid -source version: NOSUCH");
92   }
93 
94   @Test
invalidRelease()95   public void invalidRelease() {
96     IllegalArgumentException expected =
97         assertThrows(
98             IllegalArgumentException.class,
99             () -> LanguageVersion.fromJavacopts(ImmutableList.of("--release", "NOSUCH")));
100     assertThat(expected).hasMessageThat().contains("invalid --release version: NOSUCH");
101   }
102 
103   @Test
parseRelease()104   public void parseRelease() {
105     assertThat(LanguageVersion.fromJavacopts(ImmutableList.of("--release", "16")).release())
106         .hasValue(16);
107     assertThat(
108             LanguageVersion.fromJavacopts(ImmutableList.of("-source", "8", "-target", "8"))
109                 .release())
110         .isEmpty();
111   }
112 
113   @Test
parseTarget()114   public void parseTarget() {
115     assertThat(
116             LanguageVersion.fromJavacopts(
117                     ImmutableList.of("--release", "12", "-source", "8", "-target", "11"))
118                 .target())
119         .isEqualTo(11);
120     assertThat(
121             LanguageVersion.fromJavacopts(
122                     ImmutableList.of("-source", "8", "-target", "11", "--release", "12"))
123                 .target())
124         .isEqualTo(12);
125   }
126 
127   @Test
releaseUnderride()128   public void releaseUnderride() {
129     assertThat(
130             LanguageVersion.fromJavacopts(ImmutableList.of("--release", "12", "-source", "8"))
131                 .release())
132         .isEmpty();
133     assertThat(
134             LanguageVersion.fromJavacopts(ImmutableList.of("--release", "12", "-target", "8"))
135                 .release())
136         .isEmpty();
137   }
138 
139   @Test
unsupportedSourceVersion()140   public void unsupportedSourceVersion() {
141     LanguageVersion languageVersion =
142         LanguageVersion.fromJavacopts(ImmutableList.of("-source", "9999"));
143     assertThat(languageVersion.sourceVersion()).isEqualTo(SourceVersion.latestSupported());
144   }
145 }
146