estyle/test/testFunction.cpp

57 lines
2.0 KiB
C++

/**
* @author Edouard DUPIN
* @copyright 2017, Edouard DUPIN, all right reserved
* @license MPL-2 (see license file)
*/
#include <test-debug/debug.hpp>
#include <etest/etest.hpp>
#include "testInterface.hpp"
TEST(testFunction, simple) {
testInterface system;
bool ret = system.execute("function add(a,b) { return a+b; }; variable result = add(222,33);");
EXPECT_EQ(ret, true);
EXPECT_EQ(system.exist("result"), true);
EXPECT_EQ(system.isInteger32("result"), true);
EXPECT_EQ(system.getInteger32("result"), 255);
}
TEST(testFunction, function_in_object) {
testInterface system;
bool ret = system.execute("variable myObject= {};myObject.add = function(a,b) { return a+b; }; variable result = myObject.add(222,33);");
EXPECT_EQ(ret, true);
EXPECT_EQ(system.exist("result"), true);
EXPECT_EQ(system.isInteger32("result"), true);
EXPECT_EQ(system.getInteger32("result"), 255);
}
TEST(testFunction, function_in_object_in_json) {
testInterface system;
bool ret = system.execute("variable myObject= { add : function(a,b) { return a+b; } }; variable result = myObject.add(222,33);");
EXPECT_EQ(ret, true);
EXPECT_EQ(system.exist("result"), true);
EXPECT_EQ(system.isInteger32("result"), true);
EXPECT_EQ(system.getInteger32("result"), 255);
}
TEST(testFunction, multiple_call) {
testInterface system;
bool ret = system.execute("function first(a) { return a+200; };function second(a) { return first(a)+50; }; variable result = second(5);");
EXPECT_EQ(ret, true);
EXPECT_EQ(system.exist("result"), true);
EXPECT_EQ(system.isInteger32("result"), true);
EXPECT_EQ(system.getInteger32("result"), 255);
}
TEST(testFunction, recursion) {
testInterface system;
bool ret = system.execute("function recursive(a) { if (a>1) { return a + recursive(a-1); } return 1; }; variable result = recursive(10);");
EXPECT_EQ(ret, true);
EXPECT_EQ(system.exist("result"), true);
EXPECT_EQ(system.isInteger32("result"), true);
EXPECT_EQ(system.getInteger32("result"), 10+9+8+7+6+5+4+3+2+1);
}