Last active
August 29, 2015 13:57
-
-
Save mephistobooks/9410650 to your computer and use it in GitHub Desktop.
OOo Basic のUnit Testのためのヘルパー関数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd"> | |
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Helper" script:language="StarBasic">' **************************************************************** | |
Option Explicit | |
' **************************************************************** | |
Private LBAUnit_helper_title As String | |
Private LBAUnit_helper_console As String | |
Private LBAUnit_helper_num_assertions As Long | |
Private LBAUnit_helper_num_failed As Long | |
Private LBAUnit_helper_s_ok As String | |
Private LBAUnit_helper_s_bug As String | |
' **************************************************************** | |
Public Function title() As String | |
title = LBAUnit_helper_title | |
End Function | |
Public Function console() As String | |
console = LBAUnit_helper_console | |
End Function | |
Public Function num_assertions() As Long | |
num_assertions = LBAUnit_helper_num_assertions | |
End Function | |
Public Function num_failed() As Long | |
num_failed = LBAUnit_helper_num_failed | |
End Function | |
Public Sub add_num_assertions | |
LBAUnit_helper_num_assertions = LBAUnit_helper_num_assertions + 1 | |
End Sub | |
Public Sub add_num_failed | |
LBAUnit_helper_num_failed = LBAUnit_helper_num_failed + 1 | |
End Sub | |
Public Function s_ok() As String | |
s_ok = LBAUnit_helper_s_ok | |
End Function | |
Public Function s_bug() As String | |
s_bug = LBAUnit_helper_s_bug | |
End Function | |
' **************************************************************** | |
' by-reference variable substituion via calling function. | |
' (we cannot do paper = LBAUnit_helper_console, directly!!!) | |
' | |
Private Function __write_str_impl( msg As String, paper As String ) As String | |
If Len(paper) = 0 Then | |
paper = paper & msg | |
Else paper = paper & Chr(13) & msg | |
End If | |
__write_str_impl = paper | |
End Function | |
Private Function __write_str( msg As String, Optional paper As String ) As String | |
Dim ret As String | |
If IsMissing(paper) Then | |
ret = __write_str_impl( msg, LBAUnit_helper_console ) | |
Else | |
ret = __write_str_impl( msg, paper ) | |
End If | |
__write_str = ret | |
End Function | |
' **************************************************************** | |
Public Sub prep( Optional msg_title As String ) | |
'LBAUnit_helper_console = Nothing | |
'LBAUnit_helper_console = "" | |
LBAUnit_helper_title = "LBAUnit assertions" | |
If Not IsMissing( msg_title ) Then | |
LBAUnit_helper_title = msg_title | |
End If | |
LBAUnit_helper_console = String(76,"-")+Chr(13) | |
LBAUnit_helper_num_assertions = 0 | |
LBAUnit_helper_num_failed = 0 | |
' | |
Dim s_bug_fire As String | |
Dim s_bug_lady As String | |
Dim s_bug_sushi As String | |
Dim s_ok_check As String | |
Dim s_bug As String | |
Dim s_ok As String | |
s_bug_fire = "🔥" | |
s_bug_lady = "🐞" | |
s_bug_sushi = "🍣" | |
s_ok_check = "✅" | |
LBAUnit_helper_s_ok = s_ok_check | |
LBAUnit_helper_s_bug = s_bug_sushi | |
End Sub | |
' Multi-byte String(). | |
' | |
' | |
Private Function MBString( t As Integer, str As String ) As String | |
Dim i As Integer | |
Dim ret As String | |
t = t - 1 | |
If t < 1 Then | |
Exit Function | |
End If | |
' | |
ret = str | |
For i = 1 To t | |
ret = ret & str | |
Next i | |
MBString = ret | |
End Function | |
' write msg into pseudo-console. | |
Public Sub con_puts( msg As String ) | |
__write_str msg | |
End Sub | |
' display the pseudo-console. | |
Public Sub con_display( Optional msgbox_t As Integer ) | |
If Not IsMissing( msgbox_t ) Then | |
MsgBox console(), msgbox_t, title() | |
Else | |
MsgBox console(), 1, title() | |
End If | |
End Sub | |
' display test result. | |
Public Sub puts_results() | |
'Dim ostr As String | |
Dim ratio_passed As Long | |
Dim ratio_failed As Long | |
Dim passed_s As String | |
Dim failed_s as String | |
con_puts( "# of assertions: " & LBAUnit_helper_num_assertions ) | |
con_puts( "# of fails: " & LBAUnit_helper_num_failed ) | |
con_puts( "" ) | |
' Percentage. | |
ratio_failed = 10 * num_failed() / num_assertions() | |
ratio_passed = 10 - ratio_failed | |
failed_s = MBString( ratio_failed, s_bug() ) | |
passed_s = MBString( ratio_passed, s_ok() ) | |
'__write_str( "Progress: " & passed_s & failed_s, ostr ) | |
con_puts( "Progress: " & 100*ratio_passed/(ratio_passed+ratio_failed) & "% are passed." ) | |
con_puts( passed_s & failed_s ) | |
'MsgBox ostr | |
If 0 = num_failed() Then | |
con_display(64) ' ! | |
Else | |
con_display(32) ' ? | |
End If | |
End Sub | |
' | |
Public Sub Debug_Print( msg As String ) | |
'Print "DEBUG: " & msg | |
puts(msg, "DEBUG:") | |
End Sub | |
Public Sub puts( msg As String, Optional header As String ) | |
Dim str As String | |
' construct message string. | |
str = " " & msg | |
If Not IsMissing( header ) Then | |
str = CStr( header ) & str | |
End If | |
MsgBox str | |
End Sub | |
' **************************************************************** | |
' primitive assert. | |
Public Function __assert( exp As Variant, act As Variant, Optional msg_failure As String ) As Boolean | |
Dim ret As Boolean | |
'ret = (exp = act) | |
If exp = act Then | |
ret = True | |
Else ret = False | |
End If | |
If Not( IsMissing(msg_failure)) And Not( ret) Then | |
con_puts msg_failure | |
End If | |
__assert = ret | |
End Function | |
' primitive assert with statistics | |
Public Function assert( meta_exp As Boolean, exp As Variant, act As Variant, Optional msg As String ) As Boolean | |
Dim ret As Boolean | |
Dim arg As Boolean | |
'ret = (exp = act) | |
If exp = act Then | |
arg = True | |
Else arg = False | |
End If | |
' | |
If IsMissing( msg ) Then | |
ret = __assert( meta_exp, arg ) | |
Else | |
ret = __assert( meta_exp, arg, msg ) | |
End If | |
add_num_assertions() | |
If Not ret Then | |
add_num_failed() | |
End If | |
assert = ret | |
End Function | |
' API of Unit Test | |
Public Function assert_equal( exp As Variant, act As Variant, Optional msg As String ) As Boolean | |
Dim ret As Boolean | |
If IsMissing( msg ) Then | |
ret = assert( True, exp, act ) | |
Else | |
ret = assert( True, exp, act, msg ) | |
End If | |
assert_equal = ret | |
End Function | |
Public Function assert_not_equal( exp As Variant, act As Variant, Optional msg As String ) As Boolean | |
Dim ret As Boolean | |
If IsMissing( msg ) Then | |
ret = assert( False, exp, act ) | |
Else | |
ret = assert( False, exp, act, msg ) | |
End If | |
assert_not_equal = ret | |
End Function | |
</script:module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd"> | |
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="SandBox" script:language="StarBasic">REM ***** BASIC ***** | |
Option Explicit | |
Sub Main | |
Helper.puts ( msg:= " foo ", "Test" ) | |
Helper.prep() | |
Helper.con_puts Helper.assert_equal( 0, 1 ) | |
Helper.con_puts Helper.assert_equal( 0, 0 ) | |
Helper.con_puts Helper.assert_not_equal( 1, 0 ) | |
Helper.con_puts Helper.assert_not_equal( 1, 1 ) | |
Helper.puts_results() | |
End Sub | |
sub boo( sm as string ) as string | |
sm = "boochanged" & sm | |
end sub | |
sub aaa | |
Dim s as String | |
dim b as Object | |
Print TypeName("foo") | |
s = "foo" | |
Set b = s | |
s = "bar" | |
Print b | |
boo(b) | |
print b | |
end sub | |
</script:module> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment