this repo has no description
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2#include "strarray-builtins.h"
3
4#include "gtest/gtest.h"
5
6#include "builtins.h"
7#include "test-utils.h"
8
9namespace py {
10namespace testing {
11
12using StrArrayBuiltinsTest = RuntimeFixture;
13
14TEST_F(StrArrayBuiltinsTest, DunderNewAndDunderInitCreateStrArray) {
15 HandleScope scope(thread_);
16 ASSERT_FALSE(runFromCStr(runtime_, "obj = _str_array('hello')").isError());
17 StrArray self(&scope, mainModuleAt(runtime_, "obj"));
18 EXPECT_TRUE(isStrEqualsCStr(runtime_->strFromStrArray(self), "hello"));
19}
20
21TEST_F(StrArrayBuiltinsTest, DunderInitWithWrongTypeRaisesTypeError) {
22 EXPECT_TRUE(raisedWithStr(runFromCStr(runtime_, "obj = _str_array(b'hello')"),
23 LayoutId::kTypeError,
24 "_str_array can only be initialized with str"));
25}
26
27TEST_F(StrArrayBuiltinsTest, DunderReprWithNonStrArrayRaisesTypeError) {
28 EXPECT_TRUE(raisedWithStr(
29 runFromCStr(runtime_, "_str_array.__repr__(b'')"), LayoutId::kTypeError,
30 "'__repr__' for '_str_array' objects doesn't apply to a 'bytes' object"));
31}
32
33TEST_F(StrArrayBuiltinsTest, DunderReprWithSimpleStrArrayReturnsStr) {
34 HandleScope scope(thread_);
35 ASSERT_FALSE(
36 runFromCStr(runtime_, "obj = _str_array('hello').__repr__()").isError());
37 Str self(&scope, mainModuleAt(runtime_, "obj"));
38 EXPECT_TRUE(isStrEqualsCStr(*self, "_str_array('hello')"));
39}
40
41TEST_F(StrArrayBuiltinsTest, DunderStrWithNonStrArrayRaisesTypeError) {
42 EXPECT_TRUE(raisedWithStr(
43 runFromCStr(runtime_, "_str_array.__str__(b'')"), LayoutId::kTypeError,
44 "'__str__' for '_str_array' objects doesn't apply to a 'bytes' object"));
45}
46
47TEST_F(StrArrayBuiltinsTest, DunderStrWithEmptyStrArrayReturnsEmptyStr) {
48 HandleScope scope(thread_);
49 StrArray self(&scope, runtime_->newStrArray());
50 Object repr(&scope, runBuiltin(METH(_str_array, __str__), self));
51 EXPECT_TRUE(isStrEqualsCStr(*repr, ""));
52}
53
54TEST_F(StrArrayBuiltinsTest, DunderStrWithSimpleStrArrayReturnsStr) {
55 HandleScope scope(thread_);
56 StrArray self(&scope, runtime_->newStrArray());
57 const char* test_str = "foo";
58 Str foo(&scope, runtime_->newStrFromCStr(test_str));
59 runtime_->strArrayAddStr(thread_, self, foo);
60 Object repr(&scope, runBuiltin(METH(_str_array, __str__), self));
61 EXPECT_TRUE(isStrEqualsCStr(*repr, test_str));
62}
63
64} // namespace testing
65} // namespace py