def testUpdateAcceptsDict(self): """Test update() accepts a dict.""" kl1 = kernel_cmdline.KernelArgList('a=b') other = {'arg': 'value'} kl1.update(other) expected = kernel_cmdline.KernelArgList('a=b arg=value') self.assertEqual(expected, kl1)
def testUpdateAcceptsKernelArgList(self): """Test update() accepts a KernelArg.""" kl1 = kernel_cmdline.KernelArgList('a=b') kl2 = kernel_cmdline.KernelArgList('arg=value') kl1.update(kl2) expected = kernel_cmdline.KernelArgList('a=b arg=value') self.assertEqual(expected, kl1)
def testIadd(self): """Test that += yields the correct KernelArgList.""" kv1 = kernel_cmdline.KernelArgList('a b') kv2 = kernel_cmdline.KernelArgList('a d') kv1 += kv2 self.assertEqual(type(kv1), kernel_cmdline.KernelArgList) self.assertEqual(kv1, kernel_cmdline.KernelArgList('a b a d'))
def testAdd(self): """Adding two KernelArgLists yields the correct KernelArgList.""" kv1 = kernel_cmdline.KernelArgList('a b') kv2 = kernel_cmdline.KernelArgList('a d') res = kv1 + kv2 self.assertEqual(type(res), kernel_cmdline.KernelArgList) self.assertEqual(res, kernel_cmdline.KernelArgList('a b a d'))
def testGetslice(self): """Test that __getslice__ works.""" kl = kernel_cmdline.KernelArgList('a b c d') sl = kl[1:3] self.assertEqual(type(sl), kernel_cmdline.KernelArgList) self.assertEqual( kernel_cmdline.KernelArgList('a b c d')[1:3], kernel_cmdline.KernelArgList('b c'))
def testDelitem(self): """Test del.""" kl = kernel_cmdline.KernelArgList('a b=3 c d e') del kl[0] del kl['b'] with self.assertRaises(KeyError): del kl['z'] self.assertEqual(kl, kernel_cmdline.KernelArgList('c d e'))
def testNotEqual(self): """Test __ne__().""" kv1 = kernel_cmdline.KernelArgList('a b= c') kv2 = kernel_cmdline.KernelArgList('a b= c') kv3 = kernel_cmdline.KernelArgList('a b= c d') self.assertFalse(kv1 != kv2) self.assertFalse(kv1 != kv2._data) self.assertTrue(kv1 != kv3)
def testSetslice(self): """Test that __setslice__ works.""" kv = kernel_cmdline.KernelArgList('a x y d') # Test setting a slice to a KernelArgList. ins = kernel_cmdline.KernelArgList('b c') kv[1:3] = ins self.assertEqual(kernel_cmdline.KernelArgList('a b c d'), kv) # Test setting a slice to a list of KernelArg. kv[1:2] = [kernel_cmdline.KernelArg('a', '3')] self.assertEqual(kernel_cmdline.KernelArgList('a a=3 c d'), kv) # Test setting a slice to a string. kv[1:2] = 'x y=4' self.assertEqual(kernel_cmdline.KernelArgList('a x y=4 c d'), kv)
def testRejectsInvalidInput(self): """Test that invalid command line strings are rejected.""" # Non-KernelArg values. with self.assertRaises(ValueError): kernel_cmdline.KernelArgList({1: 2, 3: 4}) # The first and non-first arguments are different in the re. with self.assertRaises(ValueError): kernel_cmdline.KernelArgList('=3') with self.assertRaises(ValueError): kernel_cmdline.KernelArgList('a =3') # Various bad quote usages. with self.assertRaises(ValueError): kernel_cmdline.KernelArgList('a b="foo"3 c') with self.assertRaises(ValueError): kernel_cmdline.KernelArgList('a b=" c')
def testSetitemRejectsBadValues(self): """Test that __setitem__ rejects bad values.""" kv = kernel_cmdline.KernelArgList('a b=3 c d') with self.assertRaises(ValueError): # Strings are not KernelArgs kv['foo'] = 'bar' with self.assertRaises(ValueError): # Int is not KernelArg kv['foo'] = 1
def testParses(self): """Verify that DmLine correctly parses a line, and returns it.""" text = '0 1 verity a1 a2=v2 a3' dl = kernel_cmdline.DmLine(text) self.assertEqual(0, dl.start) self.assertEqual(1, dl.num) self.assertEqual('verity', dl.target_type) self.assertEqual(kernel_cmdline.KernelArgList('a1 a2=v2 a3'), dl.args) self.assertEqual(text, dl.Format())
def testContains(self): """Accepts KernelArg.""" kv1 = kernel_cmdline.KernelArg('a', None) kv2 = kernel_cmdline.KernelArg('arg', 'value') kv3 = kernel_cmdline.KernelArg('z', None) kl = kernel_cmdline.KernelArgList('a arg=value b c') self.assertTrue(kv1 in kl) self.assertTrue(kv2 in kl) self.assertFalse(kv3 in kl)
def testAllowsWhitespace(self): """Verify that leading/trailing whitespace is ignored.""" text = '0 1 verity a1 a2=v2 a3' dl = kernel_cmdline.DmLine(' %s ' % text) self.assertEqual(0, dl.start) self.assertEqual(1, dl.num) self.assertEqual('verity', dl.target_type) self.assertEqual(kernel_cmdline.KernelArgList('a1 a2=v2 a3'), dl.args) self.assertEqual(text, dl.Format())
def testInsert(self): """Test that insert() works.""" kv = kernel_cmdline.KernelArgList('a b=3 c d') kv.insert(1, kernel_cmdline.KernelArg('f', None)) kv.insert('c', kernel_cmdline.KernelArg('g', None)) with self.assertRaises(KeyError): kv.insert('z', kernel_cmdline.KernelArg('h', None)) expected = kernel_cmdline.KernelArgList([ kernel_cmdline.KernelArg('a', None), kernel_cmdline.KernelArg('f', None), kernel_cmdline.KernelArg('b', '3'), kernel_cmdline.KernelArg('g', None), kernel_cmdline.KernelArg('c', None), kernel_cmdline.KernelArg('d', None) ]) self.assertEqual(expected, kv) with self.assertRaises(ValueError): kv.insert('z', 'badval') # Verify that KernelArgList is a bad value. with self.assertRaises(ValueError): kv.insert('z', expected)
def testForcesInternalType(self): """Test that the internal type is correctly forced.""" expected = [ kernel_cmdline.KernelArg('c', 'd'), kernel_cmdline.KernelArg('a', 'b'), ] # Pass in an OrderedDict with |expected| being the keys. kl = kernel_cmdline.KernelArgList( collections.OrderedDict((x, 0) for x in expected)) self.assertEqual(type(kl._data), list) self.assertEqual(kl, expected) # Test len(). self.assertEqual(2, len(kl)) # Test __iter__(). self.assertEqual(expected, [x for x in kl])
def testSimple(self): """Test a simple command line string.""" expected = [ kernel_cmdline.KernelArg('a', None), kernel_cmdline.KernelArg('b', ''), kernel_cmdline.KernelArg('c', 'd'), kernel_cmdline.KernelArg('e.f', '"d e"'), kernel_cmdline.KernelArg('a', None), kernel_cmdline.KernelArg('--', None), kernel_cmdline.KernelArg('x', None), kernel_cmdline.KernelArg('y', ''), kernel_cmdline.KernelArg('z', 'zz'), kernel_cmdline.KernelArg('y', None) ] kl = kernel_cmdline.KernelArgList( 'a b= c=d e.f="d e" a -- x y= z=zz y') self.assertEqual(kl, expected) self.assertEqual(len(expected), len(kl)) self.assertEqual(expected, [x for x in kl])
def testSetitemAcceptsInt(self): """Test that __setitem__ works with an integer index.""" kv = kernel_cmdline.KernelArgList('a b=3 c d') new_val = kernel_cmdline.KernelArg('b', '4') kv[1] = new_val self.assertEqual(kv[1], new_val)
def testUpdateAppends(self): """Test update() appends new arg.""" kv = kernel_cmdline.KernelArgList('a b c') kv.update(kernel_cmdline.KernelArgList('d=99'), e='zz') expected = kernel_cmdline.KernelArgList('a b c d=99 e=zz') self.assertEqual(expected, kv)
def testContainsAcceptsString(self): """Accepts KernelArg.""" kl = kernel_cmdline.KernelArgList('a arg=value b c') self.assertTrue('a' in kl) self.assertTrue('arg' in kl) self.assertFalse('z' in kl)
def testDelslice(self): """Test del.""" kl = kernel_cmdline.KernelArgList('a b=3 c d e') del kl[1:3] self.assertEqual(kl, kernel_cmdline.KernelArgList('a d e'))
def testUpdateAcceptsSetKernelArgUsage(self): kl1 = kernel_cmdline.KernelArgList('a dm="foo baz" b') kl1.update({'dm': DM}) expected = kernel_cmdline.KernelArgList('a dm="%s" b' % DM) self.assertEqual(expected, kl1)
def testUpdateAcceptsKwargs(self): """Test update() with kwargs.""" kv1 = kernel_cmdline.KernelArgList('a b c') kv1.update(b='f') kv2 = kernel_cmdline.KernelArgList('a b=f c') self.assertEqual(kv2, kv1)
def testGetitemAcceptsInt(self): """Test that __getitem__ works with an integer index.""" self.assertEqual( kernel_cmdline.KernelArgList('a b=3 c d').__getitem__(1), kernel_cmdline.KernelArg('b', '3'))
def testIndex(self): """Test that index finds the correct thing.""" kv = kernel_cmdline.KernelArgList('a b=c d e') self.assertEqual(1, kv.index(1)) self.assertEqual(1, kv.index('b')) self.assertEqual(None, kv.index('z'))
def testget(self): """Test that Get returns the correct value for all key types.""" kv = kernel_cmdline.KernelArgList('a b=c d e') self.assertEqual(kernel_cmdline.KernelArg('d', None), kv.get(2)) self.assertEqual(kernel_cmdline.KernelArg('d', None), kv.get('d')) self.assertEqual('default', kv.get('z', default='default'))
def testFormat(self): """Test that Format works.""" self.assertEqual('a x= b=c d', kernel_cmdline.KernelArgList('a x= b=c d').Format())
def testGetitemAcceptsStr(self): """Test that __getitem__ works with a str.""" self.assertEqual( kernel_cmdline.KernelArgList('a b=3 c d').__getitem__('b'), kernel_cmdline.KernelArg('b', '3'))
def testGetItemRaisesKeyError(self): """Test that __getitem__ raises KeyError on invalid key.""" kv = kernel_cmdline.KernelArgList('a x y d') with self.assertRaises(KeyError): kv.__getitem__('z')
def testSetitemAppendsWithNewKeyStr(self): """Test that __setitem__ appends with a new key (str).""" kv = kernel_cmdline.KernelArgList('a b=3 c d') new_val = kernel_cmdline.KernelArg('y', '4') kv.__setitem__('y', new_val) self.assertEqual(kv[4], new_val)
def testSetitemAcceptsStr(self): """Test that __setitem__ works with a str.""" kv = kernel_cmdline.KernelArgList('a b=3 c d') new_val = kernel_cmdline.KernelArg('b', '4') kv['b'] = new_val self.assertEqual(kv[1], new_val)