示例#1
0
    def test_function_values(self):
        """
        Make sure we can add and update values with functions instead of patterns
        """

        self.param_dict.add_parameter(
            FunctionParameter("fn_foo",
                              self.pick_byte2,
                              lambda x: str(x),
                              direct_access=True,
                              startup_param=True,
                              value=1,
                              visibility=ParameterDictVisibility.READ_WRITE))
        self.param_dict.add_parameter(
            FunctionParameter(
                "fn_bar",
                lambda x: bool(x & 2),  # bit map example
                lambda x: str(x),
                direct_access=True,
                startup_param=True,
                value=False,
                visibility=ParameterDictVisibility.READ_WRITE))

        # check defaults just to be safe
        val = self.param_dict.get("fn_foo")
        self.assertEqual(val, 1)
        val = self.param_dict.get("fn_bar")
        self.assertEqual(val, False)

        self.param_dict.update(1005)  # just change first in list
        val = self.param_dict.get("fn_foo")
        self.assertEqual(val, 3)
        val = self.param_dict.get("fn_bar")
        self.assertEqual(val, False)

        # fn_bar does not get updated here
        result = self.param_dict.update_many(1205)
        self.assertEqual(result['fn_foo'], True)
        self.assertEqual(len(result), 1)
        val = self.param_dict.get("fn_foo")
        self.assertEqual(val, 4)
        val = self.param_dict.get("fn_bar")
        self.assertEqual(val, False)

        # both are updated now
        result = self.param_dict.update_many(6)
        self.assertEqual(result['fn_foo'], True)
        self.assertEqual(result['fn_bar'], True)
        self.assertEqual(len(result), 2)

        val = self.param_dict.get("fn_foo")
        self.assertEqual(val, 0)
        val = self.param_dict.get("fn_bar")
        self.assertEqual(val, True)
示例#2
0
    def test_mixed_pdv_types(self):
        """ Verify we can add different types of PDVs in one container """
        self.param_dict.add_parameter(
            FunctionParameter("fn_foo",
                              self.pick_byte2,
                              lambda x: str(x),
                              direct_access=True,
                              startup_param=True,
                              value=1,
                              visibility=ParameterDictVisibility.READ_WRITE))
        self.param_dict.add_parameter(
            RegexParameter("foo",
                           r'.*foo=(\d+).*',
                           lambda match: int(match.group(1)),
                           lambda x: str(x),
                           direct_access=True,
                           startup_param=True,
                           value=10,
                           visibility=ParameterDictVisibility.READ_WRITE))
        self.param_dict.add("bar",
                            r'.*bar=(\d+).*',
                            lambda match: int(match.group(1)),
                            lambda x: str(x),
                            direct_access=False,
                            startup_param=True,
                            value=15,
                            visibility=ParameterDictVisibility.READ_WRITE)

        self.assertEqual(self.param_dict.get("fn_foo"), 1)
        self.assertEqual(self.param_dict.get("foo"), 10)
        self.assertEqual(self.param_dict.get("bar"), 15)
示例#3
0
 def test_function_val(self):
     pdv = FunctionParameter("foo",
                             self.pick_byte2,
                             lambda x: str(x),
                             value=12)
     self.assertEqual(pdv.get_value(), 12)
     self.assertRaises(TypeError, pdv.update(1))
     result = pdv.update("1205")
     self.assertEqual(pdv.get_value(), 4)
     self.assertEqual(result, True)
示例#4
0
 def test_set(self):
     """
     Test a simple set of the parameter. Make sure the right values get
     called and the correct exceptions are raised.
     """
     new_param = FunctionParameter(
         "foo",
         self.pick_byte2,
         lambda x: str(x),
         direct_access=True,
         startup_param=True,
         value=1000,
         visibility=ParameterDictVisibility.READ_WRITE)
     self.assertEquals(new_param.get_value(), 1000)
     self.assertEquals(self.param_dict.get("foo"), None)
     # overwrites existing param
     self.param_dict.add_parameter(new_param)
     self.assertEquals(self.param_dict.get("foo"), 1000)
     self.param_dict.set_value("foo", 2000)
     self.assertEquals(self.param_dict.get("foo"), 2000)