Skip to content

Commit

Permalink
Update a few more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescourtney committed Feb 20, 2024
1 parent 5c2c47c commit 87283bd
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 27 deletions.
9 changes: 7 additions & 2 deletions src/FlatSharp.Compiler/SchemaModel/ValueUnionSchemaModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ protected override void OnWriteCode(CodeWriter writer, CompileContext context)
writer.AppendLine($"if (this.Discriminator != {item.value.Value})");
using (writer.WithBlock())
{
writer.AppendLine($"{typeof(FSThrow).GGCTN()}.{nameof(FSThrow.InvalidOperation)}(\"Union Discriminator != {item.value.Value}\");");
writer.AppendLine($"{typeof(FSThrow).GGCTN()}.{nameof(FSThrow.InvalidOperation_UnionIsNotOfType)}();");
}

writer.AppendLine($"return this.UncheckedGetItem{item.value.Value}();");
Expand Down Expand Up @@ -257,7 +257,12 @@ private void WriteAcceptMethod(
writer.AppendLine($"case {index}: return visitor.Visit(this.UncheckedGetItem{item.value.Value}());");
}

writer.AppendLine($"default: return {typeof(FSThrow).GGCTN()}.{nameof(FSThrow.InvalidOperation)}<TReturn>(\"Unexpected discriminator\");");
writer.AppendLine($"default:");
using (writer.IncreaseIndent())
{
writer.AppendLine($"{typeof(FSThrow).GGCTN()}.{nameof(FSThrow.InvalidOperation_InvalidUnionDiscriminator)}<{this.Name}>(disc);");
writer.AppendLine("return default(TReturn);");
}
}
}
}
Expand Down
23 changes: 20 additions & 3 deletions src/FlatSharp.Runtime/FSThrow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,25 @@ public static class FSThrow
#region InvalidOperation

[DoesNotReturn]
public static void InvalidOperation(string message) => throw new InvalidOperationException(message);
public static void InvalidOperation(string message)
=> throw new InvalidOperationException(message);

[DoesNotReturn]
public static void InvalidOperation_SizeNotMultipleOfAlignment(Type elementType, int size, int alignment)
=> throw new InvalidOperationException($"Type '{elementType.FullName}' does not support Unsafe Span operations because the size ({size}) is not a multiple of the alignment ({alignment}).");

[DoesNotReturn]
public static void InvalidOperation_RequiredPropertyNotSet(string propertyName)
=> throw new InvalidOperationException($"Table property '{propertyName}' is marked as required, but was not set.");

[DoesNotReturn]
public static T InvalidOperation_InvalidUnionDiscriminator<T>(byte discriminator)
=> throw new InvalidOperationException($"Unexpected union discriminator value '{discriminator}' for Union {typeof(T).FullName}");

[DoesNotReturn]
public static void InvalidOperation_UnionIsNotOfType()
=> throw new InvalidOperationException("The union is not of the requested type.");

[DoesNotReturn]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T InvalidOperation<T>(string message)
Expand All @@ -43,11 +56,11 @@ public static T InvalidOperation<T>(string message)
#region InvalidData

[DoesNotReturn]
public static void InvalidData_UOffsetTooSmall(uint uoffset)
public static void InvalidData_UOffsetTooSmall(uint uoffset)
=> throw new InvalidDataException($"FlatBuffer was in an invalid format: Decoded uoffset_t had value less than {sizeof(uint)}. Value = {uoffset}");

[DoesNotReturn]
public static void InvalidData_VTableTooShort()
public static void InvalidData_VTableTooShort()
=> throw new InvalidDataException("FlatBuffer was in an invalid format: VTable was not long enough to be valid.");

[DoesNotReturn]
Expand All @@ -58,6 +71,10 @@ public static void InvalidData_InvalidNull()
public static void InvalidData_DepthLimit()
=> throw new InvalidDataException("FlatSharp passed the configured depth limit when deserializing. This can be configured with 'IGeneratedSerializer.WithSettings'.");

[DoesNotReturn]
public static void InvalidData_RequiredPropertyNotSet(string propertyName)
=> throw new InvalidDataException($"Table property '{propertyName}' is marked as required, but was missing from the buffer.");

