this repo has no description
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2#include "Python.h"
3#include "gtest/gtest.h"
4
5#include "capi-fixture.h"
6#include "capi-testing.h"
7
8namespace py {
9namespace testing {
10
11using PosixExtensionApiTest = ExtensionApi;
12
13TEST_F(PosixExtensionApiTest, FsPathWithNonPathReturnsNull) {
14 PyObjectPtr result(PyOS_FSPath(Py_None));
15 ASSERT_NE(PyErr_Occurred(), nullptr);
16 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_TypeError));
17 EXPECT_EQ(result, nullptr);
18}
19
20TEST_F(PosixExtensionApiTest, FsPathWithStrReturnsSameStr) {
21 PyObjectPtr str(PyUnicode_FromString("foo"));
22 PyObjectPtr result(PyOS_FSPath(str));
23 ASSERT_EQ(PyErr_Occurred(), nullptr);
24 EXPECT_EQ(result, str);
25}
26
27TEST_F(PosixExtensionApiTest, FsPathWithBytesReturnsSameBytes) {
28 PyObjectPtr bytes(PyBytes_FromString("foo"));
29 PyObjectPtr result(PyOS_FSPath(bytes));
30 ASSERT_EQ(PyErr_Occurred(), nullptr);
31 EXPECT_EQ(result, bytes);
32}
33
34TEST_F(PosixExtensionApiTest, FsPathWithNonCallableFsPathRaisesTypeError) {
35 PyRun_SimpleString(R"(
36class Foo():
37 __fspath__ = None
38foo = Foo()
39 )");
40 PyObjectPtr foo(mainModuleGet("foo"));
41 PyObjectPtr result(PyOS_FSPath(foo));
42 ASSERT_NE(PyErr_Occurred(), nullptr);
43 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_TypeError));
44 EXPECT_EQ(result, nullptr);
45}
46
47TEST_F(PosixExtensionApiTest, FsPathWithNonStrOrBytesResultRaisesTypeError) {
48 PyRun_SimpleString(R"(
49class Foo():
50 def __fspath__(self):
51 return 1
52foo = Foo()
53 )");
54 PyObjectPtr foo(mainModuleGet("foo"));
55 PyObjectPtr result(PyOS_FSPath(foo));
56 ASSERT_NE(PyErr_Occurred(), nullptr);
57 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_TypeError));
58 EXPECT_EQ(result, nullptr);
59}
60
61TEST_F(PosixExtensionApiTest, FsPathReturnsPath) {
62 PyRun_SimpleString(R"(
63class Foo():
64 def __fspath__(self):
65 return "/some/path"
66foo = Foo()
67 )");
68 PyObjectPtr foo(mainModuleGet("foo"));
69 PyObjectPtr result(PyOS_FSPath(foo));
70 ASSERT_EQ(PyErr_Occurred(), nullptr);
71 EXPECT_TRUE(isUnicodeEqualsCStr(result, "/some/path"));
72}
73
74} // namespace testing
75} // namespace py