-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test mutiple calls to exec / handle resolve.Error (#27)
In #26 another change snuck in to retain the globals across multiple calls to exec, so that it could be used to define multiple things. While writing a test for this behavor, I discovered that it is possible for starlark-go's Eval to return a resolve.ErrorList, in addition to a starlark.EvalError or a syntax.Error, so I had to add handling for that as well.
- Loading branch information
Showing
7 changed files
with
174 additions
and
13 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
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
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,28 @@ | ||
import pytest | ||
|
||
from pystarlark import ResolveError, Starlark | ||
|
||
ADD_ONE = """ | ||
def add_one(x): | ||
return x + 1 | ||
""" | ||
|
||
ADD_TWO = """ | ||
def add_two(x): | ||
return add_one(add_one(x)) | ||
""" | ||
|
||
|
||
def test_multi_exec(): | ||
s = Starlark() | ||
|
||
s.exec(ADD_ONE) | ||
|
||
assert s.eval("add_one(1)") == 2 | ||
|
||
with pytest.raises(ResolveError): | ||
s.eval("add_two(1)") | ||
|
||
s.exec(ADD_TWO) | ||
|
||
assert s.eval("add_two(1)") == 3 |
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,61 @@ | ||
from pystarlark import ResolveError, Starlark | ||
|
||
|
||
def test_eval_resolveerror(): | ||
s = Starlark() | ||
raised = False | ||
|
||
try: | ||
s.eval("add_one(1)") | ||
except ResolveError as e: | ||
assert isinstance(e.errors, list) | ||
assert len(e.errors) == 1 | ||
assert e.errors[0].line == 1 | ||
assert e.errors[0].column == 1 | ||
assert e.errors[0].msg == "undefined: add_one" | ||
raised = True | ||
|
||
try: | ||
s.eval("from_bad(True) + to_worse(True)") | ||
except ResolveError as e: | ||
assert isinstance(e.errors, list) | ||
assert len(e.errors) == 2 | ||
assert e.errors[0].line == 1 | ||
assert e.errors[0].column == 1 | ||
assert e.errors[0].msg == "undefined: from_bad" | ||
assert e.errors[1].line == 1 | ||
assert e.errors[1].column == 18 | ||
assert e.errors[1].msg == "undefined: to_worse" | ||
raised = True | ||
|
||
assert raised | ||
|
||
|
||
def test_exec_resolveerror(): | ||
s = Starlark() | ||
raised = False | ||
|
||
try: | ||
s.exec("add_one(1)") | ||
except ResolveError as e: | ||
assert isinstance(e.errors, list) | ||
assert len(e.errors) == 1 | ||
assert e.errors[0].line == 1 | ||
assert e.errors[0].column == 1 | ||
assert e.errors[0].msg == "undefined: add_one" | ||
raised = True | ||
|
||
try: | ||
s.exec("from_bad(True) + to_worse(True)") | ||
except ResolveError as e: | ||
assert isinstance(e.errors, list) | ||
assert len(e.errors) == 2 | ||
assert e.errors[0].line == 1 | ||
assert e.errors[0].column == 1 | ||
assert e.errors[0].msg == "undefined: from_bad" | ||
assert e.errors[1].line == 1 | ||
assert e.errors[1].column == 18 | ||
assert e.errors[1].msg == "undefined: to_worse" | ||
raised = True | ||
|
||
assert raised |