|
16 | 16 | package org.scalatest
|
17 | 17 |
|
18 | 18 | import org.scalactic._
|
| 19 | +import org.scalatest.words.{TypeCheckWord, CompileWord} |
19 | 20 | import org.scalatest.exceptions._
|
20 | 21 |
|
21 | 22 | import scala.quoted._
|
@@ -157,4 +158,161 @@ object CompileMacro {
|
157 | 158 | )($prettifier)
|
158 | 159 | }
|
159 | 160 | }
|
| 161 | + |
| 162 | + // check that a code snippet does not compile |
| 163 | + def assertNotCompileImpl[T](self: Expr[T], compileWord: Expr[CompileWord], pos: Expr[source.Position])(shouldOrMust: String)(implicit qctx: QuoteContext): Expr[Assertion] = { |
| 164 | + import qctx.tasty.{_, given} |
| 165 | + |
| 166 | + // parse and type check a code snippet, generate code to throw TestFailedException if both parse and type check succeeded |
| 167 | + def checkNotCompile(code: String): Expr[Assertion] = |
| 168 | + if (/*!typeChecks(code)*/ true) '{ Succeeded } // FIXME |
| 169 | + else '{ |
| 170 | + val messageExpr = Resources.expectedCompileErrorButGotNone(${ Expr(code) }) |
| 171 | + throw new TestFailedException((_: StackDepthException) => Some(messageExpr), None, $pos) |
| 172 | + } |
| 173 | + |
| 174 | + self.unseal.underlyingArgument match { |
| 175 | + |
| 176 | + case Apply( |
| 177 | + Apply( |
| 178 | + Select(_, shouldOrMustTerconvertToStringShouldOrMustWrapperTermName), |
| 179 | + List( |
| 180 | + Literal(Constant(code: String)) |
| 181 | + ) |
| 182 | + ), |
| 183 | + _ |
| 184 | + ) if shouldOrMustTerconvertToStringShouldOrMustWrapperTermName == "convertToString" + shouldOrMust.capitalize + "Wrapper" => |
| 185 | + // LHS is a normal string literal, call checkCompile with the extracted code string to generate code |
| 186 | + checkNotCompile(code) |
| 187 | + |
| 188 | + case Apply( |
| 189 | + Apply( |
| 190 | + Ident(shouldOrMustTerconvertToStringShouldOrMustWrapperTermName), |
| 191 | + List( |
| 192 | + Literal(Constant(code: String)) |
| 193 | + ) |
| 194 | + ), |
| 195 | + _ |
| 196 | + ) if shouldOrMustTerconvertToStringShouldOrMustWrapperTermName == "convertToString" + shouldOrMust.capitalize + "Wrapper" => |
| 197 | + checkNotCompile(code) |
| 198 | + |
| 199 | + case other => |
| 200 | + qctx.error("The '" + shouldOrMust + " compile' syntax only works with String literals.") |
| 201 | + '{???} |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + // used by shouldNot compile syntax, delegate to assertNotCompileImpl to generate code |
| 206 | + def shouldNotCompileImpl(self: Expr[Matchers#AnyShouldWrapper[_]], compileWord: Expr[CompileWord])(pos: Expr[source.Position])(implicit qctx: QuoteContext): Expr[Assertion] = |
| 207 | + assertNotCompileImpl(self, compileWord, pos)("should") |
| 208 | + |
| 209 | + // used by mustNot compile syntax, delegate to assertNotCompileImpl to generate code |
| 210 | + def mustNotCompileImpl(self: Expr[MustMatchers#AnyMustWrapper[_]], compileWord: Expr[CompileWord])(pos: Expr[source.Position])(implicit qctx: QuoteContext): Expr[Assertion] = |
| 211 | + assertNotCompileImpl(self, compileWord, pos)("must") |
| 212 | + |
| 213 | + // check that a code snippet does not compile |
| 214 | + def assertNotTypeCheckImpl(self: Expr[Matchers#AnyShouldWrapper[_]], typeCheckWord: Expr[TypeCheckWord], pos: Expr[source.Position])(shouldOrMust: String)(implicit qctx: QuoteContext): Expr[Assertion] = { |
| 215 | + import qctx.tasty.{_, given} |
| 216 | + |
| 217 | + // parse and type check a code snippet, generate code to throw TestFailedException if both parse and type check succeeded |
| 218 | + def checkNotTypeCheck(code: String): Expr[Assertion] = |
| 219 | + if (/*!typeChecks(code)*/ true) '{ Succeeded } // FIXME |
| 220 | + else '{ |
| 221 | + val messageExpr = Resources.expectedTypeErrorButGotNone(${ Expr(code) }) |
| 222 | + throw new TestFailedException((_: StackDepthException) => Some(messageExpr), None, $pos) |
| 223 | + } |
| 224 | + |
| 225 | + val methodName = shouldOrMust + "Not" |
| 226 | + |
| 227 | + self.unseal.underlyingArgument match { |
| 228 | + case Apply( |
| 229 | + Apply( |
| 230 | + Select(_, shouldOrMustTerconvertToStringShouldOrMustWrapperTermName), |
| 231 | + List( |
| 232 | + Literal(code) |
| 233 | + ) |
| 234 | + ), |
| 235 | + _ |
| 236 | + ) if shouldOrMustTerconvertToStringShouldOrMustWrapperTermName == "convertToString" + shouldOrMust.capitalize + "Wrapper" => |
| 237 | + // LHS is a normal string literal, call checkNotTypeCheck with the extracted code string to generate code |
| 238 | + checkNotTypeCheck(code.toString) |
| 239 | + |
| 240 | + case Apply( |
| 241 | + Apply( |
| 242 | + Ident(shouldOrMustTerconvertToStringShouldOrMustWrapperTermName), |
| 243 | + List( |
| 244 | + Literal(Constant(code: String)) |
| 245 | + ) |
| 246 | + ), |
| 247 | + _ |
| 248 | + ) if shouldOrMustTerconvertToStringShouldOrMustWrapperTermName == "convertToString" + shouldOrMust.capitalize + "Wrapper" => |
| 249 | + // LHS is a normal string literal, call checkNotTypeCheck with the extracted code string to generate code |
| 250 | + checkNotTypeCheck(code.toString) |
| 251 | + |
| 252 | + case _ => |
| 253 | + qctx.error("The '" + shouldOrMust + "Not typeCheck' syntax only works with String literals.") |
| 254 | + '{???} |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + // used by shouldNot typeCheck syntax, delegate to assertNotTypeCheckImpl to generate code |
| 259 | + def shouldNotTypeCheckImpl(self: Expr[Matchers#AnyShouldWrapper[_]], typeCheckWord: Expr[TypeCheckWord])(pos: Expr[source.Position])(implicit qctx: QuoteContext): Expr[Assertion] = |
| 260 | + assertNotTypeCheckImpl(self, typeCheckWord, pos)("should") |
| 261 | + |
| 262 | + // used by mustNot typeCheck syntax, delegate to assertNotTypeCheckImpl to generate code |
| 263 | + def mustNotTypeCheckImpl(self: Expr[Matchers#AnyShouldWrapper[_]], typeCheckWord: Expr[TypeCheckWord])(pos: Expr[source.Position])(implicit qctx: QuoteContext): Expr[Assertion] = |
| 264 | + assertNotTypeCheckImpl(self, typeCheckWord, pos)("must") |
| 265 | + |
| 266 | + // check that a code snippet compiles |
| 267 | + def assertCompileImpl[T](self: Expr[T], compileWord: Expr[CompileWord], pos: Expr[source.Position])(shouldOrMust: String)(implicit qctx: QuoteContext): Expr[Assertion] = { |
| 268 | + import qctx.tasty.{_, given} |
| 269 | + |
| 270 | + // parse and type check a code snippet, generate code to throw TestFailedException if both parse and type check succeeded |
| 271 | + def checkCompile(code: String): Expr[Assertion] = |
| 272 | + if (/*typeChecks(code)*/ true) '{ Succeeded } // FIXME |
| 273 | + else '{ |
| 274 | + val messageExpr = Resources.expectedNoErrorButGotTypeError("", ${ Expr(code) }) |
| 275 | + throw new TestFailedException((_: StackDepthException) => Some(messageExpr), None, $pos) |
| 276 | + } |
| 277 | + |
| 278 | + self.unseal.underlyingArgument match { |
| 279 | + |
| 280 | + case Apply( |
| 281 | + Apply( |
| 282 | + Select(_, shouldOrMustTerconvertToStringShouldOrMustWrapperTermName), |
| 283 | + List( |
| 284 | + Literal(Constant(code: String)) |
| 285 | + ) |
| 286 | + ), |
| 287 | + _ |
| 288 | + ) if shouldOrMustTerconvertToStringShouldOrMustWrapperTermName == "convertToString" + shouldOrMust.capitalize + "Wrapper" => |
| 289 | + // LHS is a normal string literal, call checkCompile with the extracted code string to generate code |
| 290 | + checkCompile(code.toString) |
| 291 | + |
| 292 | + case Apply( |
| 293 | + Apply( |
| 294 | + Ident(shouldOrMustTerconvertToStringShouldOrMustWrapperTermName), |
| 295 | + List( |
| 296 | + Literal(Constant(code: String)) |
| 297 | + ) |
| 298 | + ), |
| 299 | + _ |
| 300 | + ) if shouldOrMustTerconvertToStringShouldOrMustWrapperTermName == "convertToString" + shouldOrMust.capitalize + "Wrapper" => |
| 301 | + // LHS is a normal string literal, call checkCompile with the extracted code string to generate code |
| 302 | + checkCompile(code.toString) |
| 303 | + |
| 304 | + case other => |
| 305 | + println("###other: " + other) |
| 306 | + qctx.error("The '" + shouldOrMust + " compile' syntax only works with String literals.") |
| 307 | + '{???} |
| 308 | + } |
| 309 | + } |
| 310 | + |
| 311 | + // used by should compile syntax, delegate to assertCompileImpl to generate code |
| 312 | + def shouldCompileImpl(self: Expr[Matchers#AnyShouldWrapper[_]], compileWord: Expr[CompileWord])(pos: Expr[source.Position])(implicit qctx: QuoteContext): Expr[Assertion] = |
| 313 | + assertCompileImpl(self, compileWord, pos)("should") |
| 314 | + |
| 315 | + // used by should compile syntax, delegate to assertCompileImpl to generate code |
| 316 | + def mustCompileImpl(self: Expr[MustMatchers#AnyMustWrapper[_]], compileWord: Expr[CompileWord])(pos: Expr[source.Position])(implicit qctx: QuoteContext): Expr[Assertion] = |
| 317 | + assertCompileImpl(self, compileWord, pos)("must") |
160 | 318 | }
|
0 commit comments