fielddescriptor_test.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.FieldDescriptorTest');
  15. goog.setTestOnly('goog.proto2.FieldDescriptorTest');
  16. goog.require('goog.proto2.FieldDescriptor');
  17. goog.require('goog.proto2.Message');
  18. goog.require('goog.testing.jsunit');
  19. function testFieldDescriptorConstruction() {
  20. var messageType = {};
  21. var fieldDescriptor = new goog.proto2.FieldDescriptor(messageType, 10, {
  22. name: 'test',
  23. repeated: true,
  24. packed: true,
  25. fieldType: goog.proto2.FieldDescriptor.FieldType.INT32,
  26. type: Number
  27. });
  28. assertEquals(10, fieldDescriptor.getTag());
  29. assertEquals('test', fieldDescriptor.getName());
  30. assertEquals(true, fieldDescriptor.isRepeated());
  31. assertEquals(true, fieldDescriptor.isPacked());
  32. assertEquals(
  33. goog.proto2.FieldDescriptor.FieldType.INT32,
  34. fieldDescriptor.getFieldType());
  35. assertEquals(Number, fieldDescriptor.getNativeType());
  36. assertEquals(0, fieldDescriptor.getDefaultValue());
  37. }
  38. function testGetDefaultValueOfString() {
  39. var fieldDescriptor = new goog.proto2.FieldDescriptor({}, 10, {
  40. name: 'test',
  41. fieldType: goog.proto2.FieldDescriptor.FieldType.STRING,
  42. type: String
  43. });
  44. assertEquals('', fieldDescriptor.getDefaultValue());
  45. }
  46. function testGetDefaultValueOfBool() {
  47. var fieldDescriptor = new goog.proto2.FieldDescriptor({}, 10, {
  48. name: 'test',
  49. fieldType: goog.proto2.FieldDescriptor.FieldType.BOOL,
  50. type: Boolean
  51. });
  52. assertEquals(false, fieldDescriptor.getDefaultValue());
  53. }
  54. function testGetDefaultValueOfInt64() {
  55. var fieldDescriptor = new goog.proto2.FieldDescriptor({}, 10, {
  56. name: 'test',
  57. fieldType: goog.proto2.FieldDescriptor.FieldType.INT64,
  58. type: String
  59. });
  60. assertEquals('0', fieldDescriptor.getDefaultValue());
  61. }
  62. function testRepeatedField() {
  63. var messageType = {};
  64. var fieldDescriptor = new goog.proto2.FieldDescriptor(
  65. messageType, 10,
  66. {name: 'test', repeated: true, fieldType: 7, type: Number});
  67. assertEquals(true, fieldDescriptor.isRepeated());
  68. assertEquals(false, fieldDescriptor.isRequired());
  69. assertEquals(false, fieldDescriptor.isOptional());
  70. }
  71. function testRequiredField() {
  72. var messageType = {};
  73. var fieldDescriptor = new goog.proto2.FieldDescriptor(
  74. messageType, 10,
  75. {name: 'test', required: true, fieldType: 7, type: Number});
  76. assertEquals(false, fieldDescriptor.isRepeated());
  77. assertEquals(true, fieldDescriptor.isRequired());
  78. assertEquals(false, fieldDescriptor.isOptional());
  79. }
  80. function testOptionalField() {
  81. var messageType = {};
  82. var fieldDescriptor = new goog.proto2.FieldDescriptor(
  83. messageType, 10, {name: 'test', fieldType: 7, type: Number});
  84. assertEquals(false, fieldDescriptor.isRepeated());
  85. assertEquals(false, fieldDescriptor.isRequired());
  86. assertEquals(true, fieldDescriptor.isOptional());
  87. }
  88. function testContaingType() {
  89. var MessageType = function() { MessageType.base(this, 'constructor'); };
  90. goog.inherits(MessageType, goog.proto2.Message);
  91. MessageType.getDescriptor = function() {
  92. if (!MessageType.descriptor_) {
  93. // The descriptor is created lazily when we instantiate a new instance.
  94. var descriptorObj = {
  95. 0: {name: 'test_message', fullName: 'this.is.a.test_message'},
  96. 10: {name: 'test', fieldType: 7, type: Number}
  97. };
  98. MessageType.descriptor_ =
  99. goog.proto2.Message.createDescriptor(MessageType, descriptorObj);
  100. }
  101. return MessageType.descriptor_;
  102. };
  103. MessageType.prototype.getDescriptor = MessageType.getDescriptor;
  104. var descriptor = MessageType.getDescriptor();
  105. var fieldDescriptor = descriptor.getFields()[0];
  106. assertEquals('10', fieldDescriptor.getTag());
  107. assertEquals(descriptor, fieldDescriptor.getContainingType());
  108. }