Add tests for Map.entries
authorMaciej Piechotka <uzytkownik2@gmail.com>
Sun, 27 Sep 2009 23:41:53 +0000 (01:41 +0200)
committerDidier 'Ptitjes <ptitjes@free.fr>
Mon, 28 Sep 2009 15:47:49 +0000 (17:47 +0200)
tests/testmap.vala

index e416263..f69ee66 100644 (file)
@@ -34,6 +34,7 @@ public abstract class MapTests : Gee.TestCase {
                          test_has_key_size_is_empty);
                add_test ("[Map] keys", test_keys);
                add_test ("[Map] values", test_values);
+               add_test ("[Map] entries", test_entries);
                add_test ("[Map] set all", test_set_all);
                add_test ("[Map] unset all", test_unset_all);
                add_test ("[Map] has all", test_has_all);
@@ -242,6 +243,45 @@ public abstract class MapTests : Gee.TestCase {
                assert (valueCollection.size == 0);
        }
 
+       public void test_entries () {
+               // Check entries on empty map
+               var entryCollection = test_map.entries;
+               assert (entryCollection.size == 0);
+
+               // Check entries on map with one item
+               test_map.set ("one", "value_of_one");
+               assert (entryCollection.size == 1);
+               assert (entryCollection.contains (new TestEntry<string,string> ("one", "value_of_one")));
+               entryCollection = test_map.entries;
+               assert (entryCollection.size == 1);
+               assert (entryCollection.contains (new TestEntry<string,string> ("one", "value_of_one")));
+
+               // Check modify entry set directly
+               if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
+                                      TestTrapFlags.SILENCE_STDERR)) {
+                       assert (! entryCollection.add (new TestEntry<string,string> ("two", "value_of_two")));
+                       return;
+               }
+               Test.trap_assert_failed ();
+               Test.trap_assert_stderr ("*code should not be reached*");
+
+               // Check entries on map with multiple items
+               test_map.set ("two", "value_of_two");
+               assert (entryCollection.size == 2);
+               assert (entryCollection.contains (new TestEntry<string,string> ("one", "value_of_one")));
+               assert (entryCollection.contains (new TestEntry<string,string> ("two", "value_of_two")));
+               entryCollection = test_map.entries;
+               assert (entryCollection.size == 2);
+               assert (entryCollection.contains (new TestEntry<string,string> ("one", "value_of_one")));
+               assert (entryCollection.contains (new TestEntry<string,string> ("two", "value_of_two")));
+
+               // Check keys on map clear
+               test_map.clear ();
+               assert (entryCollection.size == 0);
+               entryCollection = test_map.entries;
+               assert (entryCollection.size == 0);
+       }
+
        public void test_set_all () {
                var another_map = new HashMap<string,string> (str_hash,
                                                              str_equal,
@@ -462,4 +502,17 @@ public abstract class MapTests : Gee.TestCase {
                assert (value.get_int () == test_map.size);
                value.unset ();
        }
+       
+       
+       
+       public class TestEntry<K,V> : Map.Entry<K,V> {
+               public TestEntry (K key, V value) {
+                       this._key = key;
+                       this.value = value;
+               }
+               
+               public override K key { get {return _key; } }
+               private K _key;
+               public override V value { get; set; }
+       }
 }