Commit 8dc90b3c authored by Charlie Hu's avatar Charlie Hu Committed by Commit Bot

Fix wrong constructor bug in document_policy_features.cc

In previous CL, a bug on type name where 'bool' should have be 'Bool'
causes bool value calling PolicyValue constructor for double, with
extra param.

This CL fixes the bug and add tests on parse_default_value function.

Change-Id: Ica555f337336fe83ad061f3455769b5a0b3ffe67
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2069856
Commit-Queue: Charlie Hu <chenleihu@google.com>
Reviewed-by: default avatarIan Clelland <iclelland@chromium.org>
Cr-Commit-Position: refs/heads/master@{#744041}
parent 2b6031e5
...@@ -43,6 +43,9 @@ def _RunTests(input_api, output_api): ...@@ -43,6 +43,9 @@ def _RunTests(input_api, output_api):
}, { }, {
'file_name': 'make_runtime_features_utilities_unittest.py', 'file_name': 'make_runtime_features_utilities_unittest.py',
'affected_list': [r'.*make_runtime_features_utilities.*'] 'affected_list': [r'.*make_runtime_features_utilities.*']
}, {
'file_name': 'make_document_policy_features_unittest.py',
'affected_list': [r'.*make_document_policy_features.*']
}] }]
test_commands = [] test_commands = []
for test in tests: for test in tests:
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
import json5_generator import json5_generator
import template_expander import template_expander
from make_document_policy_features_util import parse_default_value
class DocumentPolicyFeatureWriter(json5_generator.Writer): class DocumentPolicyFeatureWriter(json5_generator.Writer):
...@@ -12,24 +13,6 @@ class DocumentPolicyFeatureWriter(json5_generator.Writer): ...@@ -12,24 +13,6 @@ class DocumentPolicyFeatureWriter(json5_generator.Writer):
def __init__(self, json5_file_path, output_dir): def __init__(self, json5_file_path, output_dir):
super(DocumentPolicyFeatureWriter, self).__init__(json5_file_path, output_dir) super(DocumentPolicyFeatureWriter, self).__init__(json5_file_path, output_dir)
def parse_default_value(default_value, value_type):
""" Parses default_value string to actual usable C++ expression.
@param default_value_str: default_value field specified in document_policy_features.json5
@param value_type: value_type field specified in document_policy_features.json5
"""
policy_value_type = "mojom::PolicyValueType::k{}".format(value_type)
if default_value == 'max':
return "PolicyValue::CreateMaxPolicyValue({})".format(policy_value_type)
if default_value == 'min':
return "PolicyValue::CreateMinPolicyValue({})".format(policy_value_type)
if value_type in {'bool'}: # types that have only one corresponding PolicyValueType
return "PolicyValue({})".format(default_value)
else:
return "PolicyValue({}, {})".format(default_value, policy_value_type)
@template_expander.use_jinja('templates/' + self.file_basename + '.cc.tmpl') @template_expander.use_jinja('templates/' + self.file_basename + '.cc.tmpl')
def generate_implementation(): def generate_implementation():
return { return {
......
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import unittest
from make_document_policy_features_util import parse_default_value
class MakeDocumentPolicyFeaturesTest(unittest.TestCase):
def test_parse_default_value(self):
self.assertEqual(
parse_default_value("max", "DecDouble"),
"PolicyValue::CreateMaxPolicyValue(mojom::PolicyValueType::kDecDouble)")
self.assertEqual(
parse_default_value("min", "DecDouble"),
"PolicyValue::CreateMinPolicyValue(mojom::PolicyValueType::kDecDouble)")
self.assertEqual(parse_default_value("false", "Bool"), "PolicyValue(false)")
self.assertEqual(
parse_default_value("0.5", "DecDouble"),
"PolicyValue(0.5, mojom::PolicyValueType::kDecDouble)")
with self.assertRaises(ValueError):
parse_default_value("max", "NotImplemented")
if __name__ == "__main__":
unittest.main()
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
def parse_default_value(default_value, value_type, recognized_types=('Bool', 'DecDouble'),
single_ctor_param_types=('Bool')):
""" Parses default_value string to actual usable C++ expression.
@param default_value_str: default_value field specified in document_policy_features.json5
@param value_type: value_type field specified in document_policy_features.json5
@param recognized_types: types that are valid for value_type
@param single_ctor_param_types: types with single param PolicyValue constructor
- Bool corresponds to PolicyValue(bool) which only has one constructor param
- DecDouble and IncDouble both take an extra constructor param for value_type
in constructor PolicyValue(double, mojom::PolicyValueType)
@returns: a C++ expression that has type mojom::PolicyValue
"""
if (value_type not in recognized_types):
raise ValueError("{} is not recognized as valid value_type({})".format(
value_type, recognized_types))
policy_value_type = "mojom::PolicyValueType::k{}".format(value_type)
if default_value == 'max':
return "PolicyValue::CreateMaxPolicyValue({})".format(policy_value_type)
if default_value == 'min':
return "PolicyValue::CreateMinPolicyValue({})".format(policy_value_type)
# types that have only one corresponding PolicyValueType
if value_type in single_ctor_param_types:
return "PolicyValue({})".format(default_value)
else:
return "PolicyValue({}, {})".format(default_value, policy_value_type)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment