4 Dim passCount
As Integer
5 Dim failCount
As Integer
8 Function doUnitTest() As String
9 result
= verify_format()
10 If failCount
<> 0 And passCount
> 0 Then
17 Function verify_format() as String
21 result
= "Test Results" & Chr
$(10) & "============" & Chr
$(10)
23 'Predefined_Datetime_Format_Sample
24 Predefined_Number_Format_Sample
25 'Custom_Datetime_Format_Sample
26 Custom_Number_Format_Sample
27 Custom_Text_Format_Sample
28 result
= result
& Chr
$(10) & "Tests passed: " & passCount
& Chr
$(10) & "Tests failed: " & failCount
& Chr
$(10)
29 verify_format
= result
34 Sub Predefined_Datetime_Format_Sample()
35 Dim testName
As String
36 Dim myDate
, MyTime
, TestStr
As String
39 testName
= "Test Predefined_Datetime_Format_Sample function"
41 On Error GoTo errorHandler
43 ' The date/time format have a little different between ms office and OOo due to different locale and system...
44 TestStr
= Format(myDate
, "General Date") ' 1/6/98
46 TestLog_ASSERT
IsDate(TestStr
), "General Date: " & TestStr
& " (Test only applies to en_US locale)"
47 'TestLog_ASSERT TestStr = "1/6/98", "General Date: " & TestStr
49 TestStr
= Format(myDate
, "Long Date") ' Tuesday, January 06, 1998
50 TestLog_ASSERT TestStr
= "Tuesday, January 06, 1998", "Long Date: " & TestStr
& " (Test only applies to en_US locale)"
51 'TestLog_ASSERT IsDate(TestStr), "Long Date: " & TestStr
53 TestStr
= Format(myDate
, "Medium Date") ' 06-Jan-98
54 'TestLog_ASSERT TestStr = "06-Jan-98", "Medium Date: " & TestStr
55 TestLog_ASSERT
IsDate(TestStr
), "Medium Date: " & TestStr
& " (Test only applies to en_US locale)"
58 TestStr
= Format(myDate
, "Short Date") ' 1/6/98
59 'TestLog_ASSERT TestStr = "1/6/98", "Short Date: " & TestStr
60 TestLog_ASSERT
IsDate(TestStr
), "Short Date: " & TestStr
& " (Test only applies to en_US locale)"
62 TestStr
= Format(MyTime
, "Long Time") ' 5:08:06 PM
63 'TestLog_ASSERT TestStr = "5:08:06 PM", "Long Time: " & TestStr
64 TestLog_ASSERT
IsDate(TestStr
), "Long Time: " & TestStr
& " (Test only applies to en_US locale)"
67 TestStr
= Format(MyTime
, "Medium Time") ' 05:08 PM
68 'TestLog_ASSERT TestStr = "05:08 PM", "Medium Time: " & TestStr
69 TestLog_ASSERT
IsDate(TestStr
), "Medium Time: " & TestStr
& " (Test only applies to en_US locale)"
72 TestStr
= Format(MyTime
, "Short Time") ' 17:08
73 'TestLog_ASSERT TestStr = "17:08", "Short Time: " & TestStr
74 TestLog_ASSERT
IsDate(TestStr
), "Short Time: " & TestStr
& " (Test only applies to en_US locale)"
77 TestLog_ASSERT (false), testName
& ": hit error handler"
80 Sub Predefined_Number_Format_Sample()
81 Dim myNumber
, TestStr
As String
82 Dim testName
As String
83 testName
= "Test Predefined_Number_Format_Sample function"
84 myNumber
= 562486.2356
86 On Error GoTo errorHandler
88 TestStr
= Format(myNumber
, "General Number") '562486.2356
89 TestLog_ASSERT TestStr
= "562486.2356", "General Number: " & TestStr
92 TestStr
= Format(0.2, "Fixed") '0.20
93 TestLog_ASSERT TestStr
= "0.20", "Fixed: " & TestStr
96 TestStr
= Format(myNumber
, "Standard") '562,486.24
97 TestLog_ASSERT TestStr
= "562,486.24", "Standard: " & TestStr
100 TestStr
= Format(0.7521, "Percent") '75.21%
101 TestLog_ASSERT TestStr
= "75.21%", "Percent: " & TestStr
104 TestStr
= Format(myNumber
, "Scientific") '5.62E+05
105 TestLog_ASSERT TestStr
= "5.62E+05", "Scientific: " & TestStr
108 TestStr
= Format(-3456.789, "Scientific") '-3.46E+03
109 TestLog_ASSERT TestStr
= "-3.46E+03", "Scientific: " & TestStr
112 TestStr
= Format(0, "Yes/No") 'No
113 TestLog_ASSERT TestStr
= "No", "Yes/No: " & TestStr
116 TestStr
= Format(23, "Yes/No") 'Yes
117 TestLog_ASSERT TestStr
= "Yes", "Yes/No: " & TestStr
120 TestStr
= Format(0, "True/False") 'False
121 TestLog_ASSERT TestStr
= "False", "True/False: " & TestStr
124 TestStr
= Format(23, "True/False") 'True
125 TestLog_ASSERT TestStr
= "True", "True/False: " & TestStr
128 TestStr
= Format(0, "On/Off") 'Off
129 TestLog_ASSERT TestStr
= "Off", "On/Off: " & TestStr
132 TestStr
= Format(23, "On/Off") 'On
133 TestLog_ASSERT TestStr
= "On", "On/Off: " & TestStr
138 TestLog_ASSERT (false), testName
& ": hit error handler"
142 Sub Custom_Datetime_Format_Sample()
143 Dim myDate
, MyTime
, TestStr
As String
144 Dim testName
As String
149 testName
= "Test Custom_Datetime_Format_Sample function"
150 On Error GoTo errorHandler
152 TestStr
= Format("01/06/98 17:08:06", "c") ' 1/6/98 5:08:06 PM
153 TestLog_ASSERT TestStr
= "1/6/98 5:08:06 PM", "c: " & TestStr
& " (Test only applies to en_US locale)"
156 TestStr
= Format(myDate
, "dddddd") ' (Long Date), Tuesday, January 06, 1998
157 TestLog_ASSERT TestStr
= "Tuesday, January 06, 1998", "dddddd: " & TestStr
& " (Test only applies to en_US locale)"
160 TestStr
= Format(myDate
, "mm-dd-yyyy") ' 01-06-19s98
161 TestLog_ASSERT TestStr
= "01-06-1998", "mm-dd-yyyy: " & TestStr
& " (Test only applies to en_US locale)"
164 TestStr
= Format(myDate
, "d") ' 6
165 TestLog_ASSERT TestStr
= "6", "d: " & TestStr
& " (Test only applies to en_US locale)"
168 TestStr
= Format(myDate
, "dd") ' 06
169 TestLog_ASSERT TestStr
= "06", "dd: " & TestStr
& " (Test only applies to en_US locale)"
172 TestStr
= Format(myDate
, "ddd") ' Tue
173 TestLog_ASSERT TestStr
= "Tue", "ddd: " & TestStr
& " (Test only applies to en_US locale)"
176 TestStr
= Format(myDate
, "dddd") ' Tuesday
177 TestLog_ASSERT TestStr
= "Tuesday", "dddd: " & TestStr
& " (Test only applies to en_US locale)"
180 TestStr
= Format(MyTime
, "h") ' 5
181 TestLog_ASSERT TestStr
= "5", "h: " & TestStr
& " (Test only applies to en_US locale)"
184 TestStr
= Format(MyTime
, "hh") ' 05
185 TestLog_ASSERT TestStr
= "05", "hh: " & TestStr
& " (Test only applies to en_US locale)"
188 TestStr
= Format(MyTime
, "n") ' 8
189 TestLog_ASSERT TestStr
= "8", "n: " & TestStr
& " (Test only applies to en_US locale)"
192 TestStr
= Format(MyTime
, "nn") ' 08
193 TestLog_ASSERT TestStr
= "08", "nn: " & TestStr
& " (Test only applies to en_US locale)"
196 TestStr
= Format(myDate
, "m") ' 1
197 TestLog_ASSERT TestStr
= "1", "m: " & TestStr
& " (Test only applies to en_US locale)"
200 TestStr
= Format(myDate
, "mm") ' 01
201 TestLog_ASSERT TestStr
= "01", "mm: " & TestStr
& " (Test only applies to en_US locale)"
204 TestStr
= Format(myDate
, "mmm") ' Jan
205 TestLog_ASSERT TestStr
= "Jan", "mmm: " & TestStr
& " (Test only applies to en_US locale)"
208 TestStr
= Format(myDate
, "mmmm") ' January
209 TestLog_ASSERT TestStr
= "January", "mmmm: " & TestStr
& " (Test only applies to en_US locale)"
212 TestStr
= Format(MyTime
, "s") ' 6
213 TestLog_ASSERT TestStr
= "6", "s: " & TestStr
& " (Test only applies to en_US locale)"
216 TestStr
= Format(MyTime
, "ss") ' 06
217 TestLog_ASSERT TestStr
= "06", "ss: " & TestStr
& " (Test only applies to en_US locale)"
223 TestStr
= Format(MyTime
, "hh:mm:ss AM/PM") ' 05:08:06 PM
224 TestLog_ASSERT TestStr
= "05:08:06 PM", "hh:mm:ss AM/PM: " & TestStr
& " (Test only applies to en_US locale)"
227 TestStr
= Format(MyTime
, "hh:mm:ss") ' 17:08:06
228 TestLog_ASSERT TestStr
= "17:08:06", "hh:mm:ss: " & TestStr
& " (Test only applies to en_US locale)"
231 TestStr
= Format(myDate
, "ww") ' 2
232 TestLog_ASSERT TestStr
= "2", "ww: " & TestStr
& " (Test only applies to en_US locale)"
235 TestStr
= Format(myDate
, "w") ' 3
236 TestLog_ASSERT TestStr
= "3", "w: " & TestStr
& " (Test only applies to en_US locale)"
239 TestStr
= Format(myDate
, "y") ' 6
240 TestLog_ASSERT TestStr
= "6", "y: " & TestStr
& " (Test only applies to en_US locale)"
243 TestStr
= Format(myDate
, "yy") ' 98
244 TestLog_ASSERT TestStr
= "98", "yy: " & TestStr
& " (Test only applies to en_US locale)"
247 TestStr
= Format(myDate
, "yyyy") ' 1998
248 TestLog_ASSERT TestStr
= "1998", "yyyy: " & TestStr
& " (Test only applies to en_US locale)"
253 TestLog_ASSERT (false), testName
& ": hit error handler"
256 Sub Custom_Number_Format_Sample()
257 Dim TestStr
As String
258 Dim testName
As String
260 testName
= "Test Custom_Number_Format_Sample function"
261 On Error GoTo errorHandler
263 TestStr
= Format(23.675, "00.0000") ' 23.6750
264 TestLog_ASSERT TestStr
= "23.6750", "00.0000: " & TestStr
267 TestStr
= Format(23.675, "00.00") ' 23.68
268 TestLog_ASSERT TestStr
= "23.68", "00.00: " & TestStr
271 TestStr
= Format(2658, "00000") ' 02658
272 TestLog_ASSERT TestStr
= "02658", "00000: " & TestStr
275 TestStr
= Format(2658, "00.00") ' 2658.00
276 TestLog_ASSERT TestStr
= "2658.00", "00.00: " & TestStr
279 TestStr
= Format(23.675, "##.####") ' 23.675
280 TestLog_ASSERT TestStr
= "23.675", "##.####: " & TestStr
283 TestStr
= Format(23.675, "##.##") ' 23.68
284 TestLog_ASSERT TestStr
= "23.68", "##.##: " & TestStr
287 TestStr
= Format(12345.25, "#,###.##") '12,345.25
288 TestLog_ASSERT TestStr
= "12,345.25", "#,###.##: " & TestStr
291 TestStr
= Format(0.25, "##.00%") '25.00%
292 TestLog_ASSERT TestStr
= "25.00%", "##.00%: " & TestStr
295 TestStr
= Format(1000000, "#,###") '1,000,000
296 TestLog_ASSERT TestStr
= "1,000,000", "#,###: " & TestStr
299 TestStr
= Format(1.09837555, "#.#####E+###") '1.09838E+000
300 TestLog_ASSERT TestStr
= "1.09838E+000", "#.#####E+###: " & TestStr
303 TestStr
= Format(1.09837555, "###.####E#") '1.0984E0 with engineering notation
304 TestLog_ASSERT TestStr
= "1.0984E0", "###.####E#: " & TestStr
307 TestStr
= Format(1098.37555, "###.####E#") '1.0984E3 with engineering notation
308 TestLog_ASSERT TestStr
= "1.0984E3", "###.####E#: " & TestStr
311 TestStr
= Format(1098375.55, "###.####E#") '1.0984E6 with engineering notation
312 TestLog_ASSERT TestStr
= "1.0984E6", "###.####E#: " & TestStr
315 TestStr
= Format(1.09837555, "######E#") '1E0 with engineering notation
316 TestLog_ASSERT TestStr
= "1E0", "######E#: " & TestStr
319 TestStr
= Format(123456.789, "###E0") '123E3 with engineering notation
320 TestLog_ASSERT TestStr
= "123E3", "###E0: " & TestStr
323 TestStr
= Format(123567.89, "###E0") '124E3 with engineering notation
324 TestLog_ASSERT TestStr
= "124E3", "###E0: " & TestStr
327 TestStr
= Format(12, "###E0") '12E0 with engineering notation
328 TestLog_ASSERT TestStr
= "12E0", "###E0: " & TestStr
331 TestStr
= Format(12, "000E0") '012E0 with engineering notation
332 TestLog_ASSERT TestStr
= "012E0", "000E0: " & TestStr
335 TestStr
= Format(0.12345, "###E0") '123E-3 with engineering notation
336 TestLog_ASSERT TestStr
= "123E-3", "###E0: " & TestStr
339 TestStr
= Format(123456, "####E0") '12E4 with interval-4 notation
340 TestLog_ASSERT TestStr
= "12E4", "####E0: " & TestStr
343 TestStr
= Format(2345.25, "$#,###.##") '$2.345.25
344 TestLog_ASSERT TestStr = "$2,345.25", "$#,###.##: " & TestStr
347 TestStr
= Format(0.25, "##.###\%") '.25%
348 TestLog_ASSERT TestStr
= ".25%", "##.###\%: " & TestStr
353 TestLog_ASSERT (false), testName
& ": hit error handler"
356 Sub Custom_Text_Format_Sample()
357 Dim myText
, TestStr
As String
360 Dim testName
As String
362 testName
= "Test Custom_Text_Format_Sample function"
363 On Error GoTo errorHandler
365 TestStr
= Format(myText
, "<") 'vba
366 TestLog_ASSERT TestStr
= "vba", "<: " & TestStr
369 TestStr
= Format("vba", ">") 'VBA
370 TestLog_ASSERT TestStr
= "VBA", ">: " & TestStr
375 TestLog_ASSERT (false), testName
& "hit error handler"
381 Dim testName
As String
382 Dim TestDateTime
As Date
383 Dim TestStr
As String
384 testName
= "Test Format function"
386 On Error GoTo errorHandler
388 TestDateTime
= "1/27/2001 5:04:23 PM"
390 ' Returns the value of TestDateTime in user-defined date/time formats.
392 TestStr
= Format(TestDateTime
, "h:m:s")
393 TestLog_ASSERT TestStr
= "17:4:23", "the format of h:m:s: " & TestStr
395 ' Returns "05:04:23 PM".
396 TestStr
= Format(TestDateTime
, "ttttt")
397 TestLog_ASSERT TestStr
= "5:04:23 PM", "the format of ttttt: " & TestStr
399 ' Returns "Saturday, Jan 27 2001".
400 TestStr
= Format(TestDateTime
, "dddd, MMM d yyyy")
401 TestLog_ASSERT TestStr
= "Saturday, Jan 27 2001", "the format of dddd, MMM d yyyy: " & TestStr
403 ' Returns "17:04:23".
404 TestStr
= Format(TestDateTime
, "HH:mm:ss")
405 TestLog_ASSERT TestStr
= "17:04:23", "the format of HH:mm:ss: " & TestStr
409 TestLog_ASSERT TestStr
= "23", "no format:" & TestStr
411 ' User-defined numeric formats.
412 ' Returns "5,459.40".
413 TestStr
= Format(5459.4, "##,##0.00")
414 TestLog_ASSERT TestStr
= "5,459.40", "the format of ##,##0.00: " & TestStr
417 TestStr
= Format(334.9, "###0.00")
418 TestLog_ASSERT TestStr
= "334.90", "the format of ###0.00: " & TestStr
421 TestStr
= Format(5, "0.00%")
422 TestLog_ASSERT TestStr
= "500.00%", "the format of 0.00%: " & TestStr
425 TestLog_ASSERT (false), testName
& ": hit error handler"
428 Sub TestLog_ASSERT(assertion
As Boolean, Optional testId
As String, Optional testComment
As String)
430 If assertion
= True Then
431 passCount
= passCount
+ 1
433 Dim testMsg
As String
434 If Not IsMissing(testId
) Then
435 testMsg
= testMsg
+ " : " + testId
437 If Not IsMissing(testComment
) And Not (testComment
= "") Then
438 testMsg
= testMsg
+ " (" + testComment
+ ")"
441 result
= result
& Chr
$(10) & " Failed: " & testMsg
442 failCount
= failCount
+ 1