From b9d3a43ea66692f87747f02289820a8bd25a375f Mon Sep 17 00:00:00 2001
From: Stephane Del Pino <stephane.delpino44@gmail.com>
Date: Thu, 4 Jul 2019 10:44:27 +0200
Subject: [PATCH] Replace check_ast function by the CHECK_AST macro

Improves the display of error throwing line on test failure
---
 tests/test_ASTBuilder.cpp | 75 ++++++++++++++++++---------------------
 1 file changed, 34 insertions(+), 41 deletions(-)

diff --git a/tests/test_ASTBuilder.cpp b/tests/test_ASTBuilder.cpp
index 6656bccd8..ecf89d06a 100644
--- a/tests/test_ASTBuilder.cpp
+++ b/tests/test_ASTBuilder.cpp
@@ -4,27 +4,20 @@
 #include <ASTPrinter.hpp>
 #include <sstream>
 
-void
-build_ast(const std::string_view& data)
-{
-  using namespace language;
-  string_input input{data, "test.pgs"};
-  buildAST(input);
-}
-
-void
-check_ast(const std::string_view& data, const std::string_view& expected_output)
-{
-  using namespace language;
-
-  string_input input{data, "test.pgs"};
-  auto ast = buildAST(input);
-
-  std::stringstream ast_output;
-  ast_output << '\n' << ASTPrinter{*ast, ASTPrinter::Format::raw, {ASTPrinter::Info::none}};
-
-  REQUIRE(ast_output.str() == expected_output);
-}
+#define CHECK_AST(data, expected_output)                                                       \
+  {                                                                                            \
+    static_assert(std::is_same_v<std::decay_t<decltype(data)>, std::string_view>);             \
+    static_assert(std::is_same_v<std::decay_t<decltype(expected_output)>, std::string_view>);  \
+    using namespace language;                                                                  \
+                                                                                               \
+    string_input input{data, "test.pgs"};                                                      \
+    auto ast = buildAST(input);                                                                \
+                                                                                               \
+    std::stringstream ast_output;                                                              \
+    ast_output << '\n' << ASTPrinter{*ast, ASTPrinter::Format::raw, {ASTPrinter::Info::none}}; \
+                                                                                               \
+    REQUIRE(ast_output.str() == expected_output);                                              \
+  }
 
 TEST_CASE("ASTBuilder", "[language]")
 {
@@ -64,7 +57,7 @@ string s = "foo";
      +-(language::name:s)
      `-(language::literal:"foo")
 )";
-      check_ast(data, result);
+      CHECK_AST(data, result);
     }
 
     SECTION("affectations")
@@ -110,7 +103,7 @@ string s; s = "foo";
      +-(language::name:s)
      `-(language::literal:"foo")
 )";
-      check_ast(data, result);
+      CHECK_AST(data, result);
     }
 
     SECTION("empty blocs simplification")
@@ -127,7 +120,7 @@ string s; s = "foo";
       std::string_view result = R"(
 (root)
 )";
-      check_ast(data, result);
+      CHECK_AST(data, result);
     }
 
     SECTION("operators precedence")
@@ -150,7 +143,7 @@ string s; s = "foo";
          +-(language::real:3.2)
          `-(language::integer:4)
 )";
-        check_ast(data, result);
+        CHECK_AST(data, result);
       }
 
       SECTION("parented expression")
@@ -167,7 +160,7 @@ string s; s = "foo";
      |   `-(language::integer:3)
      `-(language::integer:6)
 )";
-        check_ast(data, result);
+        CHECK_AST(data, result);
       }
 
       SECTION("all operators mix")
@@ -222,7 +215,7 @@ string s; s = "foo";
      `-(language::unary_not)
          `-(language::false_kw)
 )";
-        check_ast(data, result);
+        CHECK_AST(data, result);
       }
     }
 
@@ -241,7 +234,7 @@ not not not false;
  `-(language::unary_not)
      `-(language::false_kw)
 )";
-        check_ast(data, result);
+        CHECK_AST(data, result);
       }
 
       SECTION("multiple unary plus")
@@ -254,7 +247,7 @@ not not not false;
 (root)
  `-(language::integer:3)
 )";
-        check_ast(data, result);
+        CHECK_AST(data, result);
       }
 
       SECTION("multiple unary minus")
@@ -270,7 +263,7 @@ not not not false;
  `-(language::unary_minus)
      `-(language::integer:2)
 )";
-        check_ast(data, result);
+        CHECK_AST(data, result);
       }
 
       SECTION("sums and unary plus/minus")
@@ -293,7 +286,7 @@ not not not false;
      +-(language::integer:4)
      `-(language::integer:3)
 )";
-        check_ast(data, result);
+        CHECK_AST(data, result);
       }
     }
 
@@ -311,7 +304,7 @@ not not not false;
  `-(language::post_minusminus)
      `-(language::integer:2)
 )";
-      check_ast(data, result);
+      CHECK_AST(data, result);
     }
 
     SECTION("statement bloc simplification (one instruction per bloc)")
@@ -338,7 +331,7 @@ if (a > 0) {
          `-(language::unary_minus)
              `-(language::integer:1)
 )";
-      check_ast(data, result);
+      CHECK_AST(data, result);
     }
 
     SECTION("statement bloc simplification (one instruction in first bloc)")
@@ -370,7 +363,7 @@ if (a > 0) {
              `-(language::post_plusplus)
                  `-(language::name:a)
 )";
-      check_ast(data, result);
+      CHECK_AST(data, result);
     }
 
     SECTION("statement bloc simplification (one instruction in second bloc)")
@@ -400,7 +393,7 @@ if (a > 0) {
          +-(language::name:a)
          `-(language::integer:3)
 )";
-      check_ast(data, result);
+      CHECK_AST(data, result);
     }
 
     SECTION("statement bloc non-simplification (one declaration in each bloc)")
@@ -432,7 +425,7 @@ if (a > 0) {
                  +-(language::integer:2)
                  `-(language::name:a)
 )";
-      check_ast(data, result);
+      CHECK_AST(data, result);
     }
 
     SECTION("statement bloc simplification (one declaration in first bloc)")
@@ -467,7 +460,7 @@ if (a > 0) {
          `-(language::unary_plusplus)
              `-(language::name:a)
 )";
-      check_ast(data, result);
+      CHECK_AST(data, result);
     }
 
     SECTION("statement bloc simplification (one declaration in second bloc)")
@@ -502,7 +495,7 @@ if (a > 0) {
                  +-(language::integer:2)
                  `-(language::name:a)
 )";
-      check_ast(data, result);
+      CHECK_AST(data, result);
     }
 
     SECTION("for-statements simplification")
@@ -529,7 +522,7 @@ for(N i=0; i<10; ++i) {
          +-(language::name:i)
          `-(language::integer:3)
 )";
-      check_ast(data, result);
+      CHECK_AST(data, result);
     }
 
     SECTION("for-statements simplification (complex bloc)")
@@ -564,7 +557,7 @@ for(N i=0; i<10; ++i) {
                  +-(language::name:i)
                  `-(language::real:5.)
 )";
-      check_ast(data, result);
+      CHECK_AST(data, result);
     }
 
     SECTION("ostream simplifications")
@@ -589,7 +582,7 @@ clog << "log " << l << "\n";
      +-(language::name:l)
      `-(language::literal:"\n")
 )";
-      check_ast(data, result);
+      CHECK_AST(data, result);
     }
   }
 }
-- 
GitLab