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

Side by Side Diff: runtime/vm/object_test.cc

Issue 517243003: Add unit tests for the JSON format of several of our object types. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 3 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | 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
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "platform/globals.h" 5 #include "platform/globals.h"
6 6
7 #include "vm/assembler.h" 7 #include "vm/assembler.h"
8 #include "vm/bigint_operations.h" 8 #include "vm/bigint_operations.h"
9 #include "vm/class_finalizer.h" 9 #include "vm/class_finalizer.h"
10 #include "vm/dart_api_impl.h" 10 #include "vm/dart_api_impl.h"
(...skipping 4196 matching lines...) Expand 10 before | Expand all | Expand 10 after
4207 ObjectAccumulator acc(&objects); 4207 ObjectAccumulator acc(&objects);
4208 heap->IterateObjects(&acc); 4208 heap->IterateObjects(&acc);
4209 for (intptr_t i = 0; i < objects.length(); ++i) { 4209 for (intptr_t i = 0; i < objects.length(); ++i) {
4210 JSONStream js; 4210 JSONStream js;
4211 objects[i]->PrintJSON(&js, false); 4211 objects[i]->PrintJSON(&js, false);
4212 EXPECT_SUBSTRING("\"type\":", js.ToCString()); 4212 EXPECT_SUBSTRING("\"type\":", js.ToCString());
4213 } 4213 }
4214 } 4214 }
4215 4215
4216 4216
4217 // Often the JSON output contains values like:
4218 //
4219 // "id":"classes/46"
4220 //
4221 // That are very fragile. We use the stripValue function to strip
4222 // these from the JSON output before testing it. Like so:
4223 //
4224 // "id":""
4225 //
4226 // This function is written to work even if 'in' and 'out' refer to
4227 // the same string.
4228 static void stripValue(const char* value, const char* in, char* out) {
Cutch 2014/09/03 17:12:16 value is really the prefix of the value right? yo
4229 char* pos = strstr(in, value);
4230 while (pos != NULL) {
4231 // Copy up to pos into the output buffer.
4232 while (in < pos) {
4233 *out++ = *in++;
4234 }
4235
4236 // Skip to the close quote.
4237 in += strcspn(in, "\"");
4238 pos = strstr(in, value);
4239 }
4240 // Copy the remainder of in to out.
4241 while (*in != '\0') {
4242 *out++ = *in++;
4243 }
4244 *out = '\0';
4245 }
4246
4247
4248 TEST_CASE(PrintJSONPrimitives) {
4249 char buffer[1024];
4250 Isolate* isolate = Isolate::Current();
4251
4252 // Class reference
4253 {
4254 JSONStream js;
4255 Class& cls = Class::Handle(isolate->object_store()->bool_class());
4256 cls.PrintJSON(&js, true);
4257 stripValue("classes", js.ToCString(), buffer);
4258 EXPECT_STREQ(
4259 "{\"type\":\"@Class\",\"id\":\"\",\"name\":\"bool\"}",
4260 buffer);
4261 }
4262 // Function reference
4263 {
4264 JSONStream js;
4265 Class& cls = Class::Handle(isolate->object_store()->bool_class());
4266 const String& func_name = String::Handle(String::New("toString"));
4267 Function& func = Function::Handle(cls.LookupFunction(func_name));
4268 ASSERT(!func.IsNull());
4269 func.PrintJSON(&js, true);
4270 stripValue("classes", js.ToCString(), buffer);
4271 EXPECT_STREQ(
4272 "{\"type\":\"@Function\",\"id\":\"\",\"name\":\"toString\","
4273 "\"owningClass\":{\"type\":\"@Class\",\"id\":\"\",\"name\":\"bool\"},"
4274 "\"kind\":\"kRegularFunction\"}",
4275 buffer);
4276 }
4277 // Library reference
4278 {
4279 JSONStream js;
4280 Library& lib = Library::Handle(isolate->object_store()->core_library());
4281 lib.PrintJSON(&js, true);
4282 stripValue("libraries", js.ToCString(), buffer);
4283 EXPECT_STREQ(
4284 "{\"type\":\"@Library\",\"id\":\"\",\"name\":\"dart.core\","
4285 "\"url\":\"dart:core\"}",
4286 buffer);
4287 }
4288 // Bool reference
4289 {
4290 JSONStream js;
4291 Bool::True().PrintJSON(&js, true);
4292 stripValue("classes", js.ToCString(), buffer);
4293 EXPECT_STREQ("{\"type\":\"@Bool\",\"id\":\"objects\\/bool-true\","
4294 "\"class\":{\"type\":\"@Class\",\"id\":\"\","
4295 "\"name\":\"bool\"},\"valueAsString\":\"true\"}",
4296 buffer);
4297 }
4298 // Smi reference
4299 {
4300 JSONStream js;
4301 const Integer& smi = Integer::Handle(Integer::New(7));
4302 smi.PrintJSON(&js, true);
4303 stripValue("classes", js.ToCString(), buffer);
4304 stripValue("_Smi@", buffer, buffer);
4305 EXPECT_STREQ(
4306 "{\"type\":\"@Smi\","
4307 "\"class\":{\"type\":\"@Class\",\"id\":\"\",\"name\":\"_Smi\","
4308 "\"vmName\":\"\"},"
4309 "\"id\":\"objects\\/int-7\",\"valueAsString\":\"7\"}",
4310 buffer);
4311 }
4312 // Mint reference
4313 {
4314 JSONStream js;
4315 const Integer& smi = Integer::Handle(Integer::New(Mint::kMinValue));
Cutch 2014/09/03 17:12:16 This might not work on 64-bit platforms. Build and
4316 smi.PrintJSON(&js, true);
4317 stripValue("classes", js.ToCString(), buffer);
4318 stripValue("objects", buffer, buffer);
4319 stripValue("_Mint@", buffer, buffer);
4320 EXPECT_STREQ(
4321 "{\"type\":\"@Mint\","
4322 "\"class\":{\"type\":\"@Class\",\"id\":\"\",\"name\":\"_Mint\","
4323 "\"vmName\":\"\"},"
4324 "\"id\":\"\",\"valueAsString\":\"-9223372036854775808\"}",
4325 buffer);
4326 }
4327 // Bigint reference
4328 {
4329 JSONStream js;
4330 const String& bigint_str =
4331 String::Handle(String::New("44444444444444444444444444444444"));
4332 const Integer& bigint = Integer::Handle(Integer::New(bigint_str));
4333 bigint.PrintJSON(&js, true);
4334 stripValue("classes", js.ToCString(), buffer);
4335 stripValue("objects", buffer, buffer);
4336 stripValue("_Bigint@", buffer, buffer);
4337 EXPECT_STREQ(
4338 "{\"type\":\"@Bigint\","
4339 "\"class\":{\"type\":\"@Class\",\"id\":\"\",\"name\":\"_Bigint\","
4340 "\"vmName\":\"\"},"
4341 "\"id\":\"\",\"valueAsString\":\"44444444444444444444444444444444\"}",
4342 buffer);
4343 }
4344 // Double reference
4345 {
4346 JSONStream js;
4347 const Double& dub = Double::Handle(Double::New(0.1234));
4348 dub.PrintJSON(&js, true);
4349 stripValue("classes", js.ToCString(), buffer);
4350 stripValue("objects", buffer, buffer);
4351 stripValue("_Double@", buffer, buffer);
4352 EXPECT_STREQ(
4353 "{\"type\":\"@Double\","
4354 "\"class\":{\"type\":\"@Class\",\"id\":\"\",\"name\":\"_Double\","
4355 "\"vmName\":\"\"},"
4356 "\"id\":\"\",\"valueAsString\":\"0.1234\"}",
4357 buffer);
4358 }
4359 // String reference
4360 {
4361 JSONStream js;
4362 const String& str = String::Handle(String::New("dw"));
4363 str.PrintJSON(&js, true);
4364 stripValue("classes", js.ToCString(), buffer);
4365 stripValue("objects", buffer, buffer);
4366 stripValue("_OneByteString@", buffer, buffer);
4367 EXPECT_STREQ(
4368 "{\"type\":\"@String\","
4369 "\"class\":{\"type\":\"@Class\",\"id\":\"\","
4370 "\"name\":\"_OneByteString\",\"vmName\":\"\"},"
4371 "\"id\":\"\",\"valueAsString\":\"\\\"dw\\\"\"}",
4372 buffer);
4373 }
4374 // Array reference
4375 {
4376 JSONStream js;
4377 const Array& array = Array::Handle(Array::New(0));
4378 array.PrintJSON(&js, true);
4379 stripValue("classes", js.ToCString(), buffer);
4380 stripValue("objects", buffer, buffer);
4381 stripValue("_List@", buffer, buffer);
4382 EXPECT_STREQ(
4383 "{\"type\":\"@Array\","
4384 "\"class\":{\"type\":\"@Class\",\"id\":\"\",\"name\":\"_List\","
4385 "\"vmName\":\"\"},"
4386 "\"id\":\"\",\"length\":0}",
4387 buffer);
4388 }
4389 // GrowableObjectArray reference
4390 {
4391 JSONStream js;
4392 const GrowableObjectArray& array =
4393 GrowableObjectArray::Handle(GrowableObjectArray::New());
4394 array.PrintJSON(&js, true);
4395 stripValue("classes", js.ToCString(), buffer);
4396 stripValue("objects", buffer, buffer);
4397 stripValue("_GrowableList@", buffer, buffer);
4398 EXPECT_STREQ(
4399 "{\"type\":\"@GrowableObjectArray\","
4400 "\"class\":{\"type\":\"@Class\",\"id\":\"\",\"name\":\"_GrowableList\","
4401 "\"vmName\":\"\"},\"id\":\"\",\"length\":0}",
4402 buffer);
4403 }
4404 // LinkedHashMap reference
4405 {
4406 JSONStream js;
4407 const LinkedHashMap& array = LinkedHashMap::Handle(LinkedHashMap::New());
4408 array.PrintJSON(&js, true);
4409 stripValue("classes", js.ToCString(), buffer);
4410 stripValue("objects", buffer, buffer);
4411 stripValue("_InternalLinkedHashMap@", buffer, buffer);
4412 EXPECT_STREQ(
4413 "{\"type\":\"@LinkedHashMap\","
4414 "\"class\":{\"type\":\"@Class\",\"id\":\"\","
4415 "\"name\":\"_InternalLinkedHashMap\",\"vmName\":\"\"},\"id\":\"\"}",
4416 buffer);
4417 }
4418 // UserTag reference
4419 {
4420 JSONStream js;
4421 Instance& tag = Instance::Handle(isolate->object_store()->default_tag());
4422 tag.PrintJSON(&js, true);
4423 stripValue("classes", js.ToCString(), buffer);
4424 stripValue("objects", buffer, buffer);
4425 stripValue("_UserTag@", buffer, buffer);
4426 EXPECT_STREQ(
4427 "{\"type\":\"@UserTag\","
4428 "\"class\":{\"type\":\"@Class\",\"id\":\"\",\"name\":\"_UserTag\","
4429 "\"vmName\":\"\"},"
4430 "\"id\":\"\"}",
4431 buffer);
4432 }
4433 // Type reference
4434 // TODO(turnidge): Add in all of the other Type siblings.
4435 {
4436 JSONStream js;
4437 Instance& type = Instance::Handle(isolate->object_store()->bool_type());
4438 type.PrintJSON(&js, true);
4439 stripValue("classes", js.ToCString(), buffer);
4440 stripValue("objects", buffer, buffer);
4441 stripValue("_Type@", buffer, buffer);
4442 EXPECT_STREQ(
4443 "{\"type\":\"@Type\","
4444 "\"class\":{\"type\":\"@Class\",\"id\":\"\",\"name\":\"_Type\","
4445 "\"vmName\":\"\"},\"id\":\"\","
4446 "\"typeClass\":{\"type\":\"@Class\",\"id\":\"\",\"name\":\"bool\"},"
4447 "\"name\":\"bool\"}",
4448 buffer);
4449 }
4450 // Null reference
4451 {
4452 JSONStream js;
4453 Object::null_object().PrintJSON(&js, true);
4454 EXPECT_STREQ(
4455 "{\"type\":\"@Null\",\"id\":\"objects\\/null\","
4456 "\"valueAsString\":\"null\"}",
4457 js.ToCString());
4458 }
4459 // Sentinel reference
4460 {
4461 JSONStream js;
4462 Object::sentinel().PrintJSON(&js, true);
4463 EXPECT_STREQ(
4464 "{\"type\":\"Sentinel\",\"id\":\"objects\\/not-initialized\","
4465 "\"valueAsString\":\"<not initialized>\"}",
4466 js.ToCString());
4467 }
4468 // Transition sentinel reference
4469 {
4470 JSONStream js;
4471 Object::transition_sentinel().PrintJSON(&js, true);
4472 EXPECT_STREQ(
4473 "{\"type\":\"Sentinel\",\"id\":\"objects\\/being-initialized\","
4474 "\"valueAsString\":\"<being initialized>\"}",
4475 js.ToCString());
4476 }
4477 // LiteralToken reference. This is meant to be an example of a
4478 // "weird" type that isn't usually returned by the VM Service except
4479 // when we are doing direct heap inspection.
4480 {
4481 JSONStream js;
4482 LiteralToken& tok = LiteralToken::Handle(LiteralToken::New());
4483 tok.PrintJSON(&js, true);
4484 stripValue("objects", js.ToCString(), buffer);
4485 EXPECT_STREQ(
4486 "{\"type\":\"@LiteralToken\",\"id\":\"\"}",
4487 buffer);
4488 }
4489 }
4490
4491
4217 TEST_CASE(InstanceEquality) { 4492 TEST_CASE(InstanceEquality) {
4218 // Test that Instance::OperatorEquals can call a user-defined operator==. 4493 // Test that Instance::OperatorEquals can call a user-defined operator==.
4219 const char* kScript = 4494 const char* kScript =
4220 "class A {\n" 4495 "class A {\n"
4221 " bool operator==(A other) { return true; }\n" 4496 " bool operator==(A other) { return true; }\n"
4222 "}\n" 4497 "}\n"
4223 "main() {\n" 4498 "main() {\n"
4224 " A a = new A();\n" 4499 " A a = new A();\n"
4225 "}"; 4500 "}";
4226 4501
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
4259 EXPECT_VALID(h_result); 4534 EXPECT_VALID(h_result);
4260 Integer& result = Integer::Handle(); 4535 Integer& result = Integer::Handle();
4261 result ^= Api::UnwrapHandle(h_result); 4536 result ^= Api::UnwrapHandle(h_result);
4262 String& foo = String::Handle(String::New("foo")); 4537 String& foo = String::Handle(String::New("foo"));
4263 Integer& expected = Integer::Handle(); 4538 Integer& expected = Integer::Handle();
4264 expected ^= foo.HashCode(); 4539 expected ^= foo.HashCode();
4265 EXPECT(result.IsIdenticalTo(expected)); 4540 EXPECT(result.IsIdenticalTo(expected));
4266 } 4541 }
4267 4542
4268 } // namespace dart 4543 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698