From e2f44c0696c6f4ea63d90bd401a11470b967f21d Mon Sep 17 00:00:00 2001
From: pompolic <pompolic@special-circumstanc.es>
Date: Mon, 11 Jul 2022 20:53:30 +0200
Subject: [PATCH] Test for .read_member() including HParsedToken* type lookup

---
 gdb-port/tests/unit/ast.py | 34 +++++++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)

diff --git a/gdb-port/tests/unit/ast.py b/gdb-port/tests/unit/ast.py
index 4951093..7fe8bf4 100644
--- a/gdb-port/tests/unit/ast.py
+++ b/gdb-port/tests/unit/ast.py
@@ -47,13 +47,9 @@ class HParseResultCreation(unittest.TestCase):
 		hpr_pointer_type_patcher = unittest.mock.patch.object(HParseResult, 'HParseResult_t_p', spec=gdb.Type)
 		gdbv_patcher = unittest.mock.patch('gdb.Value', autospec=True)
 		gdb_lookup_type_patcher = unittest.mock.patch.object(gdb, 'lookup_type')
-		hpr_ast_not_null_patcher = unittest.mock.patch.object(HParseResult, 'read_AST_not_null', return_value=True)
-		hpr_make_hparsedtoken_patcher = unittest.mock.patch.object(HParseResult, 'make_HParsedToken', spec=HParsedToken)
 		init_patcher = unittest.mock.patch.object(HParseResult, '__init__', return_value=None)
 
 		hpr_pointer_type_patcher.start()
-		#hpr_ast_not_null_patcher.start()
-		#hpr_make_hparsedtoken_patcher.start()
 		gdbv_mock_object = gdbv_patcher.start()
 		gdb_lookup_type_mock_object = gdb_lookup_type_patcher.start()
 		init_patcher.start()
@@ -62,6 +58,11 @@ class HParseResultCreation(unittest.TestCase):
 		result.address = 0xdeadbeef
 		member = result.read_member('ast')
 
+		init_patcher.stop()
+		gdb_lookup_type_patcher.stop()
+		gdbv_patcher.stop()
+		hpr_pointer_type_patcher.stop()
+
 		# Check that struct being indexed is at self.address
 		self.assertEqual(gdbv_mock_object.mock_calls[0], unittest.mock.call(0xdeadbeef))
 		# Check that the value returned is parse_result_gdb_value['foo']
@@ -71,7 +72,30 @@ class HParseResultCreation(unittest.TestCase):
 
 	def test_read_member_with_type_lookup(self):
 		gdb_lookup_type_patcher = unittest.mock.patch.object(gdb, 'lookup_type')
-		raise Exception("Not implemented")
+		gdbv_patcher = unittest.mock.patch('gdb.Value', autospec=True)
+		init_patcher = unittest.mock.patch.object(HParseResult, '__init__', return_value=None)
+		# This is cached in the class the first time read_member() or read_AST_not_null() is called
+		# To test the case where it's not yet cached, we explicitly set it to None
+		HParseResult.HParseResult_t_p = None
+
+		gdb_lookup_type_mock_object = gdb_lookup_type_patcher.start()
+		gdbv_mock_object = gdbv_patcher.start()
+		init_patcher.start()
+
+		result = HParseResult(0xdeadbeef)
+		result.address = 0xdeadbeef
+		member = result.read_member('ast')
+
+		init_patcher.stop()
+		gdbv_patcher.stop()
+		gdb_lookup_type_patcher.stop()
+
+		# Check that HParseResult* has been looked up
+		self.assertEqual(gdb_lookup_type_mock_object.mock_calls, [unittest.mock.call('HParseResult'), unittest.mock.call().pointer()])
+		# Check that the struct being indexed is at self.address
+		self.assertEqual(gdbv_mock_object.mock_calls[0], unittest.mock.call(0xdeadbeef))
+		# Check that the value returned is parse_result_gdb_value['foo']
+		self.assertEqual(gdbv_mock_object.mock_calls[-1], unittest.mock.call().cast().__getitem__('ast'))
 
 	# TODO: should read_member employ a whitelist for member_name, or should this trigger an exeption?
 	def test_read_member_invalid_param(self):
-- 
GitLab