Last active
August 27, 2019 17:06
-
-
Save shekibobo/69da58bf8540460bd78223614f7fea01 to your computer and use it in GitHub Desktop.
Sample test of StringFormatDetector against equivalent Java and Kotlin classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.android.tools.lint.checks.StringFormatDetector | |
import com.android.tools.lint.checks.infrastructure.LintDetectorTest | |
import com.android.tools.lint.detector.api.Detector | |
import com.android.tools.lint.detector.api.Issue | |
class StringFormatDetectorTest : LintDetectorTest() { | |
override fun getDetector(): Detector = StringFormatDetector() | |
override fun getIssues(): MutableList<Issue> = mutableListOf( | |
StringFormatDetector.ARG_COUNT, | |
StringFormatDetector.ARG_TYPES, | |
StringFormatDetector.INVALID, | |
StringFormatDetector.POTENTIAL_PLURAL | |
) | |
companion object { | |
val STUB_R: TestFile = java( | |
""" | |
package test.pkg; | |
public final class R { | |
public static final class string { | |
public static final int hello = 1; | |
public static final int hello2 = 2; | |
public static final int score = 3; | |
public static final int score2 = 5; | |
} | |
public static final class layout { | |
public static final int main = 4; | |
} | |
} | |
""".trimIndent() | |
) | |
val STUB_FORMAT_STRINGS_ES: TestFile = xml( | |
"res/values/format_strings.xml", | |
""" | |
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<string name="hello">Hello %1${'$'}s</string> | |
<string name="hello2">Hello %1${'$'}s, %2${'$'}s?</string> | |
<string name="missing">Hello %3${'$'}s World</string> | |
<string name="score">Score: %1${'$'}d</string> | |
<string name="score2">Score: %1${'$'}b</string> | |
</resources> | |
""".trimIndent() | |
) | |
val STUB_FORMAT_STRINGS_DEFAULT = xml( | |
"res/values-es/format_strings.xml", | |
""" | |
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<string name="hello">%1${'$'}d</string> | |
<string name="hello2">%3${'$'}d: %1${'$'}s, %2${'$'}s?</string> | |
</resources> | |
""".trimIndent() | |
) | |
} | |
fun test_java_Basic() { | |
lint() | |
.files( | |
manifest(), | |
STUB_R, | |
STUB_FORMAT_STRINGS_ES, | |
STUB_FORMAT_STRINGS_DEFAULT, | |
java( | |
"src/test/pkg/StringFormatActivity.java", | |
""" | |
package test.pkg; | |
import android.app.Activity; | |
import android.os.Bundle; | |
public class StringFormatActivity extends Activity { | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
String target = "World"; | |
String hello = getResources().getString(R.string.hello); | |
String output1 = String.format(hello, target); | |
String hello2 = getResources().getString(R.string.hello2); | |
String output2 = String.format(hello2, target, "How are you"); | |
setContentView(R.layout.main); | |
String score = getResources().getString(R.string.score); | |
int points = 50; | |
boolean won = true; | |
String output3 = String.format(score, points); | |
String output4 = String.format(score, true); // wrong | |
String output = String.format(score, won); // wrong | |
String output5 = String.format(score, 75); | |
String.format(getResources().getString(R.string.hello2), target, "How are you"); | |
//getResources().getString(R.string.hello, target, "How are you"); | |
getResources().getString(R.string.hello2, target, "How are you"); | |
} | |
// Test constructor handling (issue 35588) | |
public StringFormatActivity() { | |
String target = "World"; | |
String hello = getResources().getString(R.string.hello); | |
String output1 = String.format(hello, target); | |
} | |
public void testPrimitiveWrappers() { | |
Boolean won = true; | |
Integer value = 1; | |
String score = getResources().getString(R.string.score); | |
String score2 = getResources().getString(R.string.score2); | |
String output1 = String.format(score, won); // wrong | |
String output2 = String.format(score, value); // ok | |
String output3 = String.format(score2, won); // ok | |
} | |
} | |
""".trimIndent() | |
) | |
) | |
.issues(*issues.toTypedArray()) | |
.run() | |
.expect( | |
""" | |
src/test/pkg/StringFormatActivity.java:13: Error: Wrong argument type for formatting argument '#1' in hello: conversion is 'd', received String (argument #2 in method call) [StringFormatMatches] | |
String output1 = String.format(hello, target); | |
~~~~~~ | |
res/values-es/format_strings.xml:3: Conflicting argument declaration here | |
src/test/pkg/StringFormatActivity.java:15: Error: Wrong argument count, format string hello2 requires 3 but format call supplies 2 [StringFormatMatches] | |
String output2 = String.format(hello2, target, "How are you"); | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
res/values-es/format_strings.xml:4: This definition requires 3 arguments | |
src/test/pkg/StringFormatActivity.java:21: Error: Wrong argument type for formatting argument '#1' in score: conversion is 'd', received boolean (argument #2 in method call) (Did you mean formatting character b?) [StringFormatMatches] | |
String output4 = String.format(score, true); // wrong | |
~~~~ | |
res/values/format_strings.xml:6: Conflicting argument declaration here | |
src/test/pkg/StringFormatActivity.java:22: Error: Wrong argument type for formatting argument '#1' in score: conversion is 'd', received boolean (argument #2 in method call) (Did you mean formatting character b?) [StringFormatMatches] | |
String output = String.format(score, won); // wrong | |
~~~ | |
res/values/format_strings.xml:6: Conflicting argument declaration here | |
src/test/pkg/StringFormatActivity.java:24: Error: Wrong argument count, format string hello2 requires 3 but format call supplies 2 [StringFormatMatches] | |
String.format(getResources().getString(R.string.hello2), target, "How are you"); | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
res/values-es/format_strings.xml:4: This definition requires 3 arguments | |
src/test/pkg/StringFormatActivity.java:26: Error: Wrong argument count, format string hello2 requires 3 but format call supplies 2 [StringFormatMatches] | |
getResources().getString(R.string.hello2, target, "How are you"); | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
res/values-es/format_strings.xml:4: This definition requires 3 arguments | |
src/test/pkg/StringFormatActivity.java:33: Error: Wrong argument type for formatting argument '#1' in hello: conversion is 'd', received String (argument #2 in method call) [StringFormatMatches] | |
String output1 = String.format(hello, target); | |
~~~~~~ | |
res/values-es/format_strings.xml:3: Conflicting argument declaration here | |
src/test/pkg/StringFormatActivity.java:41: Error: Wrong argument type for formatting argument '#1' in score: conversion is 'd', received Boolean (argument #2 in method call) (Did you mean formatting character b?) [StringFormatMatches] | |
String output1 = String.format(score, won); // wrong | |
~~~ | |
res/values/format_strings.xml:6: Conflicting argument declaration here | |
res/values-es/format_strings.xml:3: Error: Inconsistent formatting types for argument #1 in format string hello ('%1${'$'}d'): Found both 's' and 'd' (in values/format_strings.xml) [StringFormatMatches] | |
<string name="hello">%1${'$'}d</string> | |
~~~~ | |
res/values/format_strings.xml:3: Conflicting argument type here | |
res/values-es/format_strings.xml:4: Warning: Inconsistent number of arguments in formatting string hello2; found both 2 and 3 [StringFormatCount] | |
<string name="hello2">%3${'$'}d: %1${'$'}s, %2${'$'}s?</string> | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
res/values/format_strings.xml:4: Conflicting number of arguments here | |
res/values/format_strings.xml:5: Warning: Formatting string 'missing' is not referencing numbered arguments [1, 2] [StringFormatCount] | |
<string name="missing">Hello %3${'$'}s World</string> | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
9 errors, 2 warnings | |
""".trimIndent() | |
) | |
} | |
fun test_kotlin_Basic() { | |
lint() | |
.files( | |
manifest(), | |
STUB_R, | |
STUB_FORMAT_STRINGS_ES, | |
STUB_FORMAT_STRINGS_DEFAULT, | |
kotlin( | |
"src/test/pkg/StringFormatActivity.kt", | |
""" | |
package test.pkg; | |
import android.app.Activity; | |
import android.os.Bundle; | |
class StringFormatActivity : Activity() { | |
/** Called when the activity is first created. */ | |
public override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
val target = "World" | |
val hello = resources.getString(R.string.hello) | |
val output1 = String.format(hello, target) | |
val hello2 = resources.getString(R.string.hello2) | |
val output2 = String.format(hello2, target, "How are you") | |
setContentView(R.layout.main) | |
val score = resources.getString(R.string.score) | |
val points = 50 | |
val won = true | |
val output3 = String.format(score, points) | |
val output4 = String.format(score, true) // wrong | |
val output = String.format(score, won) // wrong | |
val output5 = String.format(score, 75) | |
String.format(resources.getString(R.string.hello2), target, "How are you") | |
//getResources().getString(R.string.hello, target, "How are you"); | |
resources.getString(R.string.hello2, target, "How are you") | |
} | |
// Test constructor handling (issue 35588) | |
init { | |
val target = "World" | |
val hello = resources.getString(R.string.hello) | |
val output1 = String.format(hello, target) | |
} | |
fun testPrimitiveWrappers() { | |
val won = true | |
val value = 1 | |
val score = resources.getString(R.string.score) | |
val score2 = resources.getString(R.string.score2) | |
val output1 = String.format(score, won) // wrong | |
val output2 = String.format(score, value) // ok | |
val output3 = String.format(score2, won) // ok | |
} | |
} | |
""".trimIndent() | |
) | |
) | |
.issues(*issues.toTypedArray()) | |
.run() | |
.expect( | |
""" | |
src/test/pkg/StringFormatActivity.kt:9: Error: Wrong argument type for formatting argument '#1' in hello: conversion is 'd', received String (argument #2 in method call) [StringFormatMatches] | |
val output1 = String.format(hello, target) | |
~~~~~~ | |
res/values-es/format_strings.xml:3: Conflicting argument declaration here | |
src/test/pkg/StringFormatActivity.kt:11: Error: Wrong argument count, format string hello2 requires 3 but format call supplies 2 [StringFormatMatches] | |
val output2 = String.format(hello2, target, "How are you") | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
res/values-es/format_strings.xml:4: This definition requires 3 arguments | |
src/test/pkg/StringFormatActivity.kt:17: Error: Wrong argument type for formatting argument '#1' in score: conversion is 'd', received boolean (argument #2 in method call) (Did you mean formatting character b?) [StringFormatMatches] | |
val output4 = String.format(score, true) // wrong | |
~~~~ | |
res/values/format_strings.xml:6: Conflicting argument declaration here | |
src/test/pkg/StringFormatActivity.kt:18: Error: Wrong argument type for formatting argument '#1' in score: conversion is 'd', received boolean (argument #2 in method call) (Did you mean formatting character b?) [StringFormatMatches] | |
val output = String.format(score, won) // wrong | |
~~~ | |
res/values/format_strings.xml:6: Conflicting argument declaration here | |
src/test/pkg/StringFormatActivity.kt:25: Error: Wrong argument count, format string hello2 requires 3 but format call supplies 2 [StringFormatMatches] | |
resources.getString(R.string.hello2, target, "How are you") | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
res/values-es/format_strings.xml:4: This definition requires 3 arguments | |
src/test/pkg/StringFormatActivity.kt:22: Error: Wrong argument count, format string hello2 requires 3 but format call supplies 2 [StringFormatMatches] | |
getResources().getString(R.string.hello2, target, "How are you") | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
res/values-es/format_strings.xml:4: This definition requires 3 arguments | |
src/test/pkg/StringFormatActivity.kt:29: Error: Wrong argument type for formatting argument '#1' in hello: conversion is 'd', received String (argument #2 in method call) [StringFormatMatches] | |
val output1 = String.format(hello, target) | |
~~~~~~ | |
res/values-es/format_strings.xml:3: Conflicting argument declaration here | |
src/test/pkg/StringFormatActivity.kt:37: Error: Wrong argument type for formatting argument '#1' in score: conversion is 'd', received Boolean (argument #2 in method call) (Did you mean formatting character b?) [StringFormatMatches] | |
val output1 = String.format(score, won) // wrong | |
~~~ | |
res/values/format_strings.xml:6: Conflicting argument declaration here | |
res/values-es/format_strings.xml:3: Error: Inconsistent formatting types for argument #1 in format string hello ('%1${'$'}d'): Found both 's' and 'd' (in values/format_strings.xml) [StringFormatMatches] | |
<string name="hello">%1${'$'}d</string> | |
~~~~ | |
res/values/format_strings.xml:3: Conflicting argument type here | |
res/values-es/format_strings.xml:4: Warning: Inconsistent number of arguments in formatting string hello2; found both 2 and 3 [StringFormatCount] | |
<string name="hello2">%3${'$'}d: %1${'$'}s, %2${'$'}s?</string> | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
res/values/format_strings.xml:4: Conflicting number of arguments here | |
res/values/format_strings.xml:5: Warning: Formatting string 'missing' is not referencing numbered arguments [1, 2] [StringFormatCount] | |
<string name="missing">Hello %3${'$'}s World</string> | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
9 errors, 2 warnings | |
""".trimIndent() | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment