GNOME Bugzilla – Bug 704072
add assert() variants similar to g_assert_cmp*()
Last modified: 2018-05-22 14:53:04 UTC
In my project, I have a lot of test cases which compare strings. The built-in assert() function is not so helpful for failure cases, since it doesn't print the actual value evaluated. What do you think about adding some variants of assert(), like g_assert_cmpstr() in glib or assertEqual() in python? I guess it requires changes to codegen to generate the macros in C, and new code attributes to call them.
I still badly want this feature, so here is a proposal for a naive implementation: 1. Introduce a new attribute which marks an argument that will be "unquoted" in codegen, something like: [Assert (operator_pos = 1.1)] public void assert_cmpstr (string s1, string cmp, string s2); or more generic: [Assert] public void assert_cmpstr (string s1, [CCode (quoted = true)] string cmp, string s2); 2. In Vala code, write tests with the CMP argument quoted as a string: assert_cmpstr (a, ">", b); 3. Codegen translates it to: g_assert_cmpstr (a, >, b); How does this sound? I know it's not elegant though.
Sounds good, that also opens a new world of hacks :P Does this work already maybe? [CCode (cname=">")] public const string GT; assert_cmpstr(a, GT, b);
Oh, yes, it works like a charm! Thanks for the info. So, maybe those assert_cmp*() and GT, LT, etc. could be simply added to glib-2.0.vapi?
Yes, probably for now it's better to avoid extending the syntax for such cases. Don't know if Jurg has a better solution.
I don't see a reason to add syntax for it. However, the compiler could recognize the usual assert (a < b); and translate it to g_assert_cmp* as appropriate. We already have special handling for assert to provide an understandable message, however, that would be a good improvement.
Yes, that would be ideal, as we can get detailed diagnostics without any modification to the existing test code. FWIW, I'm attaching a patch.
Created attachment 270761 [details] [review] codegen: Emit g_assert_cmp*() call for assert (a OP b) if possible
Thanks for your patch, however using directly g_assert won't point you to the relevant vala code. Why are you avoiding the _vala_assert approach? The vala source reference must be written in the assert message, using _vala variants of glib asserts, otherwise whenever an assert happens you don't know where it happened.
Created attachment 272354 [details] [review] codegen: Emit assertion code using g_assertion_message_cmp{str,num} -- Right, thanks for looking at it. The new patch will fix it, although I gave up dealing with nullable value types.
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/395.