generate_flag_labels_test.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env vpython3
  2. #
  3. # Copyright 2020 The Chromium Authors. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. import unittest
  7. import generate_flag_labels as gen_labels
  8. class _GenerateFlagLabelsTest(unittest.TestCase):
  9. """Unittests for the generate_flag_labels module.
  10. """
  11. def testGetSwitchId(self):
  12. # Arbitrarily, this test verifies the WebViewExtraHeadersSameOriginOnly
  13. # feature since we know from field metrics this is logged correctly.
  14. self.assertEqual(
  15. -1988840552,
  16. gen_labels.GetSwitchId('WebViewExtraHeadersSameOriginOnly:disabled'))
  17. def testFormatName_baseFeature(self):
  18. self.assertEqual('SomeFeature',
  19. gen_labels.FormatName('FooFeatures.SOME_FEATURE', True))
  20. self.assertEqual(
  21. 'SomeWebViewFeature',
  22. gen_labels.FormatName('FooFeatures.SOME_WEBVIEW_FEATURE', True))
  23. def testFormatName_commandLine(self):
  24. self.assertEqual('some-switch',
  25. gen_labels.FormatName('FooSwitches.SOME_SWITCH', False))
  26. self.assertEqual(
  27. 'some-webview-switch',
  28. gen_labels.FormatName('FooSwitches.SOME_WEBVIEW_SWITCH', False))
  29. def testExtractFlagsFromJavaLines(self):
  30. test_data = """
  31. // Same line
  32. Flag.commandLine(FooSwitches.SOME_SWITCH,
  33. "Some description"),
  34. // Different line
  35. Flag.commandLine(
  36. FooSwitches.SOME_OTHER_SWITCH,
  37. "Some other description"),
  38. // Same line
  39. Flag.baseFeature(FooFeatures.SOME_FEATURE,
  40. "Some description"),
  41. // Different line
  42. Flag.baseFeature(
  43. FooFeatures.SOME_OTHER_FEATURE,
  44. "Some other description"),
  45. """.split('\n')
  46. flags = gen_labels.ExtractFlagsFromJavaLines(test_data)
  47. self.assertEqual(4, len(flags))
  48. self.assertEqual('some-switch', flags[0].name)
  49. self.assertFalse(flags[0].is_base_feature)
  50. self.assertEqual('some-other-switch', flags[1].name)
  51. self.assertFalse(flags[1].is_base_feature)
  52. self.assertEqual('SomeFeature', flags[2].name)
  53. self.assertTrue(flags[2].is_base_feature)
  54. self.assertEqual('SomeOtherFeature', flags[3].name)
  55. self.assertTrue(flags[3].is_base_feature)
  56. if __name__ == '__main__':
  57. unittest.main()