Skip to content

Commit

Permalink
Create a new snippets project for Java (robolectric#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 authored Nov 2, 2024
1 parent 8883bc3 commit 2e7d7ba
Show file tree
Hide file tree
Showing 36 changed files with 858 additions and 13 deletions.
13 changes: 13 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ updates:
directory: "/"
schedule:
interval: "weekly"
groups:
actions:
patterns:
- "*"

- package-ecosystem: "gradle"
directory: "/snippets"
schedule:
interval: "weekly"
groups:
gradle:
patterns:
- "*"

- package-ecosystem: "pip"
directory: "/"
Expand Down
42 changes: 42 additions & 0 deletions .github/workflows/validate-snippets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: "Validate snippets"

on:
merge_group:
pull_request:
push:
branches: [ master ]

jobs:
validate_snippets:
name: "Validate snippets"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
sparse-checkout: |
snippets
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
distribution: 'adopt'
java-version: 17

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4

- name: Check codestyle
uses: axel-op/googlejavaformat-action@v3
with:
args: "--replace"
files: snippets/**/*.java
skip-commit: true

- name: Print codestyle issues
run: git --no-pager diff --exit-code

- name: Build
run: |
cd snippets
./gradlew check
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,35 @@ Then you can execute the following command to access the documentation locally a
mkdocs serve --open
```

### Using code snippets

The code snippets displayed throughout the website are stored in the [`snippets`](snippets) Android
project.

To use a new code snippet, follow these steps:

1. Define your code snippet in the `snippets/java` module.
2. Surround it with

```java
// --8<-- [start:my_code_snippet_identifier]
my code snippet
// --8<-- [end:my_code_snippet_identifier]
```

3. To use it in your Markdown file, use the following syntax:

````markdown
```java
--8<-- "snippets/java/path/to/my/snippet/MyCodeSnippet.java:my_code_snippet_identifier"
```
````

> [!NOTE]
>
> The migration of the code snippets to the `snippets` project is a work in progress.
> New code snippets **should** be added in the `snippets` project.
### Validate your Markdown files

If you modified any Markdown file, we recommend using [`DavidAnson/markdownlint-cli2`](https://github.com/DavidAnson/markdownlint-cli2) to ensure that the formatting rules are respected.
Expand Down
14 changes: 1 addition & 13 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,7 @@ Tests run inside the JVM in seconds. With Robolectric you can write tests like t
=== "Java"

```java
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {
@Test
public void clickingButton_shouldChangeMessage() {
try (ActivityController<MyActivity> controller = Robolectric.buildActivity(MyActivity.class)) {
controller.setup(); // Moves the Activity to the RESUMED state
MyActivity activity = controller.get();

activity.findViewById(R.id.button).performClick();
assertEquals(((TextView) activity.findViewById(R.id.text)).getText(), "Robolectric Rocks!");
}
}
}
--8<-- "snippets/java/src/test/java/org/robolectric/snippets/java/MyActivityTest.java:index_sample_test"
```

=== "Kotlin"
Expand Down
15 changes: 15 additions & 0 deletions snippets/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
4 changes: 4 additions & 0 deletions snippets/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
alias(libs.plugins.android.application) apply false
}
22 changes: 22 additions & 0 deletions snippets/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. For more details, visit
# https://developer.android.com/r/tools/gradle-multi-project-decoupled-projects
org.gradle.parallel=true

# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true

# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
11 changes: 11 additions & 0 deletions snippets/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[versions]
android-gradle-plugin = "8.7.2"
junit = "4.13.2"
robolectric = "4.13"

[libraries]
junit = { group = "junit", name = "junit", version.ref = "junit" }
robolectric = { group = "org.robolectric", name = "robolectric", version.ref = "robolectric" }

[plugins]
android-application = { id = "com.android.application", version.ref = "android-gradle-plugin" }
Binary file added snippets/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 7 additions & 0 deletions snippets/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 2e7d7ba

Please sign in to comment.