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

' TODO:
' Public Function ValidateSync(itLink As IteratorLinkCached)

Private iLink_ As ItemHyperlink

Public Function Setup()
  ' Mandatory setup function
  Set iLink_ = New ItemHyperlink
End Function

Public Function Teardown()
  ' Mandatory teardown function
End Function

Public Function t_Init()
  On Error GoTo PROPAGATE_ERROR
  
  Call Dev_NewCase("Init default sub")
  Call iLink_.Init(PT_INTERNET, 123)
  Call Dev_ExpectEQ(PT_INTERNET, iLink_.LinkSystem)
  Call Dev_ExpectEQ(123, iLink_.DocumentID)
  Call Dev_ExpectEQ("sub_0", iLink_.BookmarkName)
  
  Call Dev_NewCase("Init custom sub")
  Dim iLink2 As New ItemHyperlink
  Call iLink2.Init(PT_INTERNET, 123, "sub_123")
  Call Dev_ExpectEQ(PT_INTERNET, iLink2.LinkSystem)
  Call Dev_ExpectEQ(123, iLink2.DocumentID)
  Call Dev_ExpectEQ("sub_123", iLink2.BookmarkName)
  
  Call Dev_NewCase("Reinit")
  Call iLink2.Init(PT_MOBILE, 321, "sub_321")
  Call Dev_ExpectEQ(PT_MOBILE, iLink2.LinkSystem)
  Call Dev_ExpectEQ(321, iLink2.DocumentID)
  Call Dev_ExpectEQ("sub_321", iLink2.BookmarkName)
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_ToAddress()
  On Error GoTo PROPAGATE_ERROR
  
  Call Dev_NewCase("Mobile garant")
  Call iLink_.Init(PT_MOBILE, 1337)
  Call Dev_ExpectEQ("http://mobileonline.garant.ru/document/redirect/1337/0", iLink_.ToAddress)
  
  Call Dev_NewCase("Local garant")
  Call iLink_.Init(PT_LOCAL, 1337)
  Call Dev_ExpectEQ("garantf1://1337.0/", iLink_.ToAddress)
  
  Call Dev_NewCase("IVO garant")
  Call iLink_.Init(PT_INTERNET, 1337)
  Call Dev_ExpectEQ("http://internet.garant.ru/document/redirect/1337/0", iLink_.ToAddress)
  
  Call Dev_NewCase("Custom sub")
  Call iLink_.Init(PT_LOCAL, 1337, "sub_123")
  Call Dev_ExpectEQ("garantf1://1337.123/", iLink_.ToAddress)
  
  Call Dev_NewCase("Custom lnk")
  Call iLink_.Init(PT_LOCAL, 1337, "lnk_123")
  Call Dev_ExpectEQ("garantf1://1337.123/", iLink_.ToAddress)
  
  Call Dev_NewCase("Invalid sub")
  Call iLink_.Init(PT_LOCAL, 1337, "sub")
  Call Dev_ExpectEQ("garantf1://1337.0/", iLink_.ToAddress)
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function

Public Function t_FromString()
  On Error GoTo PROPAGATE_ERROR
  
  Call iLink_.Init(PT_LOCAL, 123)
  Call Dev_ExpectFalse(iLink_.FromAddress(""), "Empty input")
  Call Dev_ExpectEQ(PT_LOCAL, iLink_.LinkSystem, "Do not reset on failed load")
  
  Call Dev_ExpectFalse(iLink_.FromAddress("invalid"), "Invalid input")
  Call Dev_ExpectFalse(iLink_.FromAddress("http://internet.garant.ru/document/redirect/"), "Missing GID")
  Call Dev_ExpectFalse(iLink_.FromAddress("http://internet.garant.ru/document/redirect/123/"), "Missing SUB")
  Call Dev_ExpectFalse(iLink_.FromAddress("garantf1://1338.3"), "Missing slash")
  Call Dev_ExpectFalse(iLink_.FromAddress("http://internet.garant.ru/document/redirect/abc/1"), "Invalid GID")
  Call Dev_ExpectFalse(iLink_.FromAddress("http://internet.garant.ru/document/redirect/123/abc"), "Invalid SUB")
  
  Call Dev_NewCase("Mobile garant")
  Call Dev_ExpectTrue(iLink_.FromAddress("http://mobileonline.garant.ru/document/redirect/1337/2"))
  Call Dev_ExpectEQ(PT_MOBILE, iLink_.LinkSystem)
  Call Dev_ExpectEQ(1337, iLink_.DocumentID)
  Call Dev_ExpectEQ("sub_2", iLink_.BookmarkName)
  
  Call Dev_NewCase("Local garant")
  Call Dev_ExpectTrue(iLink_.FromAddress("garantf1://1338.3/"))
  Call Dev_ExpectEQ(PT_LOCAL, iLink_.LinkSystem)
  Call Dev_ExpectEQ(1338, iLink_.DocumentID)
  Call Dev_ExpectEQ("sub_3", iLink_.BookmarkName)
  
  Call Dev_NewCase("IVO garant")
  Call Dev_ExpectTrue(iLink_.FromAddress("http://internet.garant.ru/document/redirect/1339/4"))
  Call Dev_ExpectEQ(PT_INTERNET, iLink_.LinkSystem)
  Call Dev_ExpectEQ(1339, iLink_.DocumentID)
  Call Dev_ExpectEQ("sub_4", iLink_.BookmarkName)
  
  Exit Function
PROPAGATE_ERROR:
  Call Dev_LogError(Err.Number, Err.Description)
End Function
