Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame^] | 18 | "strconv" |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 19 | "strings" |
| 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | func TestKotlin(t *testing.T) { |
| 24 | ctx := testJava(t, ` |
| 25 | java_library { |
| 26 | name: "foo", |
| 27 | srcs: ["a.java", "b.kt"], |
| 28 | } |
| 29 | |
| 30 | java_library { |
| 31 | name: "bar", |
| 32 | srcs: ["b.kt"], |
| 33 | libs: ["foo"], |
| 34 | static_libs: ["baz"], |
| 35 | } |
| 36 | |
| 37 | java_library { |
| 38 | name: "baz", |
| 39 | srcs: ["c.java"], |
| 40 | } |
| 41 | `) |
| 42 | |
| 43 | fooKotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc") |
| 44 | fooJavac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 45 | fooJar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar") |
| 46 | |
| 47 | if len(fooKotlinc.Inputs) != 2 || fooKotlinc.Inputs[0].String() != "a.java" || |
| 48 | fooKotlinc.Inputs[1].String() != "b.kt" { |
| 49 | t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, fooKotlinc.Inputs) |
| 50 | } |
| 51 | |
| 52 | if len(fooJavac.Inputs) != 1 || fooJavac.Inputs[0].String() != "a.java" { |
| 53 | t.Errorf(`foo inputs %v != ["a.java"]`, fooJavac.Inputs) |
| 54 | } |
| 55 | |
| 56 | if !strings.Contains(fooJavac.Args["classpath"], fooKotlinc.Output.String()) { |
| 57 | t.Errorf("foo classpath %v does not contain %q", |
| 58 | fooJavac.Args["classpath"], fooKotlinc.Output.String()) |
| 59 | } |
| 60 | |
| 61 | if !inList(fooKotlinc.Output.String(), fooJar.Inputs.Strings()) { |
| 62 | t.Errorf("foo jar inputs %v does not contain %q", |
| 63 | fooJar.Inputs.Strings(), fooKotlinc.Output.String()) |
| 64 | } |
| 65 | |
| 66 | fooHeaderJar := ctx.ModuleForTests("foo", "android_common").Output("turbine-combined/foo.jar") |
| 67 | bazHeaderJar := ctx.ModuleForTests("baz", "android_common").Output("turbine-combined/baz.jar") |
| 68 | barKotlinc := ctx.ModuleForTests("bar", "android_common").Rule("kotlinc") |
| 69 | |
| 70 | if len(barKotlinc.Inputs) != 1 || barKotlinc.Inputs[0].String() != "b.kt" { |
| 71 | t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, barKotlinc.Inputs) |
| 72 | } |
| 73 | |
| 74 | if !inList(fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) { |
| 75 | t.Errorf(`expected %q in bar implicits %v`, |
| 76 | fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) |
| 77 | } |
| 78 | |
| 79 | if !inList(bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) { |
| 80 | t.Errorf(`expected %q in bar implicits %v`, |
| 81 | bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) |
| 82 | } |
| 83 | } |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame^] | 84 | |
| 85 | func TestKapt(t *testing.T) { |
| 86 | ctx := testJava(t, ` |
| 87 | java_library { |
| 88 | name: "foo", |
| 89 | srcs: ["a.java", "b.kt"], |
| 90 | annotation_processors: ["bar"], |
| 91 | } |
| 92 | |
| 93 | java_library_host { |
| 94 | name: "bar", |
| 95 | } |
| 96 | `) |
| 97 | |
| 98 | kapt := ctx.ModuleForTests("foo", "android_common").Rule("kapt") |
| 99 | kotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc") |
| 100 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 101 | |
| 102 | // Test that the kotlin and java sources are passed to kapt and kotlinc |
| 103 | if len(kapt.Inputs) != 2 || kapt.Inputs[0].String() != "a.java" || kapt.Inputs[1].String() != "b.kt" { |
| 104 | t.Errorf(`foo kapt inputs %v != ["a.java", "b.kt"]`, kapt.Inputs) |
| 105 | } |
| 106 | if len(kotlinc.Inputs) != 2 || kotlinc.Inputs[0].String() != "a.java" || kotlinc.Inputs[1].String() != "b.kt" { |
| 107 | t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, kotlinc.Inputs) |
| 108 | } |
| 109 | |
| 110 | // Test that only the java sources are passed to javac |
| 111 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 112 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 113 | } |
| 114 | |
| 115 | // Test that the kapt srcjar is a dependency of kotlinc and javac rules |
| 116 | if !inList(kapt.Output.String(), kotlinc.Implicits.Strings()) { |
| 117 | t.Errorf("expected %q in kotlinc implicits %v", kapt.Output.String(), kotlinc.Implicits.Strings()) |
| 118 | } |
| 119 | if !inList(kapt.Output.String(), javac.Implicits.Strings()) { |
| 120 | t.Errorf("expected %q in javac implicits %v", kapt.Output.String(), javac.Implicits.Strings()) |
| 121 | } |
| 122 | |
| 123 | // Test that the kapt srcjar is extracted by the kotlinc and javac rules |
| 124 | if kotlinc.Args["srcJars"] != kapt.Output.String() { |
| 125 | t.Errorf("expected %q in kotlinc srcjars %v", kapt.Output.String(), kotlinc.Args["srcJars"]) |
| 126 | } |
| 127 | if javac.Args["srcJars"] != kapt.Output.String() { |
| 128 | t.Errorf("expected %q in javac srcjars %v", kapt.Output.String(), kotlinc.Args["srcJars"]) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestKaptEncodeFlags(t *testing.T) { |
| 133 | // Compares the kaptEncodeFlags against the results of the example implementation at |
| 134 | // https://kotlinlang.org/docs/reference/kapt.html#apjavac-options-encoding |
| 135 | tests := []struct { |
| 136 | in [][2]string |
| 137 | out string |
| 138 | }{ |
| 139 | { |
| 140 | // empty input |
| 141 | in: [][2]string{}, |
| 142 | out: "rO0ABXcEAAAAAA==", |
| 143 | }, |
| 144 | { |
| 145 | // common input |
| 146 | in: [][2]string{ |
| 147 | {"-source", "1.8"}, |
| 148 | {"-target", "1.8"}, |
| 149 | }, |
| 150 | out: "rO0ABXcgAAAAAgAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjg=", |
| 151 | }, |
| 152 | { |
| 153 | // input that serializes to a 255 byte block |
| 154 | in: [][2]string{ |
| 155 | {"-source", "1.8"}, |
| 156 | {"-target", "1.8"}, |
| 157 | {"a", strings.Repeat("b", 218)}, |
| 158 | }, |
| 159 | out: "rO0ABXf/AAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA2mJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJi", |
| 160 | }, |
| 161 | { |
| 162 | // input that serializes to a 256 byte block |
| 163 | in: [][2]string{ |
| 164 | {"-source", "1.8"}, |
| 165 | {"-target", "1.8"}, |
| 166 | {"a", strings.Repeat("b", 219)}, |
| 167 | }, |
| 168 | out: "rO0ABXoAAAEAAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA22JiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYg==", |
| 169 | }, |
| 170 | { |
| 171 | // input that serializes to a 257 byte block |
| 172 | in: [][2]string{ |
| 173 | {"-source", "1.8"}, |
| 174 | {"-target", "1.8"}, |
| 175 | {"a", strings.Repeat("b", 220)}, |
| 176 | }, |
| 177 | out: "rO0ABXoAAAEBAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA3GJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmI=", |
| 178 | }, |
| 179 | } |
| 180 | |
| 181 | for i, test := range tests { |
| 182 | t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 183 | got := kaptEncodeFlags(test.in) |
| 184 | if got != test.out { |
| 185 | t.Errorf("\nwant %q\n got %q", test.out, got) |
| 186 | } |
| 187 | }) |
| 188 | } |
| 189 | } |