1 /* 2 * Copyright 2017 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 android.support.tools.jetifier.processor.transform 18 19 import com.android.tools.build.jetifier.core.rule.RewriteRule 20 import com.android.tools.build.jetifier.core.type.JavaType 21 import com.google.common.truth.Truth 22 import org.junit.Test 23 24 class RewriteRuleTest { 25 noRegEx_shouldRewritenull26 @Test fun noRegEx_shouldRewrite() { 27 RuleTester 28 .testThatRule("A/B", "A/C") 29 .rewritesType("A/B") 30 .into("A/C") 31 } 32 noRegEx_underscore_shouldRewritenull33 @Test fun noRegEx_underscore_shouldRewrite() { 34 RuleTester 35 .testThatRule("A/B_B", "A/C") 36 .rewritesType("A/B_B") 37 .into("A/C") 38 } 39 groupRegEx_shouldRewritenull40 @Test fun groupRegEx_shouldRewrite() { 41 RuleTester 42 .testThatRule("A/B/(.*)", "A/{0}") 43 .rewritesType("A/B/C/D") 44 .into("A/C/D") 45 } 46 groupRegEx__innerClass_shouldRewritenull47 @Test fun groupRegEx__innerClass_shouldRewrite() { 48 RuleTester 49 .testThatRule("A/B/(.*)", "A/{0}") 50 .rewritesType("A/B/C\$D") 51 .into("A/C\$D") 52 } 53 fieldRule_innerClass_groupRegEx_shouldRewritenull54 @Test fun fieldRule_innerClass_groupRegEx_shouldRewrite() { 55 RuleTester 56 .testThatRule("A/B$(.*)", "A/C\${0}") 57 .rewritesType("A/B\$D") 58 .into("A/C\$D") 59 } 60 typeRewrite_ignorenull61 @Test fun typeRewrite_ignore() { 62 RuleTester 63 .testThatRule("A/B", "ignore") 64 .rewritesType("A/B") 65 .isIgnored() 66 } 67 typeRewrite_ignoreInPreprocessornull68 @Test fun typeRewrite_ignoreInPreprocessor() { 69 RuleTester 70 .testThatRule("A/B", "ignoreInPreprocessorOnly") 71 .rewritesType("A/B") 72 .isIgnored() 73 } 74 75 object RuleTester { 76 testThatRulenull77 fun testThatRule(from: String, to: String) = RuleTesterStep1(from, to) 78 79 class RuleTesterStep1(val from: String, val to: String) { 80 81 fun rewritesType(inputType: String) = RuleTesterFinalTypeStep(from, to, inputType) 82 } 83 84 class RuleTesterFinalTypeStep( 85 val fromType: String, 86 val toType: String, 87 val inputType: String 88 ) { 89 intonull90 fun into(expectedResult: String) { 91 val fieldRule = RewriteRule(fromType, toType) 92 val result = fieldRule.apply(JavaType(inputType)) 93 94 Truth.assertThat(result).isNotNull() 95 Truth.assertThat(result.result!!.fullName).isEqualTo(expectedResult) 96 } 97 isIgnorednull98 fun isIgnored() { 99 val fieldRule = RewriteRule(fromType, toType) 100 val result = fieldRule.apply(JavaType(inputType)) 101 102 Truth.assertThat(result).isNotNull() 103 Truth.assertThat(result.isIgnored).isTrue() 104 } 105 } 106 } 107 } 108 109