Repository navigation
Expand file tree
/
Copy pathFallbackFunction.cpp
More file actions
83 lines (72 loc) · 2.02 KB
/
Copy pathFallbackFunction.cpp
File metadata and controls
83 lines (72 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "catch.hpp"
#include <system.h>
#include <story.h>
#include <runner.h>
#include <globals.h>
#include <compiler.h>
#include <cmath>
using namespace ink::runtime;
SCENARIO(
"run a story with external function and fallback function", "[external-functions][runtime]"
)
{
GIVEN("a story with two external functions, one with fallback")
{
std::unique_ptr<story> ink{story::from_file(INK_TEST_RESOURCE_DIR "FallBack.bin")};
runner thread = ink->new_runner();
WHEN("both external functions are bound")
{
int cnt_sqrt = 0;
auto fn_sqrt = [&cnt_sqrt](double x) -> double {
++cnt_sqrt;
return static_cast<int>(sqrt(x));
};
int cnt_greeting = 0;
auto fn_greeting = [&cnt_greeting]() -> const char* {
++cnt_greeting;
return "Hohooh";
};
thread->bind("sqrt", fn_sqrt);
thread->bind("greeting", fn_greeting);
std::string out;
REQUIRE_NOTHROW(out = thread->getall());
THEN("the bound greeting is used and both functions are called the correct number of times")
{
REQUIRE(
out
== "Hohooh ! A small demonstration of my power:\nMath 4 * 4 = 16, stunning i would "
"say\n"
);
REQUIRE(cnt_sqrt == 2);
REQUIRE(cnt_greeting == 1);
}
}
WHEN("only the function without a fallback is bound")
{
int cnt_sqrt = 0;
auto fn_sqrt = [&cnt_sqrt](double x) -> double {
++cnt_sqrt;
return static_cast<int>(sqrt(x));
};
thread->bind("sqrt", fn_sqrt);
std::string out;
REQUIRE_NOTHROW(out = thread->getall());
THEN("the fallback greeting is used and sqrt is called the correct number of times")
{
REQUIRE(
out
== "Hello ! A small demonstration of my power:\nMath 4 * 4 = 16, stunning i would say\n"
);
REQUIRE(cnt_sqrt == 2);
}
}
WHEN("no functions are bound")
{
THEN("running the story throws an exception for the missing non-fallback function")
{
std::string out;
REQUIRE_THROWS_AS(out = thread->getall(), ink::ink_exception);
}
}
}
}