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 =======================================================================================================================
12 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
13 ''' SF_Register
14 ''' ===========
15 ''' The ScriptForge framework includes
16 ''' the master ScriptForge library
17 ''' a number of
"associated
" libraries SF*
18 ''' any user/contributor extension wanting to fit into the framework
20 ''' The main methods in this module allow the current library to cling to ScriptForge
21 ''' - RegisterScriptServices
22 ''' Register the list of services implemented by the current library
23 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
25 REM ================================================================== EXCEPTIONS
27 Private Const UNITTESTLIBRARYERROR =
"UNITTESTLIBRARYERROR
"
29 REM ============================================================== PUBLIC METHODS
31 REM -----------------------------------------------------------------------------
32 Public Sub RegisterScriptServices() As Variant
33 ''' Register into ScriptForge the list of the services implemented by the current library
34 ''' Each library pertaining to the framework must implement its own version of this method
36 ''' It consists in successive calls to the RegisterService() and RegisterEventManager() methods
37 ''' with
2 arguments:
38 ''' ServiceName: the name of the service as a case-insensitive string
39 ''' ServiceReference: the reference as an object
40 ''' If the reference refers to a module, then return the module as an object:
41 ''' GlobalScope.Library.Module
42 ''' If the reference is a class instance, then return a string referring to the method
43 ''' containing the New statement creating the instance
44 ''' "libraryname.modulename.function
"
46 With GlobalScope.ScriptForge.SF_Services
47 .RegisterService(
"UnitTest
",
"SFUnitTests.SF_Register._NewUnitTest
")
' Reference to the function initializing the service
50 End Sub
' SFUnitTests.SF_Register.RegisterScriptServices
52 REM =========================================================== PRIVATE FUNCTIONS
54 REM -----------------------------------------------------------------------------
55 Public Function _NewUnitTest(Optional ByVal pvArgs As Variant) As Object
56 ''' Create a new instance of the SF_UnitTest class
58 ''' Location: if empty, the location of the library is presumed to be in GlobalScope.BasicLibraries
59 ''' Alternatives are:
60 ''' - the name of a document: see SF_UI.WindowName
61 ''' - an explicit SFDocuments.Document instance
62 ''' - the component containing the library, typically ThisComponent
63 ''' LibraryName: the name of the library containing the test code
64 ''' Returns:
65 ''' The instance or Nothing
66 ''' Exceptions:
67 ''' UNITTESTLIBRARYNOTFOUND The library could not be found
69 Dim oUnitTest As Object
' Return value
70 Dim vLocation As Variant
' Alias of pvArgs(
0)
71 Dim vLibraryName As Variant
' alias of pvArgs(
1)
72 Dim vLocations As Variant
' "user
",
"share
" or document
73 Dim sLocation As String
' A single location
74 Dim sTargetLocation As String
' "user
" or the document name
75 Dim vLanguages As Variant
' "Basic
",
"Python
", ... programming languages
76 Dim sLanguage As String
' A single programming language
77 Dim vLibraries As Variant
' Library names
78 Dim sLibrary As String
' A single library
79 Dim vModules As Variant
' Module names
80 Dim sModule As String
' A single module
81 Dim vModuleNames As Variant
' Module names
82 Dim oRoot As Object
' com.sun.star.script.browse.BrowseNodeFactory
83 Dim iLibrary As Integer
' The index of the target location in vLibraries
85 Dim FSO As Object
' SF_FileSystem
86 Dim i As Integer, j As Integer, k As Integer, l As Integer
88 Const cstService =
"SFUnitTests.UnitTest
"
90 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
93 If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array()
94 If UBound(pvArgs)
>=
0 Then vLocation = pvArgs(
0) Else vLocation =
""
95 If IsEmpty(vLocation) Then vLocation =
""
96 If UBound(pvArgs)
>=
1 Then vLibraryName = pvArgs(
1) Else vLibraryName =
""
97 If IsEmpty(vLibraryName) Then vLibraryName =
""
98 If Not ScriptForge.SF_Utils._Validate(vLocation,
"Location
", Array(V_STRING, ScriptForge.V_OBJECT)) Then GoTo Finally
99 If Not ScriptForge.SF_Utils._Validate(vLibraryName,
"LibraryName
", V_STRING) Then GoTo Finally
101 Set oUnitTest = Nothing
102 Set FSO = CreateScriptService(
"ScriptForge.FileSystem
")
104 ' Determine the library container hosting the test code
106 ' Browsing starts from root element
107 Set oRoot = SF_Utils._GetUNOService(
"BrowseNodeFactory
").createView(com.sun.star.script.browse.BrowseNodeFactoryViewTypes.MACROORGANIZER)
109 If Len(vLibraryName)
> 0 Then
111 ' Determine the target location, as a string. The location is either:
112 ' - the last component of a document
's file name
113 ' -
"user
" = My Macros
& Dialogs
114 If VarType(vLocation) = ScriptForge.V_OBJECT Then
115 sTargetLocation = FSO.GetName(vLocation.URL)
116 ElseIf Len(vLocation) =
0 Then
117 sTargetLocation =
"user
" ' Testing code is presumed NOT in
"share
"
119 sTargetLocation = FSO.GetName(vLocation)
122 ' Exploration is done via tree nodes
124 If Not IsNull(oRoot) Then
125 If oRoot.hasChildNodes() Then
126 vLocations = oRoot.getChildNodes()
127 For i =
0 To UBound(vLocations)
128 sLocation = vLocations(i).getName()
129 If sLocation = sTargetLocation Then
130 If vLocations(i).hasChildNodes() Then
131 vLanguages = vLocations(i).getChildNodes()
132 For j =
0 To UBound(vLanguages)
133 sLanguage = vLanguages(j).getName()
134 ' Consider Basic libraries only
135 If sLanguage =
"Basic
" Then
136 If vLanguages(j).hasChildNodes() Then
137 vLibraries = vLanguages(j).getChildNodes()
138 For k =
0 To UBound(vLibraries)
139 sLibrary = vLibraries(k).getName()
140 ' Consider the targeted library only
141 If sLibrary = vLibraryName Then
143 If vLibraries(k).hasChildNodes() Then
144 vModules = vLibraries(k).getChildNodes()
145 vModuleNames = Array()
146 For l =
0 To UBound(vModules)
147 sModule = vModules(l).getName()
148 vModuleNames = ScriptForge.SF_Array.Append(vModuleNames, sModule)
156 If iLibrary
>=
0 Then Exit For
160 If iLibrary
>=
0 Then Exit For
164 If iLibrary
< 0 Then GoTo CatchLibrary
169 ' Create the unittest Basic object and initialize its attributes
170 Set oUnitTest = New SF_UnitTest
172 Set .[Me] = oUnitTest
173 If Len(vLibraryName)
> 0 Then
174 .LibrariesContainer = sTargetLocation
175 .Scope = Iif(sTargetLocation =
"user
",
"application
",
"document
")
176 .Libraries = vLibraries
177 .LibraryName = sLibrary
178 .LibraryIndex = iLibrary
180 .ModuleNames = vModuleNames
181 ._ExecutionMode = .FULLMODE
182 ._WhenAssertionFails = .FAILSTOPSUITE
183 ' Launch the test timer
184 .TestTimer = CreateScriptService(
"ScriptForge.Timer
", True)
186 ._ExecutionMode = .SIMPLEMODE
187 ._WhenAssertionFails = .FAILIMMEDIATESTOP
192 Set _NewUnitTest = oUnitTest
197 ScriptForge.SF_Exception.RaiseFatal(UNITTESTLIBRARYERROR, vLibraryName)
199 End Function
' SFUnitTests.SF_Register._NewUnitTest
201 REM ============================================== END OF SFUNITTESTS.SF_REGISTER