Imports System
Imports System.Text
Imports System.Collections.Generic
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports Mercury.Mobile

<TestClass()> _
Public Class Iterator_tests

    Private testContextInstance As TestContext

    '''<summary>
    '''Gets or sets the test context which provides
    '''information about and functionality for the current test run.
    '''</summary>
    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(ByVal value As TestContext)
            testContextInstance = value
        End Set
    End Property

#Region "Additional test attributes"
    '
    ' You can use the following additional attributes as you write your tests:
    '
    ' Use ClassInitialize to run code before running the first test in the class
    ' <ClassInitialize()> Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)
    ' End Sub
    '
    ' Use ClassCleanup to run code after all tests in a class have run
    ' <ClassCleanup()> Public Shared Sub MyClassCleanup()
    ' End Sub
    '
    ' Use TestInitialize to run code before running each test
    ' <TestInitialize()> Public Sub MyTestInitialize()
    ' End Sub
    '
    ' Use TestCleanup to run code after each test has run
    ' <TestCleanup()> Public Sub MyTestCleanup()
    ' End Sub
    '
#End Region

    Private _numbers As List(Of Integer)

    <TestMethod()> _
    Public Sub Iterator_iterates_through_indexed_collection()
        ' Setup collection
        Me._numbers = New List(Of Integer)()
        For i As Integer = 0 To 999
            Me._numbers.Add(i + 1)
        Next

        ' Setup iterator
        Dim index As Integer = -1
        Dim enumerator = New Iterator(Of Integer, Integer)(AddressOf Iterator_Method, index)

        Dim cIdx As Integer = 0
        For Each current As Integer In enumerator
            Assert.IsTrue(current = cIdx + 1)
            cIdx += 1
        Next
    End Sub

    Private Function Iterator_Method(ByRef current As Integer, ByRef state As Integer) As Boolean
        If state < Me._numbers.Count - 1 Then
            state += 1
            current = Me._numbers(state)
            Return True
        End If
        Return False
    End Function

End Class
