kotlin_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. package java
  15. import (
  16. "android/soong/android"
  17. "strconv"
  18. "strings"
  19. "testing"
  20. )
  21. func TestKotlin(t *testing.T) {
  22. ctx, _ := testJava(t, `
  23. java_library {
  24. name: "foo",
  25. srcs: ["a.java", "b.kt"],
  26. }
  27. java_library {
  28. name: "bar",
  29. srcs: ["b.kt"],
  30. libs: ["foo"],
  31. static_libs: ["baz"],
  32. }
  33. java_library {
  34. name: "baz",
  35. srcs: ["c.java"],
  36. }
  37. `)
  38. fooKotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
  39. fooJavac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
  40. fooJar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
  41. if len(fooKotlinc.Inputs) != 2 || fooKotlinc.Inputs[0].String() != "a.java" ||
  42. fooKotlinc.Inputs[1].String() != "b.kt" {
  43. t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, fooKotlinc.Inputs)
  44. }
  45. if len(fooJavac.Inputs) != 1 || fooJavac.Inputs[0].String() != "a.java" {
  46. t.Errorf(`foo inputs %v != ["a.java"]`, fooJavac.Inputs)
  47. }
  48. if !strings.Contains(fooJavac.Args["classpath"], fooKotlinc.Output.String()) {
  49. t.Errorf("foo classpath %v does not contain %q",
  50. fooJavac.Args["classpath"], fooKotlinc.Output.String())
  51. }
  52. if !inList(fooKotlinc.Output.String(), fooJar.Inputs.Strings()) {
  53. t.Errorf("foo jar inputs %v does not contain %q",
  54. fooJar.Inputs.Strings(), fooKotlinc.Output.String())
  55. }
  56. fooHeaderJar := ctx.ModuleForTests("foo", "android_common").Output("turbine-combined/foo.jar")
  57. bazHeaderJar := ctx.ModuleForTests("baz", "android_common").Output("turbine-combined/baz.jar")
  58. barKotlinc := ctx.ModuleForTests("bar", "android_common").Rule("kotlinc")
  59. if len(barKotlinc.Inputs) != 1 || barKotlinc.Inputs[0].String() != "b.kt" {
  60. t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, barKotlinc.Inputs)
  61. }
  62. if !inList(fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
  63. t.Errorf(`expected %q in bar implicits %v`,
  64. fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
  65. }
  66. if !inList(bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
  67. t.Errorf(`expected %q in bar implicits %v`,
  68. bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
  69. }
  70. }
  71. func TestKapt(t *testing.T) {
  72. ctx, _ := testJava(t, `
  73. java_library {
  74. name: "foo",
  75. srcs: ["a.java", "b.kt"],
  76. plugins: ["bar", "baz"],
  77. }
  78. java_plugin {
  79. name: "bar",
  80. processor_class: "com.bar",
  81. srcs: ["b.java"],
  82. }
  83. java_plugin {
  84. name: "baz",
  85. processor_class: "com.baz",
  86. srcs: ["b.java"],
  87. }
  88. `)
  89. buildOS := android.BuildOs.String()
  90. kapt := ctx.ModuleForTests("foo", "android_common").Rule("kapt")
  91. kotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
  92. javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
  93. bar := ctx.ModuleForTests("bar", buildOS+"_common").Rule("javac").Output.String()
  94. baz := ctx.ModuleForTests("baz", buildOS+"_common").Rule("javac").Output.String()
  95. // Test that the kotlin and java sources are passed to kapt and kotlinc
  96. if len(kapt.Inputs) != 2 || kapt.Inputs[0].String() != "a.java" || kapt.Inputs[1].String() != "b.kt" {
  97. t.Errorf(`foo kapt inputs %v != ["a.java", "b.kt"]`, kapt.Inputs)
  98. }
  99. if len(kotlinc.Inputs) != 2 || kotlinc.Inputs[0].String() != "a.java" || kotlinc.Inputs[1].String() != "b.kt" {
  100. t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, kotlinc.Inputs)
  101. }
  102. // Test that only the java sources are passed to javac
  103. if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
  104. t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
  105. }
  106. // Test that the kapt srcjar is a dependency of kotlinc and javac rules
  107. if !inList(kapt.Output.String(), kotlinc.Implicits.Strings()) {
  108. t.Errorf("expected %q in kotlinc implicits %v", kapt.Output.String(), kotlinc.Implicits.Strings())
  109. }
  110. if !inList(kapt.Output.String(), javac.Implicits.Strings()) {
  111. t.Errorf("expected %q in javac implicits %v", kapt.Output.String(), javac.Implicits.Strings())
  112. }
  113. // Test that the kapt srcjar is extracted by the kotlinc and javac rules
  114. if kotlinc.Args["srcJars"] != kapt.Output.String() {
  115. t.Errorf("expected %q in kotlinc srcjars %v", kapt.Output.String(), kotlinc.Args["srcJars"])
  116. }
  117. if javac.Args["srcJars"] != kapt.Output.String() {
  118. t.Errorf("expected %q in javac srcjars %v", kapt.Output.String(), kotlinc.Args["srcJars"])
  119. }
  120. // Test that the processors are passed to kapt
  121. expectedProcessorPath := "-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + bar +
  122. " -P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + baz
  123. if kapt.Args["kaptProcessorPath"] != expectedProcessorPath {
  124. t.Errorf("expected kaptProcessorPath %q, got %q", expectedProcessorPath, kapt.Args["kaptProcessorPath"])
  125. }
  126. expectedProcessor := "-P plugin:org.jetbrains.kotlin.kapt3:processors=com.bar -P plugin:org.jetbrains.kotlin.kapt3:processors=com.baz"
  127. if kapt.Args["kaptProcessor"] != expectedProcessor {
  128. t.Errorf("expected kaptProcessor %q, got %q", expectedProcessor, kapt.Args["kaptProcessor"])
  129. }
  130. // Test that the processors are not passed to javac
  131. if javac.Args["processorPath"] != "" {
  132. t.Errorf("expected processorPath '', got %q", javac.Args["processorPath"])
  133. }
  134. if javac.Args["processor"] != "-proc:none" {
  135. t.Errorf("expected processor '-proc:none', got %q", javac.Args["processor"])
  136. }
  137. }
  138. func TestKaptEncodeFlags(t *testing.T) {
  139. // Compares the kaptEncodeFlags against the results of the example implementation at
  140. // https://kotlinlang.org/docs/reference/kapt.html#apjavac-options-encoding
  141. tests := []struct {
  142. in [][2]string
  143. out string
  144. }{
  145. {
  146. // empty input
  147. in: [][2]string{},
  148. out: "rO0ABXcEAAAAAA==",
  149. },
  150. {
  151. // common input
  152. in: [][2]string{
  153. {"-source", "1.8"},
  154. {"-target", "1.8"},
  155. },
  156. out: "rO0ABXcgAAAAAgAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjg=",
  157. },
  158. {
  159. // input that serializes to a 255 byte block
  160. in: [][2]string{
  161. {"-source", "1.8"},
  162. {"-target", "1.8"},
  163. {"a", strings.Repeat("b", 218)},
  164. },
  165. out: "rO0ABXf/AAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA2mJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJi",
  166. },
  167. {
  168. // input that serializes to a 256 byte block
  169. in: [][2]string{
  170. {"-source", "1.8"},
  171. {"-target", "1.8"},
  172. {"a", strings.Repeat("b", 219)},
  173. },
  174. out: "rO0ABXoAAAEAAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA22JiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYg==",
  175. },
  176. {
  177. // input that serializes to a 257 byte block
  178. in: [][2]string{
  179. {"-source", "1.8"},
  180. {"-target", "1.8"},
  181. {"a", strings.Repeat("b", 220)},
  182. },
  183. out: "rO0ABXoAAAEBAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA3GJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmI=",
  184. },
  185. }
  186. for i, test := range tests {
  187. t.Run(strconv.Itoa(i), func(t *testing.T) {
  188. got := kaptEncodeFlags(test.in)
  189. if got != test.out {
  190. t.Errorf("\nwant %q\n got %q", test.out, got)
  191. }
  192. })
  193. }
  194. }