Revert "tdf#158280 Replace usage of InputDialog with SvxNameDialog"
[LibreOffice.git] / basic / qa / vba_tests / format.vb
blob67abc5ffe24ac55ea467d689c342ecdac512bd4e
2 ' This file is part of the LibreOffice project.
4 ' This Source Code Form is subject to the terms of the Mozilla Public
5 ' License, v. 2.0. If a copy of the MPL was not distributed with this
6 ' file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 Option VBASupport 1
10 Option Explicit
12 Function doUnitTest() As String
13 TestUtil.TestInit
15 'Predefined_Datetime_Format_Sample
16 Predefined_Number_Format_Sample
17 'Custom_Datetime_Format_Sample
18 Custom_Number_Format_Sample
19 Custom_Text_Format_Sample
20 testFormat
22 doUnitTest = TestUtil.GetResult()
23 End Function
25 Sub Predefined_Datetime_Format_Sample()
26 Dim TestStr As String
27 const myDate = "01/06/98"
28 const MyTime = "17:08:06"
30 On Error GoTo errorHandler
32 ' These tests only apply to en_US locale
34 ' The date/time format have a little different between ms office and OOo due to different locale and system...
35 TestStr = Format(myDate, "General Date") ' 1/6/98
36 TestUtil.Assert(IsDate(TestStr), "IsDate(TestStr)", "General Date")
37 'TestUtil.AssertEqual(TestStr, "1/6/98", "General Date")
39 TestStr = Format(myDate, "Long Date") ' Tuesday, January 06, 1998
40 TestUtil.AssertEqual(TestStr, "Tuesday, January 06, 1998", "Long Date")
41 'TestUtil.Assert(IsDate(TestStr), "IsDate(TestStr)", "Long Date")
43 TestStr = Format(myDate, "Medium Date") ' 06-Jan-98
44 'TestUtil.AssertEqual(TestStr, "06-Jan-98", "Medium Date")
45 TestUtil.Assert(IsDate(TestStr), "IsDate(TestStr)", "Medium Date")
47 TestStr = Format(myDate, "Short Date") ' 1/6/98
48 'TestUtil.AssertEqual(TestStr, "1/6/98", "Short Date")
49 TestUtil.Assert(IsDate(TestStr), "IsDate(TestStr)", "Short Date")
51 TestStr = Format(MyTime, "Long Time") ' 5:08:06 PM
52 'TestUtil.AssertEqual(TestStr, "5:08:06 PM", "Long Time")
53 TestUtil.Assert(IsDate(TestStr), "IsDate(TestStr)", "Long Time")
55 TestStr = Format(MyTime, "Medium Time") ' 05:08 PM
56 'TestUtil.AssertEqual(TestStr, "05:08 PM", "Medium Time")
57 TestUtil.Assert(IsDate(TestStr), "IsDate(TestStr)", "Medium Time")
59 TestStr = Format(MyTime, "Short Time") ' 17:08
60 'TestUtil.AssertEqual(TestStr, "17:08", "Short Time")
61 TestUtil.Assert(IsDate(TestStr), "IsDate(TestStr)", "Short Time")
62 Exit Sub
63 errorHandler:
64 TestUtil.ReportErrorHandler("Predefined_Datetime_Format_Sample", Err, Error$, Erl)
65 End Sub
67 Sub Predefined_Number_Format_Sample()
68 On Error GoTo errorHandler
70 TestUtil.AssertEqual(Format(562486.2356, "General Number"), "562486.2356", "Format(562486.2356, ""General Number"")")
71 TestUtil.AssertEqual(Format(0.2, "Fixed"), "0.20", "Format(0.2, ""Fixed"")")
72 TestUtil.AssertEqual(Format(562486.2356, "Standard"), "562,486.24", "Format(562486.2356, ""Standard"")")
73 TestUtil.AssertEqual(Format(0.7521, "Percent"), "75.21%", "Format(0.7521, ""Percent"")")
74 TestUtil.AssertEqual(Format(562486.2356, "Scientific"), "5.62E+05", "Format(562486.2356, ""Scientific"")")
75 TestUtil.AssertEqual(Format(-3456.789, "Scientific"), "-3.46E+03", "Format(-3456.789, ""Scientific"")")
76 TestUtil.AssertEqual(Format(0, "Yes/No"), "No", "Format(0, ""Yes/No"")")
77 TestUtil.AssertEqual(Format(23, "Yes/No"), "Yes", "Format(23, ""Yes/No"")")
78 TestUtil.AssertEqual(Format(0, "True/False"), "False", "Format(0, ""True/False"")")
79 TestUtil.AssertEqual(Format(23, "True/False"), "True", "Format(23, ""True/False"")")
80 TestUtil.AssertEqual(Format(0, "On/Off"), "Off", "Format(0, ""On/Off"")")
81 TestUtil.AssertEqual(Format(23, "On/Off"), "On", "Format(23, ""On/Off"")")
83 Exit Sub
84 errorHandler:
85 TestUtil.ReportErrorHandler("Predefined_Number_Format_Sample", Err, Error$, Erl)
86 End Sub
88 Sub Custom_Datetime_Format_Sample()
89 const myDate = "01/06/98"
90 const MyTime = "05:08:06"
91 const MyTimePM = "17:08:06"
93 On Error GoTo errorHandler
95 ' These tests only apply to en_US locale
96 TestUtil.AssertEqual(Format("01/06/98 17:08:06", "c"), "1/6/98 5:08:06 PM", "Format(""01/06/98 17:08:06"", ""c"")")
97 TestUtil.AssertEqual(Format(myDate, "dddddd"), "Tuesday, January 06, 1998", "Format(myDate, ""dddddd"")")
98 TestUtil.AssertEqual(Format(myDate, "mm-dd-yyyy"), "01-06-1998", "Format(myDate, ""mm-dd-yyyy"")")
99 TestUtil.AssertEqual(Format(myDate, "d"), "6", "Format(myDate, ""d"")")
100 TestUtil.AssertEqual(Format(myDate, "dd"), "06", "Format(myDate, ""dd"")")
101 TestUtil.AssertEqual(Format(myDate, "ddd"), "Tue", "Format(myDate, ""ddd"")")
102 TestUtil.AssertEqual(Format(myDate, "dddd"), "Tuesday", "Format(myDate, ""dddd"")")
103 TestUtil.AssertEqual(Format(MyTime, "h"), "5", "Format(MyTime, ""h"")")
104 TestUtil.AssertEqual(Format(MyTime, "hh"), "05", "Format(MyTime, ""hh"")")
105 TestUtil.AssertEqual(Format(MyTime, "n"), "8", "Format(MyTime, ""n"")")
106 TestUtil.AssertEqual(Format(MyTime, "nn"), "08", "Format(MyTime, ""nn"")")
107 TestUtil.AssertEqual(Format(myDate, "m"), "1", "Format(myDate, ""m"")")
108 TestUtil.AssertEqual(Format(myDate, "mm"), "01", "Format(myDate, ""mm"")")
109 TestUtil.AssertEqual(Format(myDate, "mmm"), "Jan", "Format(myDate, ""mmm"")")
110 TestUtil.AssertEqual(Format(myDate, "mmmm"), "January", "Format(myDate, ""mmmm"")")
111 TestUtil.AssertEqual(Format(MyTime, "s"), "6", "Format(MyTime, ""s"")")
112 TestUtil.AssertEqual(Format(MyTime, "ss"), "06", "Format(MyTime, ""ss"")")
113 TestUtil.AssertEqual(Format(MyTimePM, "hh:mm:ss AM/PM"), "05:08:06 PM", "Format(MyTimePM, ""hh:mm:ss AM/PM"")")
114 TestUtil.AssertEqual(Format(MyTimePM, "hh:mm:ss"), "17:08:06", "Format(MyTimePM, ""hh:mm:ss"")")
115 TestUtil.AssertEqual(Format(myDate, "ww"), "2", "Format(myDate, ""ww"")")
116 TestUtil.AssertEqual(Format(myDate, "w"), "3", "Format(myDate, ""w"")")
117 TestUtil.AssertEqual(Format(myDate, "y"), "6", "Format(myDate, ""y"")")
118 TestUtil.AssertEqual(Format(myDate, "yy"), "98", "Format(myDate, ""yy"")")
119 TestUtil.AssertEqual(Format(myDate, "yyyy"), "1998", "Format(myDate, ""yyyy"")")
121 Exit Sub
122 errorHandler:
123 TestUtil.ReportErrorHandler("Custom_Datetime_Format_Sample", Err, Error$, Erl)
124 End Sub
126 Sub Custom_Number_Format_Sample()
127 On Error GoTo errorHandler
129 TestUtil.AssertEqual(Format(23.675, "00.0000"), "23.6750", "Format(23.675, ""00.0000"")")
130 TestUtil.AssertEqual(Format(23.675, "00.00"), "23.68", "Format(23.675, ""00.00"")")
131 TestUtil.AssertEqual(Format(2658, "00000"), "02658", "Format(2658, ""00000"")")
132 TestUtil.AssertEqual(Format(2658, "00.00"), "2658.00", "Format(2658, ""00.00"")")
133 TestUtil.AssertEqual(Format(23.675, "##.####"), "23.675", "Format(23.675, ""##.####"")")
134 TestUtil.AssertEqual(Format(23.675, "##.##"), "23.68", "Format(23.675, ""##.##"")")
135 TestUtil.AssertEqual(Format(12345.25, "#,###.##"), "12,345.25", "Format(12345.25, ""#,###.##"")")
136 TestUtil.AssertEqual(Format(0.25, "##.00%"), "25.00%", "Format(0.25, ""##.00%"")")
137 TestUtil.AssertEqual(Format(1000000, "#,###"), "1,000,000", "Format(1000000, ""#,###"")")
138 TestUtil.AssertEqual(Format(1.09837555, "#.#####E+000"), "1.09838E+000", "Format(1.09837555, ""#.#####E+000"")")
139 TestUtil.AssertEqual(Format(1.09837555, "###.####E#"), "1.0984E0", "Format(1.09837555, ""###.####E#"")")
140 TestUtil.AssertEqual(Format(1098.37555, "###.####E#"), "1.0984E3", "Format(1098.37555, ""###.####E#"")")
141 TestUtil.AssertEqual(Format(1098375.55, "###.####E#"), "1.0984E6", "Format(1098375.55, ""###.####E#"")")
142 TestUtil.AssertEqual(Format(1.09837555, "######E#"), "1E0", "Format(1.09837555, ""######E#"")")
143 TestUtil.AssertEqual(Format(123456.789, "###E0"), "123E3", "Format(123456.789, ""###E0"")")
144 TestUtil.AssertEqual(Format(123567.89, "###E0"), "124E3", "Format(123567.89, ""###E0"")")
145 TestUtil.AssertEqual(Format(12, "###E0"), "12E0", "Format(12, ""###E0"")")
146 TestUtil.AssertEqual(Format(12, "000E0"), "012E0", "Format(12, ""000E0"")")
147 TestUtil.AssertEqual(Format(0.12345, "###E0"), "123E-3", "Format(0.12345, ""###E0"")")
148 TestUtil.AssertEqual(Format(123456, "####E0"), "12E4", "Format(123456, ""####E0"")")
149 TestUtil.AssertEqual(Format(2345.25, "$#,###.##"), "$2,345.25", "Format(2345.25, ""$#,###.##"")")
150 TestUtil.AssertEqual(Format(0.25, "##.###\%"), ".25%", "Format(0.25, ""##.###\%"")")
151 ' tdf#158890 blank replaced by figure blank
152 TestUtil.AssertEqual(Format(12.25, "0.???"), "12.25", "Format(12.25, ""0.???"")")
154 Exit Sub
155 errorHandler:
156 TestUtil.ReportErrorHandler("Custom_Number_Format_Sample", Err, Error$, Erl)
157 End Sub
159 Sub Custom_Text_Format_Sample()
160 On Error GoTo errorHandler
162 TestUtil.AssertEqual(Format("VBA", "<"), "vba", "Format(""VBA"", ""<"")")
163 TestUtil.AssertEqual(Format("vba", ">"), "VBA", "Format(""vba"", "">"")")
165 Exit Sub
166 errorHandler:
167 TestUtil.ReportErrorHandler("Custom_Text_Format_Sample", Err, Error$, Erl)
168 End Sub
170 Sub testFormat()
171 On Error GoTo errorHandler
173 const TestDateTime = #2001-1-27T17:04:23#
174 TestUtil.AssertEqual(Format(TestDateTime, "h:m:s"), "17:4:23", "Format(TestDateTime, ""h:m:s"")")
175 TestUtil.AssertEqual(Format(TestDateTime, "ttttt"), "5:04:23 PM", "Format(TestDateTime, ""ttttt"")")
176 TestUtil.AssertEqual(Format(TestDateTime, "dddd, MMM d yyyy"), "Saturday, Jan 27 2001", "Format(TestDateTime, ""dddd, MMM d yyyy"")")
177 TestUtil.AssertEqual(Format(TestDateTime, "HH:mm:ss"), "17:04:23", "Format(TestDateTime, ""HH:mm:ss"")")
179 TestUtil.AssertEqual(Format(23), "23", "Format(23)")
180 TestUtil.AssertEqual(Format(5459.4, "##,##0.00"), "5,459.40", "Format(5459.4, ""##,##0.00"")")
181 TestUtil.AssertEqual(Format(334.9, "###0.00"), "334.90", "Format(334.9, ""###0.00"")")
182 TestUtil.AssertEqual(Format(5, "0.00%"), "500.00%", "Format(5, ""0.00%"")")
184 Exit Sub
185 errorHandler:
186 TestUtil.ReportErrorHandler("testFormat", Err, Error$, Erl)
187 End Sub