Files
cadquery/tests/test_cqgi.py

222 lines
6.4 KiB
Python
Raw Normal View History

2015-12-07 16:35:10 -05:00
"""
Tests CQGI functionality
2015-12-08 21:35:01 -05:00
Currently, this includes:
Parsing a script, and detecting its available variables
Altering the values at runtime
defining a build_object function to return results
"""
2015-12-07 16:35:10 -05:00
from cadquery import cqgi
from tests import BaseTest
2015-12-08 21:35:01 -05:00
import textwrap
2015-12-07 16:35:10 -05:00
2015-12-08 21:35:01 -05:00
TESTSCRIPT = textwrap.dedent(
"""
height=2.0
width=3.0
(a,b) = (1.0,1.0)
foo="bar"
2015-12-07 16:35:10 -05:00
2015-12-08 21:35:01 -05:00
result = "%s|%s|%s|%s" % ( str(height) , str(width) , foo , str(a) )
2019-01-30 06:04:29 -05:00
show_object(result)
2015-12-08 21:35:01 -05:00
"""
)
2015-12-07 16:35:10 -05:00
2016-04-03 21:01:36 -04:00
TEST_DEBUG_SCRIPT = textwrap.dedent(
"""
height=2.0
width=3.0
(a,b) = (1.0,1.0)
foo="bar"
debug(foo, { "color": 'yellow' } )
result = "%s|%s|%s|%s" % ( str(height) , str(width) , foo , str(a) )
2019-01-30 06:04:29 -05:00
show_object(result)
2016-04-03 21:01:36 -04:00
debug(height )
"""
)
2015-12-07 16:35:10 -05:00
2015-12-08 21:35:01 -05:00
class TestCQGI(BaseTest):
2015-12-07 16:35:10 -05:00
def test_parser(self):
model = cqgi.CQModel(TESTSCRIPT)
metadata = model.metadata
2015-12-08 21:35:01 -05:00
self.assertEqual(
set(metadata.parameters.keys()), {"height", "width", "a", "b", "foo"}
)
2015-12-07 16:35:10 -05:00
2016-04-03 21:01:36 -04:00
def test_build_with_debug(self):
model = cqgi.CQModel(TEST_DEBUG_SCRIPT)
result = model.build()
debugItems = result.debugObjects
self.assertTrue(len(debugItems) == 2)
2019-02-04 16:26:30 -05:00
self.assertTrue(debugItems[0].shape == "bar")
self.assertTrue(debugItems[0].options == {"color": "yellow"})
2019-02-04 16:26:30 -05:00
self.assertTrue(debugItems[1].shape == 2.0)
self.assertTrue(debugItems[1].options == {})
2016-04-03 21:01:36 -04:00
2015-12-07 16:35:10 -05:00
def test_build_with_empty_params(self):
model = cqgi.CQModel(TESTSCRIPT)
2015-12-08 21:35:01 -05:00
result = model.build()
2015-12-07 16:35:10 -05:00
self.assertTrue(result.success)
self.assertTrue(len(result.results) == 1)
2019-02-04 16:26:30 -05:00
self.assertTrue(result.results[0].shape == "2.0|3.0|bar|1.0")
2015-12-07 16:35:10 -05:00
def test_build_with_different_params(self):
model = cqgi.CQModel(TESTSCRIPT)
result = model.build({"height": 3.0})
2019-02-04 16:26:30 -05:00
self.assertTrue(result.results[0].shape == "3.0|3.0|bar|1.0")
2015-12-08 21:35:01 -05:00
2016-04-05 21:04:09 -04:00
def test_describe_parameters(self):
script = textwrap.dedent(
"""
a = 2.0
describe_parameter(a,'FirstLetter')
"""
)
model = cqgi.CQModel(script)
a_param = model.metadata.parameters["a"]
2016-04-05 21:04:09 -04:00
self.assertTrue(a_param.default_value == 2.0)
self.assertTrue(a_param.desc == "FirstLetter")
self.assertTrue(a_param.varType == cqgi.NumberParameterType)
2016-04-05 21:04:09 -04:00
def test_describe_parameter_invalid_doesnt_fail_script(self):
script = textwrap.dedent(
"""
a = 2.0
describe_parameter(a, 2 - 1 )
"""
)
model = cqgi.CQModel(script)
a_param = model.metadata.parameters["a"]
self.assertTrue(a_param.name == "a")
2015-12-08 21:35:01 -05:00
def test_build_with_exception(self):
badscript = textwrap.dedent(
"""
raise ValueError("ERROR")
"""
)
model = cqgi.CQModel(badscript)
result = model.build({})
self.assertFalse(result.success)
self.assertIsNotNone(result.exception)
2018-02-03 23:22:46 +01:00
self.assertTrue(result.exception.args[0] == "ERROR")
2015-12-08 21:35:01 -05:00
def test_that_invalid_syntax_in_script_fails_immediately(self):
badscript = textwrap.dedent(
"""
this doesnt even compile
"""
)
with self.assertRaises(Exception) as context:
model = cqgi.CQModel(badscript)
self.assertTrue("invalid syntax" in context.exception.args)
2015-12-08 21:35:01 -05:00
def test_that_two_results_are_returned(self):
script = textwrap.dedent(
"""
h = 1
2019-01-30 06:04:29 -05:00
show_object(h)
2015-12-08 21:35:01 -05:00
h = 2
2019-01-30 06:04:29 -05:00
show_object(h)
2015-12-08 21:35:01 -05:00
"""
)
model = cqgi.CQModel(script)
result = model.build({})
self.assertEqual(2, len(result.results))
2019-02-04 16:26:30 -05:00
self.assertEqual(1, result.results[0].shape)
self.assertEqual(2, result.results[1].shape)
2015-12-08 21:35:01 -05:00
def test_that_assinging_number_to_string_works(self):
script = textwrap.dedent(
"""
h = "this is a string"
2019-01-30 06:04:29 -05:00
show_object(h)
2015-12-08 21:35:01 -05:00
"""
)
result = cqgi.parse(script).build({"h": 33.33})
2019-02-04 16:26:30 -05:00
self.assertEqual(result.results[0].shape, "33.33")
2015-12-08 21:35:01 -05:00
def test_that_assigning_string_to_number_fails(self):
script = textwrap.dedent(
"""
h = 20.0
2019-01-30 06:04:29 -05:00
show_object(h)
2015-12-08 21:35:01 -05:00
"""
)
result = cqgi.parse(script).build({"h": "a string"})
self.assertTrue(isinstance(result.exception, cqgi.InvalidParameterError))
2015-12-08 21:35:01 -05:00
def test_that_assigning_unknown_var_fails(self):
script = textwrap.dedent(
2015-12-08 22:36:19 -05:00
"""
2015-12-08 21:35:01 -05:00
h = 20.0
2019-01-30 06:04:29 -05:00
show_object(h)
2015-12-08 21:35:01 -05:00
"""
)
result = cqgi.parse(script).build({"w": "var is not there"})
self.assertTrue(isinstance(result.exception, cqgi.InvalidParameterError))
2015-12-08 22:36:19 -05:00
def test_that_cq_objects_are_visible(self):
script = textwrap.dedent(
"""
r = cadquery.Workplane('XY').box(1,2,3)
2019-01-30 06:04:29 -05:00
show_object(r)
2015-12-08 22:36:19 -05:00
"""
)
2015-12-09 21:01:14 -05:00
result = cqgi.parse(script).build()
2015-12-08 22:36:19 -05:00
self.assertTrue(result.success)
self.assertIsNotNone(result.first_result)
2015-12-09 21:01:14 -05:00
def test_that_options_can_be_passed(self):
script = textwrap.dedent(
"""
r = cadquery.Workplane('XY').box(1,2,3)
show_object(r, options={"rgba":(128, 255, 128, 0.0)})
"""
)
result = cqgi.parse(script).build()
self.assertTrue(result.success)
self.assertIsNotNone(result.first_result.options)
2015-12-09 21:01:14 -05:00
def test_setting_boolean_variable(self):
script = textwrap.dedent(
"""
h = True
2019-01-30 06:04:29 -05:00
show_object( "*%s*" % str(h) )
2015-12-09 21:01:14 -05:00
"""
)
result = cqgi.parse(script).build({"h": False})
2015-12-09 21:01:14 -05:00
self.assertTrue(result.success)
self.assertEqual(result.first_result.shape, "*False*")
2015-12-09 21:01:14 -05:00
def test_that_only_top_level_vars_are_detected(self):
script = textwrap.dedent(
"""
h = 1.0
w = 2.0
def do_stuff():
x = 1
y = 2
2019-01-30 06:04:29 -05:00
show_object( "result" )
2015-12-09 21:01:14 -05:00
"""
)
model = cqgi.parse(script)
self.assertEqual(2, len(model.metadata.parameters))