Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caused by: java.lang.IllegalArgumentException: Error building function signature with range 414 - 211 for element: @JvmStatic @Parameterized.Parameters( #4887

Closed
waliahimanshu opened this issue Jun 1, 2022 · 7 comments
Assignees
Labels
bug good first issue Issue that is easy to pickup for people that are new to the project help wanted

Comments

@waliahimanshu
Copy link

waliahimanshu commented Jun 1, 2022

Expected Behavior

Successfully analysis of test class with @Parameterized.Parameters with a large function returning a list of arrays of around 27 items

Observed Behavior

Caused by: java.lang.IllegalArgumentException: Error building function signature with range 414 - 211 for element: @JvmStatic
        @Parameterized.Parameters(
            name = "{index}-Case-Source-{0}-FT-{1}-expectedFeature-{2}-expectedFeatureList-{3}"
        )
        fun data() =
              listOf(
                    arrayOf(CTA_PREMIUM_SEARCH, emptyList<FeatureToggle>(), PREMIUM_SEARCH, listOf(AD_FREE)),
                    arrayOf(
                        CTA_PREMIUM_SEARCH,
                        listOf(SAVES_LIMIT_TEST, SAVES_LIMIT_PROMOTION),
                        PREMIUM_SEARCH,
                        listOf(UNLIMITED_SAVED_RECIPES, AD_FREE)
                    ),
                    arrayOf(
                        CTA_PREMIUM_SEARCH,
                        listOf(SAVES_LIMIT_TEST),
                        PREMIUM_SEARCH,
                        listOf(UNLIMITED_SAVED_RECIPES, AD_FREE)
                    ),
                    arrayOf(
                        CTA_PREMIUM_SEARCH,
                        listOf(SAVES_LIMIT_PROMOTION),
                        PREMIUM_SEARCH,
                        listOf(UNLIMITED_SAVED_RECIPES, AD_FREE)
                    ),
                    arrayOf(
                        CTA_PREMIUM_SEARCH,
                        listOf(FeatureToggle.HALL_OF_FAME),
                        PREMIUM_SEARCH,
                        listOf(HALL_OF_FAME, AD_FREE)
                    ),
                    arrayOf(
                        CTA_PREMIUM_SEARCH,
                        listOf(PREMIUM_SEARCH_FILTERS),
                        PREMIUM_SEARCH,
                        listOf(PREMIUM_FILTERS, AD_FREE)
                    ),
                    arrayOf(
                        CTA_PREMIUM_SEARCH,
                        listOf(FeatureToggle.HALL_OF_FAME, SAVES_LIMIT_TEST, SAVES_LIMIT_PROMOTION, PREMIUM_SEARCH_FILTERS),
                        PREMIUM_SEARCH,
                        listOf(UNLIMITED_SAVED_RECIPES, PREMIUM_FILTERS, HALL_OF_FAME, AD_FREE)
                    ),
                    arrayOf(
                        CTA_PREMIUM_FILTERS,
                        emptyList<FeatureToggle>(),
                        PREMIUM_FILTERS,
                        listOf(PREMIUM_SEARCH, AD_FREE)
                    ),
                    arrayOf(
                        CTA_PREMIUM_FILTERS,
                        listOf(SAVES_LIMIT_TEST, SAVES_LIMIT_PROMOTION),
                        PREMIUM_FILTERS,
                        listOf(PREMIUM_SEARCH, UNLIMITED_SAVED_RECIPES, AD_FREE)
                    ),
                    arrayOf(
                        CTA_PREMIUM_FILTERS,
                        listOf(FeatureToggle.HALL_OF_FAME),
                        PREMIUM_FILTERS,
                        listOf(PREMIUM_SEARCH, HALL_OF_FAME, AD_FREE)
                    ),
                    arrayOf(
                        CTA_PREMIUM_FILTERS,
                        listOf(FeatureToggle.HALL_OF_FAME, SAVES_LIMIT_TEST, SAVES_LIMIT_PROMOTION),
                        PREMIUM_FILTERS,
                        listOf(PREMIUM_SEARCH, UNLIMITED_SAVED_RECIPES, HALL_OF_FAME, AD_FREE)
                    ),
                    arrayOf(CTA_AGNOSTIC, emptyList<FeatureToggle>(), PREMIUM_SEARCH, listOf(AD_FREE)),
                    arrayOf(
                        CTA_AGNOSTIC,
                        listOf(SAVES_LIMIT_TEST, SAVES_LIMIT_PROMOTION),
                        PREMIUM_SEARCH,
                        listOf(UNLIMITED_SAVED_RECIPES, AD_FREE)
                    ),
                    arrayOf(
                        CTA_AGNOSTIC,
                        listOf(SAVES_LIMIT_TEST),
                        PREMIUM_SEARCH,
                        listOf(UNLIMITED_SAVED_RECIPES, AD_FREE)
                    ),
                    arrayOf(
                        CTA_AGNOSTIC,
                        listOf(SAVES_LIMIT_PROMOTION),
                        PREMIUM_SEARCH,
                        listOf(UNLIMITED_SAVED_RECIPES, AD_FREE)
                    ),
                    arrayOf(
                        CTA_AGNOSTIC,
                        listOf(FeatureToggle.HALL_OF_FAME),
                        PREMIUM_SEARCH,
                        listOf(HALL_OF_FAME, AD_FREE)
                    ),
                    arrayOf(
                        CTA_AGNOSTIC,
                        listOf(PREMIUM_SEARCH_FILTERS),
                        PREMIUM_SEARCH,
                        listOf(PREMIUM_FILTERS, AD_FREE)
                    ),
                    arrayOf(
                        CTA_AGNOSTIC,
                        listOf(FeatureToggle.HALL_OF_FAME, PREMIUM_SEARCH_FILTERS, SAVES_LIMIT_TEST, SAVES_LIMIT_PROMOTION),
                        PREMIUM_SEARCH,
                        listOf(UNLIMITED_SAVED_RECIPES, PREMIUM_FILTERS, HALL_OF_FAME, AD_FREE)
                    ),
                    arrayOf(CTA_HALL_OF_FAME, emptyList<FeatureToggle>(), HALL_OF_FAME, listOf(PREMIUM_SEARCH, AD_FREE)),
                    arrayOf(
                        CTA_HALL_OF_FAME,
                        listOf(SAVES_LIMIT_TEST, SAVES_LIMIT_PROMOTION),
                        HALL_OF_FAME,
                        listOf(PREMIUM_SEARCH, UNLIMITED_SAVED_RECIPES, AD_FREE)
                    ),
                    arrayOf(
                        CTA_HALL_OF_FAME,
                        listOf(SAVES_LIMIT_TEST),
                        HALL_OF_FAME,
                        listOf(PREMIUM_SEARCH, UNLIMITED_SAVED_RECIPES, AD_FREE)
                    ),
                    arrayOf(
                        CTA_HALL_OF_FAME,
                        listOf(SAVES_LIMIT_PROMOTION),
                        HALL_OF_FAME,
                        listOf(PREMIUM_SEARCH, UNLIMITED_SAVED_RECIPES, AD_FREE)
                    ),
                    arrayOf(
                        CTA_HALL_OF_FAME,
                        listOf(PREMIUM_SEARCH_FILTERS),
                        HALL_OF_FAME,
                        listOf(PREMIUM_SEARCH, PREMIUM_FILTERS, AD_FREE)
                    ),
                    arrayOf(
                        CTA_UNLIMITED_SAVED_RECIPES,
                        emptyList<FeatureToggle>(),
                        UNLIMITED_SAVED_RECIPES,
                        listOf(PREMIUM_SEARCH, AD_FREE)
                    ),
                    arrayOf(
                        CTA_UNLIMITED_SAVED_RECIPES,
                        listOf(FeatureToggle.HALL_OF_FAME),
                        UNLIMITED_SAVED_RECIPES,
                        listOf(PREMIUM_SEARCH, HALL_OF_FAME, AD_FREE)
                    ),
                    arrayOf(
                        CTA_UNLIMITED_SAVED_RECIPES,
                        listOf(PREMIUM_SEARCH_FILTERS),
                        UNLIMITED_SAVED_RECIPES,
                        listOf(PREMIUM_SEARCH, PREMIUM_FILTERS, AD_FREE)
                    ),
                    arrayOf(
                        CTA_RELATED_RECIPES,
                        emptyList<FeatureToggle>(),
                        RELATED_RECIPES,
                        listOf(PREMIUM_SEARCH, AD_FREE)
                    ),
                    arrayOf(
                        CTA_RELATED_RECIPES,
                        listOf(RECIPE_PS_RECOMMENDATIONS),
                        RELATED_RECIPES,
                        listOf(PREMIUM_SEARCH, AD_FREE)
                    ),
                )
        at io.gitlab.arturbosch.detekt.api.internal.SignaturesKt.buildFunctionSignature(Signatures.kt:108)
        at io.gitlab.arturbosch.detekt.api.internal.SignaturesKt.searchSignature(Signatures.kt:71)
        at io.gitlab.arturbosch.detekt.api.internal.SignaturesKt.buildFullSignature(Signatures.kt:46)
        at io.gitlab.arturbosch.detekt.api.Entity$Companion.from(Entity.kt:74)
        at io.gitlab.arturbosch.detekt.api.Entity$Companion.from(Entity.kt:66)
        at io.gitlab.arturbosch.detekt.api.Entity$Companion.atName(Entity.kt:51)
        at io.gitlab.arturbosch.detekt.rules.complexity.LongMethod.postVisit(LongMethod.kt:63)
        at io.gitlab.arturbosch.detekt.api.BaseRule.visitFile(BaseRule.kt:47)
        at io.gitlab.arturbosch.detekt.core.Analyzer.analyze$executeRules(Analyzer.kt:122)
        at io.gitlab.arturbosch.detekt.core.Analyzer.analyze(Analyzer.kt:134)
        at io.gitlab.arturbosch.detekt.core.Analyzer.access$analyze(Analyzer.kt:31)
        at io.gitlab.arturbosch.detekt.core.Analyzer$runAsync$tasks$1$1.invoke(Analyzer.kt:85)
        at io.gitlab.arturbosch.detekt.core.Analyzer$runAsync$tasks$1$1.invoke(Analyzer.kt:83)
        at io.gitlab.arturbosch.detekt.core.TaskPoolKt.task$lambda-0(TaskPool.kt:12)

Steps to Reproduce

A parametrized test with a function having

companion object {
        @JvmStatic
        @Parameterized.Parameters(
            name = "{index}-Case-subscriptionSource-{0}-featureToggles-{1}-expectedHighlightFeature-{2}-expectedShowcaseFeatureList-{3}"
        )
        fun data() = listOf(
                    arrayOf(item1, item2,item3, item4),
                   arrayOf(item1, item2,item3, item4),
                  arrayOf(item1, item2,item3, item4),
                  ........ total 27 items in the list  
        )

Context

Your Environment

  • Version of detekt used:
  • Version of Gradle used (if applicable):
  • Gradle scan link (add --scan option when running the gradle task):
  • Operating System and version:
  • Link to your project (if it's a public repository):
@cortinico
Copy link
Member

Yup seems like a valid bug to me. A starting point could be to write a broken test for:

private fun buildFunctionSignature(element: KtNamedFunction): String {
var methodStart = 0
element.docComment?.let {
methodStart = it.endOffset - it.startOffset
}
var methodEnd = element.endOffset - element.startOffset
val typeReference = element.typeReference
if (typeReference != null) {
methodEnd = typeReference.endOffset - element.startOffset
} else {
element.valueParameterList?.let {
methodEnd = it.endOffset - element.startOffset
}
}
require(methodStart < methodEnd) {
"Error building function signature with range $methodStart - $methodEnd for element: ${element.text}"
}
return element.text.substring(methodStart, methodEnd)
}

And refine it till it's green 👍

@cortinico cortinico added help wanted good first issue Issue that is easy to pickup for people that are new to the project labels Jun 1, 2022
@VitalyVPinchuk
Copy link
Contributor

May I try to fix it?

@cortinico
Copy link
Member

Yup I can assign this to you @VitalyVPinchuk 👍

@VitalyVPinchuk
Copy link
Contributor

Sorry @waliahimanshu I can't make it fail.
No matter how many items are there I always get
Test.kt$Test.Companion$@JvmStatic @Parameterized.Parameters( name = "{index}-Case-subscriptionSource-{0}-featureToggles-{1}-expectedHighlightFeature-{2}-expectedShowcaseFeatureList-{3}" ) fun data()

Could you please provide full listing of your test class? Probably, the problem is somewhere else.

@waliahimanshu
Copy link
Author

waliahimanshu commented Jun 8, 2022

Sorry @waliahimanshu I can't make it fail. No matter how many items are there I always get Test.kt$Test.Companion$@JvmStatic @Parameterized.Parameters( name = "{index}-Case-subscriptionSource-{0}-featureToggles-{1}-expectedHighlightFeature-{2}-expectedShowcaseFeatureList-{3}" ) fun data()

Could you please provide full listing of your test class? Probably, the problem is somewhere else.

I created a gist here https://gist.github.com/waliahimanshu/d918a2ca42e210da4ed2c7b130766417
I included the enums used in the function in question. You might want to simulate code for the rest of the test class.

@VitalyVPinchuk
Copy link
Contributor

VitalyVPinchuk commented Jun 8, 2022

Thanks for sharing the code.
But it still doesn't fail for me.

This is what I do:
main...VitalyVPinchuk:parameterized-test-fix

The output is this:

Test.kt$WelcomeNewPsUserViewModelViewStatesTest$@Before fun setup()
Test.kt$WelcomeNewPsUserViewModelViewStatesTest$@Test fun `Given a SubscriptionSource and a set of Feature Toggles When vm initialised Then correct view state for reason is set`()
Test.kt$WelcomeNewPsUserViewModelViewStatesTest$private fun givenSubscriptionSource(): UserSuccessfullySubscribeViewState
Test.kt$WelcomeNewPsUserViewModelViewStatesTest$private fun givenEnabledFeatureToggles()
Test.kt$WelcomeNewPsUserViewModelViewStatesTest$private fun createViewModel(subscriptionSource: SubscriptionSource, query: String = "")
Test.kt$WelcomeNewPsUserViewModelViewStatesTest$private fun getFakeFeatureViewState( feature: PremiumFeature, isSearchActive: Boolean = false, ): PremiumFeatureViewState
Test.kt$WelcomeNewPsUserViewModelViewStatesTest.Companion$@JvmStatic @Parameterized.Parameters( name = "{index}-Case-subscriptionSource-{0}-featureToggles-{1}-expectedHighlightFeature-{2}-expectedShowcaseFeatureList-{3}" ) fun data(): List<Array<out Any?>>

Probably @cortinico @BraisGabin might want to have a look

@waliahimanshu
Copy link
Author

waliahimanshu commented Jun 13, 2022

Hi @VitalyVPinchuk I tested your test class and it turns out the comment in the functions breaks the buildFunctionSignature method not the long function itslef.

If you run the test by removing the comment, the test passes and wth comment it breaks.

private fun getCode() = """

@RunWith(Parameterized::class)
class WelcomeNewPsUserViewModelViewStatesTest(
    private val subscriptionSource: SubscriptionSource,
    private val enabledFeatureToggles: List<FeatureToggle>,
    private val expectedHighlightFeature: PremiumFeature,
    private val expectedShowcaseFeatureList: List<PremiumFeature>,
) {
   
    companion object {
        @JvmStatic
        @Parameterized.Parameters(
            name = "{index}-Case-subscriptionSource-{0}-featureToggles-{1}-expectedHighlightFeature-{2}-expectedShowcaseFeatureList-{3}"
        )
        fun data(): List<Array<out Any?>> =
            /**
             * For the test data the array configuration goes as follows:
             * 0 - call to action that triggered the premium subscription and the opening of the Welcome Screen
             * 1 - array of enabled feature toggles
             * 2 - expected Highlighted feature in the welcome screen
             * 3 - expected list of premium features to be shown in the welcome screen
             */
            listOf(
                arrayOf(CTA_PREMIUM_SEARCH, emptyList<FeatureToggle>(), PREMIUM_SEARCH, listOf(AD_FREE)),
                arrayOf(
                    CTA_PREMIUM_SEARCH,
                    listOf(SAVES_LIMIT_TEST, SAVES_LIMIT_PROMOTION),
                    PREMIUM_SEARCH,
                    listOf(UNLIMITED_SAVED_RECIPES, AD_FREE)
                ),
               
}
}
""".trimIndent()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug good first issue Issue that is easy to pickup for people that are new to the project help wanted
Projects
None yet
Development

No branches or pull requests

4 participants