Skip to content

Commit

Permalink
emptyness check for texttree
Browse files Browse the repository at this point in the history
  • Loading branch information
pshirshov committed Mar 14, 2024
1 parent 4867f4c commit 80ed8ba
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ object TextTree {
}
}

def isEmpty: Boolean = {
target match {
case _: ValueNode[T] => false
case s: StringNode => s.value.isEmpty
case s: Shift[T] => s.nested.isEmpty
case t: Trim[T] => t.nested.isEmpty
case n: Node[T] =>
n.chunks.forall(_.isEmpty)
}
}

def nonEmpty: Boolean = !isEmpty

def flatten: TextTree[T] = {
target match {
case v: ValueNode[T] => Node(NEList(v))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ class TextTreeTest extends AnyWordSpec {
accept(t5)
}
}

"support emptiness check" in {
val q1 = q""
val q2 = q"$q1"
val q3 = q"x$q1"

assert(q1.isEmpty)
assert(q2.isEmpty)
assert(q3.nonEmpty)

}
}
}

Expand Down

0 comments on commit 80ed8ba

Please sign in to comment.