Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: gin/data_object_builder_unittest.cc

Issue 2845463002: gin: Create a DataObjectBuilder class to help create simple data objects. (Closed)
Patch Set: devlin Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« gin/data_object_builder.h ('K') | « gin/data_object_builder.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "gin/data_object_builder.h"
6
7 #include "gin/dictionary.h"
8 #include "gin/public/isolate_holder.h"
9 #include "gin/test/v8_test.h"
10
11 namespace gin {
12 namespace {
13
14 using DataObjectBuilderTest = V8Test;
15
16 // It should create ordinary data properties.
17 TEST_F(DataObjectBuilderTest, CreatesDataProperties) {
18 v8::Isolate* isolate = instance_->isolate();
19 v8::HandleScope handle_scope(isolate);
20 v8::Local<v8::Context> context = context_.Get(isolate);
21
22 v8::Local<v8::Object> object =
23 DataObjectBuilder(isolate).Set("key", 42).Finish();
24 ASSERT_TRUE(object->HasOwnProperty(context, StringToSymbol(isolate, "key"))
25 .ToChecked());
26
27 v8::Local<v8::Value> descriptor_object;
28 ASSERT_TRUE(
29 object->GetOwnPropertyDescriptor(context, StringToSymbol(isolate, "key"))
30 .ToLocal(&descriptor_object));
31 gin::Dictionary descriptor(isolate, descriptor_object.As<v8::Object>());
32
33 int32_t value = 0;
34 ASSERT_TRUE(descriptor.Get("value", &value));
35 EXPECT_EQ(42, value);
36
37 bool writable = false;
38 ASSERT_TRUE(descriptor.Get("writable", &writable));
39 EXPECT_TRUE(writable);
40
41 bool enumerable = false;
42 ASSERT_TRUE(descriptor.Get("enumerable", &enumerable));
43 EXPECT_TRUE(enumerable);
44
45 bool configurable = false;
46 ASSERT_TRUE(descriptor.Get("configurable", &configurable));
47 EXPECT_TRUE(configurable);
48 }
49
50 // It should not invoke setters on the prototype chain.
51 TEST_F(DataObjectBuilderTest, DoesNotInvokeSetters) {
52 v8::Isolate* isolate = instance_->isolate();
53 v8::HandleScope handle_scope(isolate);
54 v8::Local<v8::Context> context = context_.Get(isolate);
55
56 // Install a setter on the object prototype.
57 v8::Local<v8::Value> object_constructor;
58 ASSERT_TRUE(context->Global()
59 ->Get(context, StringToSymbol(isolate, "Object"))
60 .ToLocal(&object_constructor));
61 v8::Local<v8::Value> object_prototype;
62 ASSERT_TRUE(object_constructor.As<v8::Function>()
63 ->Get(context, StringToSymbol(isolate, "prototype"))
64 .ToLocal(&object_prototype));
65 ASSERT_TRUE(
66 object_prototype.As<v8::Object>()
67 ->SetAccessor(context, StringToSymbol(isolate, "key"),
68 [](v8::Local<v8::Name>,
69 const v8::PropertyCallbackInfo<v8::Value>&) {},
70 [](v8::Local<v8::Name>, v8::Local<v8::Value>,
71 const v8::PropertyCallbackInfo<void>&) {
72 ADD_FAILURE() << "setter should not be invoked";
73 })
74 .ToChecked());
75
76 // Create an object.
77 DataObjectBuilder(isolate).Set("key", 42).Finish();
78 }
79
80 // The internal handle is cleared when the builder is finished.
81 // This makes the class harder to abuse, so that its methods cannot be used
82 // after something may have modified the object in unexpected ways.
83 TEST_F(DataObjectBuilderTest, ReleasesOnFinish) {
84 v8::Isolate* isolate = instance_->isolate();
85 v8::HandleScope handle_scope(isolate);
86
87 DataObjectBuilder builder(isolate);
88 EXPECT_FALSE(builder.Finish().IsEmpty());
89 EXPECT_TRUE(builder.Finish().IsEmpty());
90 }
91
92 // As is the normal behaviour of CreateDataProperty, new data properties should
93 // replace existing ones. Since no non-configurable ones are present, nor should
94 // the object be non-extensible, this should work.
95 TEST_F(DataObjectBuilderTest, ReplacesExistingProperties) {
96 v8::Isolate* isolate = instance_->isolate();
97 v8::HandleScope handle_scope(isolate);
98
99 v8::Local<v8::Object> object =
100 DataObjectBuilder(isolate).Set("value", 42).Set("value", 55).Finish();
101
102 gin::Dictionary dictionary(isolate, object);
103 int32_t value;
104 ASSERT_TRUE(dictionary.Get("value", &value));
105 EXPECT_EQ(55, value);
106 }
107
108 } // namespace
109 } // namespace gin
OLDNEW
« gin/data_object_builder.h ('K') | « gin/data_object_builder.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698