Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test to demonstrate interaction of ClassNameHint with other hints #54

Merged
2 commits merged into from
Oct 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 202 additions & 1 deletion src/Json.Schema.ToDotNet.UnitTests/Hints/ClassNameHintTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,208 @@ public int GetHashCode(FileData obj)
}
}
}"
)
),

new TestCase(
"Renamed class has a base class",
@"{
""type"": ""object"",
""properties"": {
""file"": {
""$ref"": ""#/definitions/file""
}
},
""definitions"": {
""file"": {
""type"": ""object"",
""properties"": {
""path"": {
""type"": ""string""
}
}
}
}
}",
Copy link
Author

@ghost ghost Oct 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The schema defines a root object with a single property named file of type file. #ByDesign


"FileData",

@"{
""file"": [
{
""kind"": ""ClassNameHint"",
""arguments"": {
""className"": ""FileData""
}
}
],
Copy link
Author

@ghost ghost Oct 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ClassNameHint says that the generated class is named FileData. #ByDesign

""fileData"": [
{
""kind"": ""BaseTypeHint"",
""arguments"": {
""baseTypeNames"": [
""PropertyBagHolder""
]
}
}
Copy link
Author

@ghost ghost Oct 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The BaseTypeHint says that the renamed class has a base class PropertyBagHolder. #ByDesign

]
}",

// PrimaryClassText
@"using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace N
{
[DataContract]
[GeneratedCode(""Microsoft.Json.Schema.ToDotNet"", """ + VersionConstants.FileVersion + @""")]
public partial class C
{
public static IEqualityComparer<C> ValueComparer => CEqualityComparer.Instance;

public bool ValueEquals(C other) => ValueComparer.Equals(this, other);
public int ValueGetHashCode() => ValueComparer.GetHashCode(this);

[DataMember(Name = ""file"", IsRequired = false, EmitDefaultValue = false)]
public FileData File { get; set; }
}
}",

// PrimaryClassComparerText
@"using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;

namespace N
{
/// <summary>
/// Defines methods to support the comparison of objects of type C for equality.
/// </summary>
[GeneratedCode(""Microsoft.Json.Schema.ToDotNet"", """ + VersionConstants.FileVersion + @""")]
internal sealed class CEqualityComparer : IEqualityComparer<C>
{
internal static readonly CEqualityComparer Instance = new CEqualityComparer();

public bool Equals(C left, C right)
{
if (ReferenceEquals(left, right))
{
return true;
}

if (ReferenceEquals(left, null) || ReferenceEquals(right, null))
{
return false;
}

if (!FileData.ValueComparer.Equals(left.File, right.File))
{
return false;
}

return true;
}

public int GetHashCode(C obj)
{
if (ReferenceEquals(obj, null))
{
return 0;
}

int result = 17;
unchecked
{
if (obj.File != null)
{
result = (result * 31) + obj.File.ValueGetHashCode();
}
}

return result;
}
}
}",

// HintedClassText
@"using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace N
{
[DataContract]
[GeneratedCode(""Microsoft.Json.Schema.ToDotNet"", """ + VersionConstants.FileVersion + @""")]
public partial class FileData : PropertyBagHolder
Copy link
Author

@ghost ghost Oct 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PropertyBagHolder [](start = 36, length = 17)

The renamed class has the correct base type. #ByDesign

{
public static IEqualityComparer<FileData> ValueComparer => FileDataEqualityComparer.Instance;

public bool ValueEquals(FileData other) => ValueComparer.Equals(this, other);
public int ValueGetHashCode() => ValueComparer.GetHashCode(this);

[DataMember(Name = ""path"", IsRequired = false, EmitDefaultValue = false)]
public string Path { get; set; }
}
}",

// HintedClassComparerText
@"using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;

namespace N
{
/// <summary>
/// Defines methods to support the comparison of objects of type FileData for equality.
/// </summary>
[GeneratedCode(""Microsoft.Json.Schema.ToDotNet"", """ + VersionConstants.FileVersion + @""")]
internal sealed class FileDataEqualityComparer : IEqualityComparer<FileData>
{
internal static readonly FileDataEqualityComparer Instance = new FileDataEqualityComparer();

public bool Equals(FileData left, FileData right)
{
if (ReferenceEquals(left, right))
{
return true;
}

if (ReferenceEquals(left, null) || ReferenceEquals(right, null))
{
return false;
}

if (left.Path != right.Path)
{
return false;
}

return true;
}
Copy link
Author

@ghost ghost Oct 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} [](start = 8, length = 1)

Now this is a separate, existing, bug, which I will file. The generated equality comparer doesn't compare the base classes! #Pending

Copy link
Author

@ghost ghost Oct 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michaelcfanning Microsoft/jschema#55, "Equality comparer doesn't compare base types".


In reply to: 226104727 [](ancestors = 226104727)


public int GetHashCode(FileData obj)
{
if (ReferenceEquals(obj, null))
{
return 0;
}

int result = 17;
unchecked
{
if (obj.Path != null)
{
result = (result * 31) + obj.Path.GetHashCode();
}
}

return result;
}
}
}"
)
};

[Theory(DisplayName = nameof(ClassNameHint))]
Expand Down