How to unit test an Erlang function? - unit-testing

get_ue_supported_srvcc([]) ->
?SRVCC_3GPP_NONE_SUPPORT;
get_ue_supported_srvcc([#sip_contactV{extensionsP = EP} | T]) ->
case b2bLib:support_tags_to_value(EP) of
?SRVCC_3GPP_NONE_SUPPORT ->
get_ue_supported_srvcc(T);
Flag ->
Flag
end.
I want create a unit test for this function,
Here is my unit test case:
get_ue_supported_srvcc_test() ->
Contact =
[#sip_contactV{extensionsP =
[{"+sip.instance",
{quoted_string,"<urn:gsma:imei:35502406-005233-0>"}},
{"+g.3gpp.icsi-ref",
{quoted_string,"urn%3Aurn-7%3A3gpp-service.ims.icsi.mmtel"}},
"+g.3gpp.mid-call",
"+g.3gpp.srvcc-alerting",
"+g.3gpp.ps2cs-srvcc-orig-pre-alerting",
"video"]}],
?assertEqual(7, b2bAtcfLib:get_ue_supported_srvcc(Contact)).
But when I run it, I get this error:
======================== EUnit ========================
module 'b2bAtcfLib'
b2bAtcfLib_tests: get_ue_supported_srvcc_test (module 'b2bAtcfLib_tests')...*failed*
in function b2bLib:support_tags_to_value/1
called as support_tags_to_value([{"+sip.instance",{quoted_string,"<urn:gsma:imei:35502406-005233-0>"}},
{"+g.3gpp.icsi-ref",
{quoted_string,"urn%3Aurn-7%3A3gpp-service.ims.icsi.mmtel"}},
"+g.3gpp.mid-call","+g.3gpp.srvcc-alerting",
"+g.3gpp.ps2cs-srvcc-orig-pre-alerting","video"])
in call from b2bAtcfLib:get_ue_supported_srvcc/1 (src/b2bAtcfLib.erl, line 1735)
in call from b2bAtcfLib_tests:'-get_ue_supported_srvcc_test/0-fun-0-'/1 (test/unit/b2bAtcfLib_tests.erl, line 49)
in call from b2bAtcfLib_tests:get_ue_supported_srvcc_test/0
**error:undef
output:<<"">>
[done in 0.008 s]
=======================================================
The error means b2bLib:support_tags_to_value/1 is undef.
The define for this function b2bLib:support_tags_to_value:
support_tags_to_value(FieldStr) ->
lists:sum([Val || {Tag, Val} <- ?TAGLIST, lists:member(Tag, FieldStr)]).

The error is:
**error:undef
That means that the test is calling a function that's not defined. Either the module couldn't be found, or the module in question doesn't define a function with that name and arity.
The whole error message is a bit confusing. Now that we know that we got a "function undefined" error, we should be looking at this line:
in function b2bLib:support_tags_to_value/1
Even though it says that the error occurred "in" this function, this is the function that's undefined.
So either the test is run in such a way that it doesn't find the b2bLib module, or that module doesn't define a function called support_tags_to_value taking one argument. If it's the former, add -pa path/to/ebin to the Erlang command line in order to add the right directory to the code path.

Related

How to make a command line argument on Ros2 Humble at launch

I want to use the command line argument on ros2.
In my node :
const std::string name ="test"; const rclcpp::ParameterValue test(10); const rclcpp::ParameterValue testArg(rclcpp::Node::declare_parameter(name,test,ParamDesc,true)); cout << testArg.to_value_msg().integer_value << "\n";
Then when I throw in with the argument:
$ ros2 run dip_sensors TI6843AOP_node --ros-args -p test:=42 10
The node starts well but it indicates 10 when I passed it 42.
I can set the argument this way later but not at startup :
$ ros2 param set /NameNode test 42 Set parameter successful
Then
$ ros2 param get /NameNode test Integer value is:42
Do you have any idea of my mistakes?
Thanks,
Dimi
I have the solution and it's simple. I misread the function description and to activate the overriding you must not put the argument true but false:
testArg(rclcpp::Node::declare_parameter(name,test,ParamDesc,true)); --> bad testArg(rclcpp::Node::declare_parameter(name,test,ParamDesc,false)); --> good

OS.FileSys.mkDir that does not raise exception when the directory already exists

I am trying to write a function that creates a directory but does not raise an error when the directory already exists. This is the function:
fun ensureDir s =
(OS.FileSys.mkDir s)
handle OS.SysErr (_, SOME Posix.Error.exist) => ()
I based the pattern OS.SysErr (_, SOME Posix.Error.exist) on the fact that OS.FileSys.mkDir fails with the following error message when the directory already exists:
Poly/ML:
Exception- SysErr ("File exists", SOME EEXIST) raised
SML/NJ:
uncaught exception SysErr [SysErr: File exists [exist]]
raised at: <mkdir.c>
However, I get this error when I try to define the function in the Poly/ML shell:
poly: : error: qualified name Posix.Error.exist illegal here
Static Errors
This is the error in SML/NJ:
Error: variable found where constructor is required: Posix.Error.exist
What mistake did I make in the function definition?
(Poly/ML 5.7.1; SML/NJ 110.79; Ubuntu 20.04)
Posix.Error.exist is a val and not a constructor (like SOME) so
you cannot use it as a pattern in a pattern matching construct. Here
is an equivalent code for what you intended to do.
fun ensureDir s =
(OS.FileSys.mkDir s)
handle e as (OS.SysErr (_, SOME err)) =>
if err = Posix.Error.exist
then ()
else raise e
;

How to pass WMIC parameters for WmiSetBrightness

The following line in PowerShell works (brightness changes to 30):
(Get-WmiObject -Namespace root\wmi -Class WmiMonitorBrightnessMethods).WmiSetBrightness(0,30)
However, when using WMIC I get the following error:
C:\>wmic /NAMESPACE:\\root\wmi PATH WmiMonitorBrightnessMethods CALL WmiSetBrightness Brightness=30 Timeout=0
Executing (WmiMonitorBrightnessMethods)->WmiSetBrightness()
ERROR:
Description = Invalid method Parameter(s)
What is the issue and how can I get this to work?
Turns out it works if I add a WHERE clause:
C:\>wmic /NAMESPACE:\\root\wmi PATH WmiMonitorBrightnessMethods WHERE "Active=TRUE" CALL WmiSetBrightness Brightness=30 Timeout=0
Executing (\\MY-HOSTNAME\root\wmi:WmiMonitorBrightnessMethods.InstanceName="DISPLAY\\...\\...")->WmiSetBrightness()
Method execution successful.
Not very intuitive, to say the least, but oh well.

go test unexpectedly passes -test.timeout flag to program under test

I have a small cli program in go that implements one command line flag (-config=<config file>) using flag.String().
When I call go test it errors out because my program says flag provided but not defined: -test.timeout.
go test -v -x -args -config=./config.yml gives me:
/usr/lib/go/pkg/tool/linux_amd64/link -o $WORK/b001/... .../b001/myprogram.test -test.timeout=10m0s -test.v=true -config=./config.yml
In other words, it passes the test arguments on to myprogram, even though I expect this to be inhibited by the -args flag (should only pass arguments after the -args flag?).
Is this a bug, resp. is there a way to inhibit go test from passing any flags to the tested program other than the ones I specifically tell it to)?
EDIT:
The relevant code snippet:
package main
import (
"context"
"encoding/json"
"flag"
// ...
)
// ...
var (
configpath = flag.String("config", "config.yml", "Config file to use, if omitted defaults to config.yml")
// ...
func init() {
var err error
flag.Parse()
_, err = configGlobal.getConfig(*configpath)
if err != nil {
panic("No config loaded")
}
// ...
}
Normally running the program without arguments or with the argument -config ./config.yml works, obviously passing other flags doesn't.

Cakephp 3.0 Unit testing Issue

When i execute the plugins/SamplePlugin test cases, it executing perfect except the controller functions which are related to urls.
The test case function like
public function testIndex()
{
$this->get('/sample-plugin /mycontroller/index');
$this->assertResponseOk();
}
when i execute the above testcase the exception
There was 1 error:
1) SamplePlugin\Test\TestCase\Controller\MyControllerTest::test
Index
include(D:\xampp\htdocs\EATZ_V2_3.X\vendor\cakephp\cakephp\tests\test_app\config\routes.php): failed to open stream: No such file or directory
D:\xampp\htdocs\MyApp\vendor\cakephp\cakephp\src\Routing\Router.php:974
D:\xampp\htdocs\MyApp\vendor\cakephp\cakephp\src\Routing\Router.php:974
D:\xampp\htdocs\MyApp\vendor\cakephp\cakephp\src\Routing\Router.php:547
D:\xampp\htdocs\MyApp\vendor\cakephp\cakephp\src\TestSuite\IntegrationTestCase.php:451
D:\xampp\htdocs\MyApp\vendor\cakephp\cakephp\src\TestSuite\IntegrationTestCase.php:392
D:\xampp\htdocs\MyApp\vendor\cakephp\cakephp\src\TestSuite\IntegrationTestCase.php:312
D:\xampp\htdocs\MyApp\vendor\cakephp\cakephp\src\TestSuite\IntegrationTestCase.php:233
D:\xampp\htdocs\MyApp\plugins\SamplePlugin\tests\TestCase\Controller\MyControllerTest.php:29
D:\xampp\php\pear\PHPUnit\TextUI\Command.php:176
D:\xampp\php\pear\PHPUnit\TextUI\Command.php:129
FAILURES!
Tests: 30, Assertions: 42, Errors: 1.
please resove the issue.thanks in advance!!!
The error states that the routes.php file is missing in the folder config. Refer to the CakePHP 3 Documentation to create a meaningful routes.php.