' Gambas test file

Private $sTestRoot As String = User.home &/ ".local"

Public Sub _Setup()

  TSR.CreateTestEnvironment()
  CComponent._init()
  
End

Public Sub _Teardown()
  
  CComponent.Clear()
  TSR.TeardownTestEnvironment()
  
End

  
Public Sub Components()
'  Test.Note("Testing Needed Component")
  
  ScriptMain._ResetAll()
  CComponent._init()
  
  Assert.Equals(CComponent.WhoImplement["form"][0], "gb.gui", "Check if 'form' is correctly set on init of components")
  CComponent.GetNeededComp("gb.form")
  Assert.Equals(CComponent.$cComponents[CComponent.$cComponents.first], "gb.gui", "Forms need gui first in components")
  
End

Public Sub ValidFileRebuildCheck()

 ' Test.note("Testing If rebuild Of script required check")
  Assert.Equals(ScriptMain.IsValid("TestFile.info"), True, "Check if rebuild required - not needed")
  Assert.Equals(ScriptMain.IsValid("TestFileFail.info"), False, "Check if rebuild required - Needed")
  
End

Public Sub CheckLibraryVersions()
  
  Assert.Equals(Libraries.CheckLibVersions($sTestRoot &/ "libtest/libe", "3.1"), $sTestRoot &/ "libtest/libe:3.1.9.gambas", "Veryify we can get the correct library n.n.x")
  Assert.Equals(Libraries.CheckLibVersions($sTestRoot &/ "libtest/libe", "2"), $sTestRoot &/ "libtest/libe:2.2.3.gambas", "Veryify we can get the correct library n.x.x")
  Assert.Equals(Libraries.CheckLibVersions($sTestRoot &/ "libtest/libe", "1.1.0"), $sTestRoot &/ "libtest/libe:1.1.0.gambas", "Veryify we can get the correct library n.n.n")
  Assert.Equals(Libraries.CheckLibVersions($sTestRoot &/ "libtest/libe", "1"), $sTestRoot &/ "libtest/libe:1.1.0.gambas", "Veryify we can get the correct library n.x.x")
  Assert.Equals(Libraries.CheckLibVersions($sTestRoot &/ "libtest/libe", ""), $sTestRoot &/ "libtest/libe:3.1.9.gambas", "Veryify we can get the correct library x.x.x")
  Assert.Equals(Libraries.CheckLibVersions($sTestRoot &/ "libtest/", "3.4.4"), "", "Veryify we can get the correct library bad directory name")
  Assert.Equals(Libraries.CheckLibVersions($sTestRoot &/ "libtest/libd", ""), "", "Veryify we can get the correct library Bad Lib Name")
  
End

