def test_process_conditional_greater_than_equal(self): parser = BaseParser("") self.assertTrue(parser.InActiveCode()) self.assertTrue(parser.ProcessConditional("!IF 30 >= 50")) self.assertFalse(parser.InActiveCode()) parser.ProcessConditional("!endif") self.assertTrue(parser.InActiveCode()) self.assertTrue(parser.ProcessConditional("!IF 30 >= 30")) self.assertTrue(parser.InActiveCode()) parser.ProcessConditional("!endif") self.assertTrue(parser.InActiveCode()) self.assertTrue(parser.ProcessConditional("!IF 50 >= 30")) self.assertTrue(parser.InActiveCode())
def test_process_conditional_less_than(self): parser = BaseParser("") self.assertTrue(parser.InActiveCode()) self.assertTrue(parser.ProcessConditional("!IF 70 < 50")) self.assertFalse(parser.InActiveCode()) parser.ProcessConditional("!endif") self.assertTrue(parser.InActiveCode()) self.assertTrue(parser.ProcessConditional("!IF 70 < 70")) self.assertFalse(parser.InActiveCode()) parser.ProcessConditional("!endif") self.assertTrue(parser.InActiveCode()) self.assertTrue(parser.ProcessConditional("!IF 50 < 70")) self.assertTrue(parser.InActiveCode())
def test_conditional_ifndef(self): parser = BaseParser("") # simple confirmation of expected behavior # don't need to test VariableReplacement # that is done in the tests replace methods self.assertTrue(parser.InActiveCode()) parser.ProcessConditional("!ifndef 0") self.assertTrue(parser.InActiveCode()) parser.PopConditional() self.assertTrue(parser.InActiveCode()) parser.ProcessConditional("!ifndef blahblah") self.assertFalse(parser.InActiveCode()) parser.PopConditional()
def test_replace_boolean_constants(self): parser = BaseParser("") parser.SetInputVars({ "true": "True", "false": "False", "b_true": True, "b_false": False }) line = "$(true)" self.assertEqual(parser.ReplaceVariables(line), "TRUE") line = "$(false)" self.assertEqual(parser.ReplaceVariables(line), "FALSE") line = "$(b_true)" self.assertEqual(parser.ReplaceVariables(line), "TRUE") line = "$(b_false)" self.assertEqual(parser.ReplaceVariables(line), "FALSE")
def test_process_in_conditional(self): parser = BaseParser("") parser.SetInputVars({"TOOL_CHAIN_TAG": "GCC5_TEST"}) self.assertTrue(parser.ProcessConditional( '!if ("GCC49" in $(TOOL_CHAIN_TAG)) OR ("GCC5" in $(TOOL_CHAIN_TAG))')) self.assertTrue(parser.InActiveCode()) parser.ResetParserState() parser.SetInputVars({"TOOL_CHAIN_TAG": "TESTGCC49"}) self.assertTrue(parser.ProcessConditional( '!if ("GCC49" in $(TOOL_CHAIN_TAG)) OR ("GCC5" in $(TOOL_CHAIN_TAG))')) self.assertTrue(parser.InActiveCode()) parser.ResetParserState() # Don't give it a tool chain tag that isn't in the things we're searching for parser.SetInputVars({"TOOL_CHAIN_TAG": "NOTFOUND"}) self.assertTrue(parser.ProcessConditional( '!if ("GCC49" in $(TOOL_CHAIN_TAG)) OR ("GCC5" in $(TOOL_CHAIN_TAG))')) self.assertFalse(parser.InActiveCode())
def test_find_path(self): # we're using write lines to make sure everything wo parser = BaseParser("") parser.Lines = ["hello"] package_paths = ["Common/Test", "SM_MAGIC"] root_path = tempfile.mkdtemp() target_filedir = os.path.join(root_path, "BuildPkg") parser.TargetFilePath = target_filedir parser.SetPackagePaths(package_paths) parser.SetBaseAbsPath(root_path) os.makedirs(target_filedir) index = 0 root_file = "root.txt" target_file = "target.txt" for package in package_paths: pack_path = os.path.join(root_path, package) os.makedirs(pack_path) parser.WriteLinesToFile( os.path.join(pack_path, f"package_{index}.txt")) index += 1 root_filepath = os.path.join(root_path, root_file) target_filepath = os.path.join(target_filedir, target_file) parser.WriteLinesToFile(root_filepath) parser.WriteLinesToFile(target_filepath) root_found = parser.FindPath(root_file) self.assertEqual(root_found, root_filepath) target_found = parser.FindPath(target_file) self.assertEqual(target_found, target_filepath) # check package relative packages for index in range(len(package_paths)): file_name = f"package_{index}.txt" pp_found = parser.FindPath(file_name) self.assertTrue(os.path.exists(pp_found)) # invalid files invalid_filename = "YOU_WONT_FIND_ME.txt" invalid_file = os.path.join(root_path, invalid_filename) invalid_result = parser.FindPath(invalid_filename) self.assertEqual(invalid_file, invalid_result)
def test_parse_guid(self): guid1 = "{ 0xD3B36F2C, 0xD551, 0x11D4, {0x9A, 0x46, 0x0, 0x90, 0x27, 0x3F, 0xC1,0xD }}" guid1_answer = "D3B36F2C-D551-11D4-9A46-0090273FC10D" parser = BaseParser("") guid1_result = parser.ParseGuid(guid1) self.assertEqual(guid1_answer, guid1_result) # try a bad guid and make sure it fails since it's missing an element guid2 = "{ 0xD3B36F2C, 0xD551, 0x11D4, { 0x9A, 0x46, 0x00, 0x90, 0x27, 0x3F, 0xC1 }}" with self.assertRaises(RuntimeError): parser.ParseGuid(guid2) # check one that's too long guid3 = "{ 0xD3B36FbadC, 0xD551, 0x11D4, { 0x9A, 0x46, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }}" with self.assertRaises(RuntimeError): parser.ParseGuid(guid3) # check one that's too short guid4 = "{ 0x3, 0x1, 0x4, { 0xA, 0x6, 0x0, 0x9, 0x2, 0xF, 0x1, 0xD }}" guid4_answer = "00000003-0001-0004-0A06-0009020F010D" guid4_result = parser.ParseGuid(guid4) self.assertEqual(guid4_result, guid4_answer)
def test_replace_macro_without_resolution(self): parser = BaseParser("") parser.SetInputVars({"name": "sean"}) line = "!if $(Unknown_Token)!" self.assertEqual(parser.ReplaceVariables(line), "!if 0!")
def test_process_conditional_invalid_operators(self): parser = BaseParser("") # check weird operators with self.assertRaises(RuntimeError): self.assertTrue(parser.ProcessConditional("!IF 50 <> 50"))
def test_process_else(self): parser = BaseParser("") # check to make sure we can't do a malformed endif self.assertTrue(parser.ProcessConditional("!if TRUE")) self.assertTrue(parser.ProcessConditional("!else")) self.assertFalse(parser.InActiveCode())
def test_process_conditional_true_equals_one(self): parser = BaseParser("") # check != with true and false self.assertTrue(parser.ProcessConditional("!IF TRUE == 1")) self.assertTrue(parser.InActiveCode()) self.assertTrue(parser.ProcessConditional("!EnDiF"))
def test_process_conditional_true_cannot_be_greater_than_hex(self): parser = BaseParser("") # check != with true and false with self.assertRaises(ValueError): parser.ProcessConditional("!IF 0x7 >= TRUE")
def test_process_conditional_ands_ors(self): parser = BaseParser("") self.assertTrue(parser.ProcessConditional("!if TRUE == FALSE OR TRUE == TRUE"))
def test_process_conditional_false_equals_zero(self): parser = BaseParser("") self.assertTrue(parser.ProcessConditional("!IF FALSE == 0")) self.assertTrue(parser.InActiveCode()) self.assertTrue(parser.ProcessConditional("!EnDiF"))
def test_emulator_conditional_not_in(self): parser = BaseParser("") parser.SetInputVars({"TOOL_CHAIN_TAG": "VS2019"}) self.assertTrue(parser.ProcessConditional('!if "XCODE5" not in $(TOOL_CHAIN_TAG)')) self.assertTrue(parser.InActiveCode()) parser.ResetParserState()
def test_emulator_conditional_parens_order(self): ''' Makes sure the parenthesis affect the order of expressions ''' parser = BaseParser("") self.assertFalse(parser.EvaluateConditional('!if TRUE OR FALSE AND FALSE')) self.assertTrue(parser.EvaluateConditional('!if TRUE OR (FALSE AND FALSE)')) parser.ResetParserState()
def test_replace_macro_using_dollarsign(self): parser = BaseParser("") parser.SetInputVars({"name": "sean"}) line = "Hello $(name)!" self.assertEqual(parser.ReplaceVariables(line), "Hello sean!")
def test_process_invalid_conditional(self): parser = BaseParser("") with self.assertRaises(RuntimeError): parser.EvaluateConditional('!if TRUE AND FALSE AND') with self.assertRaises(RuntimeError): parser.EvaluateConditional('TRUE AND FALSE AND')
def test_replace_macro_local_var_priority(self): parser = BaseParser("") parser.SetInputVars({"name": "sean"}) parser.LocalVars["name"] = "fred" line = "Hello $(name)!" self.assertEqual(parser.ReplaceVariables(line), "Hello fred!")