예제 #1
0
 def test_select_relative(self):
     m = Model.fromDict(test_model)
     components = m.select("/components")
     compA = components.select("componentA")
     self.assertEqual(compA, test_model["components"]["componentA"])
     leaf = compA.select("properties/dead")
     self.assertEqual(leaf, False)
예제 #2
0
    def test_gen_select(self):
        @gen()
        def gen_color(component):
            return f"""*
            color: {component.select("properties/color")}
            *"""

        @gen()
        def gen_component(component):
            parent = None
            if component.has("properties/parent"):
                parent = component.select("properties/parent->").name
                parent = f"parent: {parent}"

            return f"""*
            # Component {component.select("properties/name")}:
              {parent}
              {gen_color(component)}
              nicknames:
                {["- " + x for x in component.select("properties/nicknames")]}

            *"""

        @gen()
        def gen_all(model):
            components = [
                gen_component(component)
                for component in model.select("/components/*")
            ]

            print(components)

            return f"""*
            {components}
            *"""

        m = Model.fromDict(test_model, refindicator="$")
        self.assertEqual(
            gen_all(m), """# Component componentA:

  color: blue
  nicknames:
    - cA
    - compA
    - A

# Component componentB:
  parent: componentA
  color: red
  nicknames:
    - cB
    - compB
    - B
""")
예제 #3
0
    def test_select_absolute(self):
        m = Model.fromDict(test_model)
        # Sub-dict selection
        self.assertDictEqual(m.select("/components"), test_model["components"])
        self.assertEqual(m.select("/components").type, dict)

        # Two-level sub-dict selection
        self.assertEqual(
            m.select("/components/componentB"),
            (Model.fromDict(test_model["components"]["componentB"],
                            name="componentB",
                            root=test_model)))

        # Leaf selection
        self.assertEqual(m.select("/components/componentB/properties/color"),
                         "red")
        self.assertEqual(
            m.select("/components/componentB/properties/color").type, str)
        self.assertEqual(m.select("/components/componentA/properties/nothing"),
                         None)
        self.assertEqual(
            m.select("/components/componentA/properties/nothing").type,
            type(None))
        self.assertEqual(m.select("/week"), test_model["week"])
        self.assertEqual(m.select("/week").type, list)
        self.assertRaisesRegex(SelectableError,
                               "Could not find path '/doesnotexist'.*",
                               m.select, "/doesnotexist")
        self.assertRaisesRegex(
            SelectableError, "Could not find path '/components/componentX'.*",
            m.select, "/components/componentX")
        self.assertRaisesRegex(
            SelectableError,
            "Could not find path '/components/componentB/properties/height'.*",
            m.select, "/components/componentB/properties/height")
        self.assertRaisesRegex(
            SelectableError,
            "Cannot iterate over '/components/componentB/properties/color'",
            m.select, "/components/componentB/properties/color/*")
예제 #4
0
 def test_select_replace(self):
     comp = "componentA"  # noqa
     prop = "color"  # noqa
     m = Model.fromDict(test_model)
     self.assertEqual(m.select("/components/<comp>/properties/<prop>"),
                      "blue")
     # We cannot use assertRaisesRegex because of the scope trickery in
     # select with paths containing <...>
     try:
         m.select("/components/<comp>/properties/<fail>")
     except SelectableError as e:
         self.assertEqual(str(e), "Could not find 'fail' in local scope")
     else:
         raise Exception("test_select_replace_failed")
예제 #5
0
 def test_select_ref(self):
     m = Model.fromDict(test_model, refindicator="$")
     self.assertEqual(m.select("/components/componentB/properties/parent"),
                      "$/components/componentA")
     self.assertEqual(
         m.select("/components/componentB/properties/parent->"),
         test_model["components"]["componentA"])
     ref = m.select("/components/componentB/properties/parent")  # noqa
     self.assertEqual(m.select("<ref>->"),
                      test_model["components"]["componentA"])
     # Relative path seletion
     rref = m.select("/components/componentB/favoriteprop->")
     self.assertEqual(rref, "red")
     # Broken path selection
     self.assertRaisesRegex(
         SelectableError, "Could not find path '/components/componentX'.*",
         m.select, "/components/componentA/properties/brokenref->")
예제 #6
0
 def test_select_star(self):
     m = Model.fromDict(test_model)
     self.assertEqual(m.select("/components/*"),
                      (test_model["components"]["componentA"],
                       test_model["components"]["componentB"]))
     self.assertEqual(m.select("/components/*").name, "*")
     self.assertEqual(m.select("/components/*").type, tuple)
     # Selectables have extra attributes, check those
     self.assertEqual([el.name for el in m.select("/components/*")],
                      ["componentA", "componentB"])
     self.assertEqual([el.refindicator for el in m.select("/components/*")],
                      [m.refindicator, m.refindicator])
     self.assertEqual([el.root for el in m.select("/components/*")], [m, m])
     self.assertEqual(
         m.select("/components/*").select("/components/componentA"),
         test_model["components"]["componentA"])
     self.assertEqual(
         m.select("/components/*").select("1"),
         test_model["components"]["componentB"])
예제 #7
0
    def test_select_array(self):
        m = Model.fromDict(test_model)
        self.assertEqual(
            m.select("/components/componentA/properties/nicknames"),
            test_model["components"]["componentA"]["properties"]["nicknames"])
        self.assertEqual(
            m.select("/components/componentA/properties/nicknames/0"), "cA")
        self.assertEqual(
            m.select("/components/componentA/properties/nicknames/1"), "compA")
        self.assertEqual(
            m.select("/components/componentA/properties/nicknames/2"), "A")
        self.assertRaisesRegex(
            SelectableError, "Could not find path " +
            "'/components/componentA/properties/nicknames/3'.*", m.select,
            "/components/componentA/properties/nicknames/3")

        self.assertEqual(m.select("/week/0"), "mon")
        self.assertEqual(m.select("/week/2"), "wed")
        self.assertEqual(m.select("/week/-1"), "fri")
        self.assertRaisesRegex(SelectableError,
                               "Array navigation requires integers", m.select,
                               "/week/a")
예제 #8
0
파일: example.py 프로젝트: alnvdl/fstringen
from fstringen import gen, Model

model = Model.fromDict({
    "structs": {
        "mystruct": {
            "color": "string"
        },
        "anotherstruct": {
            "name": "string",
            "age": "int"
        }
    }
})


@gen()
def gen_struct(struct):
    fields = [
        "{} {}".format(field.name, field) for field in struct.select("*")
    ]

    return f"""*
    type {struct.name} struct {{
        {fields}
    }}

    *"""


@gen(model=model, fname="myfile.go", comment="//")
def gen_myfile(model):