VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "s_IteratorLaw"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' ====== Test law iterator ======
Option Explicit

' TODO:
' RemoveRow
' Public Function CreateIndexItem() As ItemCachedLaw
' Public Function Links() As IteratorLinkLaw
' Public Function Find(target$, colN As LawStruct) As Boolean
' Public Function SetData(dataItem As ItemLaw, nLinkBase&)

Private db_ As InfoDatabase
Private iterator_ As IteratorLaw

Public Function Setup()
  ' Mandatory setup function
  Set db_ = New InfoDatabase
  Set iterator_ = New IteratorLaw
  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_DOCS))
  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 iterator_.Init(db_, ThisWorkbook.Sheets(HT03_SHT_DOCS))
  Call Dev_ExpectEQ(newData.gid_, iterator_.ID)
  
  Call Dev_NewCase("Custom empty row")
  Call iterator_.Init(db_, ThisWorkbook.Sheets(HT03_SHT_DOCS), 10)
  Call Dev_ExpectEQ(0, iterator_.ID)
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_RandomAccess()
  On Error GoTo PROPAGATE_ERROR
  
  Call Dev_NewCase("Acccess to existing data")
  Dim newData As New ItemLaw: Set newData = GetDummyData
  Call db_.SetItem(newData.Clone)
  Set iterator_ = db_.Data(newData.gid_)
  Call Dev_ExpectEQ(iterator_.ID, iterator_.GetVaueIn(S_CD_ID))
  Call iterator_.SetVaueIn(S_CD_ID, 1338)
  Call Dev_ExpectEQ(1338, iterator_.GetVaueIn(S_CD_ID))
  
  Call Dev_NewCase("Acccess outside boundaries")
  Call iterator_.SetVaueIn(100, 1337)
  Call Dev_ExpectEQ(1337, iterator_.GetVaueIn(100))
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_NamedAccess()
  On Error GoTo PROPAGATE_ERROR
  
  Call Dev_NewCase("Read acccess")
  Dim newData As New ItemLaw: Set newData = GetDummyData
  Call db_.SetItem(newData.Clone)
  Set iterator_ = db_.Data(newData.gid_)
  Call Dev_ExpectEQ(newData.gid_, iterator_.ID)
  Call Dev_ExpectEQ(newData.status_, iterator_.Status)
  Call Dev_ExpectEQ(newData.type_, iterator_.DType)
  Call Dev_ExpectEQ(newData.fileName_, iterator_.FileName)
  Call Dev_ExpectEQ(newData.title_, iterator_.Title)
  Call Dev_ExpectEQ(newData.typeTxt_, iterator_.TypeText)
  Call Dev_ExpectEQ(newData.creator_, iterator_.Creator)
  Call Dev_ExpectEQ(newData.date_, iterator_.CreationDate)
  Call Dev_ExpectEQ(newData.header_, iterator_.Header)
  Call Dev_ExpectEQ(newData.attach_, iterator_.Attachment)
  Call Dev_ExpectEQ(newData.isScoped_, iterator_.IsScoped)
  Call Dev_ExpectEQ(newData.isDummy_, iterator_.IsDummy)
  Call Dev_ExpectEQ(HT03_INVALID_ROW, iterator_.LinkBase)
  
  Call Dev_NewCase("Modify access")
  iterator_.FileName = "newFile"
  Call Dev_ExpectEQ("newFile", iterator_.FileName)
  iterator_.Status = T_LAWS_NOTDEFINED
  Call Dev_ExpectEQ(T_LAWS_NOTDEFINED, iterator_.Status)
  Call Dev_ExpectNE(T_LAWS_NOTDEFINED, db_.Cache(newData.gid_).status_, "Do not update Cache")
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_Iteration()
  On Error GoTo PROPAGATE_ERROR
  
  Dim data1 As New ItemLaw: Set data1 = GetDummyData
  Dim data2 As New ItemLaw: Set data2 = GetDummyData
  data2.gid_ = data1.gid_ + 1
  Call db_.SetItem(data1.Clone)
  Call db_.SetItem(data2.Clone)
  
  Call Dev_NewCase("Database begin")
  Set iterator_ = db_.Laws
  Dim nRow&: nRow = HT03_DEFAULT_FIRST_LINE
  Call Dev_ExpectFalse(iterator_.IsDone, "Is not end")
  Call Dev_ExpectEQ(nRow, iterator_.Row, "Increment row")
  Call Dev_ExpectEQ(data1.gid_, iterator_.ID, "Validate data")
  
  Call Dev_NewCase("Simple increment")
  Call iterator_.Increment
  nRow = nRow + 1
  Call Dev_ExpectFalse(iterator_.IsDone, "Is not end")
  Call Dev_ExpectEQ(nRow, iterator_.Row, "Increment row")
  Call Dev_ExpectEQ(data2.gid_, iterator_.ID, "Validate data")
  
  Call Dev_NewCase("Increment past end")
  Call iterator_.Increment
  nRow = nRow + 1
  Call Dev_ExpectTrue(iterator_.IsDone, "Is end")
  Call Dev_ExpectEQ(nRow, iterator_.Row, "Increment row")
  
  Call Dev_NewCase("Increment past end")
  Call iterator_.Increment
  nRow = nRow + 1
  Call Dev_ExpectTrue(iterator_.IsDone, "Is end")
  Call Dev_ExpectEQ(nRow, iterator_.Row, "Increment row")
  
  Call iterator_.GoFirst
  Call Dev_ExpectEQ(HT03_DEFAULT_FIRST_LINE, iterator_.Row, "Goto First")
  
  Call iterator_.GoLast
  Call Dev_ExpectEQ(nRow - 2, iterator_.Row, "Goto Last")
  
  Call iterator_.GoFirst
  Call iterator_.Increment(3)
  Call Dev_ExpectEQ(HT03_DEFAULT_FIRST_LINE + 3, iterator_.Row, "Increment custom")
  
  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_.Laws
  Call Dev_ExpectFalse(iterator_.IsActive, "Empty DB")
  
  iterator_.ID = 1337
  
  iterator_.Status = T_LAWS_NOTDEFINED
  Call Dev_ExpectFalse(iterator_.IsActive, "Undefined")
  
  iterator_.Status = T_LAWS_IGNORE
  Call Dev_ExpectFalse(iterator_.IsActive, "Ignored")
  
  iterator_.Status = T_LAWS_FULL
  Call Dev_ExpectTrue(iterator_.IsActive, "Full")
  
  iterator_.Status = T_LAWS_FRINGE
  Call Dev_ExpectTrue(iterator_.IsActive, "Fringe")
  
  iterator_.Status = T_LAWS_OUTER
  Call Dev_ExpectTrue(iterator_.IsActive, "Outer")
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_HasLinks()
  On Error GoTo PROPAGATE_ERROR
  
  Set iterator_ = db_.Laws
  Call Dev_ExpectFalse(iterator_.HasLinks, "Empty DB")
  
  iterator_.ID = 1337
  
  iterator_.Status = T_LAWS_NOTDEFINED
  Call Dev_ExpectFalse(iterator_.HasLinks, "Undefined")
  
  iterator_.Status = T_LAWS_IGNORE
  Call Dev_ExpectFalse(iterator_.HasLinks, "Ignored")
  
  iterator_.Status = T_LAWS_FULL
  Call Dev_ExpectTrue(iterator_.HasLinks, "Full")
  
  iterator_.Status = T_LAWS_FRINGE
  Call Dev_ExpectTrue(iterator_.HasLinks, "Fringe")
  
  iterator_.Status = T_LAWS_OUTER
  Call Dev_ExpectFalse(iterator_.HasLinks, "Outer")
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_IsLoaded()
  On Error GoTo PROPAGATE_ERROR
  
  Set iterator_ = db_.Laws
  Call Dev_ExpectFalse(iterator_.IsLoaded, "Empty DB")
  
  iterator_.ID = 1337
  iterator_.Status = T_LAWS_IGNORE
  iterator_.DType = T_LAW_01_UNKNOWN
  iterator_.IsDummy = False
  iterator_.Title = "test title"
  Call Dev_ExpectTrue(iterator_.IsLoaded, "Valid loaded")
  
  iterator_.IsDummy = True
  Call Dev_ExpectFalse(iterator_.IsLoaded, "Dummy")
  iterator_.IsDummy = False
  
  iterator_.Title = vbNullString
  Call Dev_ExpectFalse(iterator_.IsLoaded, "No title")
  
  iterator_.Title = HT03_TITLE_NOT_LOADED
  Call Dev_ExpectFalse(iterator_.IsLoaded, "Not loaded")
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_HasInfoBlanks()
  On Error GoTo PROPAGATE_ERROR
  
  Set iterator_ = db_.Laws
  Call Dev_ExpectTrue(iterator_.HasInfoBlanks, "Empty DB")
  
  Call db_.SetItem(GetDummyData)
  Call Dev_ExpectFalse(iterator_.HasInfoBlanks, "Non-empty data")
  
  iterator_.TypeText = vbNullString
  Call Dev_ExpectTrue(iterator_.HasInfoBlanks, "TypeText")
  iterator_.TypeText = "non-empty"
  
  iterator_.Creator = vbNullString
  Call Dev_ExpectTrue(iterator_.HasInfoBlanks, "Creator")
  iterator_.Creator = "non-empty"
  
  iterator_.CreationDate = vbNullString
  Call Dev_ExpectTrue(iterator_.HasInfoBlanks, "CreationDate")
  iterator_.TypeText = "non-empty"
  
  iterator_.Number = vbNullString
  Call Dev_ExpectTrue(iterator_.HasInfoBlanks, "Number")
  iterator_.Number = "non-empty"
  
  iterator_.Attachment = vbNullString
  Call Dev_ExpectTrue(iterator_.HasInfoBlanks, "Attachment")
  iterator_.Attachment = "non-empty"
  
  Call iterator_.SetVaueIn(S_D_TIME_SCOPED, "")
  Call Dev_ExpectTrue(iterator_.HasInfoBlanks, "Time-scoped")
  iterator_.IsScoped = False
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function
