1# Copyright 2019 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""Tests for front_end.test_util.""" 16 17import unittest 18 19from compiler.util import ir_data 20from compiler.util import parser_types 21from compiler.util import test_util 22 23 24class ProtoIsSupersetTest(unittest.TestCase): 25 """Tests for test_util.proto_is_superset.""" 26 27 def test_superset_extra_optional_field(self): 28 self.assertEqual( 29 (True, ""), 30 test_util.proto_is_superset( 31 ir_data.Structure( 32 field=[ir_data.Field()], 33 source_location=parser_types.parse_location("1:2-3:4")), 34 ir_data.Structure(field=[ir_data.Field()]))) 35 36 def test_superset_extra_repeated_field(self): 37 self.assertEqual( 38 (True, ""), 39 test_util.proto_is_superset( 40 ir_data.Structure( 41 field=[ir_data.Field(), ir_data.Field()], 42 source_location=parser_types.parse_location("1:2-3:4")), 43 ir_data.Structure(field=[ir_data.Field()]))) 44 45 def test_superset_missing_empty_repeated_field(self): 46 self.assertEqual( 47 (False, "field[0] missing"), 48 test_util.proto_is_superset( 49 ir_data.Structure( 50 field=[], 51 source_location=parser_types.parse_location("1:2-3:4")), 52 ir_data.Structure(field=[ir_data.Field(), ir_data.Field()]))) 53 54 def test_superset_missing_empty_optional_field(self): 55 self.assertEqual((False, "source_location missing"), 56 test_util.proto_is_superset( 57 ir_data.Structure(field=[]), 58 ir_data.Structure(source_location=ir_data.Location()))) 59 60 def test_array_element_differs(self): 61 self.assertEqual( 62 (False, 63 "field[0].source_location.start.line differs: found 1, expected 2"), 64 test_util.proto_is_superset( 65 ir_data.Structure( 66 field=[ir_data.Field(source_location=parser_types.parse_location( 67 "1:2-3:4"))]), 68 ir_data.Structure( 69 field=[ir_data.Field(source_location=parser_types.parse_location( 70 "2:2-3:4"))]))) 71 72 def test_equal(self): 73 self.assertEqual( 74 (True, ""), 75 test_util.proto_is_superset(parser_types.parse_location("1:2-3:4"), 76 parser_types.parse_location("1:2-3:4"))) 77 78 def test_superset_missing_optional_field(self): 79 self.assertEqual( 80 (False, "source_location missing"), 81 test_util.proto_is_superset( 82 ir_data.Structure(field=[ir_data.Field()]), 83 ir_data.Structure( 84 field=[ir_data.Field()], 85 source_location=parser_types.parse_location("1:2-3:4")))) 86 87 def test_optional_field_differs(self): 88 self.assertEqual((False, "end.line differs: found 4, expected 3"), 89 test_util.proto_is_superset( 90 parser_types.parse_location("1:2-4:4"), 91 parser_types.parse_location("1:2-3:4"))) 92 93 def test_non_message_repeated_field_equal(self): 94 self.assertEqual((True, ""), 95 test_util.proto_is_superset( 96 ir_data.CanonicalName(object_path=[]), 97 ir_data.CanonicalName(object_path=[]))) 98 99 def test_non_message_repeated_field_missing_element(self): 100 self.assertEqual( 101 (False, "object_path differs: found {none!r}, expected {a!r}".format( 102 none=[], 103 a=[u"a"])), 104 test_util.proto_is_superset( 105 ir_data.CanonicalName(object_path=[]), 106 ir_data.CanonicalName(object_path=[u"a"]))) 107 108 def test_non_message_repeated_field_element_differs(self): 109 self.assertEqual( 110 (False, "object_path differs: found {aa!r}, expected {ab!r}".format( 111 aa=[u"a", u"a"], 112 ab=[u"a", u"b"])), 113 test_util.proto_is_superset( 114 ir_data.CanonicalName(object_path=[u"a", u"a"]), 115 ir_data.CanonicalName(object_path=[u"a", u"b"]))) 116 117 def test_non_message_repeated_field_extra_element(self): 118 # For repeated fields of int/bool/str values, the entire list is treated as 119 # an atomic unit, and should be equal. 120 self.assertEqual( 121 (False, 122 "object_path differs: found {!r}, expected {!r}".format( 123 [u"a", u"a"], [u"a"])), 124 test_util.proto_is_superset( 125 ir_data.CanonicalName(object_path=["a", "a"]), 126 ir_data.CanonicalName(object_path=["a"]))) 127 128 def test_non_message_repeated_field_no_expected_value(self): 129 # When a repeated field is empty, it is the same as if it were entirely 130 # missing -- there is no way to differentiate those two conditions. 131 self.assertEqual((True, ""), 132 test_util.proto_is_superset( 133 ir_data.CanonicalName(object_path=["a", "a"]), 134 ir_data.CanonicalName(object_path=[]))) 135 136 137class DictFileReaderTest(unittest.TestCase): 138 """Tests for dict_file_reader.""" 139 140 def test_empty_dict(self): 141 reader = test_util.dict_file_reader({}) 142 self.assertEqual((None, ["File 'anything' not found."]), reader("anything")) 143 self.assertEqual((None, ["File '' not found."]), reader("")) 144 145 def test_one_element_dict(self): 146 reader = test_util.dict_file_reader({"m": "abc"}) 147 self.assertEqual((None, ["File 'not_there' not found."]), 148 reader("not_there")) 149 self.assertEqual((None, ["File '' not found."]), reader("")) 150 self.assertEqual(("abc", None), reader("m")) 151 152 def test_two_element_dict(self): 153 reader = test_util.dict_file_reader({"m": "abc", "n": "def"}) 154 self.assertEqual((None, ["File 'not_there' not found."]), 155 reader("not_there")) 156 self.assertEqual((None, ["File '' not found."]), reader("")) 157 self.assertEqual(("abc", None), reader("m")) 158 self.assertEqual(("def", None), reader("n")) 159 160 def test_dict_with_empty_key(self): 161 reader = test_util.dict_file_reader({"m": "abc", "": "def"}) 162 self.assertEqual((None, ["File 'not_there' not found."]), 163 reader("not_there")) 164 self.assertEqual((None, ["File 'None' not found."]), reader(None)) 165 self.assertEqual(("abc", None), reader("m")) 166 self.assertEqual(("def", None), reader("")) 167 168 169if __name__ == "__main__": 170 unittest.main() 171