this repo has no description
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2#include "test-utils.h"
3
4#include "gtest/gtest.h"
5
6#include "objects.h"
7#include "runtime.h"
8
9namespace py {
10namespace testing {
11
12using TestUtils = RuntimeFixture;
13
14TEST_F(TestUtils, IsBytearrayEquals) {
15 HandleScope scope(thread_);
16
17 const byte view[] = {'f', 'o', 'o'};
18 Object bytes(&scope, runtime_->newBytesWithAll(view));
19 auto const type_err = isBytearrayEqualsBytes(bytes, view);
20 EXPECT_FALSE(type_err);
21 const char* type_msg = "is a 'bytes'";
22 EXPECT_STREQ(type_err.message(), type_msg);
23
24 Bytearray array(&scope, runtime_->newBytearray());
25 runtime_->bytearrayExtend(thread_, array, view);
26 auto const ok = isBytearrayEqualsBytes(array, view);
27 EXPECT_TRUE(ok);
28
29 auto const not_equal = isBytearrayEqualsCStr(array, "bar");
30 EXPECT_FALSE(not_equal);
31 const char* ne_msg = "bytearray(b'foo') is not equal to bytearray(b'bar')";
32 EXPECT_STREQ(not_equal.message(), ne_msg);
33
34 Object err(&scope, Error::error());
35 auto const error = isBytearrayEqualsCStr(err, "");
36 EXPECT_FALSE(error);
37 const char* error_msg = "is an Error";
38 EXPECT_STREQ(error.message(), error_msg);
39
40 Object result(&scope,
41 thread_->raiseWithFmt(LayoutId::kValueError, "bad things"));
42 auto const exc = isBytearrayEqualsBytes(result, view);
43 EXPECT_FALSE(exc);
44 const char* exc_msg = "pending 'ValueError' exception";
45 EXPECT_STREQ(exc.message(), exc_msg);
46}
47
48TEST_F(TestUtils, IsBytesEquals) {
49 HandleScope scope(thread_);
50
51 const byte view[] = {'f', 'o', 'o'};
52 Object bytes(&scope, runtime_->newBytesWithAll(view));
53 auto const ok = isBytesEqualsBytes(bytes, view);
54 EXPECT_TRUE(ok);
55
56 ASSERT_FALSE(runFromCStr(runtime_, R"(
57class Foo(bytes): pass
58foo = Foo(b"foo")
59)")
60 .isError());
61 Object foo(&scope, mainModuleAt(runtime_, "foo"));
62 auto const subclass_ok = isBytesEqualsBytes(foo, view);
63 EXPECT_TRUE(subclass_ok);
64
65 auto const not_equal = isBytesEqualsCStr(bytes, "bar");
66 EXPECT_FALSE(not_equal);
67 const char* ne_msg = "b'foo' is not equal to b'bar'";
68 EXPECT_STREQ(not_equal.message(), ne_msg);
69
70 Object str(&scope, runtime_->newStrWithAll(view));
71 auto const type_err = isBytesEqualsBytes(str, view);
72 EXPECT_FALSE(type_err);
73 const char* type_msg = "is a 'str'";
74 EXPECT_STREQ(type_err.message(), type_msg);
75
76 Object err(&scope, Error::error());
77 auto const error = isBytesEqualsCStr(err, "");
78 EXPECT_FALSE(error);
79 const char* error_msg = "is an Error";
80 EXPECT_STREQ(error.message(), error_msg);
81
82 Object result(&scope,
83 thread_->raiseWithFmt(LayoutId::kValueError, "bad things"));
84 auto const exc = isBytesEqualsBytes(result, view);
85 EXPECT_FALSE(exc);
86 const char* exc_msg = "pending 'ValueError' exception";
87 EXPECT_STREQ(exc.message(), exc_msg);
88}
89
90TEST_F(TestUtils, IsSymbolIdEquals) {
91 EXPECT_TRUE(isSymbolIdEquals(ID(builtins), ID(builtins)));
92
93 auto const exc = isSymbolIdEquals(ID(time), ID(function));
94 EXPECT_FALSE(exc);
95 const char* exc_msg = "Expected 'function', but got 'time'";
96 EXPECT_STREQ(exc.message(), exc_msg);
97
98 auto const invalid_exc = isSymbolIdEquals(SymbolId::kInvalid, ID(function));
99 EXPECT_FALSE(invalid_exc);
100 const char* invalid_exc_msg = "Expected 'function', but got '<Invalid>'";
101 EXPECT_STREQ(invalid_exc.message(), invalid_exc_msg);
102}
103
104TEST_F(TestUtils, PyListEqual) {
105 HandleScope scope(thread_);
106
107 ASSERT_FALSE(runFromCStr(runtime_, R"(
108l = [None, False, 100, 200.5, 'hello']
109i = 123456
110)")
111 .isError());
112 Object list(&scope, mainModuleAt(runtime_, "l"));
113 Object not_list(&scope, mainModuleAt(runtime_, "i"));
114
115 auto const ok = AssertPyListEqual(
116 "", "", list, {Value::none(), false, 100, 200.5, "hello"});
117 EXPECT_TRUE(ok);
118
119 auto const bad_type = AssertPyListEqual("not_list", "", not_list, {});
120 ASSERT_FALSE(bad_type);
121 const char* type_msg = R"( Type of: not_list
122 Actual: int
123Expected: list)";
124 EXPECT_STREQ(bad_type.message(), type_msg);
125
126 auto const bad_length = AssertPyListEqual("list", "", list, {1, 2, 3});
127 ASSERT_FALSE(bad_length);
128 const char* length_msg = R"(Length of: list
129 Actual: 5
130 Expected: 3)";
131 EXPECT_STREQ(bad_length.message(), length_msg);
132
133 auto const bad_elem_type =
134 AssertPyListEqual("list", "", list, {0, 1, 2, 3, 4});
135 ASSERT_FALSE(bad_elem_type);
136 const char* elem_type_msg = R"( Type of: list[0]
137 Actual: NoneType
138Expected: int)";
139 EXPECT_STREQ(bad_elem_type.message(), elem_type_msg);
140
141 auto const bad_bool =
142 AssertPyListEqual("list", "", list, {Value::none(), true, 2, 3, 4});
143 ASSERT_FALSE(bad_bool);
144 const char* bool_msg = R"(Value of: list[1]
145 Actual: False
146Expected: True)";
147 EXPECT_STREQ(bad_bool.message(), bool_msg);
148
149 auto const bad_int =
150 AssertPyListEqual("list", "", list, {Value::none(), false, 2, 3, 4});
151 ASSERT_FALSE(bad_int);
152 const char* int_msg = R"(Value of: list[2]
153 Actual: 100
154Expected: 2)";
155 EXPECT_STREQ(bad_int.message(), int_msg);
156
157 auto const bad_float = AssertPyListEqual(
158 "list", "", list, {Value::none(), false, 100, 200.25, 4});
159 ASSERT_FALSE(bad_float);
160 const char* float_msg = R"(Value of: list[3]
161 Actual: 200.5
162Expected: 200.25)";
163 EXPECT_STREQ(bad_float.message(), float_msg);
164
165 auto const bad_str = AssertPyListEqual(
166 "list", "", list, {Value::none(), false, 100, 200.5, "four"});
167 ASSERT_FALSE(bad_str);
168 const char* str_msg = R"(Value of: list[4]
169 Actual: "hello"
170Expected: four)";
171 EXPECT_STREQ(bad_str.message(), str_msg);
172}
173
174} // namespace testing
175} // namespace py