Skip to content

Commit

Permalink
Improve #literal section of the landing page with lessons from the …
Browse files Browse the repository at this point in the history
…quickstart.
  • Loading branch information
nedtwigg committed Jan 11, 2024
1 parent 5370842 commit aa3e37b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 32 deletions.
48 changes: 16 additions & 32 deletions docs/src/pages/jvm/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,48 @@ export const showHero = true;

<NavHeading text="literal" />

Sure, you could write your assertions like this.
This is a reasonable way to test.

```java
@Test
public void login() {
given().redirects().follow(false)
.post("/confirm/login/erjchFbCXxMlUfFXx3oYiO-Rj6zM7tRGdRV8ziqRn5Jh")
.then()
.cookie("login", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiaXNzIjoiZGlmZnBsdWcuY29tIiwiYXVkIjoiZGlmZnBsdWcuY29tIiwiaWF0IjoxNTc3NjY0MDAsImVtYWlsIjoibHVrZS5za3l3YWxrZXJAZGlmZnBsdWcuY29tIiwic2FsZXMiOiJ0cnVlIn0.v7nes7_rTa0NiwTz6OLIAmRZJ-XzzGULRiAPWBiV5iI");
public void primesBelow100() {
Assertions.assertThat(primesBelow(100)).startsWith(2, 3, 5, 7).endsWith(89, 97);
}
```

But isn't this easier to read? And also more complete?
But oftentimes a more useful way to test is actually:

```java
@Test
public void login() {
expectSelfie(
given().redirects().follow(false).post("/confirm/login/erjchFbCXxMlUfFXx3oYiO-Rj6zM7tRGdRV8ziqRn5Jh").headers()
).toBe("""
content-type=text/plain
set-cookie=flash=success=You+are+now+logged+in%21;Path=/;HttpOnly
set-cookie=loginui="{\"username\":\"testuser@diffplug.com\"}";Path=/;Max-Age=604800;Expires=Wed, 01-Jan-2000 00:00:00 GMT
set-cookie=login=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiaXNzIjoiZGlmZnBsdWcuY29tIiwiYXVkIjoiZGlmZnBsdWcuY29tIiwiaWF0IjoxNTc3NjY0MDAsImVtYWlsIjoibHVrZS5za3l3YWxrZXJAZGlmZnBsdWcuY29tIiwic2FsZXMiOiJ0cnVlIn0.v7nes7_rTa0NiwTz6OLIAmRZJ-XzzGULRiAPWBiV5iI;Path=/;HttpOnly;Max-Age=604800;Expires=Wed, 01-Jan-2000 00:00:00 GMT
location=/settings
content-length=0
""");
public void testMcTestFace() {
System.out.println(primesBelow(100));
}
```

The only reason we don't test that way already is that it's too tedious to write out that big string literal. Buf if you write this...
With literal snapshots, you can `println` directly into your testcode, combining the speed and freedom of `println` with the repeatability and team friendliness of conventional assertions.

```java
@Test
public void login() {
expectSelfie(
given().redirects().follow(false).post("/confirm/login/erjchFbCXxMlUfFXx3oYiO-Rj6zM7tRGdRV8ziqRn5Jh").headers()
).toBe_TODO()
public void primesBelow100() {
expectSelfie(primesBelow(100).toString()).toBe_TODO();
}
```

... then selfie will rewrite the string literal for you when you run your tests.
It's like a printf directly into your code. And it's not just for strings, it's for any literal.
When you run the test, selfie will automatically rewrite `_TODO()` into whatever it turned out to be.

```java
@Test
public void preventCssBloat() {
int size = expectSelfie(get("/index.css").length).toBe(5_236);
if (size > 100_000) {
Assert.fail("CSS has gotten too big! " + size);
}
public void primesBelow100() {
expectSelfie(primesBelow(100).toString())
.toBe("[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]");
}
```

And from now on it's a proper assertion, but you didn't have to spend any time writing it. It's not only less work, but also more complete than the usual `.startsWith().endsWith()` rigamarole.

<NavHeading text="like-a-filesystem" />

Some snapshots are so big that it would be cumbersome to put them inline into your test code. So selfie helps you put them on disk.
Some snapshots are so big that it would be cumbersome to put them inline into your test code, so selfie helps you put them on disk.

```java
@Test public void gzipFavicon() {
Expand Down
1 change: 1 addition & 0 deletions example-junit5/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies {
testImplementation "org.junit.jupiter:junit-jupiter:$ver_JUNIT_USE"
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
testImplementation 'io.rest-assured:rest-assured:5.4.0'
testImplementation 'org.assertj:assertj-core:3.25.1'
testImplementation project(':selfie-runner-junit5')
}
test {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.example.unrelated;

import static com.diffplug.selfie.Selfie.expectSelfie;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

public class LandingPage {
public List<Integer> primesBelow(int max) {
boolean[] isPrime = new boolean[max];
Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i < max; i++) {
if (isPrime[i]) {
for (int j = i * i; j < max; j += i) {
isPrime[j] = false;
}
}
}
List<Integer> primes = new ArrayList<>();
for (int i = 2; i < max; i++) {
if (isPrime[i]) {
primes.add(i);
}
}
return primes;
}

@Test
public void primesBelow100() {
System.out.println(primesBelow(100));
Assertions.assertThat(primesBelow(100)).startsWith(2, 3, 5, 7).endsWith(89, 97);
}

@Test
public void empty() {
expectSelfie(primesBelow(100).toString())
.toBe(
"[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]");
}
}

0 comments on commit aa3e37b

Please sign in to comment.