generic-poky/meta/lib/oe/test_types.py
Chris Larson e4921fda5b Implement variable typing (sync from OE)
This implementation consists of two components:

- Type creation python modules, whose job it is to construct objects of the
  defined type for a given variable in the metadata
- typecheck.bbclass, which iterates over all configuration variables with a
  type defined and uses oe.types to check the validity of the values

This gives us a few benefits:

- Automatic sanity checking of all configuration variables with a defined type
- Avoid duplicating the "how do I make use of the value of this variable"
  logic between its users.  For variables like PATH, this is simply a split(),
  for boolean variables, the duplication can result in confusing, or even
  mismatched semantics (is this 0/1, empty/nonempty, what?)
- Make it easier to create a configuration UI, as the type information could
  be used to provide a better interface than a text edit box (e.g checkbox for
  'boolean', dropdown for 'choice')

This functionality is entirely opt-in right now.  To enable the configuration
variable type checking, simply INHERIT += "typecheck".  Example of a failing
type check:

BAZ = "foo"
BAZ[type] = "boolean"

$ bitbake -p
FATAL: BAZ: Invalid boolean value 'foo'
$

Examples of leveraging oe.types in a python snippet:

PACKAGES[type] = "list"

python () {
    import oe.data
    for pkg in oe.data.typed_value("PACKAGES", d):
        bb.note("package: %s" % pkg)
}

LIBTOOL_HAS_SYSROOT = "yes"
LIBTOOL_HAS_SYSROOT[type] = "boolean"

python () {
    import oe.data
    assert(oe.data.typed_value("LIBTOOL_HAS_SYSROOT", d) == True)
}

(From OE-Core rev: a04ce490e933fc7534db33f635b025c25329c564)

Signed-off-by: Chris Larson <chris_larson@mentor.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
2011-05-20 17:34:22 +01:00

63 lines
2.4 KiB
Python

import unittest
from oe.maketype import create, factory
class TestTypes(unittest.TestCase):
def assertIsInstance(self, obj, cls):
return self.assertTrue(isinstance(obj, cls))
def assertIsNot(self, obj, other):
return self.assertFalse(obj is other)
def assertFactoryCreated(self, value, type, **flags):
cls = factory(type)
self.assertIsNot(cls, None)
self.assertIsInstance(create(value, type, **flags), cls)
class TestBooleanType(TestTypes):
def test_invalid(self):
self.assertRaises(ValueError, create, '', 'boolean')
self.assertRaises(ValueError, create, 'foo', 'boolean')
self.assertRaises(TypeError, create, object(), 'boolean')
def test_true(self):
self.assertTrue(create('y', 'boolean'))
self.assertTrue(create('yes', 'boolean'))
self.assertTrue(create('1', 'boolean'))
self.assertTrue(create('t', 'boolean'))
self.assertTrue(create('true', 'boolean'))
self.assertTrue(create('TRUE', 'boolean'))
self.assertTrue(create('truE', 'boolean'))
def test_false(self):
self.assertFalse(create('n', 'boolean'))
self.assertFalse(create('no', 'boolean'))
self.assertFalse(create('0', 'boolean'))
self.assertFalse(create('f', 'boolean'))
self.assertFalse(create('false', 'boolean'))
self.assertFalse(create('FALSE', 'boolean'))
self.assertFalse(create('faLse', 'boolean'))
def test_bool_equality(self):
self.assertEqual(create('n', 'boolean'), False)
self.assertNotEqual(create('n', 'boolean'), True)
self.assertEqual(create('y', 'boolean'), True)
self.assertNotEqual(create('y', 'boolean'), False)
class TestList(TestTypes):
def assertListEqual(self, value, valid, sep=None):
obj = create(value, 'list', separator=sep)
self.assertEqual(obj, valid)
if sep is not None:
self.assertEqual(obj.separator, sep)
self.assertEqual(str(obj), obj.separator.join(obj))
def test_list_nosep(self):
testlist = ['alpha', 'beta', 'theta']
self.assertListEqual('alpha beta theta', testlist)
self.assertListEqual('alpha beta\ttheta', testlist)
self.assertListEqual('alpha', ['alpha'])
def test_list_usersep(self):
self.assertListEqual('foo:bar', ['foo', 'bar'], ':')
self.assertListEqual('foo:bar:baz', ['foo', 'bar', 'baz'], ':')