|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.selfie |
| 17 | + |
| 18 | +/** Given a full [Snapshot], a lens returns either null or a single [SnapshotValue]. */ |
| 19 | +@OptIn(ExperimentalStdlibApi::class) |
| 20 | +interface SnapshotLens : AutoCloseable { |
| 21 | + val defaultLensName: String |
| 22 | + fun transform(testClass: String, key: String, snapshot: Snapshot): SnapshotValue? |
| 23 | + override fun close() {} |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * A prism transforms a single [Snapshot] into a new [Snapshot], transforming / creating / removing |
| 28 | + * [SnapshotValue]s along the way. |
| 29 | + */ |
| 30 | +@OptIn(ExperimentalStdlibApi::class) |
| 31 | +interface SnapshotPrism : AutoCloseable { |
| 32 | + fun transform(className: String, key: String, snapshot: Snapshot): Snapshot |
| 33 | + override fun close() {} |
| 34 | +} |
| 35 | +fun interface SnapshotPredicate { |
| 36 | + fun test(testClass: String, key: String, snapshot: Snapshot): Boolean |
| 37 | +} |
| 38 | + |
| 39 | +/** A prism with a fluent API for creating [LensHoldingPrism]s gated by predicates. */ |
| 40 | +open class CompoundPrism : SnapshotPrism { |
| 41 | + private val prisms = mutableListOf<SnapshotPrism>() |
| 42 | + fun add(prism: SnapshotPrism): CompoundPrism { |
| 43 | + prisms.add(prism) |
| 44 | + return this |
| 45 | + } |
| 46 | + fun ifClassKeySnapshot(predicate: SnapshotPredicate): LensHoldingPrism { |
| 47 | + val prismWhere = LensHoldingPrism(predicate) |
| 48 | + add(prismWhere) |
| 49 | + return prismWhere |
| 50 | + } |
| 51 | + fun ifSnapshot(predicate: (Snapshot) -> Boolean) = ifClassKeySnapshot { _, _, snapshot -> |
| 52 | + predicate(snapshot) |
| 53 | + } |
| 54 | + fun forEverySnapshot(): LensHoldingPrism = ifSnapshot { true } |
| 55 | + fun ifString(predicate: (String) -> Boolean) = ifSnapshot { |
| 56 | + !it.value.isBinary && predicate(it.value.valueString()) |
| 57 | + } |
| 58 | + fun ifStringIsProbablyHtml(): LensHoldingPrism { |
| 59 | + val regex = Regex("<\\/?[a-z][\\s\\S]*>") |
| 60 | + return ifString { regex.find(it) != null } |
| 61 | + } |
| 62 | + override fun transform(className: String, key: String, snapshot: Snapshot): Snapshot { |
| 63 | + var current = snapshot |
| 64 | + prisms.forEach { current = it.transform(className, key, current) } |
| 65 | + return current |
| 66 | + } |
| 67 | + override fun close() = prisms.forEach(SnapshotPrism::close) |
| 68 | +} |
| 69 | + |
| 70 | +/** A prism which applies lenses to a snapshot. */ |
| 71 | +open class LensHoldingPrism(val predicate: SnapshotPredicate) : SnapshotPrism { |
| 72 | + private val lenses = mutableListOf<SnapshotPrism>() |
| 73 | + private fun addLensOrReplaceRoot(name: String?, lens: SnapshotLens): LensHoldingPrism { |
| 74 | + lenses.add( |
| 75 | + object : SnapshotPrism { |
| 76 | + override fun transform(testClass: String, key: String, snapshot: Snapshot): Snapshot { |
| 77 | + val lensValue = lens.transform(testClass, key, snapshot) |
| 78 | + return if (lensValue == null) snapshot |
| 79 | + else { |
| 80 | + if (name == null) snapshot.withNewRoot(lensValue) else snapshot.lens(name, lensValue) |
| 81 | + } |
| 82 | + } |
| 83 | + override fun close() = lens.close() |
| 84 | + }) |
| 85 | + return this |
| 86 | + } |
| 87 | + fun addLens(name: String, lens: SnapshotLens): LensHoldingPrism = addLensOrReplaceRoot(name, lens) |
| 88 | + fun addLens(lens: SnapshotLens): LensHoldingPrism = |
| 89 | + addLensOrReplaceRoot(lens.defaultLensName, lens) |
| 90 | + fun replaceRootWith(lens: SnapshotLens): LensHoldingPrism = addLensOrReplaceRoot(null, lens) |
| 91 | + override fun transform(testClass: String, key: String, snapshot: Snapshot): Snapshot { |
| 92 | + if (!predicate.test(testClass, key, snapshot)) { |
| 93 | + return snapshot |
| 94 | + } |
| 95 | + var current = snapshot |
| 96 | + lenses.forEach { current = it.transform(testClass, key, snapshot) } |
| 97 | + return current |
| 98 | + } |
| 99 | + override fun close() { |
| 100 | + lenses.forEach(SnapshotPrism::close) |
| 101 | + } |
| 102 | +} |
0 commit comments