forked from relaypro-open/YarnRunner-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request relaypro-open#6 from josefalanga/register-custom-f…
…unctions Register custom functions
- Loading branch information
Showing
12 changed files
with
164 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
id,node,lineNumber,tags |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
id,text,file,node,lineNumber | ||
line:C:\Documents\development\YarnRunner-Python\examples\yarn1\functions.yarn-Start-0,One plus one is {0},C:\Documents\development\YarnRunner-Python\examples\yarn1\functions.yarn,Start,3 | ||
line:C:\Documents\development\YarnRunner-Python\examples\yarn1\functions.yarn-Start-1,You rolled a six!,C:\Documents\development\YarnRunner-Python\examples\yarn1\functions.yarn,Start,7 | ||
line:C:\Documents\development\YarnRunner-Python\examples\yarn1\functions.yarn-Start-2,Gambler: My lucky number is {0}!,C:\Documents\development\YarnRunner-Python\examples\yarn1\functions.yarn,Start,11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
title: Start | ||
--- | ||
One plus one is {add_numbers(1, 1)} | ||
|
||
// Inside an if statement: | ||
<<if dice(6) == 6>> | ||
You rolled a six! | ||
<<endif>> | ||
|
||
// Inside a line: | ||
Gambler: My lucky number is {random_range(1,10)}! | ||
|
||
=== |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
id,node,lineNumber,tags |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
id,text,file,node,lineNumber | ||
line:C:\Documents\development\YarnRunner-Python\examples\yarn1\functions.yarn-Start-0,One plus one is {0},C:\Documents\development\YarnRunner-Python\examples\yarn1\functions.yarn,Start,3 | ||
line:C:\Documents\development\YarnRunner-Python\examples\yarn1\functions.yarn-Start-1,You rolled a six!,C:\Documents\development\YarnRunner-Python\examples\yarn1\functions.yarn,Start,7 | ||
line:C:\Documents\development\YarnRunner-Python\examples\yarn1\functions.yarn-Start-2,Gambler: My lucky number is {0}!,C:\Documents\development\YarnRunner-Python\examples\yarn1\functions.yarn,Start,11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
title: Start | ||
--- | ||
One plus one is {add_numbers(1, 1)} | ||
|
||
// Inside an if statement: | ||
<<if dice(6) == 6>> | ||
You rolled a six! | ||
<<endif>> | ||
|
||
// Inside a line: | ||
Gambler: My lucky number is {random_range(1,10)}! | ||
|
||
=== |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import os | ||
import random | ||
from .context import YarnRunner | ||
|
||
compiled_yarn_f1 = open(os.path.join(os.path.dirname( | ||
__file__), '../examples/yarn1/functions.yarnc'), 'rb') | ||
names_csv_f1 = open(os.path.join(os.path.dirname( | ||
__file__), '../examples/yarn1/functions.csv'), 'r') | ||
compiled_yarn_f2 = open(os.path.join(os.path.dirname( | ||
__file__), '../examples/yarn2/functions.yarnc'), 'rb') | ||
names_csv_f2 = open(os.path.join(os.path.dirname( | ||
__file__), '../examples/yarn2/functions.csv'), 'r') | ||
|
||
# autostart=False so the runner doesn't start before the functions are registered | ||
runner1 = YarnRunner(compiled_yarn_f1, names_csv_f1, autostart=False) | ||
runner2 = YarnRunner(compiled_yarn_f2, names_csv_f2, autostart=False) | ||
|
||
|
||
def add_numbers(first: int, second: int) -> int: | ||
return int(first + second) | ||
|
||
|
||
def dice(faces: int) -> int: | ||
return int(6) | ||
|
||
|
||
def random_range(start: int, stop: int) -> int: | ||
return int(6) | ||
|
||
|
||
def test_run_functions_1(): | ||
runner1.add_function_handler("add_numbers", add_numbers) | ||
runner1.add_function_handler("dice", dice) | ||
runner1.add_function_handler("random_range", random_range) | ||
|
||
runner1.resume() | ||
|
||
lines = runner1.get_lines() | ||
assert lines[0] == "One plus one is 2" | ||
assert lines[1] == "You rolled a six!" | ||
assert lines[2] == "Gambler: My lucky number is 6!" | ||
|
||
|
||
def test_run_functions_2(): | ||
runner2.add_function_handler("add_numbers", add_numbers) | ||
runner2.add_function_handler("dice", dice) | ||
runner2.add_function_handler("random_range", random_range) | ||
|
||
runner2.resume() | ||
|
||
lines = runner2.get_lines() | ||
assert lines[0] == "One plus one is 2" | ||
assert lines[1] == "You rolled a six!" | ||
assert lines[2] == "Gambler: My lucky number is 6!" | ||
|
||
|
||
def test_function_invocation_without_handler(): | ||
function_name = "add_numbers" | ||
|
||
runner3 = YarnRunner(compiled_yarn_f2, names_csv_f2, autostart=False) | ||
try: | ||
runner3.resume() | ||
|
||
# the runner should throw an error | ||
raise Exception( | ||
"The runner ran without any issues. This test should fail. An Exception was expected.") | ||
except Exception as e: | ||
assert str( | ||
e) == f"The function `{function_name}` is not implemented, and is not registered as a custom function." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters