Пример #1
0
def test_nuclide_uitype():
    m = MockCodeGenMachine()
    m.context = {
        "MyFactory":
        OrderedDict([('vars',
                      OrderedDict([
                          ('x', {
                              'type': 'int',
                              'uitype': 'nuclide'
                          }),
                      ]))])
    }

    # test schema is set to string
    f = SchemaFilter(m)
    f.given_classname = 'MyFactory'
    impl = f.impl()
    exp_impl = ('  return ""\n'
                '    "<interleave>\\n"\n'
                '    "<element name=\\"x\\">\\n"\n'
                '    "    <data type=\\"string\\" />\\n"\n'
                '    "</element>\\n"\n'
                '    "</interleave>\\n"\n    ;\n')
    yield assert_equal, exp_impl, impl

    # test infiletodb updates
    f = InfileToDbFilter(m)
    f.given_classname = 'MyFactory'
    impl = f.impl()
    exp_impl = (
        '  tree = tree->SubTree("config/*");\n'
        '  cyclus::InfileTree* sub;\n'
        '  int i;\n'
        '  int n;\n'
        '  x = pyne::nucname::id(cyclus::Query<std::string>(tree, "x"));\n'
        '  di.NewDatum("Info")\n'
        '  ->AddVal("x", x)\n'
        '  ->Record();\n')
    yield assert_equal, exp_impl, impl

    # test bad uitypes values fail
    m.context = {
        "MyFactory":
        OrderedDict([('vars',
                      OrderedDict([
                          ('x', {
                              'type': 'int',
                              'uitype': 'WRONG'
                          }),
                      ]))])
    }
    f = SchemaFilter(m)
    f.given_classname = 'MyFactory'
    yield assert_raises, TypeError, f.impl
Пример #2
0
def test_nuclide_uitype():
    m = MockCodeGenMachine()
    m.context = {"MyFactory": OrderedDict([('vars', OrderedDict([
            ('x', {'type': 'int', 'uitype': 'nuclide'}),
            ]))
            ])}

    # test schema is set to string
    f = SchemaFilter(m)
    f.given_classname = 'MyFactory'
    impl = f.impl()
    exp_impl = ('  return ""\n'
                '    "<interleave>\\n"\n'
                '    "    <element name=\\"x\\">\\n"\n'
                '    "        <data type=\\"string\\"/>\\n"\n'
                '    "    </element>\\n"\n'
                '    "</interleave>\\n";\n')
    yield assert_equal, exp_impl, impl

    # test infiletodb updates
    f = InfileToDbFilter(m)
    f.given_classname = 'MyFactory'
    impl = f.impl()
    exp_impl = '  cyclus::InfileTree* sub = tree->SubTree("config/*");\n  int i;\n  int n;\n  {\n    int x_val = pyne::nucname::id(cyclus::Query<std::string>(sub, "x"));\n    x = x_val;\n  }\n  di.NewDatum("Info")\n  ->AddVal("x", x)\n  ->Record();\n'

    yield assert_equal, exp_impl, impl

    # test bad uitypes values fail
    m.context = {"MyFactory": OrderedDict([('vars', OrderedDict([
            ('x', {'type': 'int', 'uitype': 'WRONG'}),
            ]))
            ])}
    f = SchemaFilter(m)
    f.given_classname = 'MyFactory'
    yield assert_raises, TypeError, f.impl
