[gtk/prop-list: 21/154] More expression tests
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/prop-list: 21/154] More expression tests
- Date: Tue, 3 Dec 2019 01:30:27 +0000 (UTC)
commit ddae2244e8867dee08e6bdec4f15db57e9a60de1
Author: Matthias Clasen <mclasen redhat com>
Date: Thu Nov 28 14:40:34 2019 -0500
More expression tests
Test type mismatches, and the this pointer
during evaluation.
testsuite/gtk/expression.c | 199 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 199 insertions(+)
---
diff --git a/testsuite/gtk/expression.c b/testsuite/gtk/expression.c
index 6077315949..94803badf9 100644
--- a/testsuite/gtk/expression.c
+++ b/testsuite/gtk/expression.c
@@ -121,6 +121,200 @@ test_closure (void)
gtk_expression_watch_unwatch (watch);
}
+static void
+test_constant (void)
+{
+ GtkExpression *expr;
+ GValue value = G_VALUE_INIT;
+ gboolean res;
+
+ expr = gtk_constant_expression_new (G_TYPE_INT, 22);
+ g_assert_cmpint (gtk_expression_get_value_type (expr), ==, G_TYPE_INT);
+ g_assert_true (gtk_expression_is_static (expr));
+
+ res = gtk_expression_evaluate (expr, NULL, &value);
+ g_assert_true (res);
+ g_assert_cmpint (g_value_get_int (&value), ==, 22);
+
+ gtk_expression_unref (expr);
+}
+
+/* Test that object expressions fail to evaluate when
+ * the object is gone.
+ */
+static void
+test_object (void)
+{
+ GtkExpression *expr;
+ GObject *obj;
+ GValue value = G_VALUE_INIT;
+ gboolean res;
+
+ obj = G_OBJECT (gtk_string_filter_new ());
+
+ expr = gtk_object_expression_new (obj);
+ g_assert_true (!gtk_expression_is_static (expr));
+ g_assert_cmpint (gtk_expression_get_value_type (expr), ==, GTK_TYPE_STRING_FILTER);
+
+ res = gtk_expression_evaluate (expr, NULL, &value);
+ g_assert_true (res);
+ g_assert_true (g_value_get_object (&value) == obj);
+ g_value_unset (&value);
+
+ g_clear_object (&obj);
+ res = gtk_expression_evaluate (expr, NULL, &value);
+ g_assert_false (res);
+
+ gtk_expression_unref (expr);
+}
+
+/* Some basic tests that nested expressions work; in particular test
+ * that watching works when things change deeper in the expression tree
+ *
+ * The setup we use is GtkFilterListModel -> GtkFilter -> "search" property,
+ * which gives us an expression tree like
+ *
+ * GtkPropertyExpression "search"
+ * -> GtkPropertyExpression "filter"
+ * -> GtkObjectExpression listmodel
+ *
+ * We test setting both the search property and the filter property.
+ */
+static void
+test_nested (void)
+{
+ GtkExpression *list_expr;
+ GtkExpression *filter_expr;
+ GtkExpression *expr;
+ GtkFilter *filter;
+ GListModel *list;
+ GtkFilterListModel *filtered;
+ GValue value = G_VALUE_INIT;
+ gboolean res;
+ GtkExpressionWatch *watch;
+ guint counter = 0;
+
+ filter = gtk_string_filter_new ();
+ gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "word");
+ list = G_LIST_MODEL (g_list_store_new (G_TYPE_OBJECT));
+ filtered = gtk_filter_list_model_new (list, filter);
+
+ list_expr = gtk_object_expression_new (G_OBJECT (filtered));
+ filter_expr = gtk_property_expression_new (GTK_TYPE_FILTER_LIST_MODEL, list_expr, "filter");
+ expr = gtk_property_expression_new (GTK_TYPE_STRING_FILTER, filter_expr, "search");
+
+ g_assert_true (!gtk_expression_is_static (expr));
+ g_assert_cmpint (gtk_expression_get_value_type (expr), ==, G_TYPE_STRING);
+
+ res = gtk_expression_evaluate (expr, NULL, &value);
+ g_assert_true (res);
+ g_assert_cmpstr (g_value_get_string (&value), ==, "word");
+ g_value_unset (&value);
+
+ watch = gtk_expression_watch (expr, filter, inc_counter, &counter, NULL);
+ gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "salad");
+ g_assert_cmpint (counter, >, 0);
+ counter = 0;
+
+ res = gtk_expression_evaluate (expr, NULL, &value);
+ g_assert_true (res);
+ g_assert_cmpstr (g_value_get_string (&value), ==, "salad");
+ g_value_unset (&value);
+
+ gtk_filter_list_model_set_filter (filtered, filter);
+ g_assert_cmpint (counter, ==, 0);
+
+ g_clear_object (&filter);
+ filter = gtk_string_filter_new ();
+ gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "salad");
+ gtk_filter_list_model_set_filter (filtered, filter);
+ g_assert_cmpint (counter, >, 0);
+ counter = 0;
+
+ res = gtk_expression_evaluate (expr, NULL, &value);
+ g_assert_true (res);
+ g_assert_cmpstr (g_value_get_string (&value), ==, "salad");
+ g_value_unset (&value);
+
+ gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "bar");
+ g_assert_cmpint (counter, >, 0);
+ counter = 0;
+
+ res = gtk_expression_evaluate (expr, NULL, &value);
+ g_assert_true (res);
+ g_assert_cmpstr (g_value_get_string (&value), ==, "bar");
+ g_value_unset (&value);
+
+ gtk_filter_list_model_set_filter (filtered, NULL);
+ g_assert_cmpint (counter, >, 0);
+
+ res = gtk_expression_evaluate (expr, NULL, &value);
+ g_assert_false (res);
+
+ gtk_expression_watch_unwatch (watch);
+
+ g_object_unref (filtered);
+ g_object_unref (list);
+ g_object_unref (filter);
+ gtk_expression_unre (expr);
+}
+
+/* Test that property expressions fail to evaluate if the
+ * expression evaluates to an object of the wrong type
+ */
+static void
+test_type_mismatch (void)
+{
+ GtkFilter *filter;
+ GtkExpression *expr;
+ GValue value = G_VALUE_INIT;
+ gboolean res;
+
+ filter = gtk_any_filter_new ();
+
+ expr = gtk_property_expression_new (GTK_TYPE_STRING_FILTER, gtk_constant_expression_new
(GTK_TYPE_ANY_FILTER, filter), "search");
+
+ res = gtk_expression_evaluate (expr, NULL, &value);
+ g_assert_false (res);
+ g_assert_false (G_IS_VALUE (&value));
+
+ gtk_expression_unref (expr);
+ g_object_unref (filter);
+}
+
+/* Some basic tests around 'this' */
+static void
+test_this (void)
+{
+ GtkFilter *filter;
+ GtkFilter *filter2;
+ GtkExpression *expr;
+ GValue value = G_VALUE_INIT;
+ gboolean res;
+
+ expr = gtk_property_expression_new (GTK_TYPE_STRING_FILTER, NULL, "search");
+
+ filter = gtk_string_filter_new ();
+ gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "word");
+
+ filter2 = gtk_string_filter_new ();
+ gtk_string_filter_set_search (GTK_STRING_FILTER (filter2), "sausage");
+
+ res = gtk_expression_evaluate (expr, filter, &value);
+ g_assert_true (res);
+ g_assert_cmpstr (g_value_get_string (&value), ==, "word");
+ g_value_unset (&value);
+
+ res = gtk_expression_evaluate (expr, filter2, &value);
+ g_assert_true (res);
+ g_assert_cmpstr (g_value_get_string (&value), ==, "sausage");
+ g_value_unset (&value);
+
+ g_object_unref (expr);
+ g_object_unref (filter2);
+ g_object_unref (filter);
+}
+
int
main (int argc, char *argv[])
{
@@ -129,6 +323,11 @@ main (int argc, char *argv[])
g_test_add_func ("/expression/property", test_property);
g_test_add_func ("/expression/closure", test_closure);
+ g_test_add_func ("/expression/constant", test_constant);
+ g_test_add_func ("/expression/object", test_object);
+ g_test_add_func ("/expression/nested", test_nested);
+ g_test_add_func ("/expression/type-mismatch", test_type_mismatch);
+ g_test_add_func ("/expression/this", test_this);
return g_test_run ();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]