[geary/mjog/invert-folder-class-hierarchy: 48/72] CollectionAssertions: Allow elements to be accessed via index




commit 9a83c17f6967fc1e4f2597784fc0a5eb3b047a4e
Author: Michael Gratton <mike vee net>
Date:   Fri Feb 19 08:36:24 2021 +1100

    CollectionAssertions: Allow elements to be accessed via index

 .../vala-unit/src/collection-assertions.vala       | 28 ++++++++++++++++++++++
 1 file changed, 28 insertions(+)
---
diff --git a/subprojects/vala-unit/src/collection-assertions.vala 
b/subprojects/vala-unit/src/collection-assertions.vala
index 3b34acda1..4a14d1a91 100644
--- a/subprojects/vala-unit/src/collection-assertions.vala
+++ b/subprojects/vala-unit/src/collection-assertions.vala
@@ -79,6 +79,18 @@ public interface ValaUnit.CollectionAssertions<E> : GLib.Object {
         throws GLib.Error;
 
 
+    /**
+     * Returns the element at the given index.
+     *
+     * This method allows access to collection contents directly, so
+     * they can be further inspected.
+     *
+     * Note the give position is 0-based, not 1-based.
+     */
+    public abstract new E get(long position)
+        throws GLib.Error;
+
+
 }
 
 internal class ValaUnit.StringCollectionAssertion : GLib.Object,
@@ -177,6 +189,10 @@ internal class ValaUnit.StringCollectionAssertion : GLib.Object,
         return this;
     }
 
+    public new string get(long position) throws GLib.Error {
+        return this.actual.substring(position, 1);
+    }
+
 }
 
 
@@ -330,6 +346,10 @@ internal class ValaUnit.ArrayCollectionAssertion<E> : GLib.Object,
         return this;
     }
 
+    public new E get(long position) throws GLib.Error {
+        return this.actual[position];
+    }
+
     private string to_collection_display() {
         var buf = new GLib.StringBuilder();
         int len = this.actual.length;
@@ -471,6 +491,14 @@ internal class ValaUnit.GeeCollectionAssertion<E> :
         return this;
     }
 
+    public new E get(long position) throws GLib.Error {
+        Gee.Iterator<E> iterator = this.actual.iterator();
+        for (int i = 0; i <= position; i++) {
+            iterator.next();
+        }
+        return iterator.get();
+    }
+
     private string to_collection_display() {
         var buf = new GLib.StringBuilder();
         int len = this.actual.size;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]