Skip to content

Commit

Permalink
Don't mutate xml literals (#931)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugo-vrijswijk authored Aug 11, 2021
1 parent f3871c0 commit f30953b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ case object EmptyString extends StringLiteral[Lit.String] {
override val tree: Lit.String = Lit.String("")

override def unapply(arg: Lit.String): Option[Lit.String] =
super.unapply(arg).filterNot(ParentIsInterpolatedString(_))
super
.unapply(arg)
.filterNot(ParentIsInterpolatedString(_))
.filterNot(IsXmlLiteral(_))
}

case object StrykerWasHereString extends StringLiteral[Lit.String] {
Expand All @@ -21,6 +24,7 @@ case object NonEmptyString extends NoInvalidPlacement[Lit.String] {
.unapply(arg)
.filter(_.value.nonEmpty)
.filterNot(ParentIsInterpolatedString(_))
.filterNot(IsXmlLiteral(_))
}

/** Not a mutation, just an extractor for pattern matching on interpolated strings
Expand All @@ -41,3 +45,13 @@ private object ParentIsInterpolatedString {
case _ => false
}
}

private object IsXmlLiteral {
def apply(arg: Lit.String): Boolean =
arg.parent match {
// Do not mutate XML literal strings
case Some(Term.Xml(parts, _)) if parts.contains(arg) => true
case Some(Pat.Xml(parts, _)) if parts.contains(arg) => true
case _ => false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,47 @@ class MutantMatcherTest extends Stryker4sSuite {
Lit.String("")
)
}

it("should not match xml literals") {
val tree = source"""class Foo {
def bar = ${Term.Xml(List(Lit.String("<foo>"), Lit.String("</foo>")), List(q"foo"))}
}"""

val result = tree collect sut.allMatchers

result should be(empty)
}

it("should not match empty strings on xml literals") {
val tree = source"""class Foo {
def bar = ${Term.Xml(List(Lit.String("<foo>"), Lit.String("")), List(q"foo"))}
}"""

val result = tree collect sut.allMatchers

result should be(empty)
}

it("should match inside xml literal args") {
val str = Lit.String("str")
val tree = source"""class Foo {
def bar = ${Term.Xml(List(Lit.String("<foo>"), Lit.String("</foo>")), List(str))}
}"""
expectMutations(
sut.matchStringLiteral,
tree,
str,
Lit.String("")
)
}

it("should not match xml interpolation") {
val tree = Pat.Xml(List(Lit.String("<foo></xml>")), List.empty)

val result = tree collect sut.allMatchers

result should be(empty)
}
}

describe("regexMutator") {
Expand Down

0 comments on commit f30953b

Please sign in to comment.