This is an automated email from the ASF dual-hosted git repository. vy pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/logging-log4j-scala.git
The following commit(s) were added to refs/heads/main by this push: new 91396bf Add tests for Scala 3 macros (#35) 91396bf is described below commit 91396bf80e119851cf6090e7c208d46fcef9f4a4 Author: Florian Schmaus <f...@geekplace.eu> AuthorDate: Mon Nov 6 21:19:41 2023 +0100 Add tests for Scala 3 macros (#35) This adds a few simple and short tests for the Scala 3 macros. Especially for the central `deconstructMessageFormat()` macro, which was previously broken and fixed in #26. Signed-off-by: Florian Schmaus <f...@geekplace.eu> --- .../apache/logging/log4j/scala/LoggerMacro.scala | 6 ++-- .../logging/log4j/scala/LoggerTestMacros.scala | 36 +++++++++++++++++++ .../logging/log4j/scala/LoggerTestScala3.scala | 41 ++++++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/log4j-api-scala_3/src/main/scala/org/apache/logging/log4j/scala/LoggerMacro.scala b/log4j-api-scala_3/src/main/scala/org/apache/logging/log4j/scala/LoggerMacro.scala index 1e050e5..a240e46 100644 --- a/log4j-api-scala_3/src/main/scala/org/apache/logging/log4j/scala/LoggerMacro.scala +++ b/log4j-api-scala_3/src/main/scala/org/apache/logging/log4j/scala/LoggerMacro.scala @@ -496,7 +496,7 @@ private object LoggerMacro { } /** Checks whether `message` is an interpolated string and transforms it into LOG4J string interpolation. */ - private def deconstructInterpolatedMessage(message: Expr[CharSequence])(using Quotes): (Expr[CharSequence], Seq[Expr[Any]]) = { + private[scala] def deconstructInterpolatedMessage(message: Expr[CharSequence])(using Quotes): (Expr[CharSequence], Seq[Expr[Any]]) = { import quotes.reflect.* import util.* @@ -530,8 +530,8 @@ private object LoggerMacro { case _ => (message, Seq.empty) } } - - private def formatArgs(args: Expr[Seq[Any]])(using q: Quotes): Seq[Expr[Object]] = { + + private[scala] def formatArgs(args: Expr[Seq[Any]])(using q: Quotes): Seq[Expr[Object]] = { import quotes.reflect.* import util.* diff --git a/log4j-api-scala_3/src/test/scala/org/apache/logging/log4j/scala/LoggerTestMacros.scala b/log4j-api-scala_3/src/test/scala/org/apache/logging/log4j/scala/LoggerTestMacros.scala new file mode 100644 index 0000000..12bee08 --- /dev/null +++ b/log4j-api-scala_3/src/test/scala/org/apache/logging/log4j/scala/LoggerTestMacros.scala @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.scala + +import scala.quoted.* + +object MacroBridge: + def deconstructMessageFormat(cs: Expr[CharSequence])(using Quotes): Expr[CharSequence] = + val (messageFormat, args) = LoggerMacro.deconstructInterpolatedMessage(cs) + messageFormat + + def deconstructArgs(cs: Expr[CharSequence])(using Quotes): Expr[Seq[Any]] = + val (messageFormat, args) = LoggerMacro.deconstructInterpolatedMessage(cs) + Expr.ofSeq(args) + +object LoggerTestMacros: + + inline def deconstructMessageFormat(inline cs: CharSequence): CharSequence = + ${ MacroBridge.deconstructMessageFormat('cs) } + + inline def deconstructArgs(inline cs: CharSequence): Seq[Any] = + ${ MacroBridge.deconstructArgs('cs) } diff --git a/log4j-api-scala_3/src/test/scala/org/apache/logging/log4j/scala/LoggerTestScala3.scala b/log4j-api-scala_3/src/test/scala/org/apache/logging/log4j/scala/LoggerTestScala3.scala new file mode 100644 index 0000000..d2d1314 --- /dev/null +++ b/log4j-api-scala_3/src/test/scala/org/apache/logging/log4j/scala/LoggerTestScala3.scala @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.scala + +import scala.collection.immutable.ArraySeq + +import org.junit.runner.RunWith +import org.scalatest.funsuite.AnyFunSuite +import org.scalatest.matchers.should.Matchers +import org.scalatestplus.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +object LoggerTestScala3 extends AnyFunSuite with Matchers { + test("simple") { + val res = LoggerTestMacros.deconstructMessageFormat("foo") + res shouldEqual "foo" + } + + test("interpolated") { + val emptyMap = Map.empty + val message = LoggerTestMacros.deconstructMessageFormat(s"interpolated $emptyMap") + message shouldEqual "interpolated {}" + + val args = LoggerTestMacros.deconstructArgs(s"second $emptyMap") + args shouldEqual ArraySeq(emptyMap) + } +}