Skip to content

Commit a0aaadc

Browse files
authored
Rename to selfie (#7)
2 parents d92efc9 + a4ba0f3 commit a0aaadc

File tree

26 files changed

+273
-94
lines changed

26 files changed

+273
-94
lines changed

README.md

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
# spotless-snapshot
1+
# Selfie
22

3-
- Precise snapshot testing for the JVM and Javascript (Native TODO)
3+
- Precise snapshot testing for the JVM and Javascript (others TBD)
44
- Supports text and binary data.
55
- Friendly to humans and version control.
66
- In-place [literal snapshots](#literal-snapshots).
77
- Allows [lenses](#lenses) to verify multiple aspects of an object under test.
88
- e.g. pretty-printed JSON, or only the rendered plaintext of an HTML document, etc.
99

10-
The example below uses the JUnit 5 runner, but we also support TODO.
11-
12-
Here is a very simple test which snapshots the HTML served at various URLs.
10+
ere is a very simple test which snapshots the HTML served at various URLs.
1311

1412
```java
15-
@Test public void gzipFavicon(Expect expect) {
16-
expect.toMatchBinarySnapshot(get("localhost:8080/favicon.ico", ContentEncoding.GZIP));
13+
@Test public void gzipFavicon() {
14+
expectSelfie(get("/favicon.ico", ContentEncoding.GZIP)).toMatchDisk();
1715
}
18-
@Test public void orderFlow(Expect expect) {
19-
expect.scenario("initial").toMatchSnapshot(get("localhost:8080/orders"));
16+
@Test public void orderFlow() {
17+
expectSelfie(get("/orders")).toMatchDisk("initial");
2018
postOrder();
21-
expect.scenario("ordered").toMatchSnapshot(get("localhost:8080/orders"));
19+
expectSelfie(get("/orders")).toMatchDisk("ordered");
2220
}
2321
```
2422

@@ -43,12 +41,12 @@ H4sIAAAAAAAA/8pIzcnJVyjPL8pJUQQAlQYXAAAA
4341

4442
### Literal snapshots
4543

46-
A great thing about snapshots is that they are fast to write and update. A downside is that the asserted value is opaque. But it doesn't have to be! Just swap `toMatchSnapshot` for `toMatchLiteral`.
44+
A great thing about snapshots is that they are fast to write and update. A downside is that the asserted value is opaque. But it doesn't have to be! Just swap `toMatchDisk` for `toBe`.
4745

4846
```java
49-
@Test public void preventCssBloat(Expect expect) {
50-
// spotless-snapshot can update this literal value for you ▼
51-
int size = expect.toMatchLiteral(get("/index.css").length, 5_236);
47+
@Test public void preventCssBloat() {
48+
// selfie can update this literal value for you ▼
49+
int size = expectSelfie(get("/index.css").length).toBe(5_236);
5250
if (size > 100_000) {
5351
Assert.fail("CSS has gotten too big!");
5452
}
@@ -62,7 +60,7 @@ Now we can see at a glance how a PR has affected the bundled size of our CSS. We
6260
When snapshotting HTML, you might want to look at only the rendered text, ignoring tags, classes, and all that.
6361

6462
```java
65-
public SnapshotConfig extends SpotlessSnapshotConfig {
63+
public SelfieConfig extends com.diffplug.selfie.SelfieConfig {
6664
@Override public @Nullable Snapshot intercept(Class<?> className, String testName, Snapshot snapshot) {
6765
if (!snapshot.getValue().isBinary()) {
6866
String content = snapshot.getValue().valueString();
@@ -92,9 +90,9 @@ Order information
9290
Tracking #ABC123
9391
```
9492

95-
Lenses can make PRs easier to review, by putting the focus on various aspects of the snapshot that are relevant to the change.
93+
Lenses can make PRs easier to review, by putting the focus on whichever aspect of the snapshot is relevant to the change.
9694

9795
### Acknowledgements
9896

99-
- JUnit test runner heavily inspired by [origin-energy's java-snapshot-testing](https://github.com/origin-energy/java-snapshot-testing).
97+
- Heavily inspired by [origin-energy's java-snapshot-testing](https://github.com/origin-energy/java-snapshot-testing).
10098
- Which in turn is heavily inspired by [Facebook's jest-snapshot](https://jestjs.io/docs/snapshot-testing).

gradle/spotless.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ spotless {
1616
target 'src/**/*.kt'
1717
licenseHeaderFile 干.file("spotless/license-${license}.java")
1818
ktfmt()
19-
for (modifier in ['', 'override ', 'public ', 'protected ', 'private ', 'internal ', 'expected ', 'actual ']) {
19+
for (modifier in ['', 'override ', 'public ', 'protected ', 'private ', 'internal ', 'infix ', 'expected ', 'actual ']) {
2020
for (key in ['inline', 'fun', 'val', 'override']) {
2121
String toCheck = "$modifier$key"
2222
replaceRegex("dense $toCheck", "\n\n(\\s*)$toCheck ", "\n\$1$toCheck ")
File renamed without changes.

snapshot-lib/src/commonMain/kotlin/com/diffplug/snapshot/ArrayMap.kt renamed to selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/ArrayMap.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.diffplug.snapshot
16+
package com.diffplug.selfie
1717

1818
import kotlin.collections.binarySearch
1919

snapshot-lib/src/commonMain/kotlin/com/diffplug/snapshot/PerCharacterEscaper.kt renamed to selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/PerCharacterEscaper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.diffplug.snapshot
16+
package com.diffplug.selfie
1717

1818
expect class PerCharacterEscaper {
1919
fun escape(input: String): String

snapshot-lib/src/commonMain/kotlin/com/diffplug/snapshot/SpotlessFile.kt renamed to selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/SnapshotFile.kt

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.diffplug.snapshot
16+
package com.diffplug.selfie
1717

1818
class ParseException(val line: Int, message: String) : IllegalArgumentException(message) {
1919
constructor(lineReader: LineReader, message: String) : this(lineReader.getLineNumber(), message)
@@ -47,11 +47,6 @@ internal data class SnapshotValueString(val value: String) : SnapshotValue {
4747
override fun valueString(): String = value
4848
}
4949

50-
internal data class SnapshotValueEmptyString(val value: String) : SnapshotValue {
51-
override fun valueBinary() = throw UnsupportedOperationException("This is an empty string value.")
52-
override fun valueString() = value
53-
}
54-
5550
data class Snapshot(
5651
val value: SnapshotValue,
5752
private val lensData: ArrayMap<String, SnapshotValue>
@@ -73,8 +68,31 @@ data class Snapshot(
7368
interface Snapshotter<T> {
7469
fun snapshot(value: T): Snapshot
7570
}
76-
fun parseSS(valueReader: SnapshotValueReader): ArrayMap<String, Snapshot> = TODO()
77-
fun serializeSS(valueWriter: StringWriter, snapshots: ArrayMap<String, Snapshot>): Unit = TODO()
71+
72+
class SnapshotFile {
73+
// this will probably become `<String, JsonObject>` we'll cross that bridge when we get to it
74+
var metadata: Map.Entry<String, String>? = null
75+
var snapshots = ArrayMap.empty<String, Snapshot>()
76+
fun serialize(valueWriter: StringWriter): Unit = TODO()
77+
78+
companion object {
79+
private val HEADER_PREFIX = """📷 """
80+
fun parse(valueReader: SnapshotValueReader): SnapshotFile {
81+
val result = SnapshotFile()
82+
val reader = SnapshotReader(valueReader)
83+
// only if the first value starts with 📷
84+
if (reader.peekKey()?.startsWith(HEADER_PREFIX) == true) {
85+
val metadataName = reader.peekKey()!!.substring(HEADER_PREFIX.length)
86+
val metadataValue = reader.valueReader.nextValue().valueString()
87+
result.metadata = entry(metadataName, metadataValue)
88+
}
89+
while (reader.peekKey() != null) {
90+
result.snapshots = result.snapshots.plus(reader.peekKey()!!, reader.nextSnapshot())
91+
}
92+
return result
93+
}
94+
}
95+
}
7896

7997
class SnapshotReader(val valueReader: SnapshotValueReader) {
8098
fun peekKey(): String? = TODO()
@@ -176,11 +194,11 @@ class SnapshotValueReader(val lineReader: LineReader) {
176194
private val headerEnd = " ═╗"
177195

178196
/**
179-
* https://github.com/diffplug/spotless-snapshot/blob/f63192a84390901a3d3543066d095ea23bf81d21/snapshot-lib/src/commonTest/resources/com/diffplug/snapshot/scenarios_and_lenses.ss#L11-L29
197+
* https://github.com/diffplug/selfie/blob/f63192a84390901a3d3543066d095ea23bf81d21/snapshot-lib/src/commonTest/resources/com/diffplug/snapshot/scenarios_and_lenses.ss#L11-L29
180198
*/
181199
private val nameEsc = PerCharacterEscaper.specifiedEscape("\\\\/∕[(])\nn\tt╔┌╗┐═─")
182200

183-
/** https://github.com/diffplug/spotless-snapshot/issues/2 */
201+
/** https://github.com/diffplug/selfie/issues/2 */
184202
private val bodyEsc = PerCharacterEscaper.selfEscape("\uD801\uDF43\uD801\uDF41")
185203
}
186204
}

snapshot-lib/src/commonTest/kotlin/com/diffplug/snapshot/ArrayMapTest.kt renamed to selfie-lib/src/commonTest/kotlin/com/diffplug/selfie/ArrayMapTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.diffplug.snapshot
16+
package com.diffplug.selfie
1717

1818
import io.kotest.assertions.throwables.shouldThrow
1919
import io.kotest.matchers.shouldBe

snapshot-lib/src/jsMain/kotlin/com/diffplug/snapshot/ArrayMap.js.kt renamed to selfie-lib/src/jsMain/kotlin/com/diffplug/selfie/ArrayMap.js.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.diffplug.snapshot
16+
package com.diffplug.selfie
1717

1818
internal actual fun <K, V> entry(key: K, value: V): Map.Entry<K, V> = E(key, value)
1919

0 commit comments

Comments
 (0)