From 9fa17e04b4008c3a9bd6635f37aad900d7fb0d8d Mon Sep 17 00:00:00 2001 From: George Kiagiadakis Date: Tue, 2 Feb 2021 12:50:19 +0200 Subject: [PATCH] object-interest: add a NOT_EQUALS verb --- lib/wp/object-interest.c | 10 +++- lib/wp/object-interest.h | 3 ++ tests/wp/object-interest.c | 97 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 1 deletion(-) diff --git a/lib/wp/object-interest.c b/lib/wp/object-interest.c index f11a976d..a86511e0 100644 --- a/lib/wp/object-interest.c +++ b/lib/wp/object-interest.c @@ -69,6 +69,7 @@ G_DEFINE_BOXED_TYPE (WpObjectInterest, wp_object_interest, * The format string is interpreted as follows: * - the first character is the constraint verb: * - `=`: %WP_CONSTRAINT_VERB_EQUALS + * - `!`: %WP_CONSTRAINT_VERB_NOT_EQUALS * - `c`: %WP_CONSTRAINT_VERB_IN_LIST * - `~`: %WP_CONSTRAINT_VERB_IN_RANGE * - `#`: %WP_CONSTRAINT_VERB_MATCHES @@ -384,6 +385,7 @@ wp_object_interest_validate (WpObjectInterest * self, GError ** error) switch (c->verb) { case WP_CONSTRAINT_VERB_EQUALS: + case WP_CONSTRAINT_VERB_NOT_EQUALS: case WP_CONSTRAINT_VERB_IN_LIST: case WP_CONSTRAINT_VERB_IN_RANGE: case WP_CONSTRAINT_VERB_MATCHES: @@ -412,6 +414,7 @@ wp_object_interest_validate (WpObjectInterest * self, GError ** error) switch (c->verb) { case WP_CONSTRAINT_VERB_EQUALS: + case WP_CONSTRAINT_VERB_NOT_EQUALS: if (!g_variant_type_equal (value_type, G_VARIANT_TYPE_STRING) && !g_variant_type_equal (value_type, G_VARIANT_TYPE_BOOLEAN) && !g_variant_type_equal (value_type, G_VARIANT_TYPE_INT32) && @@ -420,7 +423,7 @@ wp_object_interest_validate (WpObjectInterest * self, GError ** error) !g_variant_type_equal (value_type, G_VARIANT_TYPE_UINT64) && !g_variant_type_equal (value_type, G_VARIANT_TYPE_DOUBLE)) { g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT, - "WP_CONSTRAINT_VERB_EQUALS requires a basic GVariant type" + "WP_CONSTRAINT_VERB_{NOT_,}EQUALS requires a basic GVariant type" " (actual type was '%s')", g_variant_get_type_string (c->value)); return FALSE; } @@ -842,6 +845,11 @@ wp_object_interest_matches_full (WpObjectInterest * self, !constraint_verb_equals (c->subject_type, &value, c->value)) return FALSE; break; + case WP_CONSTRAINT_VERB_NOT_EQUALS: + if (exists && + constraint_verb_equals (c->subject_type, &value, c->value)) + return FALSE; + break; case WP_CONSTRAINT_VERB_MATCHES: if (!exists || !constraint_verb_matches (c->subject_type, &value, c->value)) diff --git a/lib/wp/object-interest.h b/lib/wp/object-interest.h index 75fd0e91..7cff6405 100644 --- a/lib/wp/object-interest.h +++ b/lib/wp/object-interest.h @@ -38,6 +38,8 @@ typedef enum { * WpConstraintVerb: * @WP_CONSTRAINT_VERB_EQUALS: `=` the subject's value must equal the * constraint's value + * @WP_CONSTRAINT_VERB_NOT_EQUALS: `!` the subject's value must be different + * from the constraint's value * @WP_CONSTRAINT_VERB_IN_LIST: `c` the subject's value must equal at least * one of the values in the list given as the constraint's value * @WP_CONSTRAINT_VERB_IN_RANGE: `~` the subject's value must be a number @@ -49,6 +51,7 @@ typedef enum { */ typedef enum { WP_CONSTRAINT_VERB_EQUALS = '=', + WP_CONSTRAINT_VERB_NOT_EQUALS = '!', WP_CONSTRAINT_VERB_IN_LIST = 'c', WP_CONSTRAINT_VERB_IN_RANGE = '~', WP_CONSTRAINT_VERB_MATCHES = '#', diff --git a/tests/wp/object-interest.c b/tests/wp/object-interest.c index 7919a4ec..855b190a 100644 --- a/tests/wp/object-interest.c +++ b/tests/wp/object-interest.c @@ -407,6 +407,97 @@ test_object_interest_constraint_equals (TestFixture * f, gconstpointer data) TEST_EXPECT_NO_MATCH (i); } +static void +test_object_interest_constraint_not_equals (TestFixture * f, gconstpointer data) +{ + g_autoptr (WpObjectInterest) i = NULL; + + i = wp_object_interest_new (TEST_TYPE_A, + WP_CONSTRAINT_TYPE_G_PROPERTY, "test-string", "!s", "toast", NULL); + TEST_EXPECT_NO_MATCH (i); + + i = wp_object_interest_new (TEST_TYPE_A, + WP_CONSTRAINT_TYPE_G_PROPERTY, "test-string", "!s", "fail", NULL); + TEST_EXPECT_MATCH (i); + + + i = wp_object_interest_new (TEST_TYPE_A, + WP_CONSTRAINT_TYPE_G_PROPERTY, "test-int", "!i", -30, NULL); + TEST_EXPECT_NO_MATCH (i); + + i = wp_object_interest_new (TEST_TYPE_A, + WP_CONSTRAINT_TYPE_G_PROPERTY, "test-int", "!i", 100, NULL); + TEST_EXPECT_MATCH (i); + + + i = wp_object_interest_new (TEST_TYPE_A, + WP_CONSTRAINT_TYPE_G_PROPERTY, "test-uint", "!u", 50, NULL); + TEST_EXPECT_NO_MATCH (i); + + i = wp_object_interest_new (TEST_TYPE_A, + WP_CONSTRAINT_TYPE_G_PROPERTY, "test-uint", "!u", 100, NULL); + TEST_EXPECT_MATCH (i); + + + i = wp_object_interest_new (TEST_TYPE_A, + WP_CONSTRAINT_TYPE_G_PROPERTY, "test-int64", + "!x", G_GINT64_CONSTANT (-0x1d636b02300a7aa7), NULL); + TEST_EXPECT_NO_MATCH (i); + + i = wp_object_interest_new (TEST_TYPE_A, + WP_CONSTRAINT_TYPE_G_PROPERTY, "test-int64", + "!x", G_GINT64_CONSTANT (100), NULL); + TEST_EXPECT_MATCH (i); + + + i = wp_object_interest_new (TEST_TYPE_A, + WP_CONSTRAINT_TYPE_G_PROPERTY, "test-uint64", + "!t", G_GUINT64_CONSTANT (0x1d636b02300a7aa7), NULL); + TEST_EXPECT_NO_MATCH (i); + + i = wp_object_interest_new (TEST_TYPE_A, + WP_CONSTRAINT_TYPE_G_PROPERTY, "test-uint64", + "!t", G_GUINT64_CONSTANT (100), NULL); + TEST_EXPECT_MATCH (i); + + + i = wp_object_interest_new (TEST_TYPE_A, + WP_CONSTRAINT_TYPE_G_PROPERTY, "test-double", + "!d", 3.1415926545897932384626433, NULL); + TEST_EXPECT_NO_MATCH (i); + + i = wp_object_interest_new (TEST_TYPE_A, + WP_CONSTRAINT_TYPE_G_PROPERTY, "test-double", "!d", 3.14, NULL); + TEST_EXPECT_MATCH (i); + + + i = wp_object_interest_new (TEST_TYPE_A, + WP_CONSTRAINT_TYPE_G_PROPERTY, "test-float", "!d", 3.14, NULL); + TEST_EXPECT_NO_MATCH (i); + + i = wp_object_interest_new (TEST_TYPE_A, + WP_CONSTRAINT_TYPE_G_PROPERTY, "test-float", "!d", 1.0, NULL); + TEST_EXPECT_MATCH (i); + + + i = wp_object_interest_new (TEST_TYPE_A, + WP_CONSTRAINT_TYPE_G_PROPERTY, "test-boolean", "!b", TRUE, NULL); + TEST_EXPECT_NO_MATCH (i); + + i = wp_object_interest_new (TEST_TYPE_A, + WP_CONSTRAINT_TYPE_G_PROPERTY, "test-boolean", "!b", FALSE, NULL); + TEST_EXPECT_MATCH (i); + + + i = wp_object_interest_new (TEST_TYPE_A, + WP_CONSTRAINT_TYPE_G_PROPERTY, "test-double", "!d", 2.0, + WP_CONSTRAINT_TYPE_G_PROPERTY, "test-uint", "!u", 51, + WP_CONSTRAINT_TYPE_G_PROPERTY, "test-string", "!s", "FAIL", + WP_CONSTRAINT_TYPE_G_PROPERTY, "unknown-property", "!s", "FAIL", + NULL); + TEST_EXPECT_MATCH (i); +} + static void test_object_interest_constraint_list (TestFixture * f, gconstpointer data) { @@ -803,6 +894,12 @@ main (int argc, char *argv[]) test_object_interest_constraint_equals, test_object_interest_teardown); + g_test_add ("/wp/object-interest/not-equals", + TestFixture, NULL, + test_object_interest_setup, + test_object_interest_constraint_not_equals, + test_object_interest_teardown); + g_test_add ("/wp/object-interest/list", TestFixture, NULL, test_object_interest_setup,