]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - unittests/ScriptInterpreter/Python/PythonExceptionStateTests.cpp
Vendor import of lldb trunk r256945:
[FreeBSD/FreeBSD.git] / unittests / ScriptInterpreter / Python / PythonExceptionStateTests.cpp
1 //===-- PythonExceptionStateTest.cpp ------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "gtest/gtest.h"
11
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"
16
17 #include "PythonTestSuite.h"
18
19 using namespace lldb_private;
20
21 class PythonExceptionStateTest : public PythonTestSuite
22 {
23   public:
24   protected:
25     void
26     RaiseException()
27     {
28         PyErr_SetString(PyExc_RuntimeError, "PythonExceptionStateTest test error");
29     }
30 };
31
32 TEST_F(PythonExceptionStateTest, TestExceptionStateChecking)
33 {
34     PyErr_Clear();
35     EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
36
37     RaiseException();
38     EXPECT_TRUE(PythonExceptionState::HasErrorOccurred());
39
40     PyErr_Clear();
41 }
42
43 TEST_F(PythonExceptionStateTest, TestAcquisitionSemantics)
44 {
45     PyErr_Clear();
46     PythonExceptionState no_error(false);
47     EXPECT_FALSE(no_error.IsError());
48     EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
49
50     PyErr_Clear();
51     RaiseException();
52     PythonExceptionState error(false);
53     EXPECT_TRUE(error.IsError());
54     EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
55     error.Discard();
56
57     PyErr_Clear();
58     RaiseException();
59     error.Acquire(false);
60     EXPECT_TRUE(error.IsError());
61     EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
62
63     PyErr_Clear();
64 }
65
66 TEST_F(PythonExceptionStateTest, TestDiscardSemantics)
67 {
68     PyErr_Clear();
69
70     // Test that discarding an exception does not restore the exception
71     // state even when auto-restore==true is set
72     RaiseException();
73     PythonExceptionState error(true);
74     EXPECT_TRUE(error.IsError());
75     EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
76
77     error.Discard();
78     EXPECT_FALSE(error.IsError());
79     EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
80 }
81
82 TEST_F(PythonExceptionStateTest, TestResetSemantics)
83 {
84     PyErr_Clear();
85
86     // Resetting when auto-restore is true should restore.
87     RaiseException();
88     PythonExceptionState error(true);
89     EXPECT_TRUE(error.IsError());
90     EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
91     error.Reset();
92     EXPECT_FALSE(error.IsError());
93     EXPECT_TRUE(PythonExceptionState::HasErrorOccurred());
94
95     PyErr_Clear();
96
97     // Resetting when auto-restore is false should discard.
98     RaiseException();
99     PythonExceptionState error2(false);
100     EXPECT_TRUE(error2.IsError());
101     EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
102     error2.Reset();
103     EXPECT_FALSE(error2.IsError());
104     EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
105
106     PyErr_Clear();
107 }
108
109 TEST_F(PythonExceptionStateTest, TestManualRestoreSemantics)
110 {
111     PyErr_Clear();
112     RaiseException();
113     PythonExceptionState error(false);
114     EXPECT_TRUE(error.IsError());
115     EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
116
117     error.Restore();
118     EXPECT_FALSE(error.IsError());
119     EXPECT_TRUE(PythonExceptionState::HasErrorOccurred());
120
121     PyErr_Clear();
122 }
123
124 TEST_F(PythonExceptionStateTest, TestAutoRestoreSemantics)
125 {
126     PyErr_Clear();
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.
130     {
131         RaiseException();
132         PythonExceptionState error(false);
133         EXPECT_TRUE(error.IsError());
134         EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
135     }
136     EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
137
138     PyErr_Clear();
139     {
140         RaiseException();
141         PythonExceptionState error(true);
142         EXPECT_TRUE(error.IsError());
143         EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
144     }
145     EXPECT_TRUE(PythonExceptionState::HasErrorOccurred());
146
147     PyErr_Clear();
148 }
149
150 TEST_F(PythonExceptionStateTest, TestAutoRestoreChanged)
151 {
152     // Test that if we re-acquire with different auto-restore semantics,
153     // that the new semantics are respected.
154     PyErr_Clear();
155
156     RaiseException();
157     PythonExceptionState error(false);
158     EXPECT_TRUE(error.IsError());
159
160     error.Reset();
161     EXPECT_FALSE(error.IsError());
162     EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
163
164     RaiseException();
165     error.Acquire(true);
166     EXPECT_TRUE(error.IsError());
167     EXPECT_FALSE(PythonExceptionState::HasErrorOccurred());
168
169     error.Reset();
170     EXPECT_FALSE(error.IsError());
171     EXPECT_TRUE(PythonExceptionState::HasErrorOccurred());
172
173     PyErr_Clear();
174 }