From 0e66d35269a3314ea55362e782f79e2527cb11d8 Mon Sep 17 00:00:00 2001 From: Seif Lotfy Date: Sat, 18 Aug 2012 16:13:26 +0200 Subject: [PATCH] Added foreach support for ResultSet Added foreach support for ResultSet by adding next_value method Added unit test --- libzeitgeist/result-set.vala | 34 +++++++---------------- libzeitgeist/simple-result-set.vala | 17 +++++------- test/direct/Makefile.am | 2 ++ test/direct/datamodel-test.vala | 51 +++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 34 deletions(-) diff --git a/libzeitgeist/result-set.vala b/libzeitgeist/result-set.vala index c43ffe3..77000f6 100644 --- a/libzeitgeist/result-set.vala +++ b/libzeitgeist/result-set.vala @@ -72,30 +72,6 @@ public interface ResultSet : Object public abstract uint estimated_matches (); /** - * Get the current event from the result set and advance the cursor. - * To ensure that calls to this method will succeed you can call - * zeitgeist_result_set_has_next(). - * - * To retrieve the current event without advancing the cursor call - * zeitgeist_result_set_peek() in stead of this method. - * - * @param self The #ZeitgeistResultSet to get an event from - * - * @return The #ZeitgeistEvent at the current cursor position - */ - public abstract Event next (); - - /** - * Check if a call to zeitgeist_result_set_next() will succeed. - * - * @param self The #ZeitgeistResultSet to check - * - * @return %TRUE if and only if more events can be retrieved by calling - * zeitgeist_result_set_next() - */ - public abstract bool has_next (); - - /** * Get the event at the current cursor position. * * To retrieve the current event and advance the cursor position call @@ -125,6 +101,16 @@ public interface ResultSet : Object */ public abstract uint tell (); + /** + * Get an iterator object + * @param self The #ZeitgeistResultSet to seek in + */ + public ResultSet iterator () { + return this; + } + + public abstract Event? next_value (); + } } diff --git a/libzeitgeist/simple-result-set.vala b/libzeitgeist/simple-result-set.vala index dc43f67..ae5e769 100644 --- a/libzeitgeist/simple-result-set.vala +++ b/libzeitgeist/simple-result-set.vala @@ -56,16 +56,6 @@ internal class SimpleResultSet : Object, ResultSet return num_estimated_matches; } - public Event next () - { - return events.get (cursor++); - } - - public bool has_next () - { - return cursor < events.length; - } - public Event peek () { return events.get (cursor); @@ -81,6 +71,13 @@ internal class SimpleResultSet : Object, ResultSet return cursor; } + public Event? next_value () + { + if (cursor < events.length) + return events.get (cursor++); + return null; + } + } } diff --git a/test/direct/Makefile.am b/test/direct/Makefile.am index c6a3958..c44fdc1 100644 --- a/test/direct/Makefile.am +++ b/test/direct/Makefile.am @@ -37,6 +37,8 @@ SRC_FILES = \ $(top_srcdir)/libzeitgeist/ontology.vala \ $(top_srcdir)/libzeitgeist/ontology-uris.vala \ $(top_srcdir)/libzeitgeist/mimetype.vala \ + $(top_srcdir)/libzeitgeist/result-set.vala \ + $(top_srcdir)/libzeitgeist/simple-result-set.vala \ $(NULL) datamodel-test: datamodel-test.vala $(SRC_FILES) diff --git a/test/direct/datamodel-test.vala b/test/direct/datamodel-test.vala index e565cf5..c61b585 100644 --- a/test/direct/datamodel-test.vala +++ b/test/direct/datamodel-test.vala @@ -19,12 +19,14 @@ */ using Zeitgeist; +using Assertions; int main (string[] argv) { Test.init (ref argv); Test.add_func ("/Datamodel/MatchesTemplate/anything", matches_template_anything_test); + Test.add_func ("/Datamodel/MatchesTemplate/foreach", foreach_test); return Test.run (); } @@ -67,4 +69,53 @@ void matches_template_anything_test () assert (!event.matches_template (templ)); } +Subject create_subject () +{ + var s = new Subject (); + s.uri = "scheme:///uri"; + s.interpretation = "subject_interpretation_uri"; + s.manifestation = "subject_manifestation_uri"; + s.mimetype = "text/plain"; + s.origin = "scheme:///"; + s.text = "Human readable text"; + s.storage = ""; + s.current_uri = "scheme:///uri"; + + return s; +} + +Event create_event () +{ + var e = new Event (); + e.id = 1234; + e.timestamp = 1234567890L; + e.interpretation = "interpretation_uri"; + e.manifestation = "manifestation_uri"; + e.actor = "test.desktop"; + e.origin = "source"; + + return e; +} + +void foreach_test () +{ + GenericArray events = new GenericArray (); + for (int i = 0; i < 1000; i++) + { + var e = create_event (); + e.id = i; + e.add_subject (create_subject ()); + events.add (e); + } + + SimpleResultSet result_set = new SimpleResultSet (events); + int i = 0; + foreach (Event e in result_set) + { + assert_cmpint ((int) e.id, OperatorType.EQUAL, i); + i++; + } + +} + // vim:expandtab:ts=4:sw=4 -- 1.7.9.5