Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import sergio.sastre.composable.preview.scanner.core.preview.ComposablePreview
import sergio.sastre.composable.preview.scanner.core.preview.ProvideComposablePreview
import java.lang.reflect.Method
import kotlin.math.max
import kotlin.math.min
import kotlin.reflect.KFunction
import kotlin.reflect.KParameter
import kotlin.reflect.full.declaredMemberFunctions
Expand Down Expand Up @@ -95,11 +96,14 @@ data class ComposablePreviewWithPreviewParameterMapper<T>(
val limit = getPropertyValue(previewParameterAnnotation, "limit") as? Int
?: Int.MAX_VALUE

val count = getPropertyValue(providerInstance, "count") as? Int
val displayedValuesCount = count?.let { min(it, limit) } ?: limit

val getDisplayNameMethod =
providerInstance::class.declaredMemberFunctions.getDisplayNameFunction()

return values
.take(max(0, limit))
.take(max(0, displayedValuesCount))
.mapIndexed { index, value ->
provideComposablePreview(
composablePreviewMapper = this,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package sergio.sastre.composable.preview.scanner.android.previewparameterscount

import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.tooling.preview.PreviewParameterProvider

val stringValues: Sequence<String?> = sequenceOf("Jim", "Jens", null)
class StringMinusCountParameterProvider(
override val values: Sequence<String?> = stringValues,
override val count: Int = -1,
) : PreviewParameterProvider<String?>

class StringCountGreaterThanValuesSizeParameterProvider(
override val values: Sequence<String?> = stringValues,
override val count: Int = stringValues.count() + 1,
) : PreviewParameterProvider<String?>

class StringCountGreaterThanLimitParameterProvider(
override val values: Sequence<String?> = stringValues,
override val count: Int = 2,
) : PreviewParameterProvider<String?>

class StringCountLessThanLimitParameterProvider(
override val values: Sequence<String?> = stringValues,
override val count: Int = 1,
) : PreviewParameterProvider<String?>

@Composable
fun Example(name: String?){
Text(name.toString())
}

@Preview(group = "count < 0")
@Composable
fun ExamplePreviewProviderInParameterConstructor(
@PreviewParameter(provider = StringMinusCountParameterProvider ::class) name: String?
){
Example(name)
}

@Preview(group = "count > values size")
@Composable
fun ExamplePreviewCountGreaterThanValuesSizeProviderInParameterConstructor(
@PreviewParameter(provider = StringCountGreaterThanValuesSizeParameterProvider ::class) name: String?
){
Example(name)
}

@Preview(group = "count > limit and limit = 1")
@Composable
fun ExamplePreviewCountGreaterThanLimitProviderInParameterConstructor(
@PreviewParameter(provider = StringCountGreaterThanLimitParameterProvider ::class, limit = 1) name: String?
){
Example(name)
}

@Preview(group = "count < limit and limit = 2")
@Composable
fun ExamplePreviewCountLessThanLimitProviderInParameterConstructor(
@PreviewParameter(provider = StringCountLessThanLimitParameterProvider ::class, limit = 2) name: String?
){
Example(name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import sergio.sastre.composable.preview.scanner.android.customextraannotation.Fo
import sergio.sastre.composable.preview.scanner.android.customextraannotation.ScreenshotTestConfig
import sergio.sastre.composable.preview.scanner.android.excluded.ExcludeScreenshot
import sergio.sastre.composable.preview.scanner.android.included.IncludeScreenshot
import sergio.sastre.composable.preview.scanner.android.previewparameterscount.StringCountGreaterThanLimitParameterProvider
import sergio.sastre.composable.preview.scanner.android.previewparameterscount.StringCountGreaterThanValuesSizeParameterProvider
import sergio.sastre.composable.preview.scanner.android.previewparameterscount.StringCountLessThanLimitParameterProvider
import sergio.sastre.composable.preview.scanner.android.previewparameterscount.StringMinusCountParameterProvider
import sergio.sastre.composable.preview.scanner.core.scanresult.RequiresLargeHeap
import sergio.sastre.composable.preview.scanner.core.preview.getAnnotation
import sergio.sastre.composable.preview.scanner.core.scanresult.filter.exceptions.RepeatableAnnotationNotSupportedException
Expand Down Expand Up @@ -401,6 +405,68 @@ class AndroidComposablePreviewScannerTest {
}
}

@Test
fun `GIVEN preview parameters with count less than 0 THEN it does not return previews`() {
val stringProvider = StringMinusCountParameterProvider()

val previewsWithParameterProviderInConstructor =
AndroidComposablePreviewScanner()
.scanPackageTrees("sergio.sastre.composable.preview.scanner.android.previewparameterscount")
.filterPreviews { it.group == "count < 0" }
.getPreviews()

assert(stringProvider.count <= 0)
assert(previewsWithParameterProviderInConstructor.isEmpty())
}

@Test
fun `GIVEN preview parameters with count greater than values size THEN it does return as many previews as values size`() {
val stringProvider = StringCountGreaterThanValuesSizeParameterProvider()
val count = stringProvider.count
val valuesSize = stringProvider.values.count()
assert(count > valuesSize)

val previewsWithParameterProviderInConstructor =
AndroidComposablePreviewScanner()
.scanPackageTrees("sergio.sastre.composable.preview.scanner.android.previewparameterscount")
.filterPreviews { it.group == "count > values size" }
.getPreviews()

assert(previewsWithParameterProviderInConstructor.size == valuesSize)
}

@Test
fun `GIVEN preview parameters with count greater than limit THEN it does not return as many previews as limit`() {
val stringProvider = StringCountGreaterThanLimitParameterProvider()
val limit = 1
val count = stringProvider.count
assert(count > limit)

val previewsWithParameterProviderInConstructor =
AndroidComposablePreviewScanner()
.scanPackageTrees("sergio.sastre.composable.preview.scanner.android.previewparameterscount")
.filterPreviews { it.group == "count > limit and limit = 1" }
.getPreviews()

assert(previewsWithParameterProviderInConstructor.size == limit)
}

@Test
fun `GIVEN preview parameters with count less than limit THEN it does not return as many previews as count`() {
val stringProvider = StringCountLessThanLimitParameterProvider()
val limit = 2
val count = stringProvider.count
assert(count < limit)

val previewsWithParameterProviderInConstructor =
AndroidComposablePreviewScanner()
.scanPackageTrees("sergio.sastre.composable.preview.scanner.android.previewparameterscount")
.filterPreviews { it.group == "count < limit and limit = 2" }
.getPreviews()

assert(previewsWithParameterProviderInConstructor.size == count)
}

@Test
fun `GIVEN preview parameters with StringProvider and MultiplePreviews THEN it creates one preview for every combination`() {
val stringProviderValuesSize = AndroidStringProvider().values.toList().size
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading