| 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 |
|
| 15 |
goog.provide('goog.proto2.DescriptorTest'); |
| 16 |
goog.setTestOnly('goog.proto2.DescriptorTest'); |
| 17 |
|
| 18 |
goog.require('goog.proto2.Descriptor'); |
| 19 |
goog.require('goog.proto2.Message'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
|
| 22 |
function testDescriptorConstruction() { |
| 23 |
var messageType = function() {}; |
| 24 |
var descriptor = new goog.proto2.Descriptor( |
| 25 |
messageType, {name: 'test', fullName: 'this.is.a.test'}, []); |
| 26 |
|
| 27 |
assertEquals('test', descriptor.getName()); |
| 28 |
assertEquals('this.is.a.test', descriptor.getFullName()); |
| 29 |
assertEquals(null, descriptor.getContainingType()); |
| 30 |
} |
| 31 |
|
| 32 |
function testParentDescriptor() { |
| 33 |
var parentType = function() {}; |
| 34 |
var messageType = function() {}; |
| 35 |
|
| 36 |
var parentDescriptor = new goog.proto2.Descriptor( |
| 37 |
parentType, {name: 'parent', fullName: 'this.is.a.parent'}, []); |
| 38 |
|
| 39 |
parentType.getDescriptor = function() { return parentDescriptor; }; |
| 40 |
|
| 41 |
var descriptor = new goog.proto2.Descriptor( |
| 42 |
messageType, |
| 43 |
{name: 'test', fullName: 'this.is.a.test', containingType: parentType}, |
| 44 |
[]); |
| 45 |
|
| 46 |
assertEquals(parentDescriptor, descriptor.getContainingType()); |
| 47 |
} |
| 48 |
|
| 49 |
function testStaticGetDescriptorCachesResults() { |
| 50 |
var messageType = function() {}; |
| 51 |
|
| 52 |
// This method would be provided by proto_library() BUILD rule. |
| 53 |
messageType.prototype.getDescriptor = function() { |
| 54 |
if (!messageType.descriptor_) { |
| 55 |
// The descriptor is created lazily when we instantiate a new instance. |
| 56 |
var descriptorObj = {0: {name: 'test', fullName: 'this.is.a.test'}}; |
| 57 |
messageType.descriptor_ = |
| 58 |
goog.proto2.Message.createDescriptor(messageType, descriptorObj); |
| 59 |
} |
| 60 |
return messageType.descriptor_; |
| 61 |
}; |
| 62 |
messageType.getDescriptor = messageType.prototype.getDescriptor; |
| 63 |
|
| 64 |
var descriptor = messageType.getDescriptor(); |
| 65 |
assertEquals(descriptor, messageType.getDescriptor()); // same instance |
| 66 |
} |