File: generate_allowlist_from_histograms_file_unittest.py

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (103 lines) | stat: -rwxr-xr-x 2,959 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python
# Copyright 2023 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import unittest
import xml.dom.minidom

import generate_allowlist_from_histograms_file

_EXPECTED_FILE_CONTENT = (
    """// Generated from generate_allowlist_from_histograms_file.py. \
Do not edit!

#ifndef TEST_TEST_H_
#define TEST_TEST_H_

#include <algorithm>
#include <string_view>

namespace test_namespace {

inline constexpr std::string_view kTestNameAllowList[] = {
  "All",
  "DownloadView",
  "PageInfoView",
};

constexpr bool IsValidTestName(std::string_view s) {
  return std::binary_search(
    std::cbegin(kTestNameAllowList),
    std::cend(kTestNameAllowList),
    s);
}

}  // namespace test_namespace

#endif  // TEST_TEST_H_
""")

_EXPECTED_VARIANT_LIST = ["All", "DownloadView", "PageInfoView"]

_TEST_VARIANT_INPUT = """
<variants name="BubbleName">
  <variant name="All"/>
  <variant name="DownloadView"/>
  <variant name="PageInfoView"/>
</variants>
"""

_EXPECTED_ENUM_LIST = [123, 456]

_TEST_ENUM_INPUT = """
<enum name="URLHashes">
  <int value="123" label="label1"/>
  <int value="456" label="label2"/>
</enum>
"""


class VariantAllowListTest(unittest.TestCase):

  def testGenerateSourceFileContent(self):
    namespace = "test_namespace"

    allow_list_name = "TestName"

    # Provide an unsorted list to ensure the list gets sorted since the check
    # function relies on being sorted.
    variant_list = ["DownloadView", "All", "PageInfoView"]
    content = generate_allowlist_from_histograms_file._GenerateStaticFile(
        "test/test.h", namespace, variant_list, allow_list_name)
    self.assertEqual(_EXPECTED_FILE_CONTENT, content)

  def testGenerateListFromVariants(self):
    histograms = xml.dom.minidom.parseString(_TEST_VARIANT_INPUT)
    allow_list_name = "BubbleName"
    variants = generate_allowlist_from_histograms_file._GenerateValueList(
        histograms, "variant", allow_list_name)
    self.assertEqual(_EXPECTED_VARIANT_LIST, variants)

    # Has incorrect allow list name.
    allow_list_name = "MissingVariantsName"
    with self.assertRaises(generate_allowlist_from_histograms_file.Error):
      generate_allowlist_from_histograms_file._GenerateValueList(
          histograms, "variant", allow_list_name)

  def testGenerateListFromEnums(self):
    histograms = xml.dom.minidom.parseString(_TEST_ENUM_INPUT)
    allow_list_name = "URLHashes"
    variants = generate_allowlist_from_histograms_file._GenerateValueList(
        histograms, "enum", allow_list_name)
    self.assertEqual(_EXPECTED_ENUM_LIST, variants)

    # Has incorrect allow list name.
    allow_list_name = "MissingVariantsName"
    with self.assertRaises(generate_allowlist_from_histograms_file.Error):
      generate_allowlist_from_histograms_file._GenerateValueList(
          histograms, "enum", allow_list_name)


if __name__ == "__main__":
  unittest.main()