def test_bad_zero_name(self): pair_list = self.get_test_enum().pair_list del pair_list[0] pair_list.append(schema.EnumPair('TEST_ENUM_BAD_ZERO', 0, '')) self.assert_invalid(enum_value_name_validator.EnumValueNameValidator, 'enum zero value must be named' '.*TEST_ENUM_UNSPECIFIED')
def test_enum_pair_added_without_min_version(self): added_pair = schema.EnumPair('new_pair', 1000, '') self.get_test_enum().pair_list.append(added_pair) self.assert_invalid(added_object_validator.AddedObjectValidator, self.NEW_OBJECT_ADDED_ERR_MSG)
def link_enum(self, enum): """Parses the protobuf enum descriptor turns into a Google Weave Enum. Args: enum: A protobuf enum description to add to schema Raises: InvalidType: Encountered an invalid type while linking DuplicateObject: Duplicate enum value pair encountered InvalidUsage: Option used in an invalid way """ options = enum.desc.options.Extensions[wdl_options_pb2.enumopts] enum_pairs = enum.pair_list if self.is_common(enum.full_name): self.link_common_enum(enum) elif (not enum.desc.parent or not isinstance( self.get_obj(enum.desc.parent.full_name), schema.StructEnumCollectionBase)): raise exception.InvalidType( ('Unexpected enum %s defined outside a typespace or trait' % enum)) if options is not None: if options.compatibility.HasField('min_version'): enum.min_version = options.compatibility.min_version if options.compatibility.HasField('max_version'): enum.max_version = options.compatibility.max_version for value in enum.desc.values.values(): description = self.parse_comments(value) pair_opts = value.options.Extensions[wdl_options_pb2.enumvalue] value.full_name.replace( inflection.underscore(enum.base_name).decode('utf-8').upper() + '_', '') enum_pair = schema.EnumPair(value.full_name, value.number, description) enum_pair.source_file = enum.source_file if pair_opts is not None: if pair_opts.compatibility.HasField('min_version'): enum_pair.min_version = pair_opts.compatibility.min_version if pair_opts.compatibility.HasField('max_version'): enum_pair.max_version = pair_opts.compatibility.max_version try: enum_pairs.append(enum_pair) except exception.DuplicateObject: pass enum.is_bitmask = options.bitmask if options.extends: raise exception.InvalidUsage('Extending enums is not yet supported.')
def test_bad_prefix(self): self.get_test_enum().pair_list.append( schema.EnumPair('BAD_PREFIX_THREE', 3, '')) self.assert_invalid(enum_value_name_validator.EnumValueNameValidator, 'expected name to start with.*TEST_ENUM_')
def gen_test_schema(self): schema_obj = schema.Schema() vendor = schema.Vendor('test', 0xfe00, '') schema_obj.vendor_list.append(vendor) resource = schema.Resource('TestResource', 0x0001, '') resource.file = schema.File('test_resource', 1, '') resource.version = 2 vendor.resource_list.append(resource) iface = schema.Interface('TestIface', 0x0001, '') iface.file = schema.File('test_iface', 1, '') vendor.interface_list.append(iface) trait = schema.Trait('TestTrait', 1, '') trait.file = schema.File('test_trait', 1, '') vendor.trait_list.append(trait) typespace = schema.Typespace('TestTypespace', 1, '') typespace.file = schema.File('test_typespace', 1, '') vendor.typespace_list.append(typespace) trait.version = 2 trait.stability = schema.Stability.ALPHA field = schema.Field('test_field', 1, '', schema.Field.DataType.INT32, None) trait.state_list.append(field) resource_component = schema.ResourceComponent('test_component', 1, '') resource_component.trait = trait resource.component_list.append(resource_component) interface_component = schema.InterfaceComponent( 'test_component', 1, '') interface_component.trait = trait iface.component_list.append(interface_component) group = schema.Group('test_group', 1, '') group.interface = iface resource.group_list.append(group) struct = schema.Struct('TestStruct', 1, '') trait.struct_list.append(struct) enum = schema.Enum('TestEnum', 1, '') enum.pair_list.append(schema.EnumPair('TEST_ENUM_UNSPECIFIED', 0, '')) enum.pair_list.append(schema.EnumPair('TEST_ENUM_ONE', 1, '')) enum.pair_list.append(schema.EnumPair('TEST_ENUM_TWO', 2, '')) trait.enum_list.append(enum) event = schema.Event('TestEvent', 1, '') event.field_list.append(field) trait.event_list.append(event) response = schema.CommandResponse('TestRequestResponse', 1, '') response.field_list.append(field) command = schema.Command('TestRequest', 1, '') command.parameter_list.append(field) trait.command_list.append(command) command.response = response response.parent = command return schema_obj