descriptor_test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.proto2.DescriptorTest');
  15. goog.setTestOnly('goog.proto2.DescriptorTest');
  16. goog.require('goog.proto2.Descriptor');
  17. goog.require('goog.proto2.Message');
  18. goog.require('goog.testing.jsunit');
  19. function testDescriptorConstruction() {
  20. var messageType = function() {};
  21. var descriptor = new goog.proto2.Descriptor(
  22. messageType, {name: 'test', fullName: 'this.is.a.test'}, []);
  23. assertEquals('test', descriptor.getName());
  24. assertEquals('this.is.a.test', descriptor.getFullName());
  25. assertEquals(null, descriptor.getContainingType());
  26. }
  27. function testParentDescriptor() {
  28. var parentType = function() {};
  29. var messageType = function() {};
  30. var parentDescriptor = new goog.proto2.Descriptor(
  31. parentType, {name: 'parent', fullName: 'this.is.a.parent'}, []);
  32. parentType.getDescriptor = function() { return parentDescriptor; };
  33. var descriptor = new goog.proto2.Descriptor(
  34. messageType,
  35. {name: 'test', fullName: 'this.is.a.test', containingType: parentType},
  36. []);
  37. assertEquals(parentDescriptor, descriptor.getContainingType());
  38. }
  39. function testStaticGetDescriptorCachesResults() {
  40. var messageType = function() {};
  41. // This method would be provided by proto_library() BUILD rule.
  42. messageType.prototype.getDescriptor = function() {
  43. if (!messageType.descriptor_) {
  44. // The descriptor is created lazily when we instantiate a new instance.
  45. var descriptorObj = {0: {name: 'test', fullName: 'this.is.a.test'}};
  46. messageType.descriptor_ =
  47. goog.proto2.Message.createDescriptor(messageType, descriptorObj);
  48. }
  49. return messageType.descriptor_;
  50. };
  51. messageType.getDescriptor = messageType.prototype.getDescriptor;
  52. var descriptor = messageType.getDescriptor();
  53. assertEquals(descriptor, messageType.getDescriptor()); // same instance
  54. }