diff options
author | Niels De Graef <nielsdegraef@gmail.com> | 2022-09-06 22:09:25 +0200 |
---|---|---|
committer | Niels De Graef <nielsdegraef@gmail.com> | 2023-03-02 07:31:29 +0000 |
commit | d455b34b76a25b116b7fa82d4cb5fcf862546bfa (patch) | |
tree | e41be2b1469648334a4f2c581b38747fe2ddc1e6 /tests | |
parent | 99af58107b2e49ad1b5a7fc91d290126fb8636ee (diff) | |
download | gnome-contacts-d455b34b76a25b116b7fa82d4cb5fcf862546bfa.tar.gz |
Move GVariant serialization into Chunk
Rather than building a big if-else block in the `Contacts.Io` namespace,
it's much more interesting to move the GVariant serialization into the
`Contacts.Chunk` objects themselves. That allows us to keep the
serialization logic for a specific field in one place and makes sure we
don't forget about any properties as they're not part of that big
if-else block that checks on property name.
This commit also make sure a lot of the functionality here is now unit
tested, to make sure we're not accidentally regressing.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/core/meson.build | 1 | ||||
-rw-r--r-- | tests/core/test-birthday-chunk.vala | 44 | ||||
-rw-r--r-- | tests/core/test-email-addresses-chunk.vala | 27 | ||||
-rw-r--r-- | tests/core/test-full-name-chunk.vala | 21 | ||||
-rw-r--r-- | tests/core/test-nickname-chunk.vala | 21 | ||||
-rw-r--r-- | tests/core/test-phones-chunk.vala | 23 | ||||
-rw-r--r-- | tests/core/test-structured-name-chunk.vala | 65 | ||||
-rw-r--r-- | tests/core/test-urls-chunk.vala | 23 | ||||
-rw-r--r-- | tests/io/internal/meson.build | 29 | ||||
-rw-r--r-- | tests/io/internal/test-serialise-birthday.vala | 54 | ||||
-rw-r--r-- | tests/io/internal/test-serialise-common.vala | 66 | ||||
-rw-r--r-- | tests/io/internal/test-serialise-emails.vala | 41 | ||||
-rw-r--r-- | tests/io/internal/test-serialise-full-name.vala | 42 | ||||
-rw-r--r-- | tests/io/internal/test-serialise-nickname.vala | 40 | ||||
-rw-r--r-- | tests/io/internal/test-serialise-structured-name.vala | 45 | ||||
-rw-r--r-- | tests/io/internal/test-serialise-urls.vala | 41 | ||||
-rw-r--r-- | tests/io/meson.build | 1 | ||||
-rw-r--r-- | tests/io/vcard/meson.build | 1 | ||||
-rw-r--r-- | tests/io/vcard/test-vcard-minimal-import.vala | 65 |
19 files changed, 252 insertions, 398 deletions
diff --git a/tests/core/meson.build b/tests/core/meson.build index 4e38475..86101d1 100644 --- a/tests/core/meson.build +++ b/tests/core/meson.build @@ -7,6 +7,7 @@ test_names = [ 'test-notes-chunk', 'test-phones-chunk', 'test-roles-chunk', + 'test-structured-name-chunk', 'test-urls-chunk', ] diff --git a/tests/core/test-birthday-chunk.vala b/tests/core/test-birthday-chunk.vala index d112379..2135c49 100644 --- a/tests/core/test-birthday-chunk.vala +++ b/tests/core/test-birthday-chunk.vala @@ -20,6 +20,8 @@ void main (string[] args) { Test.add_func ("/core/birthday-chunk/property_name_chunk", test_property_name); Test.add_func ("/core/birthday-chunk/is-empty", test_is_empty); Test.add_func ("/core/birthday-chunk/leap-day-birthday", test_leap_day_birthday); + Test.add_func ("/core/birthday-chunk/serialize-basic", test_serialize_basic); + Test.add_func ("/core/birthday-chunk/serialize-pre-epoch", test_serialize_pre_epoch); Test.run (); } @@ -46,7 +48,7 @@ private void test_is_empty () { assert_true (chunk.is_empty); } -void test_leap_day_birthday () { +private void test_leap_day_birthday () { var contact = new Contacts.Contact.empty (); var chunk = (Contacts.BirthdayChunk) contact.create_chunk ("birthday", null); assert_nonnull (chunk); @@ -60,4 +62,42 @@ void test_leap_day_birthday () { var feb_28_non_leap_year = new DateTime.local (2023, 2, 28, 0, 0, 0); assert_true (chunk.is_today (feb_28_non_leap_year)); -}
\ No newline at end of file +} + +private void test_serialize_basic () { + var contact = new Contacts.Contact.empty (); + var chunk = (Contacts.BirthdayChunk) contact.create_chunk ("birthday", null); + + // If the birthday is not set, serialization should give a null result + var serialized = chunk.to_gvariant (); + assert_null (serialized); + + // If the birthday is set, we should have a variant. Without checking its + // contents, it should deserialize in a new contact + var old_bd = new DateTime.utc (1992, 8, 1, 0, 0, 0); + chunk.birthday = old_bd; + serialized = chunk.to_gvariant (); + assert_nonnull (serialized); + + var contact2 = new Contacts.Contact.empty (); + var chunk2 = (Contacts.BirthdayChunk) contact2.create_chunk ("birthday", null); + chunk2.apply_gvariant (serialized); + assert_true (old_bd.equal (chunk2.birthday)); +} + +private void test_serialize_pre_epoch () { + var contact = new Contacts.Contact.empty (); + var chunk = (Contacts.BirthdayChunk) contact.create_chunk ("birthday", null); + + // Check that we didn't try to use something that doesn't allow dates before + // epoch (eg struct tm) + var old_bd = new DateTime.utc (1961, 7, 3, 0, 0, 0); + chunk.birthday = old_bd; + var serialized = chunk.to_gvariant (); + assert_nonnull (serialized); + + var contact2 = new Contacts.Contact.empty (); + var chunk2 = (Contacts.BirthdayChunk) contact2.create_chunk ("birthday", null); + chunk2.apply_gvariant (serialized); + assert_true (old_bd.equal (chunk2.birthday)); +} diff --git a/tests/core/test-email-addresses-chunk.vala b/tests/core/test-email-addresses-chunk.vala index d2d10c7..90f9247 100644 --- a/tests/core/test-email-addresses-chunk.vala +++ b/tests/core/test-email-addresses-chunk.vala @@ -17,8 +17,9 @@ void main (string[] args) { Test.init (ref args); - Test.add_func ("/core/addresses-chunk/property-name-chunk", test_property_name); - Test.add_func ("/core/addresses-chunk/get-is-empty", test_is_empty); + Test.add_func ("/core/email-addresses-chunk/property-name-chunk", test_property_name); + Test.add_func ("/core/email-addresses-chunk/get-is-empty", test_is_empty); + Test.add_func ("/core/email-addresses-chunk/serialize-basic", test_serialize_basic); Test.run (); } @@ -51,3 +52,25 @@ private void test_is_empty () { assert_true (address.is_empty); assert_true (chunk.is_empty); } + +private void test_serialize_basic () { + var contact = new Contacts.Contact.empty (); + var chunk = (Contacts.EmailAddressesChunk) contact.create_chunk ("email-addresses", null); + + // If the emailaddresss are empty, serialization should give a null result + var serialized = chunk.to_gvariant (); + assert_null (serialized); + + // If a email address is added, we should have a variant. We don't need to + // inspect the variant, we just need to know it properly deserializes + var email_addr = (Contacts.EmailAddress) chunk.get_item (0); + email_addr.raw_address = "nielsdegraef@gmail.com"; + serialized = chunk.to_gvariant (); + assert_nonnull (serialized); + + var contact2 = new Contacts.Contact.empty (); + var chunk2 = (Contacts.EmailAddressesChunk) contact2.create_chunk ("email-addresses", null); + chunk2.apply_gvariant (serialized); + assert_nonnull (chunk.get_item (0)); + assert_true (((Contacts.EmailAddress) chunk.get_item (0)).raw_address == "nielsdegraef@gmail.com"); +} diff --git a/tests/core/test-full-name-chunk.vala b/tests/core/test-full-name-chunk.vala index 4648b0c..9dd78c1 100644 --- a/tests/core/test-full-name-chunk.vala +++ b/tests/core/test-full-name-chunk.vala @@ -19,6 +19,7 @@ void main (string[] args) { Test.init (ref args); Test.add_func ("/core/full-name-chunk/property_name_chunk", test_property_name); Test.add_func ("/core/full-name-chunk/is-empty", test_is_empty); + Test.add_func ("/core/full-name-chunk/serialize-basic", test_serialize_basic); Test.run (); } @@ -44,3 +45,23 @@ private void test_is_empty () { chunk.full_name = ""; assert_true (chunk.is_empty); } + +private void test_serialize_basic () { + var contact = new Contacts.Contact.empty (); + var chunk = (Contacts.FullNameChunk) contact.create_chunk ("full-name", null); + + // If the full name is not set, serialization should give a null result + var serialized = chunk.to_gvariant (); + assert_null (serialized); + + // If full name is set, we should have a variant. We don't need to inspect + // the variant, we just need to know it properly deserializes + chunk.full_name = "Niels De Graef"; + serialized = chunk.to_gvariant (); + assert_nonnull (serialized); + + var contact2 = new Contacts.Contact.empty (); + var chunk2 = (Contacts.FullNameChunk) contact2.create_chunk ("full-name", null); + chunk2.apply_gvariant (serialized); + assert_true (chunk2.full_name == "Niels De Graef"); +} diff --git a/tests/core/test-nickname-chunk.vala b/tests/core/test-nickname-chunk.vala index e32ae52..e46efc1 100644 --- a/tests/core/test-nickname-chunk.vala +++ b/tests/core/test-nickname-chunk.vala @@ -19,6 +19,7 @@ void main (string[] args) { Test.init (ref args); Test.add_func ("/core/nickname-chunk/property_name_chunk", test_property_name); Test.add_func ("/core/nickname-chunk/is-empty", test_is_empty); + Test.add_func ("/core/nickname-chunk/serialize-basic", test_serialize_basic); Test.run (); } @@ -44,3 +45,23 @@ private void test_is_empty () { chunk.nickname = ""; assert_true (chunk.is_empty); } + +private void test_serialize_basic () { + var contact = new Contacts.Contact.empty (); + var chunk = (Contacts.NicknameChunk) contact.create_chunk ("nickname", null); + + // If the nickname is not set, serialization should give a null result + var serialized = chunk.to_gvariant (); + assert_null (serialized); + + // If nickname is set, we should have a variant. We don't need to inspect the + // variant, we just need to know it properly deserializes + chunk.nickname = "ndegraef"; + serialized = chunk.to_gvariant (); + assert_nonnull (serialized); + + var contact2 = new Contacts.Contact.empty (); + var chunk2 = (Contacts.NicknameChunk) contact2.create_chunk ("nickname", null); + chunk2.apply_gvariant (serialized); + assert_true (chunk2.nickname == "ndegraef"); +} diff --git a/tests/core/test-phones-chunk.vala b/tests/core/test-phones-chunk.vala index 9fb6c65..bea8784 100644 --- a/tests/core/test-phones-chunk.vala +++ b/tests/core/test-phones-chunk.vala @@ -19,6 +19,7 @@ void main (string[] args) { Test.init (ref args); Test.add_func ("/core/phones-chunk/property-name-chunk", test_property_name); Test.add_func ("/core/phones-chunk/get-is-empty", test_is_empty); + Test.add_func ("/core/phones-chunk/serialize-basic", test_serialize_basic); Test.run (); } @@ -51,3 +52,25 @@ private void test_is_empty () { assert_true (phone.is_empty); assert_true (chunk.is_empty); } + +private void test_serialize_basic () { + var contact = new Contacts.Contact.empty (); + var chunk = (Contacts.PhonesChunk) contact.create_chunk ("phone-numbers", null); + + // If the phones are empty, serialization should give a null result + var serialized = chunk.to_gvariant (); + assert_null (serialized); + + // If a phone is added, we should have a variant. We don't need to inspect + // the variant, we just need to know it properly deserializes + var phone = (Contacts.Phone) chunk.get_item (0); + phone.raw_number = "+321234567"; + serialized = chunk.to_gvariant (); + assert_nonnull (serialized); + + var contact2 = new Contacts.Contact.empty (); + var chunk2 = (Contacts.PhonesChunk) contact2.create_chunk ("phone-numbers", null); + chunk2.apply_gvariant (serialized); + assert_nonnull (chunk.get_item (0)); + assert_true (((Contacts.Phone) chunk.get_item (0)).raw_number == "+321234567"); +} diff --git a/tests/core/test-structured-name-chunk.vala b/tests/core/test-structured-name-chunk.vala new file mode 100644 index 0000000..3283f88 --- /dev/null +++ b/tests/core/test-structured-name-chunk.vala @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2023 Niels De Graef <nielsdegraef@gmail.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 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/>. + */ + +void main (string[] args) { + Test.init (ref args); + Test.add_func ("/core/structured-name-chunk/property_name_chunk", test_property_name); + Test.add_func ("/core/structured-name-chunk/is-empty", test_is_empty); + Test.add_func ("/core/structured-name-chunk/serialize-basic", test_serialize_basic); + Test.run (); +} + +// Make sure that "structured-name" maps to a StructuredNameChunk +private void test_property_name () { + var contact = new Contacts.Contact.empty (); + + var chunk = contact.create_chunk ("structured-name", null); + assert_nonnull (chunk); + assert_true (chunk is Contacts.StructuredNameChunk); + assert_true (chunk.property_name == "structured-name"); +} + +private void test_is_empty () { + var contact = new Contacts.Contact.empty (); + var chunk = (Contacts.StructuredNameChunk) contact.create_chunk ("structured-name", null); + assert_nonnull (chunk); + assert_true (chunk.is_empty); + + chunk.structured_name = new Folks.StructuredName.simple ("Niels", "De Graef"); + assert_false (chunk.is_empty); +} + +private void test_serialize_basic () { + var contact = new Contacts.Contact.empty (); + var chunk = (Contacts.StructuredNameChunk) contact.create_chunk ("structured-name", null); + + // If the name is not set, serialization should give a null result + var serialized = chunk.to_gvariant (); + assert_null (serialized); + + // If name is set, we should have a variant. We don't need to inspect the + // variant, we just need to know it properly deserializes + var old_name = new Folks.StructuredName.simple ("Niels", "De Graef"); + chunk.structured_name = old_name; + serialized = chunk.to_gvariant (); + assert_nonnull (serialized); + + var contact2 = new Contacts.Contact.empty (); + var chunk2 = (Contacts.StructuredNameChunk) contact2.create_chunk ("structured-name", null); + chunk2.apply_gvariant (serialized); + assert_true (chunk2.structured_name.equal (old_name)); +} diff --git a/tests/core/test-urls-chunk.vala b/tests/core/test-urls-chunk.vala index 183db5c..b70964e 100644 --- a/tests/core/test-urls-chunk.vala +++ b/tests/core/test-urls-chunk.vala @@ -20,6 +20,7 @@ void main (string[] args) { Test.add_func ("/core/urls-chunk/property-name-chunk", test_property_name); Test.add_func ("/core/urls-chunk/get-absolute-url", test_get_absolute_url); Test.add_func ("/core/urls-chunk/get-is-empty", test_is_empty); + Test.add_func ("/core/urls-chunk/serialize-basic", test_serialize_basic); Test.run (); } @@ -74,3 +75,25 @@ private void test_is_empty () { assert_true (url.is_empty); assert_true (chunk.is_empty); } + +private void test_serialize_basic () { + var contact = new Contacts.Contact.empty (); + var chunk = (Contacts.UrlsChunk) contact.create_chunk ("urls", null); + + // If the urls are empty, serialization should give a null result + var serialized = chunk.to_gvariant (); + assert_null (serialized); + + // If a url is added, we should have a variant. We don't need to inspect + // the variant, we just need to know it properly deserializes + var url = (Contacts.Url) chunk.get_item (0); + url.raw_url = "https://gnome.org"; + serialized = chunk.to_gvariant (); + assert_nonnull (serialized); + + var contact2 = new Contacts.Contact.empty (); + var chunk2 = (Contacts.UrlsChunk) contact2.create_chunk ("urls", null); + chunk2.apply_gvariant (serialized); + assert_nonnull (chunk.get_item (0)); + assert_true (((Contacts.Url) chunk.get_item (0)).raw_url == "https://gnome.org"); +} diff --git a/tests/io/internal/meson.build b/tests/io/internal/meson.build deleted file mode 100644 index 82590ef..0000000 --- a/tests/io/internal/meson.build +++ /dev/null @@ -1,29 +0,0 @@ -io_internal_testlib = library('io-internal-testlib', - files('test-serialise-common.vala'), - dependencies: libcontacts_dep, -) - -io_internal_testlib_dep = declare_dependency( - link_with: io_internal_testlib, - include_directories: include_directories('.'), -) - -io_internal_test_names = [ - 'serialise-full-name', - 'serialise-structured-name', - 'serialise-nickname', - 'serialise-birthday', - 'serialise-emails', - 'serialise-urls', -] - -foreach _test : io_internal_test_names - test_bin = executable(_test, - files('test-'+_test+'.vala'), - dependencies: [ libcontacts_dep, io_internal_testlib_dep ], - ) - - test(_test, test_bin, - suite: 'io-internal', - ) -endforeach diff --git a/tests/io/internal/test-serialise-birthday.vala b/tests/io/internal/test-serialise-birthday.vala deleted file mode 100644 index 46beef2..0000000 --- a/tests/io/internal/test-serialise-birthday.vala +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 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/>. - */ - -using Folks; - -void main (string[] args) { - Test.init (ref args); - Test.add_func ("/io/serialize_birthday", - Contacts.Tests.Io.test_serialize_birthday); - Test.add_func ("/io/serialize_birthday_pre_epoch", - Contacts.Tests.Io.test_serialize_birthday_pre_epoch); - Test.run (); -} - -namespace Contacts.Tests.Io { - - private void test_serialize_birthday () { - unowned var bd_key = PersonaStore.detail_key (PersonaDetail.BIRTHDAY); - - DateTime old_bd = new GLib.DateTime.utc (1992, 8, 1, 0, 0, 0); - var old_bd_val = Value (typeof (DateTime)); - old_bd_val.set_boxed (old_bd); - - var new_bd_val = _transform_single_value (bd_key, old_bd_val); - assert_true (new_bd_val.type () == typeof (DateTime)); - assert_true (old_bd.equal ((DateTime) new_bd_val.get_boxed ())); - } - - private void test_serialize_birthday_pre_epoch () { - unowned var bd_key = PersonaStore.detail_key (PersonaDetail.BIRTHDAY); - - DateTime old_bd = new GLib.DateTime.utc (1961, 7, 3, 0, 0, 0); - var old_bd_val = Value (typeof (DateTime)); - old_bd_val.set_boxed (old_bd); - - var new_bd_val = _transform_single_value (bd_key, old_bd_val); - assert_true (new_bd_val.type () == typeof (DateTime)); - assert_true (old_bd.equal ((DateTime) new_bd_val.get_boxed ())); - } -} diff --git a/tests/io/internal/test-serialise-common.vala b/tests/io/internal/test-serialise-common.vala deleted file mode 100644 index 8407e2c..0000000 --- a/tests/io/internal/test-serialise-common.vala +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 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/>. - */ - -using Folks; - -namespace Contacts.Tests.Io { - - // Helper to serialize and deserialize an AbstractFieldDetails - public T _transform_single_afd<T> (string prop_key, T afd) { - Gee.Set<T> afd_set = new Gee.HashSet<T> (); - afd_set.add (afd); - - Value val = Value (typeof (Gee.Set)); - val.set_object (afd_set); - - Value emails_value = _transform_single_value (prop_key, val); - var emails_set = emails_value.get_object () as Gee.Set<T>; - if (emails_set == null) - error ("GValue has null value"); - if (emails_set.size != 1) - error ("Expected %d elements but got %d", 1, emails_set.size); - - var deserialized_fd = Utils.get_first<T> (emails_set); - assert_nonnull (deserialized_fd); - - return deserialized_fd; - } - - // Helper to serialize and deserialize a single property with a GLib.Value - public GLib.Value _transform_single_value (string prop_key, GLib.Value val) { - var details = new HashTable<string, Value?> (GLib.str_hash, GLib.str_equal); - details.insert (prop_key, val); - - // Serialize - Variant serialized = Contacts.Io.serialize_to_gvariant_single (details); - if (serialized == null) - error ("Couldn't serialize single-value table for property %s", prop_key); - - // Deserialize - var details_deserialized = Contacts.Io.deserialize_gvariant_single (serialized); - if (details_deserialized == null) - error ("Couldn't deserialize details for property %s", prop_key); - - if (!details_deserialized.contains (prop_key)) - error ("Deserialized details doesn't contain value for property %s", prop_key); - Value? val_deserialized = details_deserialized.lookup (prop_key); - if (val_deserialized.type() == GLib.Type.NONE) - error ("Deserialized Value is unset"); - - return val_deserialized; - } -} diff --git a/tests/io/internal/test-serialise-emails.vala b/tests/io/internal/test-serialise-emails.vala deleted file mode 100644 index 27b15ac..0000000 --- a/tests/io/internal/test-serialise-emails.vala +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 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/>. - */ - -using Folks; - -void main (string[] args) { - Test.init (ref args); - Test.add_func ("/io/serialize_emails", - Contacts.Tests.Io.test_serialize_emails); - Test.run (); -} - -namespace Contacts.Tests.Io { - - private void test_serialize_emails () { - unowned var emails_key = PersonaStore.detail_key (PersonaDetail.EMAIL_ADDRESSES); - - var old_fd = new EmailFieldDetails ("nielsdegraef@gmail.com"); - var new_fd = _transform_single_afd<EmailFieldDetails> (emails_key, old_fd); - - if (!(new_fd is EmailFieldDetails)) - error ("Expected EmailFieldDetails but got %s", new_fd.get_type ().name ()); - - if (old_fd.value != new_fd.value) - error ("Expected '%s' but got '%s'", old_fd.value, new_fd.value); - } -} diff --git a/tests/io/internal/test-serialise-full-name.vala b/tests/io/internal/test-serialise-full-name.vala deleted file mode 100644 index 9da8319..0000000 --- a/tests/io/internal/test-serialise-full-name.vala +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 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/>. - */ - -using Folks; - -void main (string[] args) { - Test.init (ref args); - Test.add_func ("/io/serialize_full_name_simple", - Contacts.Tests.Io.test_serialize_full_name_simple); - Test.run (); -} - -namespace Contacts.Tests.Io { - - private void test_serialize_full_name_simple () { - unowned var fn_key = PersonaStore.detail_key (PersonaDetail.FULL_NAME); - - string old_fn = "Niels De Graef"; - Value old_fn_val = Value (typeof (string)); - old_fn_val.set_string (old_fn); - - var new_fn_val = _transform_single_value (fn_key, old_fn_val); - if (new_fn_val.type () != typeof (string)) - error ("Expected G_TYPE_STRING but got %s", new_fn_val.type ().name ()); - if (old_fn != new_fn_val.get_string ()) - error ("Expected '%s' but got '%s'", old_fn, new_fn_val.get_string ()); - } -} diff --git a/tests/io/internal/test-serialise-nickname.vala b/tests/io/internal/test-serialise-nickname.vala deleted file mode 100644 index 649b638..0000000 --- a/tests/io/internal/test-serialise-nickname.vala +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 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/>. - */ - -using Folks; - -void main (string[] args) { - Test.init (ref args); - Test.add_func ("/io/serialize_nickame", - Contacts.Tests.Io.test_serialize_nickname); - Test.run (); -} - -namespace Contacts.Tests.Io { - - private void test_serialize_nickname () { - unowned var nick_key = PersonaStore.detail_key (PersonaDetail.NICKNAME); - - string old_nick = "nielsdg"; - var old_nick_val = Value (typeof (string)); - old_nick_val.set_string (old_nick); - - var new_nick_val = _transform_single_value (nick_key, old_nick_val); - assert_true (new_nick_val.type () == typeof (string)); - assert_true (old_nick == new_nick_val.get_string ()); - } -} diff --git a/tests/io/internal/test-serialise-structured-name.vala b/tests/io/internal/test-serialise-structured-name.vala deleted file mode 100644 index 45f2093..0000000 --- a/tests/io/internal/test-serialise-structured-name.vala +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 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/>. - */ - -using Folks; - -void main (string[] args) { - Test.init (ref args); - Test.add_func ("/io/serialize_structured_name_simple", - Contacts.Tests.Io.test_serialize_structured_name_simple); - Test.run (); -} - -namespace Contacts.Tests.Io { - - private void test_serialize_structured_name_simple () { - unowned var sn_key = PersonaStore.detail_key (PersonaDetail.STRUCTURED_NAME); - - var old_sn = new StructuredName.simple ("Niels", "De Graef"); - Value old_sn_val = Value (typeof (StructuredName)); - old_sn_val.set_object (old_sn); - - var new_sn_val = _transform_single_value (sn_key, old_sn_val); - - if (new_sn_val.type () != typeof (StructuredName)) - error ("Expected FOLKS_TYPE_STRUCTURED_NAME but got %s", new_sn_val.type ().name ()); - - var new_sn = new_sn_val.get_object () as StructuredName; - if (!old_sn.equal (new_sn)) - error ("Expected '%s' but got '%s'", old_sn.to_string (), new_sn.to_string ()); - } -} diff --git a/tests/io/internal/test-serialise-urls.vala b/tests/io/internal/test-serialise-urls.vala deleted file mode 100644 index cf4cdf9..0000000 --- a/tests/io/internal/test-serialise-urls.vala +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 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/>. - */ - -using Folks; - -void main (string[] args) { - Test.init (ref args); - Test.add_func ("/io/serialize_urls_single", - Contacts.Tests.Io.test_serialize_urls_single); - Test.run (); -} - -namespace Contacts.Tests.Io { - - private void test_serialize_urls_single () { - unowned var urls_key = PersonaStore.detail_key (PersonaDetail.URLS); - - var old_fd = new UrlFieldDetails ("http://www.islinuxaboutchoice.com/"); - var new_fd = _transform_single_afd<UrlFieldDetails> (urls_key, old_fd); - - if (!(new_fd is UrlFieldDetails)) - error ("Expected UrlFieldDetails but got %s", new_fd.get_type ().name ()); - - if (old_fd.value != new_fd.value) - error ("Expected '%s' but got '%s'", old_fd.value, new_fd.value); - } -} diff --git a/tests/io/meson.build b/tests/io/meson.build index 2f34960..99bbbc0 100644 --- a/tests/io/meson.build +++ b/tests/io/meson.build @@ -1,2 +1 @@ -subdir('internal') subdir('vcard') diff --git a/tests/io/vcard/meson.build b/tests/io/vcard/meson.build index 9967815..38f2a66 100644 --- a/tests/io/vcard/meson.build +++ b/tests/io/vcard/meson.build @@ -6,6 +6,7 @@ test_deps = [ gee, folks, libebook, + libcontactscore_dep, ] foreach vcard_name : io_vcard_files diff --git a/tests/io/vcard/test-vcard-minimal-import.vala b/tests/io/vcard/test-vcard-minimal-import.vala index bef6596..544fdad 100644 --- a/tests/io/vcard/test-vcard-minimal-import.vala +++ b/tests/io/vcard/test-vcard-minimal-import.vala @@ -19,43 +19,38 @@ using Folks; void main (string[] args) { Test.init (ref args); - Test.add_func ("/io/test_vcard_minimal", - Contacts.Tests.Io.test_vcard_minimal); + Test.add_func ("/io/test_vcard_minimal", test_vcard_minimal); Test.run (); } -namespace Contacts.Tests.Io { - private void test_vcard_minimal () { - unowned var vcf_path = Environment.get_variable ("_VCF_FILE"); - if (vcf_path == null || vcf_path == "") - error ("No .vcf file set as envvar. Please use the meson test suite"); - - var file = GLib.File.new_for_path (vcf_path); - if (!file.query_exists ()) - error (".vcf file that is used as test input doesn't exist"); - - var parser = new Contacts.Io.VCardParser (); - HashTable<string, Value?>[] details_list = null; - try { - details_list = parser.parse (file.read (null)); - } catch (Error err) { - error ("Error while importing: %s", err.message); - } - if (details_list == null) - error ("VCardParser returned null"); - - if (details_list.length != 1) - error ("VCardParser parsed %u elements instead of 1", details_list.length); - - unowned var details = details_list[0]; - - unowned var fn_key = PersonaStore.detail_key (PersonaDetail.FULL_NAME); - if (!details.contains (fn_key)) - error ("No FN value"); - - var fn_value = details.lookup (fn_key); - unowned var fn = fn_value as string; - if (fn != "Niels De Graef") - error ("Expected '%s' but got '%s'", "Niels De Graef", fn); +private void test_vcard_minimal () { + unowned var vcf_path = Environment.get_variable ("_VCF_FILE"); + if (vcf_path == null || vcf_path == "") + error ("No .vcf file set as envvar. Please use the meson test suite"); + + var file = File.new_for_path (vcf_path); + if (!file.query_exists ()) + error (".vcf file that is used as test input doesn't exist"); + + var parser = new Contacts.Io.VCardParser (); + Contacts.Contact[]? contacts = null; + try { + contacts = parser.parse (file.read (null)); + } catch (Error err) { + error ("Error while importing: %s", err.message); } + + if (contacts == null) + error ("VCardParser returned null"); + if (contacts.length != 1) + error ("VCardParser parsed %u elements instead of 1", contacts.length); + + unowned var contact = contacts[0]; + var chunk = contact.get_most_relevant_chunk ("full-name", true); + if (chunk == null) + error ("Expected FullNameChunk, but got null"); + + unowned var fn_chunk = (Contacts.FullNameChunk) chunk; + if (fn_chunk.full_name != "Niels De Graef") + error ("Expected '%s' but got '%s'", "Niels De Graef", fn_chunk.full_name); } |