Пример #3
0
def test_schemafilter_buildschema():
    m = MockCodeGenMachine()
    m.context = {
        "MyFactory":
        OrderedDict([('vars',
                      OrderedDict([
                          ('x', {
                              'type': 'int',
                              'uitype': 'nuclide'
                          }),
                      ]))])
    }
    f = SchemaFilter(m)

    schematype = None
    uitype = None
    names = None

    cpptype = ['std::map', 'std::string', ['std::vector', 'double']]
    names = ['streams']
    exp = ('<element name="streams"><oneOrMore><element name="item">'
           '<interleave><element name="key"><data type="string" /></element>'
           '<element name="val"><oneOrMore><element name="val">'
           '<data type="double" /></element></oneOrMore></element>'
           '</interleave></element></oneOrMore></element>')
    obs = f._buildschema(cpptype, schematype, uitype, names)
    yield assert_equal, exp, obs

    cpptype = ['std::map', 'std::string', ['std::vector', 'double']]
    names = ['streams', 'name', ['efficiencies', 'val']]
    exp = ('<element name="streams"><oneOrMore><element name="item">'
           '<interleave><element name="name"><data type="string" /></element>'
           '<element name="efficiencies"><oneOrMore><element name="val">'
           '<data type="double" /></element></oneOrMore></element>'
           '</interleave></element></oneOrMore></element>')
    obs = f._buildschema(cpptype, schematype, uitype, names)
    yield assert_equal, exp, obs

    # test item aliasing
    cpptype = ['std::map', 'std::string', ['std::vector', 'double']]
    names = [['streams', 'entry'], 'name', ['efficiencies', 'val']]
    exp = ('<element name="streams"><oneOrMore><element name="entry">'
           '<interleave><element name="name"><data type="string" /></element>'
           '<element name="efficiencies"><oneOrMore><element name="val">'
           '<data type="double" /></element></oneOrMore></element>'
           '</interleave></element></oneOrMore></element>')
    obs = f._buildschema(cpptype, schematype, uitype, names)
    yield assert_equal, exp, obs
Пример #4
0
def test_schemafilter():
    """Test SchemaFilter"""
    m = MockCodeGenMachine()
    f = SchemaFilter(m)
    f.given_classname = 'MyFactory'

    args = f.methodargs()
    exp_args = ""
    yield assert_equal, exp_args, args

    impl = f.impl()
    exp_impl = ('  return ""\n'
                '    "<interleave>\\n"\n'
                '    "    <element name=\\"x\\">\\n"\n'
                '    "        <data type=\\"positiveInteger\\"/>\\n"\n'
                '    "    </element>\\n"\n'
                '    "    FREAK OUT\\n"\n'
                '    "</interleave>\\n";\n')
    yield assert_equal, exp_impl, impl

    # schema type tests
    yield assert_equal, 'string', f._type('std::string')
    yield assert_equal, 'boolean', f._type('bool')
    yield assert_equal, 'token', f._type('std::string', 'token')

    m.context = {
        "MyFactory":
        OrderedDict([('vars',
                      OrderedDict([
                          ('x', {
                              'type': ('std::map', 'int', 'double')
                          }),
                      ]))])
    }
    impl = f.impl()
    exp_impl = ('  return ""\n'
                '    "<interleave>\\n"\n'
                '    "    <element name=\\"x\\">\\n"\n'
                '    "        <oneOrMore>\\n"\n'
                '    "            <element name=\\"item\\">\\n"\n'
                '    "                <interleave>\\n"\n'
                '    "                    <element name=\\"key\\">\\n"\n'
                '    "                        <data type=\\"int\\"/>\\n"\n'
                '    "                    </element>\\n"\n'
                '    "                    <element name=\\"val\\">\\n"\n'
                '    "                        <data type=\\"double\\"/>\\n"\n'
                '    "                    </element>\\n"\n'
                '    "                </interleave>\\n"\n'
                '    "            </element>\\n"\n'
                '    "        </oneOrMore>\\n"\n'
                '    "    </element>\\n"\n'
                '    "</interleave>\\n";\n')
    yield assert_equal, exp_impl, impl
