VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "s_IteratorLinkAll"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' ====== Test link iterator for all links ======
Option Explicit

' TODO:
' Iteration
' Public Function RemoveRow()
' Public Function Find(target$, colN As LinkStruct) As Boolean
' Public Function ImportData(src As Object)

Private db_ As InfoDatabase
Private iterator_ As IteratorLinkAll

Public Function Setup()
  ' Mandatory setup function
  Set iterator_ = New IteratorLinkAll
  Set db_ = New InfoDatabase
  Dim iPath As New API_Path: Call iPath.FromString(TEST_FOLDER).ToGlobal(ThisWorkbook.Path)
  Call db_.Init(ThisWorkbook, iPath.Text)
End Function

Public Function Teardown()
  ' Mandatory teardown function
  Call ClearAll
End Function

Public Function t_Init()
  On Error GoTo PROPAGATE_ERROR
  
  Call Dev_NewCase("Empty DB")
  Call iterator_.Init(db_, ThisWorkbook.Sheets(HT03_SHT_LINKS))
  Call Dev_ExpectEQ(HT03_DEFAULT_FIRST_LINE, iterator_.Row)
  
  Call Dev_NewCase("Non-empty row")
  Dim newData As New ItemLaw: Set newData = GetDummyData
  Call db_.SetItem(newData.Clone)
  Call db_.SetLinksFor(newData.gid_, GenerateLinks)
  Call Dev_ExpectEQ(newData.gid_, iterator_.SourceID)
  Call Dev_ExpectTrue(db_.SetLinksFor(newData.gid_, GenerateLinks))

  Call Dev_NewCase("Custom empty row")
  Call iterator_.Init(db_, ThisWorkbook.Sheets(HT03_SHT_LINKS), db_.CountLinks + 10)
  Call Dev_ExpectEQ(0, iterator_.SourceID)
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_IsProcessed()
  On Error GoTo PROPAGATE_ERROR
  
  Set iterator_ = db_.Links
  Call Dev_ExpectTrue(iterator_.IsProcessed, "Empty DB")
  
  Call db_.SetItem(GetDummyData)
  Call db_.SetItem(GetDummyData)
  Dim law1 As IteratorLaw: Set law1 = db_.Laws
  Dim law2 As IteratorLaw: Set law2 = db_.Laws: Call law2.Increment
  law1.Status = T_LAWS_FULL
  law2.Status = T_LAWS_FULL
  law2.ID = law1.ID + 1
  Call db_.Recreate
  Dim nInvalid&: nInvalid = law1.ID + 2
  
  Call Dev_NewCase("Out links")
  iterator_.SourceID = law1.ID
  iterator_.DestinationID = nInvalid
  iterator_.LType = T_LINK_DEFAULT
  iterator_.Status = T_LINKS_ADD
  Call Dev_ExpectFalse(iterator_.IsProcessed, "Link not processed")
  
  iterator_.Status = T_LINKS_IGNORE
  Call Dev_ExpectTrue(iterator_.IsProcessed, "Ignored link")
  iterator_.Status = T_LINKS_ADD
  
  law1.Status = T_LAWS_OUTER
  Call db_.Recreate
  Call Dev_ExpectTrue(iterator_.IsProcessed, "Link from outer law")
  
  law1.Status = T_LAWS_FRINGE
  Call db_.Recreate
  Call Dev_ExpectTrue(iterator_.IsProcessed, "Link from fringe law to undefined law")
  law1.Status = T_LAWS_FULL
  Call db_.Recreate
  
  Call Dev_NewCase("Inside links")
  iterator_.DestinationID = law2.ID
  Call Dev_ExpectFalse(iterator_.IsProcessed, "Link not processed")
  
  law2.Status = T_LAWS_IGNORE
  Call db_.Recreate
  Call Dev_ExpectTrue(iterator_.IsProcessed, "Link to ignored")
  
  law1.Status = T_LAWS_FRINGE
  law2.Status = T_LAWS_FRINGE
  Call db_.Recreate
  Call Dev_ExpectFalse(iterator_.IsProcessed, "Link between fringe laws")
  
  law1.Status = T_LAWS_FULL
  law2.Status = T_LAWS_FULL
  Call db_.Recreate
  iterator_.LType = T_LINK_ASPECT
  Call Dev_ExpectTrue(iterator_.IsProcessed, "Processed link: aspect")
  iterator_.LType = T_LINK_DEFINITION
  Call Dev_ExpectTrue(iterator_.IsProcessed, "Processed link: definition")
  iterator_.LType = T_LINK_FOUNDATION
  Call Dev_ExpectTrue(iterator_.IsProcessed, "Processed link: foundation")
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_IsActive()
  On Error GoTo PROPAGATE_ERROR
  
  Set iterator_ = db_.Links
  Call Dev_ExpectFalse(iterator_.IsActive, "Empty DB")
  
  Call db_.SetItem(GetDummyData)
  Call db_.SetItem(GetDummyData)
  Dim law1 As IteratorLaw: Set law1 = db_.Laws
  Dim law2 As IteratorLaw: Set law2 = db_.Laws: Call law2.Increment
  law1.Status = T_LAWS_FULL
  law2.Status = T_LAWS_FULL
  law2.ID = law1.ID + 1
  Call db_.Recreate
  Dim nInvalid&: nInvalid = law1.ID + 2
  
  iterator_.SourceID = law1.ID
  iterator_.DestinationID = nInvalid
  iterator_.LType = T_LINK_ASPECT
  iterator_.Status = T_LINKS_ADD
  Call Dev_ExpectFalse(iterator_.IsActive, "Link to unknown law")
  
  iterator_.SourceID = nInvalid
  iterator_.DestinationID = law2.ID
  Call Dev_ExpectFalse(iterator_.IsActive, "Link from unknown law")
  
  iterator_.SourceID = law1.ID
  iterator_.LType = T_LINK_ASPECT
  Call Dev_ExpectTrue(iterator_.IsActive, "Valid link: aspect")
  iterator_.LType = T_LINK_DEFAULT
  Call Dev_ExpectFalse(iterator_.IsActive, "Valid link: default")
  iterator_.LType = T_LINK_DEFINITION
  Call Dev_ExpectTrue(iterator_.IsActive, "Valid link: definition")
  iterator_.LType = T_LINK_FOUNDATION
  Call Dev_ExpectTrue(iterator_.IsActive, "Valid link: foundation")
  
  iterator_.Status = T_LINKS_IGNORE
  Call Dev_ExpectFalse(iterator_.IsActive, "Valid link: ignore link")
  iterator_.Status = T_LINK_ASPECT
  
  law2.Status = T_LAWS_IGNORE
  Call db_.Recreate
  Call Dev_ExpectFalse(iterator_.IsActive, "Link to ignored law")
  
  law1.Status = T_LAWS_OUTER
  law2.Status = T_LAWS_FULL
  Call db_.Recreate
  Call Dev_ExpectFalse(iterator_.IsActive, "Link from outer law")
  
  law1.Status = T_LAWS_IGNORE
  Call db_.Recreate
  Call Dev_ExpectFalse(iterator_.IsActive, "Link from ignored law")
  
  law1.Status = T_LAWS_FRINGE
  law2.Status = T_LAWS_NOTDEFINED
  Call db_.Recreate
  Call Dev_ExpectFalse(iterator_.IsActive, "Link from fringe to undefined")
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function
