From fc83b064d5eba43c160f12e9645adedbea913044 Mon Sep 17 00:00:00 2001 From: Seif Lotfy Date: Sat, 18 Aug 2012 14:46:11 +0200 Subject: [PATCH] Add foreach support for ResultSet Added iterator class for ResultSet to support foreach statements. Added unit test --- libzeitgeist/result-set.vala | 15 ++++++++++- libzeitgeist/simple-result-set.vala | 9 +++---- test/direct/Makefile.am | 2 ++ test/direct/datamodel-test.vala | 51 +++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 6 deletions(-) diff --git a/libzeitgeist/result-set.vala b/libzeitgeist/result-set.vala index c43ffe3..2208b44 100644 --- a/libzeitgeist/result-set.vala +++ b/libzeitgeist/result-set.vala @@ -83,7 +83,10 @@ public interface ResultSet : Object * * @return The #ZeitgeistEvent at the current cursor position */ - public abstract Event next (); + public bool next () + { + return this.has_next (); + } /** * Check if a call to zeitgeist_result_set_next() will succeed. @@ -125,6 +128,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 get (); } } diff --git a/libzeitgeist/simple-result-set.vala b/libzeitgeist/simple-result-set.vala index dc43f67..682dd40 100644 --- a/libzeitgeist/simple-result-set.vala +++ b/libzeitgeist/simple-result-set.vala @@ -56,11 +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; @@ -81,6 +76,10 @@ internal class SimpleResultSet : Object, ResultSet return cursor; } + public Event get () + { + return events.get (cursor++); + } } } 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