[template-glib] tests: add simple test for script



commit 2ec4615527985df459ed3867f7d6db18e8d98754
Author: Christian Hergert <chergert redhat com>
Date:   Tue May 3 22:57:21 2022 -0700

    tests: add simple test for script
    
    There is a bunch I'd like to do to make this better now, but we have to
    get some basics in place and round off some corners which never really
    worked with GITypelib access.
    
    My goal here is to make this a bit easier to use as a basic scripting
    layer for Builder to use in it's shortcut bindings.

 src/tmpl-expr-parser.y  |  1 -
 src/tmpl-expr-scanner.l | 11 ++++----
 tests/meson.build       | 31 +++++++++++++++++++++
 tests/test-expr.c       | 73 +++++++++++++++++++++++++++++++++++++++++++++++++
 tests/test1.script      |  4 +++
 5 files changed, 114 insertions(+), 6 deletions(-)
---
diff --git a/src/tmpl-expr-parser.y b/src/tmpl-expr-parser.y
index 10df607..ba32573 100644
--- a/src/tmpl-expr-parser.y
+++ b/src/tmpl-expr-parser.y
@@ -71,7 +71,6 @@ tmpl_expr_parser_error (TmplExprParser *parser,
 %%
 
 expr: /* nothing */ EOL {
-    parser->ast = NULL;
     YYACCEPT;
   }
   | stmt EOL {
diff --git a/src/tmpl-expr-scanner.l b/src/tmpl-expr-scanner.l
index 97fc4b8..404d3e6 100644
--- a/src/tmpl-expr-scanner.l
+++ b/src/tmpl-expr-scanner.l
@@ -134,14 +134,18 @@ tmpl_expr_parser_parse_string (TmplExprParser  *self,
                                GError        **error)
 {
   YY_BUFFER_STATE buf;
-  gint ret;
+  int ret = 0;
 
   g_return_val_if_fail (self != NULL, FALSE);
 
   buf = tmpl_expr_parser__scan_string (input, self->scanner);
-  ret = tmpl_expr_parser_parse (self);
+  while (!self->reached_eof && ret == 0)
+    ret = tmpl_expr_parser_parse (self);
   tmpl_expr_parser__delete_buffer (buf, self->scanner);
 
+  if (ret == 0 && self->error_str == NULL)
+    return TRUE;
+
   if (self->error_str)
     {
       g_set_error (error,
@@ -151,9 +155,6 @@ tmpl_expr_parser_parse_string (TmplExprParser  *self,
       return FALSE;
     }
 
-  if (ret == 0)
-    return TRUE;
-
   g_set_error (error,
                TMPL_ERROR,
                TMPL_ERROR_SYNTAX_ERROR,
diff --git a/tests/meson.build b/tests/meson.build
index e69de29..ef60d90 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -0,0 +1,31 @@
+test_env = [
+  'G_TEST_SRCDIR=@0@'.format(meson.current_source_dir()),
+  'G_TEST_BUILDDIR=@0@'.format(meson.current_build_dir()),
+  'G_DEBUG=gc-friendly',
+  'GSETTINGS_BACKEND=memory',
+  'MALLOC_CHECK_=2',
+  'NO_AT_BRIDGE=1',
+]
+
+testsuite_c_args = [
+  '-UG_DISABLE_ASSERT',
+  '-UG_DISABLE_CAST_CHECKS',
+]
+
+testsuite_sources = [
+  ['test-expr'],
+]
+
+foreach test: testsuite_sources
+  test_name = test.get(0)
+  test_sources = ['@0@.c'.format(test_name)]
+
+  test_exe = executable(test_name, test_sources,
+                 c_args: testsuite_c_args,
+             link_whole: core_lib,
+           dependencies: [libtemplate_glib_deps],
+    include_directories: include_directories('../src'),
+  )
+
+  test(test_name, test_exe, suite: 'Template-GLib', env: test_env)
+endforeach
diff --git a/tests/test-expr.c b/tests/test-expr.c
new file mode 100644
index 0000000..df71b1c
--- /dev/null
+++ b/tests/test-expr.c
@@ -0,0 +1,73 @@
+/* test-expr.c
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This file is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * This file is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: LGPL-3.0-or-later
+ */
+
+#include "tmpl-glib.h"
+
+static char *
+get_test_file (const char *name)
+{
+  GFile *file;
+  char *data = NULL;
+  gsize len = 0;
+
+  g_assert_nonnull (g_getenv ("G_TEST_SRCDIR"));
+
+  file = g_file_new_build_filename (g_getenv ("G_TEST_SRCDIR"), name, NULL);
+  g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
+  g_object_unref (file);
+
+  return data;
+}
+
+static void
+test1 (void)
+{
+  GError *error = NULL;
+  char *contents = get_test_file ("test1.script");
+  TmplExpr *expr = tmpl_expr_from_string (contents, &error);
+  TmplScope *scope = tmpl_scope_new ();
+  GValue ret = G_VALUE_INIT;
+  gboolean r;
+
+  g_assert_no_error (error);
+  g_assert_nonnull (expr);
+
+  r = tmpl_expr_eval (expr, scope, &ret, &error);
+  g_assert_no_error (error);
+  g_assert_true (r);
+
+  g_print ("%s\n", G_VALUE_TYPE_NAME (&ret));
+
+  g_assert_true (G_VALUE_HOLDS_DOUBLE (&ret));
+  g_assert_cmpint (g_value_get_double (&ret), ==, 1234.0);
+
+  tmpl_scope_unref (scope);
+  tmpl_expr_unref (expr);
+  g_free (contents);
+}
+
+int
+main (int argc,
+      char *argv[])
+{
+  g_test_init (&argc, &argv, NULL);
+  g_test_add_func ("/Tmpl/Expr/test1", test1);
+  return g_test_run ();
+}
diff --git a/tests/test1.script b/tests/test1.script
new file mode 100644
index 0000000..33eda28
--- /dev/null
+++ b/tests/test1.script
@@ -0,0 +1,4 @@
+require Gio version "2.0"
+require GLib version "2.0"
+
+1234.0


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]