Пример #5
0
def test_internal_schema():
    cases = [
        {'params': {'internal': True, 'default': 7}, 'want':
            '  return ""\n    "<interleave/>\\n";\n'
            },
        {'params': {'derived_init': 'x *= 42;'}, 'want':
            ('  return ""\n'
             '    "<interleave>\\n"\n'
             '    "    <element name=\\"x\\">\\n"\n'
             '    "        <data type=\\"int\\"/>\\n"\n'
             '    "    </element>\\n"\n'
             '    "</interleave>\\n";\n')
            },
        {'params': {'derived_init': 'x *= 42;', 'default': 7}, 'want':
            ('  return ""\n'
             '    "<interleave>\\n"\n'
             '    "    <optional>\\n"\n'
             '    "        <element name=\\"x\\">\\n"\n'
             '    "            <data type=\\"int\\"/>\\n"\n'
             '    "        </element>\\n"\n'
             '    "    </optional>\\n"\n'
             '    "</interleave>\\n";\n')
            },
        {'params': {'internal': True, 'derived_init': 'x *= 42;', 'default': 7}, 'want':
            '  return ""\n    "<interleave/>\\n";\n'
            },
    ]

    for i, case in enumerate(cases):
        keys = case['params'].copy()
        m = MockCodeGenMachine()
        params = {'type': 'int'}
        params.update(keys)
        m.context = {"MyFactory": OrderedDict([('vars', OrderedDict([
                ('x', params),
            ]))])}
        f = SchemaFilter(m)
        f.given_classname = 'MyFactory'

        impl = f.impl()

        want = case['want']
        msg = 'case {0} failed\n    ---- got ----\n    {1}\n    ---- want ----\n    {2}'.format(i + 1, impl.replace('\n', '\n    '), want.replace('\n', '\n    '))
        if want != impl:
            pprint.pprint(impl)
            assert_true(False, msg)
Пример #6
0
def test_schemafilter():
    """Test SchemaFilter"""
    m = MockCodeGenMachine()
    f = SchemaFilter(m)
    f.given_classname = 'MyFactory'

    args = f.methodargs()
    exp_args = ""
    yield assert_equal, exp_args, args

    impl = f.impl()
    exp_impl = ('  return ""\n'
                '    "<interleave>\\n"\n'
                '    "<element name=\\"x\\">\\n"\n'
                '    "    <data type=\\"int\\" />\\n"\n'
                '    "</element>\\n"\n'
                "FREAK OUT\n"
                '    "</interleave>\\n"\n    ;\n')
    yield assert_equal, exp_impl, impl
Пример #7
0
def test_schemafilter_buildschema():
    m = MockCodeGenMachine()
    m.context = {"MyFactory": OrderedDict([('vars', OrderedDict([
            ('x', {'type': 'int', 'uitype': 'nuclide'}),
            ]))
            ])}
    f = SchemaFilter(m)

    schematype = None
    uitype = None
    names = None

    cpptype = ['std::map', 'std::string', ['std::vector', 'double']]
    names = ['streams']
    exp = ('<element name="streams"><oneOrMore><element name="item">'
           '<interleave><element name="key"><data type="string" /></element>'
           '<element name="val"><oneOrMore><element name="val">'
           '<data type="double" /></element></oneOrMore></element>'
           '</interleave></element></oneOrMore></element>')
    obs = f._buildschema(cpptype, schematype, uitype, names)
    yield assert_equal, exp, obs

    cpptype = ['std::map', 'std::string', ['std::vector', 'double']]
    names = ['streams', 'name', ['efficiencies', 'val']]
    exp = ('<element name="streams"><oneOrMore><element name="item">'
           '<interleave><element name="name"><data type="string" /></element>'
           '<element name="efficiencies"><oneOrMore><element name="val">'
           '<data type="double" /></element></oneOrMore></element>'
           '</interleave></element></oneOrMore></element>')
    obs = f._buildschema(cpptype, schematype, uitype, names)
    yield assert_equal, exp, obs

    # test item aliasing
    cpptype = ['std::map', 'std::string', ['std::vector', 'double']]
    names = [['streams', 'entry'], 'name', ['efficiencies', 'val']]
    exp = ('<element name="streams"><oneOrMore><element name="entry">'
           '<interleave><element name="name"><data type="string" /></element>'
           '<element name="efficiencies"><oneOrMore><element name="val">'
           '<data type="double" /></element></oneOrMore></element>'
           '</interleave></element></oneOrMore></element>')
    obs = f._buildschema(cpptype, schematype, uitype, names)
    yield assert_equal, exp, obs
