From 39dbe1d017345c2c9076124f547486e8e0e12c08 Mon Sep 17 00:00:00 2001
From: pompolic <pompolic@special-circumstanc.es>
Date: Wed, 29 Jun 2022 18:09:21 +0200
Subject: [PATCH] Tests for HParseResult.make_HParsedToken()

---
 gdb-port/tests/unit/ast.py | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/gdb-port/tests/unit/ast.py b/gdb-port/tests/unit/ast.py
index 90a4480..0e2a9d5 100644
--- a/gdb-port/tests/unit/ast.py
+++ b/gdb-port/tests/unit/ast.py
@@ -23,9 +23,34 @@ class HParseResultCreation(unittest.TestCase):
 
 	#TODO: Mocking the read_member method might be needed
 	#TODO: MagicMock breaks self.token_type comparison
+	#TODO: mock read_AST_not_null() to return true
 	def test_make_HParsedToken(self):
 		gdbv_patcher = unittest.mock.patch('gdb.Value', autospec=True)
+		hpt_patcher = unittest.mock.patch('__main__.HParsedToken', autospec=True)
+		hpr_patcher = unittest.mock.patch.object(HParseResult, 'read_AST_not_null', return_value=True)
 		gdbv_patcher.start()
+		hpt_patcher.start()
+		hpr_patcher.start()
 		hpr = HParseResult(0xdeadbeef)
 		tok = hpr.make_HParsedToken()
+		hpr_patcher.stop()
+		hpt_patcher.stop()
 		gdbv_patcher.stop()
+		#TODO: what are the invariants we need to test here?
+		# e.g.: tok.address = hpr.ast.value
+		self.assertEqual(tok.address, hpr.ast)
+
+	def test_make_HParsedToken_if_no_ast(self):
+		gdbv_patcher = unittest.mock.patch('gdb.Value', autospec=True)
+		hpt_patcher = unittest.mock.patch('__main__.HParsedToken', autospec=True)
+		hpr_patcher = unittest.mock.patch.object(HParseResult, 'read_AST_not_null', return_value=False)
+		gdbv_patcher.start()
+		hpt_patcher.start()
+		hpr_patcher.start()
+		hpr = HParseResult(0xdeadbeef)
+		tok = hpr.make_HParsedToken()
+		hpr_patcher.stop()
+		hpt_patcher.stop()
+		gdbv_patcher.stop()
+		self.assertIsNone(tok)
+
-- 
GitLab