1 /* 2 * Copyright (C) 2024 The Android Open Source Project 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.android.tools.metalava.model.testsuite.callableitem 18 19 import com.android.tools.metalava.model.Assertions.Companion.assertClass 20 import com.android.tools.metalava.model.CallableItem 21 import com.android.tools.metalava.model.Codebase 22 import com.android.tools.metalava.model.getCallableParameterDescriptorUsingDots 23 import com.android.tools.metalava.model.testsuite.BaseModelTest 24 import com.android.tools.metalava.testing.java 25 import org.intellij.lang.annotations.Language 26 import org.junit.Assert.assertEquals 27 import org.junit.Test 28 import org.junit.runners.Parameterized 29 30 class CallableParameterDescriptorUsingDotsTest : BaseModelTest() { 31 32 @Parameterized.Parameter(0) lateinit var params: TestParams 33 34 data class TestParams( 35 @Language("JAVA") val inputSource: String, 36 val constructor: Boolean = false, <lambda>null37 val callableGetter: (Codebase) -> CallableItem = { 38 val classItem = it.assertClass("test.pkg.Foo") 39 val callables = if (constructor) classItem.constructors() else classItem.methods() 40 callables.single() 41 }, 42 val expectedResult: String?, 43 ) { toStringnull44 override fun toString(): String { 45 return expectedResult.toString() 46 } 47 } 48 49 companion object { 50 private val params = 51 listOf( 52 TestParams( 53 inputSource = 54 """ 55 package test.pkg; 56 public class Foo { 57 public void method() {} 58 } 59 """, 60 expectedResult = "()", 61 ), 62 TestParams( 63 inputSource = 64 """ 65 package test.pkg; 66 public class Foo { 67 public int method(int p, String s) {return p;} 68 } 69 """, 70 expectedResult = "(ILjava.lang.String;)", 71 ), 72 TestParams( 73 inputSource = 74 """ 75 package test.pkg; 76 public class Foo { 77 public Foo(long p, byte b) {} 78 } 79 """, 80 constructor = true, 81 expectedResult = "(JB)", 82 ), 83 TestParams( 84 inputSource = 85 """ 86 package test.pkg; 87 import java.util.Map; 88 public class Foo { 89 public class Bar { 90 public Bar(Map.Entry<String, Integer> e) {} 91 } 92 } 93 """, 94 constructor = true, <lambda>null95 callableGetter = { it.assertClass("test.pkg.Foo.Bar").constructors().single() }, 96 // This is `null` because the PSI helper cannot resolve Bar to a class. 97 expectedResult = null, 98 ), 99 TestParams( 100 inputSource = 101 """ 102 package test.pkg; 103 import java.util.Map; 104 public class Foo { 105 public static class Bar { 106 public Bar(Map.Entry<String, Integer> e) {} 107 } 108 } 109 """, 110 constructor = true, <lambda>null111 callableGetter = { it.assertClass("test.pkg.Foo.Bar").constructors().single() }, 112 expectedResult = "(Ljava.util.Map.Entry;)", 113 ), 114 ) 115 paramsnull116 @JvmStatic @Parameterized.Parameters fun params() = params 117 } 118 119 @Test 120 fun `Test getCallableParameterDescriptorUsingDots`() { 121 runCodebaseTest(java(params.inputSource)) { 122 val callable = params.callableGetter(codebase) 123 assertEquals(params.expectedResult, callable.getCallableParameterDescriptorUsingDots()) 124 } 125 } 126 } 127