Public Sub CheckProcessLibrary()
  'Test.Note("Verify Library search and versioning")
  'Test.Note("")
  
  ScriptMain._ResetAll()
  
  Assert.Equals(TSR.ErrorMsgOut(Libraries, "ProcessLibrary", "DummyLibName", 1, "~/projects"), "gbs" & System.Version & ": ~/projects.1: " & ("Unknown Library") & ": DummyLibName", "Verify that error is generated when not found")
  
  Libraries.ProcessLibrary($sTestRoot &/ "libtest/libe:2", 1, "~/projects")
  Assert.equals(Libraries.$cLibraries[Libraries.$cLibraries.first], $sTestRoot &/ "libtest/libe:2.2.3.gambas", "Test for absolute path in library name")
  
 Libraries.$cLibraries.Clear()
  Libraries.ProcessLibrary("biz1.liba:3.1", 1, "~/DummyProg")
  Assert.Equals(Libraries.$cLibraries[Libraries.$cLibraries.first], "/usr/lib/gambas" & System.version & "/biz1/liba:3.1.3.gambas", "Test for auto select from system library n.n.x")
  
 Libraries.$cLibraries.Clear()
  Libraries.ProcessLibrary("biz1.liba:6.1.8", 1, "~/DummyProg")
  Assert.Equals(Libraries.$cLibraries[Libraries.$cLibraries.first], User.home &/ ".local/share/gambas" & System.version & "/lib/biz1/liba:6.1.8.gambas", "Test for auto select from User .local library n.n.n")
  
 Libraries.$cLibraries.Clear()
  Libraries.ProcessLibrary("biz1.liba:", 1, "~/DummyProg")
  Assert.Equals(Libraries.$cLibraries[Libraries.$cLibraries.first], User.home &/ ".local/share/gambas" & System.version & "/lib/biz1/liba:6.1.8.gambas", "Test for auto select from User .local library n.n.n")
  
 Libraries.$cLibraries.Clear()
  Libraries.ProcessLibrary("biz1.liba:4.2", 1, "~/DummyProg")
  Assert.Equals(Libraries.$cLibraries[Libraries.$cLibraries.first], User.home &/ ".local/share/gambas" & System.version & "/lib/biz1/liba:4.2.1.gambas", "Test for auto select from User .local library n.n.x")
  
 Libraries.$cLibraries.Clear()
  Libraries.ProcessLibrary("biz1.liba:4", 1, "~/DummyProg")
  Assert.Equals(Libraries.$cLibraries[Libraries.$cLibraries.first], User.home &/ ".local/share/gambas" & System.version & "/lib/biz1/liba:4.3.2.gambas", "Test for auto select from User .local library n.x.x")
  
 Libraries.$cLibraries.Clear()
  Libraries.ProcessLibrary("liba:", 1, "~/DummyProg")
  Assert.Equals(Libraries.$cLibraries[Libraries.$cLibraries.first], User.home &/ ".local/share/gambas" & System.version & "/lib/(unknown)/liba:7.1.8.gambas", "Test for auto select from User .local unknown library x.x.x")
  
End


Public Sub FullReadCompileCheck()
  Dim workdir As String
  Dim ShouldBefile As String[] = Split(File.Load("TestAppCompiled"), "\n")
  
  ScriptMain._ResetAll()
  ScriptMain.$oContext.$bBuildOnly = True
  ScriptMain.$oContext.$bTestMode = True
  
  workdir = Temp()
  Mkdir workdir
  Try Copy "TestApp" To workdir &/ "TestApp"
  Try Copy "TestAppInclude" To workdir &/ "TestAppInclude"
  Try Copy "TestAppInclude2" To workdir &/ "TestAppInclude2"
  
  ScriptMain.$sPath = workdir &/ "TestApp"
  Dim CompiledResult As String[] = Split(TSR.PrintErrOut(MakeVirtualProject, "_Call", ScriptMain.$oContext, ScriptMain.$sPath), "\n", "", True)

  Assert.Equals(TSR.CompareStringArray(CompiledResult, ShouldBeFile), True, "No Main Test a Basic App with all supported types")
  
End

Public Sub FullReadCompileCheckWithMain()
  Dim workdir As String
  Dim ShouldBefile As String[] = Split(File.Load("TestAppWithMainCompiled"), "\n")
  ScriptMain._ResetAll()
  CComponent._init()
  
  ScriptMain.$oContext.$bBuildOnly = True
  
  workdir = Temp()
  Mkdir workdir
  Try Copy "TestAppWithMain" To workdir &/ "TestAppWithMain"
  Try Copy "TestAppInclude" To workdir &/ "TestAppInclude"
  Try Copy "TestAppInclude2" To workdir &/ "TestAppInclude2"
  
  ScriptMain.$sPath = workdir &/ "TestAppWithMain"
  Dim CompiledResult As String[] = Split(TSR.PrintErrOut(MakeVirtualProject, "_Call", ScriptMain.$oContext, ScriptMain.$sPath), "\n", "", True)
  'File.save(User.home &/ "Desktop/maincompiled", CompiledResult.Join("\n"))
  Assert.Equals(TSR.CompareStringContent(ShouldBeFile, CompiledResult), True, "With Main Test a Basic App with all supported types")
  
End
