<commit_before>/*
 * Copyright 2001-2012 Artima, Inc.
 *
 * Licensed 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.scalatest

import reflect.macros.Context
import collection.mutable.ListBuffer
import collection.immutable.TreeMap
import reflect.internal.util.{Position, OffsetPosition, RangePosition}

class AssertionsMacro[C <: Context](val context: C) {
  import context.universe._

  def apply(booleanExpr: Expr[Boolean]): Expr[Unit] = {
    
    val predicate = parsePredicate(booleanExpr.tree)
    predicate match {
      case Some(RecognizedPredicate(left, operator, right, subsitutedExpr)) => 
        val resourceName: Option[String] = 
          operator match {
            case "==" => Some("didNotEqual")
            case "===" => Some("didNotEqual")
            case "!=" => Some("equaled")
            case "!==" => Some("equaled")
            case ">" => Some("wasNotGreaterThan")
            case ">=" => Some("wasNotGreaterThanOrEqualTo")
            case "<" => Some("wasNotLessThan")
            case "<=" => Some("wasNotLessThanOrEqualTo")
            case _ => None
          }
        
        resourceName match {
          case Some(resourceName) => 
            context.Expr(
              Block(
                ValDef(
                  Modifiers(), 
                  newTermName("$org_scalatest_assert_macro_left"), 
                  TypeTree(), 
                  left.duplicate
                ), 
                ValDef(
                  Modifiers(), 
                  newTermName("$org_scalatest_assert_macro_right"), 
                  TypeTree(), 
                  right.duplicate
                ), 
                ValDef(
                  Modifiers(), 
                  newTermName("$org_scalatest_assert_macro_result"), 
                  TypeTree(), 
                  subsitutedExpr
                ), 
                Apply(
                  Select(
                    Ident("$org_scalatest_AssertionsHelper"), 
                    newTermName("macroAssertTrue")
                  ),
                  List(Ident(newTermName("$org_scalatest_assert_macro_left")), Ident(newTermName("$org_scalatest_assert_macro_right")), Ident(newTermName("$org_scalatest_assert_macro_result")), context.literal(resourceName).tree)
                )
              )
            )
          
          case None => 
            val text: Expr[String] = context.literal(getText(booleanExpr.tree))
            context.Expr(
              Apply(
                Select(
                  Ident("$org_scalatest_AssertionsHelper"), 
                  newTermName("macroAssertTrue")
                ), 
                List(booleanExpr.tree, text.tree)
              )  
            )
        }
      case None => 
        val text: Expr[String] = context.literal(getText(booleanExpr.tree))
        context.Expr(
          Apply(
            Select(
              Ident("$org_scalatest_AssertionsHelper"), 
              newTermName("macroAssertTrue")
            ), 
            List(booleanExpr.tree, text.tree)
          )  
        )
    }
  }
  
  case class RecognizedPredicate(left: Tree, operator: String, right: Tree, subsitutedExpr: Apply)
  
  def parsePredicate(tree: Tree): Option[RecognizedPredicate] = {
    tree match {
      case apply: Apply if apply.args.size == 1 =>
        apply.fun match {
          case select: Select => <commit_msg>Added comments to AssertionsMacro.scala.<commit_after>/*
 * Copyright 2001-2012 Artima, Inc.
 *
 * Licensed 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.scalatest

import reflect.macros.Context
import collection.mutable.ListBuffer
import collection.immutable.TreeMap
import reflect.internal.util.{Position, OffsetPosition, RangePosition}

class AssertionsMacro[C <: Context](val context: C) {
  import context.universe._

  def apply(booleanExpr: Expr[Boolean]): Expr[Unit] = {
    
    val predicate = parsePredicate(booleanExpr.tree)
    predicate match {
      case Some(RecognizedPredicate(left, operator, right, subsitutedExpr)) => 
        val resourceName: Option[String] = 
          operator match {
            case "==" => Some("didNotEqual")
            case "===" => Some("didNotEqual")
            case "!=" => Some("equaled")
            case "!==" => Some("equaled")
            case ">" => Some("wasNotGreaterThan")
            case ">=" => Some("wasNotGreaterThanOrEqualTo")
            case "<" => Some("wasNotLessThan")
            case "<=" => Some("wasNotLessThanOrEqualTo")
            case _ => None
          }
        
        resourceName match {
          case Some(resourceName) =>
            /* 
             * Translate the following:
             * 
             * assert(something.aMethod == 3)
             * 
             * to: 
             * 
             * {
             *   val $org_scalatest_assert_macro_left = something.aMethod
             *   val $org_scalatest_assert_macro_right = 3
             *   val $org_scalatest_assert_macro_result = $org_scalatest_assert_macro_left ==  $org_scalatest_assert_macro_result
             *   $org_scalatest_AssertionsHelper.macroAssertTrue($org_scalatest_assert_macro_left, $org_scalatest_assert_macro_right, $org_scalatest_assert_macro_result, "didNotEqual")
             * }
             * 
             */
            context.Expr(
              Block(
                ValDef(
                  Modifiers(), 
                  newTermName("$org_scalatest_assert_macro_left"), 
                  TypeTree(), 
                  left.duplicate
                ), 
                ValDef(
                  Modifiers(), 
                  newTermName("$org_scalatest_assert_macro_right"), 
                  TypeTree(), 
                  right.duplicate
                ), 
                ValDef(
                  Modifiers(), 
                  newTermName("$org_scalatest_assert_macro_result"), 
                  TypeTree(), 
                  subsitutedExpr
                ), 
                Apply(
                  Select(
                    Ident("$org_scalatest_AssertionsHelper"), 
                    newTermName("macroAssertTrue")
                  ),
                  List(Ident(newTermName("$org_scalatest_assert_macro_left")), Ident(newTermName("$org_scalatest_assert_macro_right")), Ident(newTermName("$org_scalatest_assert_macro_result")), context.literal(resourceName).tree)
                )
              )
            )
          
          case None => 
            /* 
             * Translate the following:
             * 
             * assert(something.aMethod equals 3)
             * 
             * to: 
             * 
             * $org_scalatest_AssertionsHelper.macroAssertTrue(something.aMethod equals 3, "something.aMethod equals 3")
             * 
             */
            val text: Expr[String] = context.literal(getText(booleanExpr.tree))
            context.Expr(
              Apply(
                Select(
                  Ident("$org_scalatest_AssertionsHelper"), 
                  newTermName("macroAssertTrue")
                ), 
                List(booleanExpr.tree, text.tree)
              )  
            )
        }
      case None => 
        /* 
         * Translate the following:
         * 
         * assert(validate(1, 2, 3))
         * 
         * to: 
         * 
         * $org_scalatest_AssertionsHelper.macroAssertTrue(validate(1, 2, 3), "validate(1, 2, 3)")
         * 
         */
        val text: Expr[String] = context.literal(getText(booleanExpr.tree))
        context.Expr(
          Apply(
            Select(
              Ident("$org_scalatest_AssertionsHelper"), 
              newTermName("macroAssertTrue")
            ), 
            List(booleanExpr.tree, text.tree)
          )  
        )
    }
  }
  
  case class RecognizedPredicate(left: Tree, operator: String, right: Tree, subsitutedExpr: Apply)
  
  def parsePredicate(tree: Tree): Option[RecognizedPredicate] = {
    tree match {
      case apply: Apply if apply.args.size == 1 =>
        apply.fun match {
          case select: Select => 
{'fraction_non_alphanumeric': 0.09772202046880159, 'fraction_numerical': 0.004401892813909981, 'mean_word_length': 1.7209580838323353, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2396911', 'n_tokens_mistral': 2508, 'n_tokens_neox': 2327, 'n_words': 491}