diff options
author | Niels De Graef <nielsdegraef@gmail.com> | 2023-03-04 09:08:39 +0100 |
---|---|---|
committer | Niels De Graef <nielsdegraef@gmail.com> | 2023-03-04 09:08:39 +0100 |
commit | d62d47ad8a65d24631d5243578cbb785fc4f5ef0 (patch) | |
tree | 269e19d78c4b071683ed46e30775b92abff448bc | |
parent | 1cba54b1eb34011c55867d803b601a74b856ba1a (diff) | |
download | gnome-contacts-d62d47ad8a65d24631d5243578cbb785fc4f5ef0.tar.gz |
tests: Improve and add vCard testsnielsdg/more-vcard-tests
Add a few more vCard tests to make sure we're not regressing on our
vCard import feature.
One of the design desicions at the time was to make the
`Contacts.Io.Parser` take a `GLib.InputStream` so we can make it
agnostic of what kind of input was given. We can now use this to make
the vCard tests a bit simpler in general by using a constant string
inside the test file rather than having to deal with environment
variables to pass on a path.
-rw-r--r-- | tests/io/vcard/meson.build | 22 | ||||
-rw-r--r-- | tests/io/vcard/minimal.vcf | 4 | ||||
-rw-r--r-- | tests/io/vcard/test-vcard-bday.vala | 61 | ||||
-rw-r--r-- | tests/io/vcard/test-vcard-email.vala | 104 | ||||
-rw-r--r-- | tests/io/vcard/test-vcard-minimal.vala (renamed from tests/io/vcard/test-vcard-minimal-import.vala) | 24 |
5 files changed, 186 insertions, 29 deletions
diff --git a/tests/io/vcard/meson.build b/tests/io/vcard/meson.build index 38f2a66..14fef12 100644 --- a/tests/io/vcard/meson.build +++ b/tests/io/vcard/meson.build @@ -1,5 +1,8 @@ -io_vcard_files = [ +io_vcard_tests = [ 'minimal', + + 'bday', + 'email', ] test_deps = [ @@ -9,25 +12,18 @@ test_deps = [ libcontactscore_dep, ] -foreach vcard_name : io_vcard_files - vcf_file = meson.current_source_dir() / vcard_name + '.vcf' - - # Ideally we'd do this using a preprocessor symbol or something - vcf_test_env = environment() - vcf_test_env.append('_VCF_FILE', vcf_file) - +foreach test_name : io_vcard_tests test_sources = [ contacts_io_sources, - 'test-vcard-'+vcard_name+'-import.vala', + 'test-vcard-'+test_name+'.vala', ] - test_bin = executable(vcard_name, + test_bin = executable(test_name, test_sources, dependencies: test_deps, ) - test(vcard_name, test_bin, - suite: 'io-vcard', - env: vcf_test_env, + test(test_name, test_bin, + suite: 'vcard', ) endforeach diff --git a/tests/io/vcard/minimal.vcf b/tests/io/vcard/minimal.vcf deleted file mode 100644 index b360c5a..0000000 --- a/tests/io/vcard/minimal.vcf +++ /dev/null @@ -1,4 +0,0 @@ -BEGIN:VCARD -VERSION:3.0 -FN:Niels De Graef -END:VCARD diff --git a/tests/io/vcard/test-vcard-bday.vala b/tests/io/vcard/test-vcard-bday.vala new file mode 100644 index 0000000..0a54ac2 --- /dev/null +++ b/tests/io/vcard/test-vcard-bday.vala @@ -0,0 +1,61 @@ +/* + * 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/test_vcard_bday_yyyymmdd", test_vcard_bday_yyyymmdd); + Test.run (); +} + +const string VCARD_BDAY_YYYYMMDD = +""" +BEGIN:VCARD +VERSION:3.0 +FN:Niels De Graef +BDAY:19920801 +END:VCARD +"""; + +private void test_vcard_bday_yyyymmdd () { + var input = new MemoryInputStream.from_data (VCARD_BDAY_YYYYMMDD.data); + + var parser = new Contacts.Io.VCardParser (); + Contacts.Contact[]? contacts = null; + try { + contacts = parser.parse (input); + } catch (Error err) { + error ("Error while importing: %s", err.message); + } + + assert_nonnull (contacts); + 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 ("birthday", true); + assert_nonnull (chunk); + + unowned var bday_chunk = (Contacts.BirthdayChunk) chunk; + assert_nonnull (bday_chunk.birthday); + + int y, m, d; + bday_chunk.birthday.get_ymd (out y, out m, out d); + if (y != 1992 || m != 8 || d != 1) + error ("Expected '1992-08-01' but got %d-%d-%d", y, m, d); +} diff --git a/tests/io/vcard/test-vcard-email.vala b/tests/io/vcard/test-vcard-email.vala new file mode 100644 index 0000000..5ebecc7 --- /dev/null +++ b/tests/io/vcard/test-vcard-email.vala @@ -0,0 +1,104 @@ +/* + * 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/test_vcard_single_email", test_vcard_single_email); + Test.add_func ("/io/test_vcard_multiple_email", test_vcard_multiple_email); + Test.run (); +} + +const string VCARD_SINGLE_EMAIL = +""" +BEGIN:VCARD +VERSION:3.0 +FN:Niels De Graef +EMAIL;TYPE=HOME:nielsdegraef@gmail.com +END:VCARD +"""; + +private void test_vcard_single_email () { + var input = new MemoryInputStream.from_data (VCARD_SINGLE_EMAIL.data); + + var parser = new Contacts.Io.VCardParser (); + Contacts.Contact[]? contacts = null; + try { + contacts = parser.parse (input); + } catch (Error err) { + error ("Error while importing: %s", err.message); + } + + assert_nonnull (contacts); + 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 ("email-addresses", true); + assert_nonnull (chunk); + + unowned var emails_chunk = (Contacts.EmailAddressesChunk) chunk; + var email_addr = (Contacts.EmailAddress) emails_chunk.get_item (0); + if (email_addr.raw_address != "nielsdegraef@gmail.com") + error ("Expected nielsdegraef@gmail.com but got '%s'", + email_addr.raw_address); +} + +const string VCARD_MULTIPLE_EMAIL = +""" +BEGIN:VCARD +VERSION:3.0 +FN:Niels De Graef +EMAIL;TYPE=HOME:nielsdegraef@gmail.com +EMAIL;TYPE=WORK:ndegraef@redhat.com +END:VCARD +"""; + +private void test_vcard_multiple_email () { + var input = new MemoryInputStream.from_data (VCARD_MULTIPLE_EMAIL.data); + + var parser = new Contacts.Io.VCardParser (); + Contacts.Contact[]? contacts = null; + try { + contacts = parser.parse (input); + } catch (Error err) { + error ("Error while importing: %s", err.message); + } + + assert_nonnull (contacts); + 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 ("email-addresses", true); + assert_nonnull (chunk); + + unowned var emails_chunk = (Contacts.EmailAddressesChunk) chunk; + + // First email address + var email_addr1 = (Contacts.EmailAddress) emails_chunk.get_item (0); + if (email_addr1.raw_address != "nielsdegraef@gmail.com") + error ("Expected nielsdegraef@gmail.com but got '%s'", + email_addr1.raw_address); + + // Second email address + var email_addr2 = (Contacts.EmailAddress) emails_chunk.get_item (1); + if (email_addr2.raw_address != "ndegraef@redhat.com") + error ("Expected ndegraef@redhat.com but got '%s'", + email_addr2.raw_address); +} diff --git a/tests/io/vcard/test-vcard-minimal-import.vala b/tests/io/vcard/test-vcard-minimal.vala index 544fdad..a09f3b5 100644 --- a/tests/io/vcard/test-vcard-minimal-import.vala +++ b/tests/io/vcard/test-vcard-minimal.vala @@ -23,32 +23,32 @@ void main (string[] args) { Test.run (); } -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"); +const string VCARD_MINIMAL = +""" +BEGIN:VCARD +VERSION:3.0 +FN:Niels De Graef +END:VCARD +"""; - var file = File.new_for_path (vcf_path); - if (!file.query_exists ()) - error (".vcf file that is used as test input doesn't exist"); +private void test_vcard_minimal () { + var input = new MemoryInputStream.from_data (VCARD_MINIMAL.data); var parser = new Contacts.Io.VCardParser (); Contacts.Contact[]? contacts = null; try { - contacts = parser.parse (file.read (null)); + contacts = parser.parse (input); } catch (Error err) { error ("Error while importing: %s", err.message); } - if (contacts == null) - error ("VCardParser returned null"); + assert_nonnull (contacts); 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"); + assert_nonnull (chunk); unowned var fn_chunk = (Contacts.FullNameChunk) chunk; if (fn_chunk.full_name != "Niels De Graef") |