SF_Register.xba 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="SF_Register" script:language="StarBasic" script:moduleType="normal">REM =======================================================================================================================
  4. REM === The ScriptForge library and its associated libraries are part of the LibreOffice project. ===
  5. REM === The SFUnitTests library is one of the associated libraries. ===
  6. REM === Full documentation is available on https://help.libreoffice.org/ ===
  7. REM =======================================================================================================================
  8. Option Compatible
  9. Option Explicit
  10. &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
  11. &apos;&apos;&apos; SF_Register
  12. &apos;&apos;&apos; ===========
  13. &apos;&apos;&apos; The ScriptForge framework includes
  14. &apos;&apos;&apos; the master ScriptForge library
  15. &apos;&apos;&apos; a number of &quot;associated&quot; libraries SF*
  16. &apos;&apos;&apos; any user/contributor extension wanting to fit into the framework
  17. &apos;&apos;&apos;
  18. &apos;&apos;&apos; The main methods in this module allow the current library to cling to ScriptForge
  19. &apos;&apos;&apos; - RegisterScriptServices
  20. &apos;&apos;&apos; Register the list of services implemented by the current library
  21. &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
  22. REM ================================================================== EXCEPTIONS
  23. Private Const UNITTESTLIBRARYERROR = &quot;UNITTESTLIBRARYERROR&quot;
  24. REM ============================================================== PUBLIC METHODS
  25. REM -----------------------------------------------------------------------------
  26. Public Sub RegisterScriptServices() As Variant
  27. &apos;&apos;&apos; Register into ScriptForge the list of the services implemented by the current library
  28. &apos;&apos;&apos; Each library pertaining to the framework must implement its own version of this method
  29. &apos;&apos;&apos;
  30. &apos;&apos;&apos; It consists in successive calls to the RegisterService() and RegisterEventManager() methods
  31. &apos;&apos;&apos; with 2 arguments:
  32. &apos;&apos;&apos; ServiceName: the name of the service as a case-insensitive string
  33. &apos;&apos;&apos; ServiceReference: the reference as an object
  34. &apos;&apos;&apos; If the reference refers to a module, then return the module as an object:
  35. &apos;&apos;&apos; GlobalScope.Library.Module
  36. &apos;&apos;&apos; If the reference is a class instance, then return a string referring to the method
  37. &apos;&apos;&apos; containing the New statement creating the instance
  38. &apos;&apos;&apos; &quot;libraryname.modulename.function&quot;
  39. With GlobalScope.ScriptForge.SF_Services
  40. .RegisterService(&quot;UnitTest&quot;, &quot;SFUnitTests.SF_Register._NewUnitTest&quot;) &apos; Reference to the function initializing the service
  41. End With
  42. End Sub &apos; SFUnitTests.SF_Register.RegisterScriptServices
  43. REM =========================================================== PRIVATE FUNCTIONS
  44. REM -----------------------------------------------------------------------------
  45. Public Function _NewUnitTest(Optional ByVal pvArgs As Variant) As Object
  46. &apos;&apos;&apos; Create a new instance of the SF_UnitTest class
  47. &apos; Args:
  48. &apos;&apos;&apos; Location: if empty, the location of the library is presumed to be in GlobalScope.BasicLibraries
  49. &apos;&apos;&apos; Alternatives are:
  50. &apos;&apos;&apos; - the name of a document: see SF_UI.WindowName
  51. &apos;&apos;&apos; - an explicit SFDocuments.Document instance
  52. &apos;&apos;&apos; - the component containing the library, typically ThisComponent
  53. &apos;&apos;&apos; LibraryName: the name of the library containing the test code
  54. &apos;&apos;&apos; Returns:
  55. &apos;&apos;&apos; The instance or Nothing
  56. &apos;&apos;&apos; Exceptions:
  57. &apos;&apos;&apos; UNITTESTLIBRARYNOTFOUND The library could not be found
  58. Dim oUnitTest As Object &apos; Return value
  59. Dim vLocation As Variant &apos; Alias of pvArgs(0)
  60. Dim vLibraryName As Variant &apos; alias of pvArgs(1)
  61. Dim vLocations As Variant &apos; &quot;user&quot;, &quot;share&quot; or document
  62. Dim sLocation As String &apos; A single location
  63. Dim sTargetLocation As String &apos; &quot;user&quot; or the document name
  64. Dim vLanguages As Variant &apos; &quot;Basic&quot;, &quot;Python&quot;, ... programming languages
  65. Dim sLanguage As String &apos; A single programming language
  66. Dim vLibraries As Variant &apos; Library names
  67. Dim sLibrary As String &apos; A single library
  68. Dim vModules As Variant &apos; Module names
  69. Dim sModule As String &apos; A single module
  70. Dim vModuleNames As Variant &apos; Module names
  71. Dim oRoot As Object &apos; com.sun.star.script.browse.BrowseNodeFactory
  72. Dim iLibrary As Integer &apos; The index of the target location in vLibraries
  73. Dim FSO As Object &apos; SF_FileSystem
  74. Dim i As Integer, j As Integer, k As Integer, l As Integer
  75. Const cstService = &quot;SFUnitTests.UnitTest&quot;
  76. If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  77. Check:
  78. If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array()
  79. If UBound(pvArgs) &gt;= 0 Then vLocation = pvArgs(0) Else vLocation = &quot;&quot;
  80. If IsEmpty(vLocation) Then vLocation = &quot;&quot;
  81. If UBound(pvArgs) &gt;= 1 Then vLibraryName = pvArgs(1) Else vLibraryName = &quot;&quot;
  82. If IsEmpty(vLibraryName) Then vLibraryName = &quot;&quot;
  83. If Not ScriptForge.SF_Utils._Validate(vLocation, &quot;Location&quot;, Array(V_STRING, ScriptForge.V_OBJECT)) Then GoTo Finally
  84. If Not ScriptForge.SF_Utils._Validate(vLibraryName, &quot;LibraryName&quot;, V_STRING) Then GoTo Finally
  85. Set oUnitTest = Nothing
  86. Set FSO = CreateScriptService(&quot;ScriptForge.FileSystem&quot;)
  87. &apos; Determine the library container hosting the test code
  88. &apos; Browsing starts from root element
  89. Set oRoot = SF_Utils._GetUNOService(&quot;BrowseNodeFactory&quot;).createView(com.sun.star.script.browse.BrowseNodeFactoryViewTypes.MACROORGANIZER)
  90. If Len(vLibraryName) &gt; 0 Then
  91. &apos; Determine the target location, as a string. The location is either:
  92. &apos; - the last component of a document&apos;s file name
  93. &apos; - &quot;user&quot; = My Macros &amp; Dialogs
  94. If VarType(vLocation) = ScriptForge.V_OBJECT Then
  95. sTargetLocation = FSO.GetName(vLocation.URL)
  96. ElseIf Len(vLocation) = 0 Then
  97. sTargetLocation = &quot;user&quot; &apos; Testing code is presumed NOT in &quot;share&quot;
  98. Else
  99. sTargetLocation = FSO.GetName(vLocation)
  100. End If
  101. &apos; Exploration is done via tree nodes
  102. iLibrary = -1
  103. If Not IsNull(oRoot) Then
  104. If oRoot.hasChildNodes() Then
  105. vLocations = oRoot.getChildNodes()
  106. For i = 0 To UBound(vLocations)
  107. sLocation = vLocations(i).getName()
  108. If sLocation = sTargetLocation Then
  109. If vLocations(i).hasChildNodes() Then
  110. vLanguages = vLocations(i).getChildNodes()
  111. For j = 0 To UBound(vLanguages)
  112. sLanguage = vLanguages(j).getName()
  113. &apos; Consider Basic libraries only
  114. If sLanguage = &quot;Basic&quot; Then
  115. If vLanguages(j).hasChildNodes() Then
  116. vLibraries = vLanguages(j).getChildNodes()
  117. For k = 0 To UBound(vLibraries)
  118. sLibrary = vLibraries(k).getName()
  119. &apos; Consider the targeted library only
  120. If sLibrary = vLibraryName Then
  121. iLibrary = k
  122. If vLibraries(k).hasChildNodes() Then
  123. vModules = vLibraries(k).getChildNodes()
  124. vModuleNames = Array()
  125. For l = 0 To UBound(vModules)
  126. sModule = vModules(l).getName()
  127. vModuleNames = ScriptForge.SF_Array.Append(vModuleNames, sModule)
  128. Next l
  129. End If
  130. Exit For
  131. End If
  132. Next k
  133. End If
  134. End If
  135. If iLibrary &gt;= 0 Then Exit For
  136. Next j
  137. End If
  138. End If
  139. If iLibrary &gt;= 0 Then Exit For
  140. Next i
  141. End If
  142. End If
  143. If iLibrary &lt; 0 Then GoTo CatchLibrary
  144. End If
  145. Try:
  146. &apos; Create the unittest Basic object and initialize its attributes
  147. Set oUnitTest = New SF_UnitTest
  148. With oUnitTest
  149. Set .[Me] = oUnitTest
  150. If Len(vLibraryName) &gt; 0 Then
  151. .LibrariesContainer = sTargetLocation
  152. .Scope = Iif(sTargetLocation = &quot;user&quot;, &quot;application&quot;, &quot;document&quot;)
  153. .Libraries = vLibraries
  154. .LibraryName = sLibrary
  155. .LibraryIndex = iLibrary
  156. .Modules = vModules
  157. .ModuleNames = vModuleNames
  158. ._ExecutionMode = .FULLMODE
  159. ._WhenAssertionFails = .FAILSTOPSUITE
  160. &apos; Launch the test timer
  161. .TestTimer = CreateScriptService(&quot;ScriptForge.Timer&quot;, True)
  162. Else
  163. ._ExecutionMode = .SIMPLEMODE
  164. ._WhenAssertionFails = .FAILIMMEDIATESTOP
  165. End If
  166. End With
  167. Finally:
  168. Set _NewUnitTest = oUnitTest
  169. Exit Function
  170. Catch:
  171. GoTo Finally
  172. CatchLibrary:
  173. ScriptForge.SF_Exception.RaiseFatal(UNITTESTLIBRARYERROR, vLibraryName)
  174. GoTo Finally
  175. End Function &apos; SFUnitTests.SF_Register._NewUnitTest
  176. REM ============================================== END OF SFUNITTESTS.SF_REGISTER
  177. </script:module>