Skip to content

Commit

Permalink
Support internal functions for preview generation
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-e committed Oct 11, 2023
1 parent 2d61aeb commit e21447e
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 6 deletions.
2 changes: 1 addition & 1 deletion snapshots/sample/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ emerge {

android {
namespace = "com.emergetools.snapshots.sample"
compileSdk = 33
compileSdk = 34

defaultConfig {
applicationId = "com.emergetools.snapshots.sample"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ fun TextRowWithIconPreviewFromMain() {
)
}

@Preview
@Composable
internal fun TextRowWithIconPreviewFromMainInternalFunction() {
TextRowWithIcon(
titleText = stringResource(com.emergetools.snapshots.sample.R.string.sample_title),
subtitleText = stringResource(com.emergetools.snapshots.sample.R.string.sample_subtitle)
)
}

@SnapshotTestingPreview
@Composable
internal fun TextRowWithIconPreviewFromMainInternalFunctionMultiPreview() {
TextRowWithIcon(
titleText = stringResource(com.emergetools.snapshots.sample.R.string.sample_title),
subtitleText = stringResource(com.emergetools.snapshots.sample.R.string.sample_subtitle)
)
}

@LocalePreviews
@FontScalePreviews
@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class PreviewProcessorTest {
compileInputsAndVerifyOutputs()
}

@Test
fun `standalone preview function with internal preview produces one snapshot test`() {
compileInputsAndVerifyOutputs()
}

@Test
fun `multipreview function with two previews produces two snapshots`() {
compileInputsAndVerifyOutputs()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.emergetools.android.standalone_preview_function_with_internal_preview_produces_one_snapshot_test

import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.runtime.Composable

@Preview
@Composable
internal fun TestComposable() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.emergetools.android.standalone_preview_function_with_internal_preview_produces_one_snapshot_test

import androidx.compose.runtime.Composable
import androidx.compose.ui.test.junit4.ComposeContentTestRule
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.emergetools.android.standalone_preview_function_with_internal_preview_produces_one_snapshot_test.TestComposable
import com.emergetools.snapshots.EmergeSnapshots
import com.emergetools.snapshots.compose.SnapshotVariantProvider
import com.emergetools.snapshots.shared.ComposePreviewSnapshotConfig
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
public class TestComposable_GenSnapshot {
@get:Rule
public val composeRule: ComposeContentTestRule = createComposeRule()

public val previewConfig: ComposePreviewSnapshotConfig = ComposePreviewSnapshotConfig(originalFqn
=
"com.emergetools.android.standalone_preview_function_with_internal_preview_produces_one_snapshot_test.TestComposable")

@get:Rule
public val snapshotRule: EmergeSnapshots = EmergeSnapshots()

@Test
public fun TestComposable_GenSnapshot() {
composeRule.setContent {
SnapshotVariantProvider(previewConfig) {
TestComposable()
}
}
snapshotRule.take(composeRule, previewConfig)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ class PreviewProcessor(
return@flatMap emptyList()
}

if (function.isInternal()) {
logger.info("Skipping ${function.simpleName.asString()} as it is internal")
return@flatMap emptyList()
}

if (function.isAnnotationPresent(IgnoreEmergeSnapshot::class)) {
logger.info(
"Skipping ${function.simpleName.asString()} as it's annotated with @IgnoreEmergeSnapshot"
Expand Down

0 comments on commit e21447e

Please sign in to comment.