Skip to content

Commit b49b591

Browse files
committed
Update the python test to include importing, and fix the import callback in the test
1 parent d897244 commit b49b591

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

python/_jsonnet_test.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def try_path(dir, rel):
3232
if not os.path.isfile(full_path):
3333
return full_path, None
3434
with open(full_path) as f:
35-
return full_path, f.read()
35+
return full_path, f.read().encode()
3636

3737

3838
def import_callback(dir, rel):
@@ -68,11 +68,10 @@ def return_types():
6868

6969
class JsonnetTests(unittest.TestCase):
7070
def setUp(self):
71-
self.input_filename = os.path.join(
72-
os.path.dirname(__file__),
73-
"test.jsonnet",
74-
)
75-
self.expected_str = "true\n"
71+
base_dir = os.path.join(os.path.dirname(__file__), "testdata")
72+
self.input_filename = os.path.join(base_dir, "basic_check.jsonnet")
73+
self.trivial_filename = os.path.join(base_dir, "trivial.jsonnet")
74+
self.test_filename = os.path.join(base_dir, "test.jsonnet")
7675
with open(self.input_filename, "r") as infile:
7776
self.input_snippet = infile.read()
7877

@@ -82,16 +81,37 @@ def test_evaluate_file(self):
8281
import_callback=import_callback,
8382
native_callbacks=native_callbacks,
8483
)
85-
self.assertEqual(json_str, self.expected_str)
84+
self.assertEqual(json_str, "true\n")
8685

8786
def test_evaluate_snippet(self):
8887
json_str = _jsonnet.evaluate_snippet(
89-
"snippet",
88+
self.test_filename,
9089
self.input_snippet,
9190
import_callback=import_callback,
9291
native_callbacks=native_callbacks,
9392
)
94-
self.assertEqual(json_str, self.expected_str)
93+
self.assertEqual(json_str, "true\n")
94+
95+
def test_import(self):
96+
json_str = _jsonnet.evaluate_snippet(
97+
self.test_filename,
98+
"import 'trivial.jsonnet'",
99+
import_callback=import_callback,
100+
native_callbacks=native_callbacks,
101+
)
102+
self.assertEqual(json_str, "42\n")
103+
104+
def test_double_import(self):
105+
json_str = _jsonnet.evaluate_snippet(
106+
self.test_filename,
107+
"local x = import 'trivial.jsonnet';\n" +
108+
"local y = import 'trivial.jsonnet';\n" +
109+
"x + y",
110+
import_callback=import_callback,
111+
native_callbacks=native_callbacks,
112+
)
113+
self.assertEqual(json_str, "84\n")
114+
95115

96116
if __name__ == '__main__':
97117
unittest.main()
File renamed without changes.

0 commit comments

Comments
 (0)