[DoesNotReturn]
public static void InvalidData(string message) => throw new InvalidDataException(message);

Expand Down
3 changes: 1 addition & 2 deletions src/FlatSharp/TypeModel/TableMemberModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@ private string GetNotPresentStatement()
{
if (this.IsRequired)
{
string message = $"Table property '{this.FriendlyName}' is marked as required, but was missing from the buffer.";
return $"{typeof(FSThrow).GGCTN()}.{nameof(FSThrow.InvalidData)}(\"{message}\");";
return $"{typeof(FSThrow).GGCTN()}.{nameof(FSThrow.InvalidData_RequiredPropertyNotSet)}(\"{this.FriendlyName}\");";
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/FlatSharp/TypeModel/TableTypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ private string GetPrepareSerializeBlock(
elseBlock =
$@"else
{{
{typeof(FSThrow).GGCTN()}.{nameof(FSThrow.InvalidOperation)}(""Table property '{memberModel.FriendlyName}' is marked as required, but was not set."");
{typeof(FSThrow).GGCTN()}.{nameof(FSThrow.InvalidOperation_RequiredPropertyNotSet)}(""{memberModel.FriendlyName}"");
}}
";
}
Expand Down
28 changes: 19 additions & 9 deletions src/FlatSharp/TypeModel/UnionTypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ public override CodeGeneratedMethod CreateGetMaxSizeMethodBody(GetMaxSizeCodeGen

string body =
$@"
switch ({context.ValueVariableName}.{discriminatorPropertyName})
byte discriminator = {context.ValueVariableName}.{discriminatorPropertyName};
switch (discriminator)
{{
{string.Join("\r\n", switchCases)}
default:
return {typeof(FSThrow).GGCTN()}.{nameof(FSThrow.InvalidOperation)}<int>(""Exception determining type of union. Unexpected Union discriminator."");
{typeof(FSThrow).GGCTN()}.{nameof(FSThrow.InvalidOperation_InvalidUnionDiscriminator)}<{this.GGCTN()}>(discriminator);
return 0;
}}
";
return new CodeGeneratedMethod(body);
Expand Down Expand Up @@ -157,7 +159,8 @@ public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext c
{{
{string.Join("\r\n", switchCases)}
default:
return {typeof(FSThrow).GGCTN()}.{nameof(FSThrow.InvalidOperation)}<{this.GGCTN()}>(""Exception parsing union '{this.GetCompilableTypeName()}'. Unexpected union discriminator."");
{typeof(FSThrow).GGCTN()}.{nameof(FSThrow.InvalidOperation_InvalidUnionDiscriminator)}<{this.GGCTN()}>(discriminator);
return default({this.GGCTN()});
}}
";

Expand Down Expand Up @@ -305,7 +308,7 @@ public override CodeGeneratedMethod CreateSerializeMethodBody(SerializationCodeG
{{
{string.Join("\r\n", switchCases)}
default:
{typeof(FSThrow).GGCTN()}.{nameof(FSThrow.InvalidOperation)}(""Unexpected union discriminator. Unions must be initialized."");
{typeof(FSThrow).GGCTN()}.{nameof(FSThrow.InvalidOperation_InvalidUnionDiscriminator)}<{this.GGCTN()}>(discriminatorValue);
break;
}}";

Expand All @@ -320,15 +323,22 @@ public override CodeGeneratedMethod CreateCloneMethodBody(CloneCodeGenContext co
{
int discriminator = i + 1;
string cloneMethod = context.MethodNameMap[this.memberTypeModels[i].ClrType];
switchCases.Add($"{discriminator} => new {this.GetGlobalCompilableTypeName()}({cloneMethod}({context.ItemVariableName}.Item{discriminator})),");
switchCases.Add($@"
case {discriminator}:
return new {this.GetGlobalCompilableTypeName()}({cloneMethod}({context.ItemVariableName}.Item{discriminator}));
");
}

switchCases.Add($"_ => {typeof(FSThrow).GGCTN()}.{nameof(FSThrow.InvalidOperation)}<{this.GGCTN()}>(\"Unexpected union discriminator\")");

string body = $@"
return {context.ItemVariableName}.{nameof(IFlatBufferUnion.Discriminator)} switch {{
byte discriminator = {context.ItemVariableName}.{nameof(IFlatBufferUnion.Discriminator)};
switch (discriminator)
{{
{string.Join("\r\n", switchCases)}
}};";
default:
{typeof(FSThrow).GGCTN()}.{nameof(FSThrow.InvalidOperation_InvalidUnionDiscriminator)}<{this.GGCTN()}>(discriminator);
return default({this.GGCTN()});
}}
";

return new CodeGeneratedMethod(body);
}
Expand Down
22 changes: 13 additions & 9 deletions src/Tests/Stryker/Tests/UnionFieldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ public void InvalidGetters()
FunUnion a = new FunUnion(new RefStruct());

var ex = Assert.Throws<InvalidOperationException>(() => a.ValueStruct);
Assert.Equal("Union Discriminator != 2", ex.Message);
Assert.Equal("The union is not of the requested type.", ex.Message);

ex = Assert.Throws<InvalidOperationException>(() => a.str);
Assert.Equal("Union Discriminator != 3", ex.Message);
Assert.Equal("The union is not of the requested type.", ex.Message);

ex = Assert.Throws<InvalidOperationException>(() => a.Key);
Assert.Equal("Union Discriminator != 4", ex.Message);
Assert.Equal("The union is not of the requested type.", ex.Message);

a = new FunUnion(new ValueStruct());
ex = Assert.Throws<InvalidOperationException>(() => a.RefStruct);
Assert.Equal("Union Discriminator != 1", ex.Message);
Assert.Equal("The union is not of the requested type.", ex.Message);
}

[Fact]
Expand Down Expand Up @@ -75,19 +75,21 @@ public void StringMember(FlatBufferDeserializationOption option)
[Fact]
public void BrokenUnion()
{
string message = $"Unexpected union discriminator value '0' for Union {typeof(FunUnion).FullName}";

Root root = new() { Fields = new() { Union = new FunUnion() } };

var ex = Assert.Throws<InvalidOperationException>(() => Root.Serializer.GetMaxSize(root));
Assert.Equal("Exception determining type of union. Unexpected Union discriminator.", ex.Message);
Assert.Equal(message, ex.Message);

ex = Assert.Throws<InvalidOperationException>(() => Root.Serializer.Write(new byte[1024], root));
Assert.Equal("Unexpected union discriminator. Unions must be initialized.", ex.Message);
Assert.Equal(message, ex.Message);

ex = Assert.Throws<InvalidOperationException>(() => new FunUnion().Accept<Visitor, bool>(new Visitor()));
Assert.Equal("Unexpected discriminator", ex.Message);
Assert.Equal(message, ex.Message);

ex = Assert.Throws<InvalidOperationException>(() => new Root(root));
Assert.Equal("Unexpected union discriminator", ex.Message);
Assert.Equal(message, ex.Message);
}

private struct Visitor : FunUnion.Visitor<bool>
Expand Down Expand Up @@ -238,7 +240,9 @@ public void InvalidUnion_InvalidDiscriminator(FlatBufferDeserializationOption op
FunUnion union = root.Fields.Union.Value;
});

Assert.Equal("Exception parsing union 'FlatSharpStrykerTests.FunUnion'. Unexpected union discriminator.", ex.Message);
Assert.Equal(
$"Unexpected union discriminator value '10' for Union {typeof(FunUnion).FullName}",
ex.Message);
}

[Theory]
Expand Down
4 changes: 3 additions & 1 deletion src/Tests/Stryker/Tests/UnionVectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ public void UnionVector_Invalid_InvalidDiscriminator(FlatBufferDeserializationOp
var x = item.Vectors.Union[0];
});

Assert.Equal("Exception parsing union 'FlatSharpStrykerTests.FunUnion'. Unexpected union discriminator.", ex.Message);
Assert.Equal(
$"Unexpected union discriminator value '7' for Union {typeof(FunUnion).FullName}",
ex.Message);
}

[Theory]
Expand Down

0 comments on commit 87283bd

Please sign in to comment.