1 //===-- PythonExceptionStateTest.cpp ------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "gtest/gtest.h"
12 #include "Plugins/ScriptInterpreter/Python/lldb-python.h"
13 #include "Plugins/ScriptInterpreter/Python/PythonDataObjects.h"
14 #include "Plugins/ScriptInterpreter/Python/PythonExceptionState.h"
15 #include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h"
17 #include "PythonTestSuite.h"
19 using namespace lldb_private;
21 class PythonExceptionStateTest : public PythonTestSuite
28 PyErr_SetString(PyExc_RuntimeError, "PythonExceptionStateTest test error");
32 TEST_F(PythonExceptionStateTest, TestExceptionStateChecking)
35 EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
38 EXPECT_TRUE(PythonExceptionState::HasErrorOccurred());
43 TEST_F(PythonExceptionStateTest, TestAcquisitionSemantics)
46 PythonExceptionState no_error(false);
47 EXPECT_FALSE(no_error.IsError());
48 EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
52 PythonExceptionState error(false);
53 EXPECT_TRUE(error.IsError());
54 EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
60 EXPECT_TRUE(error.IsError());
61 EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
66 TEST_F(PythonExceptionStateTest, TestDiscardSemantics)
70 // Test that discarding an exception does not restore the exception
71 // state even when auto-restore==true is set
73 PythonExceptionState error(true);
74 EXPECT_TRUE(error.IsError());
75 EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
78 EXPECT_FALSE(error.IsError());
79 EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
82 TEST_F(PythonExceptionStateTest, TestResetSemantics)
86 // Resetting when auto-restore is true should restore.
88 PythonExceptionState error(true);
89 EXPECT_TRUE(error.IsError());
90 EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
92 EXPECT_FALSE(error.IsError());
93 EXPECT_TRUE(PythonExceptionState::HasErrorOccurred());
97 // Resetting when auto-restore is false should discard.
99 PythonExceptionState error2(false);
100 EXPECT_TRUE(error2.IsError());
101 EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
103 EXPECT_FALSE(error2.IsError());
104 EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
109 TEST_F(PythonExceptionStateTest, TestManualRestoreSemantics)
113 PythonExceptionState error(false);
114 EXPECT_TRUE(error.IsError());
115 EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
118 EXPECT_FALSE(error.IsError());
119 EXPECT_TRUE(PythonExceptionState::HasErrorOccurred());
124 TEST_F(PythonExceptionStateTest, TestAutoRestoreSemantics)
127 // Test that using the auto-restore flag correctly restores the exception
128 // state on destruction, and not using the auto-restore flag correctly
129 // does NOT restore the state on destruction.
132 PythonExceptionState error(false);
133 EXPECT_TRUE(error.IsError());
134 EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
136 EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
141 PythonExceptionState error(true);
142 EXPECT_TRUE(error.IsError());
143 EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
145 EXPECT_TRUE(PythonExceptionState::HasErrorOccurred());
150 TEST_F(PythonExceptionStateTest, TestAutoRestoreChanged)
152 // Test that if we re-acquire with different auto-restore semantics,
153 // that the new semantics are respected.
157 PythonExceptionState error(false);
158 EXPECT_TRUE(error.IsError());
161 EXPECT_FALSE(error.IsError());
162 EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
166 EXPECT_TRUE(error.IsError());
167 EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
170 EXPECT_FALSE(error.IsError());
171 EXPECT_TRUE(PythonExceptionState::HasErrorOccurred());