mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-10 17:58:20 +00:00
Reimplement sort / reverse / join function to format language, simplify text annotation variables, add tests
Previously, sort, reverse list, and join functionality was implemented as special cases in Bonsai itself. Given that it has usecases (especially in material lists, but any sort of list applies) I've moved this function into the IOS formatting language. The IOS formatting language previously wasn't capable of this, but the awesome addition by @falken10vdl made the formatting language accept queries inline, so that means it can handle lists. I also added tests for all the new functions and expression syntax (+-*/ operators). I simplified the code that gets the evaluated text literal - previously it seems to call format() multiple times.
This commit is contained in:
@@ -35,7 +35,7 @@ import ifcopenshell.util.selector as subject
|
||||
import test.bootstrap
|
||||
|
||||
|
||||
class TestFormat:
|
||||
class TestFormat(test.bootstrap.IFC4):
|
||||
def test_no_formatting(self):
|
||||
assert subject.format("123") == "123"
|
||||
assert subject.format('"123"') == "123"
|
||||
@@ -78,6 +78,38 @@ class TestFormat:
|
||||
assert subject.format('imperial_length(3.0, 4, "foot", "foot", false)') == "3' - 0\""
|
||||
assert subject.format('imperial_length(3.0, 4, "foot", "foot", False)') == "3' - 0\""
|
||||
|
||||
def test_variable_formatting(self):
|
||||
assert subject.format('{{undefined}}') is None
|
||||
assert subject.format('upper({{undefined}})') == "NONE"
|
||||
assert subject.format('int({{undefined}})') == "0"
|
||||
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
|
||||
assert subject.format('{{undefined}}', element) is None
|
||||
assert subject.format('{{class}}', element) == "IfcWall"
|
||||
assert subject.format('{{ class }}', element) == "IfcWall"
|
||||
assert subject.format('upper({{ class }})', element) == "IFCWALL"
|
||||
|
||||
def test_list_formatting(self):
|
||||
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
|
||||
material = ifcopenshell.api.material.add_material(self.file, name="CON01")
|
||||
material2 = ifcopenshell.api.material.add_material(self.file, name="CON03")
|
||||
material3 = ifcopenshell.api.material.add_material(self.file, name="CON02")
|
||||
material_set = ifcopenshell.api.material.add_material_set(self.file, set_type="IfcMaterialLayerSet")
|
||||
layer = ifcopenshell.api.material.add_layer(self.file, layer_set=material_set, material=material)
|
||||
layer = ifcopenshell.api.material.add_layer(self.file, layer_set=material_set, material=material2)
|
||||
layer = ifcopenshell.api.material.add_layer(self.file, layer_set=material_set, material=material3)
|
||||
ifcopenshell.api.material.assign_material(self.file, products=[element], material=material_set)
|
||||
assert subject.format('{{materials.Name}}', element) == "CON01, CON03, CON02"
|
||||
assert subject.format('sort({{materials.Name}})', element) == "CON01, CON02, CON03"
|
||||
assert subject.format('reverse({{materials.Name}})', element) == "CON02, CON03, CON01"
|
||||
assert subject.format('join("-", {{materials.Name}})', element) == "CON01-CON03-CON02"
|
||||
|
||||
def test_expressions(self):
|
||||
assert subject.format('2+3') == "5"
|
||||
assert subject.format('-2+3') == "1"
|
||||
assert subject.format('2-3') == "-1"
|
||||
assert subject.format('3*2') == "6"
|
||||
assert subject.format('3/2') == "1.5"
|
||||
|
||||
|
||||
class TestGetElementValue(test.bootstrap.IFC4):
|
||||
def test_selecting_an_elements_class_or_id_using_a_query(self):
|
||||
|
||||
Reference in New Issue
Block a user