Copilot commented on code in PR #6822:
URL: https://github.com/apache/texera/pull/6822#discussion_r3636038297


##########
common/pybuilder/src/test/scala/org/apache/texera/amber/pybuilder/EncodableInspectorSpec.scala:
##########
@@ -0,0 +1,192 @@
+/*
+ * 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.texera.amber.pybuilder
+
+import org.scalatest.funsuite.AnyFunSuite
+
+import scala.reflect.runtime.currentMirror
+import scala.tools.reflect.{ToolBox, ToolBoxError}
+
+/**
+  * Runtime coverage for `EncodableInspector`.
+  *
+  * `EncodableInspector` only executes while the `pyb"..."` macro expands. In 
an ordinary build
+  * that expansion happens before Jacoco is attached, so none of its 
classification logic is
+  * observed at runtime. To exercise it inside the instrumented JVM we drive 
real macro expansions
+  * through a runtime `ToolBox`.
+  *
+  * We cannot `eval` a `pyb` snippet: the macro expands to a call to the 
`private[amber]`
+  * `PythonTemplateBuilder.fromInterpolated`, which the ToolBox's synthetic 
`__wrapper` package
+  * cannot access, so evaluation always fails on that access check. 
Compilation is still useful,
+  * though, because the macro fully expands (running the whole inspector) 
*before* that
+  * post-typecheck access error surfaces. That gives two observable outcomes 
we assert on:
+  *
+  *   - '''boundary abort''': the inspector classified a *direct* argument as 
Encodable, so
+  *     `BoundaryValidator.validateCompileTime` ran and aborted with a 
`@EncodableStringAnnotation`
+  *     message (only possible when the arg was deemed Encodable and the 
context is unsafe);
+  *   - '''benign expansion''': no boundary abort fired and the only failure 
is the private-access
+  *     error on `fromInterpolated`, i.e. the inspector deemed the arg a plain 
literal (or a nested
+  *     builder), the macro finished expanding, and lowering/`wrapArg` ran.
+  *
+  * Placing an Encodable-marked value next to a "bad neighbor" therefore 
proves it was classified
+  * Encodable; placing a plain value there proves it was not.
+  */
+class EncodableInspectorSpec extends AnyFunSuite {
+
+  private lazy val tb: ToolBox[scala.reflect.runtime.universe.type] = 
currentMirror.mkToolBox()
+
+  private val header =
+    """import org.apache.texera.amber.pybuilder.PythonTemplateBuilder._
+      |import org.apache.texera.amber.pybuilder.PyStringTypes._
+      |import 
org.apache.texera.amber.pybuilder.EncodableStringAnnotation""".stripMargin
+
+  /** Marker present in every `BoundaryValidator` compile-time abort message. 
*/
+  private val boundaryMarker = "@EncodableStringAnnotation argument #"
+
+  /** Compile a self-contained `pyb` snippet; it always fails, so capture the 
ToolBox message. */
+  private def macroError(body: String): String =
+    intercept[ToolBoxError] {
+      tb.compile(tb.parse(s"{\n$header\n$body\n}"))
+    }.getMessage
+
+  /** Assert the arg was classified Encodable: an unsafe splice triggers a 
boundary abort. */
+  private def assertClassifiedEncodable(body: String): Unit = {
+    val msg = macroError(body)
+    assert(msg.contains(boundaryMarker), s"expected an Encodable boundary 
abort, got: $msg")
+  }
+
+  /** Assert the arg was NOT classified Encodable: the macro expands, only 
fromInterpolated fails. */
+  private def assertNotClassifiedEncodable(body: String): Unit = {

Review Comment:
   The Scaladoc for `assertNotClassifiedEncodable` is inaccurate: it says the 
arg was “NOT classified Encodable”, but the helper is also used by the “safe 
Encodable splice…” test (where the arg *is* Encodable, just not aborting). This 
can mislead future readers about what the assertion actually guarantees.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to