Re: GParamSpec further funcs



Torsten Schoenfeld <kaffeetisch gmx de> writes:

+In scalar context return a boolean indicating whether $value is valid for
+$paramspec: FALSE if it is valid, and TRUE otherwise.  (Note that this is
+likely the opposite of what you expect.)

Oh, I see, that's annoying, isn't it.  Maybe it could be written in a
"positive" sense the same as in the gtk reference: so true if it has to
be modified.  Or true if invalid.  (And the second return is a suitably
modified value.)

I wrote some tests for values_cmp and didn't post them yet (unless I did
and I forgot!) using the various test pspecs.  Perhaps they exercise gtk
more than the interface, though you always want to know if something
isn't reaching what it's supposed to do ...

--- e.t 02 Jun 2008 09:56:28 +1000      1.2
+++ e.t 23 Jul 2008 17:18:50 +1000      
@@ -3,7 +3,7 @@
 #
 use strict;
 use Glib ':constants';
-use Test::More tests => 231;
+use Test::More tests => 238;
 
 # first register some types with which to play below.
 
@@ -58,11 +58,25 @@
                                   'Is you is, or is you ain\'t my baby',
                                   TRUE, 'readable');
 pspec_common_ok ($pspec, 'Boolean', 'readable');
-ok ($pspec->get_default_value, "Boolean default (expect TRUE)");
+is ($pspec->get_default_value, 1, "Boolean default (expect TRUE)");
+is ($pspec->values_cmp(0,1), -1);
+is ($pspec->values_cmp(1,0), 1);
+is ($pspec->values_cmp(1,1), 0);
+is ($pspec->values_cmp(123,456), 0);
+is ($pspec->values_cmp('',''), 0);
 
 push @params, $pspec;
 
 
+$pspec = Glib::ParamSpec->boolean ('untrue', 'Untrue',
+                                   'A pernicious falsehood',
+                                   FALSE, 'readable');
+# this is PL_sv_no, not a 0, for historical compatibility
+is ($pspec->get_default_value, '', "Boolean default (expect TRUE)");
+# not pushed, just checking the default
+# push @params, $pspec;
+
+
 #
 # all of the integer types have the same interface.
 #
@@ -82,6 +96,10 @@
        is ($pspec->get_maximum, $max, "$nick max");
        is ($pspec->get_default_value, $default, "$nick default");
        push @params, $pspec;
+
+        is ($pspec->values_cmp(22,33), -1);
+        is ($pspec->values_cmp(33,22), 1);
+        is ($pspec->values_cmp(22,22), 0);
 }
 
 #
@@ -101,6 +119,12 @@
        is_float ($pspec->get_maximum, $max, "$nick maximum");
        is_float ($pspec->get_default_value, $default, "$nick default");
        ok ($pspec->get_epsilon > 0.0, "$nick epsilon");
+        is ($pspec->values_cmp(22,33), -1);
+        is ($pspec->values_cmp(33,22), 1);
+        is ($pspec->values_cmp(22,22), 0);
+        is ($pspec->values_cmp(0, $pspec->get_epsilon / 2), 0);
+        is ($pspec->values_cmp($pspec->get_epsilon / 4,
+                               - $pspec->get_epsilon / 4), 0);
        push @params, $pspec;
 }
 
@@ -115,6 +139,10 @@
 pspec_common_ok ($pspec, 'Enum', G_PARAM_READWRITE, 'Fish');
 is ($pspec->get_enum_class, 'Fish', 'enum class');
 is ($pspec->get_default_value, 'blue', "Enum default");
+is ($pspec->values_cmp('one','two'), -1);
+is ($pspec->values_cmp('red','blue'), -1);
+is ($pspec->values_cmp('blue','one'), 1);
+is ($pspec->values_cmp('red','red'), 0);
 push @params, $pspec;
 
 
@@ -124,6 +152,9 @@
 pspec_common_ok ($pspec, 'Flags', G_PARAM_READWRITE, 'Rain');
 is ($pspec->get_flags_class, 'Rain', 'flags class');
 ok ($pspec->get_default_value == ['light', 'warm'], 'Flags default');
+is ($pspec->values_cmp('warm','cold'), -1);
+is ($pspec->values_cmp(['cold','light'],['light']), 1);
+is ($pspec->values_cmp(['heavy'],'heavy'), 0);
 push @params, $pspec;
 
 
@@ -132,6 +163,11 @@
                                 # we only know one boxed type at this point.
                                 'Glib::Scalar', G_PARAM_READWRITE);
 pspec_common_ok ($pspec, 'Boxed', G_PARAM_READWRITE, 'Glib::Scalar');
+is ($pspec->get_default_value, undef, 'Boxed default');
+{ # 1 or -1 depends on address, which is unpredictable
+  ok ($pspec->values_cmp('foo','bar') != 0);
+  # almost no Glib::Scalar has an equal address, so no tests for 0 here
+}
 push @params, $pspec;
 
 
@@ -139,6 +175,8 @@
                                  'I object, Your Honor, that\'s pure conjecture!',
                                  'Skeezle', G_PARAM_READWRITE);
 pspec_common_ok ($pspec, 'Object', G_PARAM_READWRITE, 'Skeezle');
+is ($pspec->get_default_value, undef, 'Object default');
+is ($pspec->values_cmp(undef,undef), 0);
 push @params, $pspec;
 
 
@@ -154,6 +192,9 @@
 ok ($pspec->get_flags == G_PARAM_READWRITE, 'Param flags');
 is ($pspec->get_value_type, 'Glib::Param::Enum', 'Param value type');
 ok (! $pspec->get_owner_type, 'Param owner type');
+is ($pspec->get_default_value, undef, 'Param default');
+is ($pspec->values_cmp($pspec,$pspec), 0);
+ok ($pspec->values_cmp($pspec,$params[0]) != 0);
 push @params, $pspec;
 
 
@@ -162,6 +203,9 @@
                                   'ö', qw/readable/);
 pspec_common_ok ($pspec, 'Unichar', qw/readable/, 'Glib::UInt');
 is ($pspec->get_default_value, 'ö', 'Unichar default');
+is ($pspec->values_cmp('A','B'), -1);
+is ($pspec->values_cmp('B','A'), -1);
+is ($pspec->values_cmp('B','B'), 0);
 push @params, $pspec;
 
 
@@ -171,6 +215,7 @@
 $pspec = Glib::ParamSpec->IV ('iv', 'IV',
                              'This is the same as Int',
                              -20, 10, -5, G_PARAM_READWRITE);
+is ($pspec->get_default_value, -5, 'IV default');
 isa_ok ($pspec, 'Glib::Param::Long', 'IV is actually Long');
 push @params, $pspec;
 
@@ -179,6 +224,7 @@
                              'This is the same as UInt',
                              10, 20, 15, G_PARAM_READWRITE);
 isa_ok ($pspec, 'Glib::Param::ULong', 'UV is actually ULong');
+is ($pspec->get_default_value, 15, 'UV default');
 push @params, $pspec;
 
 
@@ -187,6 +233,7 @@
                                  G_PARAM_READWRITE);
 isa_ok ($pspec, 'Glib::Param::Boxed', 'Scalar is actually Boxed');
 is ($pspec->get_value_type, 'Glib::Scalar', 'boxed holding scalar');
+is ($pspec->get_default_value, undef, 'Scalar default');
 push @params, $pspec;
 
 


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