Skip to content

Commit 1a5647d

Browse files
committed
Add tests for Scala 3 macros
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 with 66ac4a7 ("Fixed Scala 3 macros (apache#26)"). Signed-off-by: Florian Schmaus <flo@geekplace.eu>
1 parent 27c732c commit 1a5647d

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

log4j-api-scala_3/src/main/scala/org/apache/logging/log4j/scala/LoggerMacro.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ private object LoggerMacro {
496496
}
497497

498498
/** Checks whether `message` is an interpolated string and transforms it into LOG4J string interpolation. */
499-
private def deconstructInterpolatedMessage(message: Expr[CharSequence])(using Quotes): (Expr[CharSequence], Seq[Expr[Any]]) = {
499+
private[scala] def deconstructInterpolatedMessage(message: Expr[CharSequence])(using Quotes): (Expr[CharSequence], Seq[Expr[Any]]) = {
500500
import quotes.reflect.*
501501
import util.*
502502

@@ -530,8 +530,8 @@ private object LoggerMacro {
530530
case _ => (message, Seq.empty)
531531
}
532532
}
533-
534-
private def formatArgs(args: Expr[Seq[Any]])(using q: Quotes): Seq[Expr[Object]] = {
533+
534+
private[scala] def formatArgs(args: Expr[Seq[Any]])(using q: Quotes): Seq[Expr[Object]] = {
535535
import quotes.reflect.*
536536
import util.*
537537

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache license, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the license for the specific language governing permissions and
15+
* limitations under the license.
16+
*/
17+
package org.apache.logging.log4j.scala
18+
19+
import scala.quoted.*
20+
21+
object MacroBridge:
22+
def deconstructMessageFormat(cs: Expr[CharSequence])(using Quotes): Expr[CharSequence] =
23+
val (messageFormat, args) = LoggerMacro.deconstructInterpolatedMessage(cs)
24+
messageFormat
25+
26+
def deconstructArgs(cs: Expr[CharSequence])(using Quotes): Expr[Seq[Any]] =
27+
val (messageFormat, args) = LoggerMacro.deconstructInterpolatedMessage(cs)
28+
Expr.ofSeq(args)
29+
30+
object LoggerTestMacros:
31+
32+
inline def deconstructMessageFormat(inline cs: CharSequence): CharSequence =
33+
${ MacroBridge.deconstructMessageFormat('cs) }
34+
35+
inline def deconstructArgs(inline cs: CharSequence): Seq[Any] =
36+
${ MacroBridge.deconstructArgs('cs) }
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache license, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the license for the specific language governing permissions and
15+
* limitations under the license.
16+
*/
17+
package org.apache.logging.log4j.scala
18+
19+
import scala.collection.immutable.ArraySeq
20+
21+
import org.junit.runner.RunWith
22+
import org.scalatest.funsuite.AnyFunSuite
23+
import org.scalatest.matchers.should.Matchers
24+
import org.scalatestplus.junit.JUnitRunner
25+
26+
@RunWith(classOf[JUnitRunner])
27+
object LoggerTestScala3 extends AnyFunSuite with Matchers {
28+
test("simple") {
29+
val res = LoggerTestMacros.deconstructMessageFormat("foo")
30+
res shouldEqual "foo"
31+
}
32+
33+
test("interpolated") {
34+
val emptyMap = Map.empty
35+
val message = LoggerTestMacros.deconstructMessageFormat(s"interpolated $emptyMap")
36+
message shouldEqual "interpolated {}"
37+
38+
val args = LoggerTestMacros.deconstructArgs(s"second $emptyMap")
39+
args shouldEqual ArraySeq(emptyMap)
40+
}
41+
}

0 commit comments

Comments
 (0)