private Sub operation(a As Int, b As Int, c As String) As Int
Select c
Case "disguisse"
Return (a + b) * 3 / 2
Case "encrypt"
Return (a * b * 2) + 5
End Select
End Sub
private Sub operation(a As Int, b As Int, c As String) As Int
Select c
Case "disguisse"
Return a + b * 3 / 2
Case "encrypt"
Return (a * b * 2) + 5
End Select
End Sub
assert(operation(5, 3, "disguisse"), 12)
private Sub assert(fun As Int, expected As Int) As Boolean
If fun <> expected Then
Log($"function returned ${fun} but was expected to return ${expected}"$)
End If
Return fun = expected
End Sub
private Sub operation(a As Int, b As Int, c As String) As Int
Select c
Case "disguisse"
Return (a + b) * 3 / 2
Case "disguisse_2"
Return a + b * 3 / 2
Case "encrypt"
Return (a * b * 2) + 5
End Select
End Sub
beside of some metaprogramming, i believe that we only need Javaobject (may be reflection too) to create such framework. as can we create something like this:I suspect it's a lot more fitting to have it built-in into B4X itself.
public Sub repl
Log(assert("operation", Array As Object(5, 3, "disguisse"), 12))
End Sub
private Sub assert(function As String, values() As Object, expected As Object) As Boolean
Dim result As Object = call_sub_dynamic(function, values)
If Not(result = expected) Then
Log($"${function} returned ${result} but was expected to return ${expected}"$)
End If
Return result = expected
End Sub
private Sub operation(a As Int, b As Int, c As String) As Int
Select c
Case "disguisse"
Return (a + b) * 3 / 2
Case "disguisse_2"
Return a + b * 3 / 2
Case "encrypt"
Return (a * b * 2) + 5
End Select
End Sub
private Sub call_sub_dynamic(name As String, arr() As Object) As Object
Dim jo As JavaObject = Me
Return jo.RunMethod($"_${name}"$, arr)
End Sub
Sub AppStart (Args() As String)
#if UNIT_TESTS
RunTests
#End If
Log("Hello world!!!")
End Sub
Sub OnePlusTwo As Int
Return 1+2
End Sub
#if UNIT_TESTS
Sub RunTests
Assert(OnePlusTwo,3)
Assert(OnePlusTwo,1)
End Sub
Sub Assert(A As Object, B As Object)
Return IIf(A=B,Pass,Fail)
End Sub
Sub Pass
Log("Pass") 'Or write to a file
End Sub
Sub Fail
Log("Fail") 'Or write to a file
End Sub
#End If
That would make it non-cross-platform.i believe that we only need Javaobject
I've never seen unit tests interwoven with the actual code like that. Seems messy to me.a really simple option is something like this
Well, if it was integrated into B4X, you could have a special type of module just for tests, that never gets included into a final build. (I'm obviously hand-waving a bit here, but I hope the idea comes through.)I think Main is probabaly the best place to have the tests because even if you have an empty code module there will be traces of the module in the Jar file - this is generally why repos have two projects instead of build configurations...
This is incorrect. B4i has native object that behaves the same. I can't talk for b4r tho.That would make it non-cross-platform.
We already have a directive # for that, for libraries, I guess it's not difficult to extendthat never gets included into a final build.
Agree. It is messy but the tests wouldnt appear in Release builds...I've never seen unit tests interwoven with the actual code like that. Seems messy to me.
Well, if it was integrated into B4X, you could have a special type of module just for tests, that never gets included into a final build. (I'm obviously hand-waving a bit here, but I hope the idea comes through.)
Which directive is that?We already have a directive # for that, for libraries, I guess it's not difficult to extend
#ExcludeFromLibraryWhich directive is that?
Yeah something like that would be perfect!#ExcludeFromLibrary
From my understanding, some programming languages use a function call assert to test the functions.
Not sure this practice is useful in RAD tool like B4X or it can be a topic for debate.
Maybe we can test the inputs like division by zero, input Double value where Integer is expected or input String value to an input that expects numeric and input a very long String to test the limit in database field.
I dont think its necessary to use Java Object, a really simple option is something like this.
B4X:Sub AppStart (Args() As String) #if UNIT_TESTS RunTests #End If Log("Hello world!!!") End Sub Sub OnePlusTwo As Int Return 1+2 End Sub #if UNIT_TESTS Sub RunTests Assert(OnePlusTwo,3) Assert(OnePlusTwo,1) End Sub Sub Assert(A As Object, B As Object) Return IIf(A=B,Pass,Fail) End Sub Sub Pass Log("Pass") 'Or write to a file End Sub Sub Fail Log("Fail") 'Or write to a file End Sub #End If
HI, All
I know about this term only theoretically.
How should it look in B4X development ? Especially in B4R ...
Any example of codes ?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?