Skip to content

Instantly share code, notes, and snippets.

@mephistobooks
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mephistobooks/9410650 to your computer and use it in GitHub Desktop.
Save mephistobooks/9410650 to your computer and use it in GitHub Desktop.
OOo Basic のUnit Testのためのヘルパー関数
<?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">&apos; ****************************************************************
Option Explicit
&apos; ****************************************************************
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
&apos; ****************************************************************
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
&apos; ****************************************************************
&apos; by-reference variable substituion via calling function.
&apos; (we cannot do paper = LBAUnit_helper_console, directly!!!)
&apos;
Private Function __write_str_impl( msg As String, paper As String ) As String
If Len(paper) = 0 Then
paper = paper &amp; msg
Else paper = paper &amp; Chr(13) &amp; 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
&apos; ****************************************************************
Public Sub prep( Optional msg_title As String )
&apos;LBAUnit_helper_console = Nothing
&apos;LBAUnit_helper_console = &quot;&quot;
LBAUnit_helper_title = &quot;LBAUnit assertions&quot;
If Not IsMissing( msg_title ) Then
LBAUnit_helper_title = msg_title
End If
LBAUnit_helper_console = String(76,&quot;-&quot;)+Chr(13)
LBAUnit_helper_num_assertions = 0
LBAUnit_helper_num_failed = 0
&apos;
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 = &quot;🔥&quot;
s_bug_lady = &quot;🐞&quot;
s_bug_sushi = &quot;🍣&quot;
s_ok_check = &quot;✅&quot;
LBAUnit_helper_s_ok = s_ok_check
LBAUnit_helper_s_bug = s_bug_sushi
End Sub
&apos; Multi-byte String().
&apos;
&apos;
Private Function MBString( t As Integer, str As String ) As String
Dim i As Integer
Dim ret As String
t = t - 1
If t &lt; 1 Then
Exit Function
End If
&apos;
ret = str
For i = 1 To t
ret = ret &amp; str
Next i
MBString = ret
End Function
&apos; write msg into pseudo-console.
Public Sub con_puts( msg As String )
__write_str msg
End Sub
&apos; 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
&apos; display test result.
Public Sub puts_results()
&apos;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( &quot;# of assertions: &quot; &amp; LBAUnit_helper_num_assertions )
con_puts( &quot;# of fails: &quot; &amp; LBAUnit_helper_num_failed )
con_puts( &quot;&quot; )
&apos; 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() )
&apos;__write_str( &quot;Progress: &quot; &amp; passed_s &amp; failed_s, ostr )
con_puts( &quot;Progress: &quot; &amp; 100*ratio_passed/(ratio_passed+ratio_failed) &amp; &quot;% are passed.&quot; )
con_puts( passed_s &amp; failed_s )
&apos;MsgBox ostr
If 0 = num_failed() Then
con_display(64) &apos; !
Else
con_display(32) &apos; ?
End If
End Sub
&apos;
Public Sub Debug_Print( msg As String )
&apos;Print &quot;DEBUG: &quot; &amp; msg
puts(msg, &quot;DEBUG:&quot;)
End Sub
Public Sub puts( msg As String, Optional header As String )
Dim str As String
&apos; construct message string.
str = &quot; &quot; &amp; msg
If Not IsMissing( header ) Then
str = CStr( header ) &amp; str
End If
MsgBox str
End Sub
&apos; ****************************************************************
&apos; primitive assert.
Public Function __assert( exp As Variant, act As Variant, Optional msg_failure As String ) As Boolean
Dim ret As Boolean
&apos;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
&apos; 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
&apos;ret = (exp = act)
If exp = act Then
arg = True
Else arg = False
End If
&apos;
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
&apos; 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>
<?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:= &quot; foo &quot;, &quot;Test&quot; )
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 = &quot;boochanged&quot; &amp; sm
end sub
sub aaa
Dim s as String
dim b as Object
Print TypeName(&quot;foo&quot;)
s = &quot;foo&quot;
Set b = s
s = &quot;bar&quot;
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