this repo has no description
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2#include "capi-testing.h"
3
4#include <limits>
5
6#include "Python.h"
7#include "gtest/gtest.h"
8
9#include "capi-fixture.h"
10
11namespace py {
12namespace testing {
13
14using ExtensionApiTestingUtilsTest = ExtensionApi;
15
16TEST_F(ExtensionApiTestingUtilsTest, ImportNonExistingModuleReturnsNull) {
17 PyObject* pyname = PyUnicode_FromString("foo");
18 EXPECT_EQ(testing::importGetModule(pyname), nullptr);
19 Py_DECREF(pyname);
20}
21
22TEST_F(ExtensionApiTestingUtilsTest, ImportExistingModuleReturnsModule) {
23 const char* c_name = "sys";
24 PyObject* pyname = PyUnicode_FromString(c_name);
25 PyObject* sysmodule = testing::importGetModule(pyname);
26 ASSERT_NE(sysmodule, nullptr);
27 EXPECT_TRUE(PyModule_CheckExact(sysmodule));
28 Py_DECREF(pyname);
29
30 PyObject* sysmodule_name = PyModule_GetNameObject(sysmodule);
31 const char* c_sysmodule_name = PyUnicode_AsUTF8(sysmodule_name);
32 EXPECT_STREQ(c_sysmodule_name, c_name);
33 Py_DECREF(sysmodule_name);
34 Py_DECREF(sysmodule);
35}
36
37TEST_F(ExtensionApiTestingUtilsTest, IsLongEqualsLong) {
38 PyObjectPtr ten(PyLong_FromLong(10));
39
40 ::testing::AssertionResult ok = isLongEqualsLong(ten, 10);
41 EXPECT_TRUE(ok);
42
43 ::testing::AssertionResult bad_value = isLongEqualsLong(ten, 24);
44 ASSERT_FALSE(bad_value);
45 EXPECT_STREQ(bad_value.message(), "10 is not equal to 24");
46
47 PyObjectPtr max_long(PyLong_FromLong(std::numeric_limits<long>::max()));
48 PyObjectPtr big_long(PyNumber_Multiply(max_long, ten));
49 ::testing::AssertionResult bad_big_value = isLongEqualsLong(big_long, 1234);
50 ASSERT_FALSE(bad_big_value);
51 EXPECT_STREQ(bad_big_value.message(),
52 "92233720368547758070 is not equal to 1234");
53
54 PyObjectPtr string(PyUnicode_FromString("hello, there!"));
55 ::testing::AssertionResult bad_type = isLongEqualsLong(string, 5678);
56 ASSERT_FALSE(bad_type);
57 EXPECT_STREQ(bad_type.message(), "'hello, there!' is not equal to 5678");
58}
59
60} // namespace testing
61} // namespace py