Пример #8
0
def test_schemafilter():
    """Test SchemaFilter"""
    m = MockCodeGenMachine()
    f = SchemaFilter(m)
    f.given_classname = 'MyFactory'

    args = f.methodargs()
    exp_args = ""
    yield assert_equal, exp_args, args

    impl = f.impl()
    exp_impl = ('  return ""\n'
                '    "<interleave>\\n"\n'
                '    "    <element name=\\"x\\">\\n"\n'
                '    "        <data type=\\"positiveInteger\\"/>\\n"\n'
                '    "    </element>\\n"\n'
                '    "    FREAK OUT\\n"\n'
                '    "</interleave>\\n";\n')
    yield assert_equal, exp_impl, impl

    # schema type tests
    yield assert_equal, 'string', f._type('std::string')
    yield assert_equal, 'boolean', f._type('bool')
    yield assert_equal, 'token', f._type('std::string', 'token')

    m.context = {"MyFactory": OrderedDict([('vars', OrderedDict([
            ('x', {'type': ('std::map', 'int', 'double')}),
            ]))
            ])}
    impl = f.impl()
    exp_impl = (
        '  return ""\n'
        '    "<interleave>\\n"\n'
        '    "    <element name=\\"x\\">\\n"\n'
        '    "        <oneOrMore>\\n"\n'
        '    "            <element name=\\"item\\">\\n"\n'
        '    "                <interleave>\\n"\n'
        '    "                    <element name=\\"key\\">\\n"\n'
        '    "                        <data type=\\"int\\"/>\\n"\n'
        '    "                    </element>\\n"\n'
        '    "                    <element name=\\"val\\">\\n"\n'
        '    "                        <data type=\\"double\\"/>\\n"\n'
        '    "                    </element>\\n"\n'
        '    "                </interleave>\\n"\n'
        '    "            </element>\\n"\n'
        '    "        </oneOrMore>\\n"\n'
        '    "    </element>\\n"\n'
        '    "</interleave>\\n";\n')
    yield assert_equal, exp_impl, impl
Пример #9
0
def test_internal_schema():
    cases = [
        {
            'params': {
                'internal': True,
                'default': 7
            },
            'want': '  return ""\n    "<interleave/>\\n";\n'
        },
        {
            'params': {
                'derived_init': 'x *= 42;'
            },
            'want': ('  return ""\n'
                     '    "<interleave>\\n"\n'
                     '    "    <element name=\\"x\\">\\n"\n'
                     '    "        <data type=\\"int\\"/>\\n"\n'
                     '    "    </element>\\n"\n'
                     '    "</interleave>\\n";\n')
        },
        {
            'params': {
                'derived_init': 'x *= 42;',
                'default': 7
            },
            'want': ('  return ""\n'
                     '    "<interleave>\\n"\n'
                     '    "    <optional>\\n"\n'
                     '    "        <element name=\\"x\\">\\n"\n'
                     '    "            <data type=\\"int\\"/>\\n"\n'
                     '    "        </element>\\n"\n'
                     '    "    </optional>\\n"\n'
                     '    "</interleave>\\n";\n')
        },
        {
            'params': {
                'internal': True,
                'derived_init': 'x *= 42;',
                'default': 7
            },
            'want': '  return ""\n    "<interleave/>\\n";\n'
        },
    ]

    for i, case in enumerate(cases):
        keys = case['params'].copy()
        m = MockCodeGenMachine()
        params = {'type': 'int'}
        params.update(keys)
        m.context = {
            "MyFactory": OrderedDict([('vars', OrderedDict([
                ('x', params),
            ]))])
        }
        f = SchemaFilter(m)
        f.given_classname = 'MyFactory'

        impl = f.impl()

        want = case['want']
        msg = 'case {0} failed\n    ---- got ----\n    {1}\n    ---- want ----\n    {2}'.format(
            i + 1, impl.replace('\n', '\n    '), want.replace('\n', '\n    '))
        if want != impl:
            pprint.pprint(impl)
            assert_true(False, msg)