|
| 1 | +/* |
| 2 | + * Copyright 2026 Typelevel |
| 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 | + * http://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 | + |
| 17 | +package org.typelevel.otel4s.sdk.testkit |
| 18 | + |
| 19 | +import cats.data.NonEmptyList |
| 20 | +import cats.syntax.show._ |
| 21 | +import org.typelevel.otel4s.Attribute |
| 22 | +import org.typelevel.otel4s.Attributes |
| 23 | + |
| 24 | +/** A partial expectation for [[Attributes]]. |
| 25 | + * |
| 26 | + * Use [[AttributesExpectation.exact]] to require the full attribute set to match or [[AttributesExpectation.subset]] |
| 27 | + * to require only a subset. |
| 28 | + */ |
| 29 | +sealed trait AttributesExpectation { |
| 30 | + |
| 31 | + /** Checks the given attributes and returns structured failures when the expectation does not match. */ |
| 32 | + def check(attributes: Attributes): Either[NonEmptyList[AttributesExpectation.Mismatch], Unit] |
| 33 | + |
| 34 | + /** Returns `true` if this expectation matches the given attributes. */ |
| 35 | + final def matches(attributes: Attributes): Boolean = |
| 36 | + check(attributes).isRight |
| 37 | +} |
| 38 | + |
| 39 | +object AttributesExpectation { |
| 40 | + |
| 41 | + /** A structured reason explaining why an [[AttributesExpectation]] did not match actual attributes. */ |
| 42 | + sealed trait Mismatch extends Product with Serializable { |
| 43 | + |
| 44 | + /** A human-readable description of the mismatch. */ |
| 45 | + def message: String |
| 46 | + } |
| 47 | + |
| 48 | + object Mismatch { |
| 49 | + |
| 50 | + /** Indicates that an expected attribute was missing. */ |
| 51 | + sealed trait MissingAttribute extends Mismatch { |
| 52 | + |
| 53 | + /** The missing attribute. */ |
| 54 | + def attribute: Attribute[_] |
| 55 | + } |
| 56 | + |
| 57 | + /** Indicates that an attribute was present unexpectedly. */ |
| 58 | + sealed trait UnexpectedAttribute extends Mismatch { |
| 59 | + |
| 60 | + /** The unexpected attribute. */ |
| 61 | + def attribute: Attribute[_] |
| 62 | + } |
| 63 | + |
| 64 | + /** Indicates that an attribute key was present, but its value differed from the expected one. */ |
| 65 | + sealed trait AttributeValueMismatch extends Mismatch { |
| 66 | + |
| 67 | + /** The expected attribute. */ |
| 68 | + def expected: Attribute[_] |
| 69 | + |
| 70 | + /** The actual attribute. */ |
| 71 | + def actual: Attribute[_] |
| 72 | + } |
| 73 | + |
| 74 | + /** Indicates that a custom predicate expectation returned `false`. */ |
| 75 | + sealed trait PredicateFailed extends Mismatch { |
| 76 | + |
| 77 | + /** An optional clue attached to the predicate. */ |
| 78 | + def clue: Option[String] |
| 79 | + } |
| 80 | + |
| 81 | + /** Creates a mismatch for a missing attribute. */ |
| 82 | + def missingAttribute(attribute: Attribute[_]): MissingAttribute = |
| 83 | + MissingAttributeImpl(attribute) |
| 84 | + |
| 85 | + /** Creates a mismatch for an unexpected attribute. */ |
| 86 | + def unexpectedAttribute(attribute: Attribute[_]): UnexpectedAttribute = |
| 87 | + UnexpectedAttributeImpl(attribute) |
| 88 | + |
| 89 | + /** Creates a mismatch for an attribute whose value differed. */ |
| 90 | + def attributeValueMismatch(expected: Attribute[_], actual: Attribute[_]): AttributeValueMismatch = |
| 91 | + AttributeValueMismatchImpl(expected, actual) |
| 92 | + |
| 93 | + /** Creates a mismatch for a failed custom predicate. */ |
| 94 | + def predicateFailed(clue: Option[String]): PredicateFailed = |
| 95 | + PredicateFailedImpl(clue) |
| 96 | + |
| 97 | + private final case class MissingAttributeImpl(attribute: Attribute[_]) extends MissingAttribute { |
| 98 | + def message: String = |
| 99 | + show"missing attribute $attribute" |
| 100 | + } |
| 101 | + |
| 102 | + private final case class UnexpectedAttributeImpl(attribute: Attribute[_]) extends UnexpectedAttribute { |
| 103 | + def message: String = |
| 104 | + show"unexpected attribute $attribute" |
| 105 | + } |
| 106 | + |
| 107 | + private final case class AttributeValueMismatchImpl(expected: Attribute[_], actual: Attribute[_]) |
| 108 | + extends AttributeValueMismatch { |
| 109 | + def message: String = |
| 110 | + show"attribute mismatch for '${expected.key.name}': expected $expected, got $actual" |
| 111 | + } |
| 112 | + |
| 113 | + private final case class PredicateFailedImpl(clue: Option[String]) extends PredicateFailed { |
| 114 | + def message: String = |
| 115 | + s"attributes predicate returned false${clue.fold("")(value => s": $value")}" |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** Creates an expectation that matches only when all attributes are equal. */ |
| 120 | + def exact(attributes: Attributes): AttributesExpectation = |
| 121 | + Exact(attributes) |
| 122 | + |
| 123 | + /** Creates an expectation that matches when all expected attributes are present in the actual set. */ |
| 124 | + def subset(attributes: Attributes): AttributesExpectation = |
| 125 | + Subset(attributes) |
| 126 | + |
| 127 | + /** Creates an expectation that matches only an empty attribute set. */ |
| 128 | + def empty: AttributesExpectation = |
| 129 | + exact(Attributes.empty) |
| 130 | + |
| 131 | + /** Creates an expectation from a custom predicate. */ |
| 132 | + def where(f: Attributes => Boolean): AttributesExpectation = |
| 133 | + Predicate(f, None) |
| 134 | + |
| 135 | + /** Creates an expectation from a custom predicate with an optional clue used in mismatch messages. */ |
| 136 | + def where(clue: String)(f: Attributes => Boolean): AttributesExpectation = |
| 137 | + Predicate(f, Some(clue)) |
| 138 | + |
| 139 | + private final case class Exact(expected: Attributes) extends AttributesExpectation { |
| 140 | + def check(attributes: Attributes): Either[NonEmptyList[Mismatch], Unit] = { |
| 141 | + val missingOrMismatched = expected.map { attribute => |
| 142 | + attributes.get(attribute.key) match { |
| 143 | + case Some(actual) if actual == attribute => |
| 144 | + ExpectationChecks.success |
| 145 | + case Some(actual) => |
| 146 | + ExpectationChecks.mismatch(Mismatch.attributeValueMismatch(attribute, actual)) |
| 147 | + case None => |
| 148 | + ExpectationChecks.mismatch(Mismatch.missingAttribute(attribute)) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + val unexpected = attributes.collect { |
| 153 | + case attribute if expected.get(attribute.key).isEmpty => |
| 154 | + Left(NonEmptyList.one(Mismatch.unexpectedAttribute(attribute))) |
| 155 | + } |
| 156 | + |
| 157 | + ExpectationChecks.combine((missingOrMismatched ++ unexpected).toList) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private final case class Subset(expected: Attributes) extends AttributesExpectation { |
| 162 | + def check(attributes: Attributes): Either[NonEmptyList[Mismatch], Unit] = |
| 163 | + ExpectationChecks.combine(expected.map { attribute => |
| 164 | + attributes.get(attribute.key) match { |
| 165 | + case Some(actual) if actual == attribute => |
| 166 | + ExpectationChecks.success |
| 167 | + case Some(actual) => |
| 168 | + ExpectationChecks.mismatch(Mismatch.attributeValueMismatch(attribute, actual)) |
| 169 | + case None => |
| 170 | + ExpectationChecks.mismatch(Mismatch.missingAttribute(attribute)) |
| 171 | + } |
| 172 | + }.toList) |
| 173 | + } |
| 174 | + |
| 175 | + private final case class Predicate( |
| 176 | + f: Attributes => Boolean, |
| 177 | + clue: Option[String] |
| 178 | + ) extends AttributesExpectation { |
| 179 | + def check(attributes: Attributes): Either[NonEmptyList[Mismatch], Unit] = |
| 180 | + Either.cond(f(attributes), (), NonEmptyList.one(Mismatch.predicateFailed(clue))) |
| 181 | + } |
| 182 | +} |
0 commit comments