Build POISON_MARKER and TANY_MARKER
[hiphop-php.git] / hphp / runtime / test / object.cpp
blob6fcf80420808bc301684834ad84bcfca73eee106
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include <gtest/gtest.h>
19 #include "hphp/runtime/base/comparisons.h"
20 #include "hphp/runtime/base/type-object.h"
21 #include "hphp/runtime/ext/string/ext_string.h"
22 #include "hphp/runtime/ext/collections/ext_collections-vector.h"
23 #include "hphp/runtime/ext/collections/ext_collections-map.h"
25 namespace HPHP {
27 TEST(Object, Serialization) {
28 String s = "O:1:\"B\":1:{s:3:\"obj\";O:1:\"A\":1:{s:1:\"a\";i:10;}}";
29 Variant v = unserialize_from_string(s, VariableUnserializer::Type::Serialize);
30 EXPECT_TRUE(v.isObject());
31 auto o = v.toObject();
32 EXPECT_TRUE(
33 !o->getClassName().asString().compare("__PHP_Incomplete_Class")
35 auto os = HHVM_FN(serialize)(o);
36 EXPECT_TRUE(
37 !os.compare( "O:1:\"B\":1:{s:3:\"obj\";O:1:\"A\":1:{s:1:\"a\";i:10;}}")
41 TEST(Object, Casts) {
42 // Test cast operations
44 EXPECT_FALSE(isa<c_Vector>(Object()));
45 EXPECT_TRUE(isa_or_null<c_Vector>(Object()));
47 auto dummy = req::make<c_Vector>();
48 Object res(dummy);
49 Object empty;
50 EXPECT_TRUE(isa<c_Vector>(res));
51 EXPECT_TRUE(isa_or_null<c_Vector>(res));
53 EXPECT_FALSE(isa<c_Map>(res));
54 EXPECT_FALSE(isa_or_null<c_Map>(res));
56 // cast tests
57 // Bad types and null pointers should throw.
58 EXPECT_EQ(cast<c_Vector>(res), dummy);
59 EXPECT_EQ(cast<ObjectData>(res), dummy);
60 try {
61 cast<c_Map>(res);
62 ADD_FAILURE();
63 } catch(...) {
64 SUCCEED();
66 try {
67 cast<c_Vector>(empty);
68 ADD_FAILURE();
69 } catch(...) {
70 SUCCEED();
73 // cast_or_null tests
74 // Bad types should throw, null pointers are ok.
75 EXPECT_EQ(cast_or_null<ObjectData>(empty), nullptr);
76 EXPECT_EQ(cast_or_null<ObjectData>(res), dummy);
78 try {
79 cast_or_null<c_Map>(res);
80 ADD_FAILURE();
81 } catch(...) {
82 SUCCEED();
85 // dyn_cast tests
86 // Bad types are ok, null pointers should throw.
87 EXPECT_EQ(dyn_cast<c_Vector>(res), dummy);
88 EXPECT_EQ(dyn_cast<ObjectData>(res), dummy);
89 EXPECT_EQ(dyn_cast<c_Map>(res), nullptr);
91 try {
92 dyn_cast<c_Vector>(empty);
93 ADD_FAILURE();
94 } catch(...) {
95 SUCCEED();
98 // dyn_cast_or_null
99 // Bad types and null pointers are ok. Should never throw.
100 EXPECT_EQ(dyn_cast_or_null<c_Map>(res), nullptr);
101 EXPECT_EQ(dyn_cast_or_null<ObjectData>(res), dummy);
102 EXPECT_EQ(dyn_cast_or_null<ObjectData>(empty), nullptr);