Skip to content

Commit

Permalink
dataobject tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanya-Solyanik committed Jan 21, 2025
1 parent 73b94ca commit 45cb178
Showing 1 changed file with 86 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public void DataObject_GetData_InvokeTypeMocked_CallsGetData(Type format, object
mockDataObject.Verify(o => o.GetData(formatName), Times.Exactly(expectedCallCount));
}

#nullable enable
#nullable enable
internal class DataObjectOverridesTryGetDataCore : DataObject
{
private readonly string _format;
Expand All @@ -399,16 +399,17 @@ protected override bool TryGetDataCore<T>(
format.Should().Be(_format);
resolver.Should().BeEquivalentTo(_resolver);
autoConvert.Should().Be(_autoConvert);
typeof(T).Should().Be(typeof(string));
typeof(T).Should().Be<string>();

Count++;
// This is a mock implementation that never returns anything.
data = default;
return false;
}
}

[Fact]
public void DataObject_TryGetData_InvokeString_CallsTryGetDataCore()
public void TryGetData_InvokeString_CallsTryGetDataCore()
{
DataObjectOverridesTryGetDataCore dataObject = new(typeof(string).FullName!, resolver: null, autoConvert: true);
dataObject.Count.Should().Be(0);
Expand All @@ -418,6 +419,18 @@ public void DataObject_TryGetData_InvokeString_CallsTryGetDataCore()
dataObject.Count.Should().Be(1);
}

[Fact]
public void TryGetData_Wrapper_InvokeString_CallsTryGetDataCore()
{
DataObjectOverridesTryGetDataCore dataObject = new(typeof(string).FullName!, resolver: null, autoConvert: true);
dataObject.Count.Should().Be(0);
DataObject wrapper = new(dataObject);

wrapper.TryGetData(out string? data).Should().BeFalse();
data.Should().BeNull();
dataObject.Count.Should().Be(1);
}

public static TheoryData<string> RestrictedAndUnrestrictedFormat =>
[
DataFormats.CommaSeparatedValue,
Expand All @@ -436,6 +449,19 @@ public void TryGetData_InvokeStringString_CallsTryGetDataCore(string format)
dataObject.Count.Should().Be(1);
}

[Theory]
[MemberData(nameof(RestrictedAndUnrestrictedFormat))]
public void TryGetData_Wrapper_InvokeStringString_CallsTryGetDataCore(string format)
{
DataObjectOverridesTryGetDataCore dataObject = new(format, null, autoConvert: true);
dataObject.Count.Should().Be(0);
DataObject wrapper = new(dataObject);

wrapper.TryGetData(format, out string? data).Should().BeFalse();
data.Should().BeNull();
dataObject.Count.Should().Be(1);
}

[Fact]
public void TryGetData_InvokeStringString_ValidationFails()
{
Expand All @@ -449,6 +475,20 @@ public void TryGetData_InvokeStringString_ValidationFails()
dataObject.Count.Should().Be(0);
}

[Fact]
public void TryGetData_Wrapper_InvokeStringString_ValidationFails()
{
string format = DataFormats.Bitmap;
DataObjectOverridesTryGetDataCore dataObject = new(format, null, autoConvert: true);
dataObject.Count.Should().Be(0);
DataObject wrapper = new(dataObject);

// Incompatible format and type.
Action tryGetData = () => wrapper.TryGetData(format, out string? data);
tryGetData.Should().Throw<NotSupportedException>();
dataObject.Count.Should().Be(0);
}

public static TheoryData<string, bool> FormatAndAutoConvert => new()
{
{ DataFormats.CommaSeparatedValue, true },
Expand All @@ -469,6 +509,19 @@ public void TryGetData_InvokeStringBoolString_CallsTryGetDataCore(string format,
dataObject.Count.Should().Be(1);
}

[Theory]
[MemberData(nameof(FormatAndAutoConvert))]
public void TryGetData_Wrapper_InvokeStringBoolString_CallsTryGetDataCore(string format, bool autoConvert)
{
DataObjectOverridesTryGetDataCore dataObject = new(format, resolver: null, autoConvert);
dataObject.Count.Should().Be(0);
DataObject wrapper = new(dataObject);

wrapper.TryGetData(format, autoConvert, out string? data).Should().BeFalse();
data.Should().BeNull();
dataObject.Count.Should().Be(1);
}

private static Type NotSupportedResolver(TypeName typeName) => throw new NotSupportedException();

[Theory]
Expand All @@ -486,7 +539,7 @@ public void TryGetData_InvokeStringBoolString_ValidationFails(bool autoConvert)

[Theory]
[MemberData(nameof(FormatAndAutoConvert))]
public void DataObject_TryGetData_InvokeStringFuncBoolString_CallsTryGetDataCore(string format, bool autoConvert)
public void TryGetData_InvokeStringFuncBoolString_CallsTryGetDataCore(string format, bool autoConvert)
{
DataObjectOverridesTryGetDataCore dataObject = new(format, DataObjectOverridesTryGetDataCore.Resolver, autoConvert);
dataObject.Count.Should().Be(0);
Expand All @@ -496,6 +549,19 @@ public void DataObject_TryGetData_InvokeStringFuncBoolString_CallsTryGetDataCore
dataObject.Count.Should().Be(1);
}

[Theory]
[MemberData(nameof(FormatAndAutoConvert))]
public void TryGetData_Wrapper_InvokeStringFuncBoolString_CallsTryGetDataCore(string format, bool autoConvert)
{
DataObjectOverridesTryGetDataCore dataObject = new(format, DataObjectOverridesTryGetDataCore.Resolver, autoConvert);
dataObject.Count.Should().Be(0);
DataObject wrapper = new(dataObject);

wrapper.TryGetData(format, DataObjectOverridesTryGetDataCore.Resolver, autoConvert, out string? data).Should().BeFalse();
data.Should().BeNull();
dataObject.Count.Should().Be(1);
}

[Theory]
[BoolData]
public void TryGetData_InvokeStringFuncBoolString_ValidationFails(bool autoConvert)
Expand All @@ -508,7 +574,22 @@ public void TryGetData_InvokeStringFuncBoolString_ValidationFails(bool autoConve
tryGetData.Should().Throw<NotSupportedException>();
dataObject.Count.Should().Be(0);
}
#nullable disable

[Theory]
[BoolData]
public void TryGetData_Wrapper_InvokeStringFuncBoolString_ValidationFails(bool autoConvert)
{
string format = DataFormats.Bitmap;
DataObjectOverridesTryGetDataCore dataObject = new(format, DataObjectOverridesTryGetDataCore.Resolver, autoConvert);
dataObject.Count.Should().Be(0);
DataObject wrapper = new DataObject(dataObject);

Action tryGetData = () => wrapper.TryGetData(format, DataObjectOverridesTryGetDataCore.Resolver, autoConvert, out string? data);
tryGetData.Should().Throw<NotSupportedException>();
dataObject.Count.Should().Be(0);
}

#nullable disable

[Theory]
[MemberData(nameof(GetData_String_TheoryData))]
Expand Down

0 comments on commit 45cb178

Please sign in to comment.