Ejemplo n.º 1
0
    def testRemove(self):
        params = InputParameters()
        params.add('foo')
        self.assertTrue(params.hasParameter('foo'))
        params.remove('foo')
        self.assertFalse(params.hasParameter('foo'))

        with self.assertRaises(MooseException) as e:
            params.remove('bar')
        self.assertIn("The parameter 'bar' does not exist", str(e.exception))
Ejemplo n.º 2
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])
Ejemplo n.º 3
0
    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])
Ejemplo n.º 4
0
 def testHasParameter(self):
     params = InputParameters()
     params.add('foo')
     self.assertTrue(params.hasParameter('foo'))
     self.assertFalse(params.hasParameter('bar'))