def testSetWithSubOption(self): andrew = InputParameters() andrew.add('year') people = InputParameters() people.add('andrew', default=andrew) people.setValue('andrew', 'year', 1949) self.assertEqual(andrew.getValue('year'), 1949) people.setValue('andrew', {'year': 1954}) self.assertEqual(andrew.getValue('year'), 1954) people.setValue('andrew_year', 1977) self.assertEqual(andrew.getValue('year'), 1977) yo_dawg = InputParameters() yo_dawg.add('nunchuck') andrew.add('skills', yo_dawg) self.assertEqual(yo_dawg.getValue('nunchuck'), None) people.setValue('andrew_skills', 'nunchuck', True) self.assertEqual(yo_dawg.getValue('nunchuck'), True) with self.assertRaises(MooseException) as e: people.setValue('andrew_day', 'python', False) self.assertIn("The parameter 'day' does not exist", str(e.exception)) with self.assertRaises(MooseException) as e: people.setValue('andrew_skills', 'python', False) self.assertIn("The parameter 'python' does not exist.", str(e.exception))
def testGet(self): params = InputParameters() params.add('foo', default=1980) self.assertEqual(params.getValue('foo'), 1980) with self.assertRaises(MooseException) as e: params.getValue('bar') self.assertIn("The parameter 'bar' does not exist", str(e.exception))
def testGetWithSubOption(self): unit = InputParameters() unit.add('name', 'pts') font = InputParameters() font.add('size', default=24) font.add('unit', unit) text = InputParameters() text.add('font', font) self.assertEqual(text.getValue('font', 'size'), 24) self.assertEqual(text.getValue('font', 'unit', 'name'), 'pts') self.assertEqual(text.getValue('font_size'), 24) self.assertEqual(text.getValue('font_unit_name'), 'pts')
def testUpdate(self): params = InputParameters() params.add('foo') params.update(foo=1980) self.assertEqual(params.getValue('foo'), 1980) params2 = InputParameters() params2.add('foo', 2013) params.update(params2) self.assertEqual(params.getValue('foo'), 2013) with self.assertRaises(MooseException) as e: params.update(foo=2011, bar=2013) self.assertIn("The parameter 'bar' does not exist.", str(e.exception)) with self.assertRaises(MooseException) as e: params.update('wrong') self.assertIn("The supplied arguments must be InputParameters object", str(e.exception))
def testSetDefault(self): params = InputParameters() params.add('foo', vtype=int) self.assertIsNone(params.getValue('foo')) params.setDefault('foo', 1980) self.assertEqual(params.getValue('foo'), 1980) params.add('bar', default=1980) params.setDefault('bar', 1949) self.assertEqual(params.getValue('bar'), 1980) self.assertEqual(params.getDefault('bar'), 1949) with self.assertRaises(MooseException) as e: params.setDefault('other', 1980) self.assertIn("The parameter 'other' does not exist", str(e.exception)) params.setValue('_error_mode', InputParameters.ErrorMode.ERROR) # use error to capture return with self.assertLogs(level='ERROR') as log: params.setDefault('foo', 'wrong') self.assertEqual(len(log.output), 1) self.assertIn("'foo' must be of type", log.output[0])
def testDeprecated(self): date = InputParameters() date.addParam('year', 'doco') self.assertIn('year', date) self.assertEqual('doco', date.parameter('year').doc) date.addParam('day', 1, 'doco') self.assertIn('day', date) self.assertEqual(date.getValue('day'), 1) self.assertEqual('doco', date.parameter('year').doc) date.addRequiredParam('month', 1, "doco") self.assertEqual(date.isRequired('day'), False) self.assertEqual(date.isRequired('month'), True) self.assertEqual(date['month'], 1) date['month'] = 2 self.assertEqual(date['month'], 2)
def testAdd(self): params = InputParameters() params.add('foo') self.assertEqual(list(params.keys(private=True)), ['_moose_object', '_error_mode', 'foo']) self.assertFalse(params.isValid('foo')) self.assertIn('foo', params) self.assertIsNone(params.getValue('foo')) self.assertTrue(params.hasParameter('foo')) params.setValue('_error_mode', InputParameters.ErrorMode.ERROR) # use error to capture return with self.assertLogs(level='ERROR') as log: params.add('foo') self.assertEqual(len(log.output), 1) self.assertIn("Cannot add parameter, the parameter 'foo' already exists.", log.output[0]) sub = InputParameters() params.add('bar', InputParameters()) with self.assertLogs(level='ERROR') as log: params.add('bar_something') self.assertEqual(len(log.output), 1) self.assertIn( "Cannot add a parameter with the name 'bar_something', a sub parameter exists with the name 'bar'.", log.output[0])
def testSet(self): params = InputParameters() params.add('foo', vtype=int) params.setValue('foo', 42) self.assertTrue(params.isValid('foo')) self.assertIn('foo', params) self.assertIsNotNone(params.getValue('foo')) self.assertEqual(params.getValue('foo'), 42) self.assertTrue(params.hasParameter('foo')) with self.assertRaises(MooseException) as e: params.setValue('bar', 1980) self.assertIn("The parameter 'bar' does not exist", str(e.exception)) # Sub-options params2 = InputParameters() params2.add('bar') params.add('sub', params2) params.setValue('sub', {'bar': 2013}) self.assertEqual(params2.getValue('bar'), 2013) self.assertEqual(params.getValue('sub').getValue('bar'), 2013) params2.setValue('bar', 1954) self.assertEqual(params2.getValue('bar'), 1954) self.assertEqual(params.getValue('sub').getValue('bar'), 1954) params3 = InputParameters() params3.add('bar', default=2011) params.setValue('sub', params3) self.assertEqual(params2.getValue('bar'), 1954) self.assertEqual(params3.getValue('bar'), 2011) self.assertEqual(params.getValue('sub').getValue('bar'), 2011) params.setValue('sub', 'bar', 1944) self.assertEqual(params2.getValue('bar'), 1954) self.assertEqual(params3.getValue('bar'), 1944) self.assertEqual(params.getValue('sub').getValue('bar'), 1944) with self.assertRaises(MooseException) as e: params.setValue('foo', 1980, 2011) self.assertIn("Extra argument(s) found: 1980", str(e.exception)) params.setValue('_error_mode', InputParameters.ErrorMode.ERROR) # log to capture return with self.assertLogs(level='ERROR') as log: params.setValue('foo', 1980, 2011) params.setValue('_error_mode', InputParameters.ErrorMode.ERROR) # log to capture return with self.assertLogs(level='ERROR') as log: params.setValue('foo') self.assertEqual(len(log.output), 1) self.assertIn("One or more names must be supplied.", log.output[0]) params.setValue('_error_mode', InputParameters.ErrorMode.ERROR) with self.assertLogs(level='ERROR') as log: params.setValue('foo', 'wrong') self.assertEqual(len(log.output), 1) self.assertIn("'foo' must be of type", log.output[0])