Add support for implicit and explicit operators (#51)
* . * xxxxxxxx * rev * .... * . * ok * 2 * . * ,
This commit is contained in:
+2
@@ -132,6 +132,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source.AkkaActor
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public LocalActorRefProviderProxy(Akka.Actor.LocalActorRefProvider instance)
|
||||
{
|
||||
_Instance = instance;
|
||||
|
||||
+2
@@ -48,6 +48,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source.PnP
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public ClientContextProxy(Microsoft.SharePoint.Client.ClientContext instance) : base(instance)
|
||||
{
|
||||
_Instance = instance;
|
||||
|
||||
+2
@@ -85,6 +85,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source.PnP
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public ClientObjectProxy(Microsoft.SharePoint.Client.ClientObject instance)
|
||||
{
|
||||
_Instance = instance;
|
||||
|
||||
+2
@@ -143,6 +143,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source.PnP
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public ClientRuntimeContextProxy(Microsoft.SharePoint.Client.ClientRuntimeContext instance)
|
||||
{
|
||||
_Instance = instance;
|
||||
|
||||
+2
@@ -46,6 +46,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source.PnP
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public SecurableObjectProxy(Microsoft.SharePoint.Client.SecurableObject instance) : base(instance)
|
||||
{
|
||||
_Instance = instance;
|
||||
|
||||
+2
@@ -1131,6 +1131,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source.PnP
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public WebProxy(Microsoft.SharePoint.Client.Web instance) : base(instance)
|
||||
{
|
||||
_Instance = instance;
|
||||
|
||||
@@ -24,6 +24,8 @@ using System;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public NoNamespaceProxy(ProxyInterfaceSourceGeneratorTests.Source.NoNamespace instance)
|
||||
{
|
||||
_Instance = instance;
|
||||
|
||||
+2
@@ -30,6 +30,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public GenericProxy(ProxyInterfaceSourceGeneratorTests.Source.Generic<T> instance)
|
||||
{
|
||||
_Instance = instance;
|
||||
|
||||
+2
@@ -27,6 +27,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public HumanProxy(ProxyInterfaceSourceGeneratorTests.Source.Human instance)
|
||||
{
|
||||
_Instance = instance;
|
||||
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
//----------------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator.
|
||||
//
|
||||
// Changes to this file may cause incorrect behavior and will be lost if
|
||||
// the code is regenerated.
|
||||
// </auto-generated>
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
#nullable enable
|
||||
using System;
|
||||
|
||||
namespace ProxyInterfaceSourceGeneratorTests.Source
|
||||
{
|
||||
public partial interface IOperatorTest
|
||||
{
|
||||
ProxyInterfaceSourceGeneratorTests.Source.OperatorTest _Instance { get; }
|
||||
|
||||
string Name { get; set; }
|
||||
|
||||
int? Id { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
#nullable disable
|
||||
+2
@@ -25,6 +25,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public MixedVisibilityProxy(ProxyInterfaceSourceGeneratorTests.Source.MixedVisibility instance)
|
||||
{
|
||||
_Instance = instance;
|
||||
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
//----------------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator.
|
||||
//
|
||||
// Changes to this file may cause incorrect behavior and will be lost if
|
||||
// the code is regenerated.
|
||||
// </auto-generated>
|
||||
//----------------------------------------------------------------------------------------
|
||||
|
||||
#nullable enable
|
||||
using System;
|
||||
|
||||
namespace ProxyInterfaceSourceGeneratorTests.Source
|
||||
{
|
||||
public partial class OperatorTestProxy : IOperatorTest
|
||||
{
|
||||
public ProxyInterfaceSourceGeneratorTests.Source.OperatorTest _Instance { get; }
|
||||
|
||||
|
||||
public string Name { get => _Instance.Name; set => _Instance.Name = value; }
|
||||
|
||||
public int? Id { get => _Instance.Id; set => _Instance.Id = value; }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static implicit operator OperatorTestProxy(string name)
|
||||
{
|
||||
return new OperatorTestProxy((OperatorTest) name);
|
||||
}
|
||||
|
||||
public static implicit operator OperatorTestProxy(int? id)
|
||||
{
|
||||
return new OperatorTestProxy((OperatorTest) id);
|
||||
}
|
||||
|
||||
public static explicit operator string(OperatorTestProxy test)
|
||||
{
|
||||
return (string) test._Instance;
|
||||
}
|
||||
|
||||
public static explicit operator int?(OperatorTestProxy test)
|
||||
{
|
||||
return (int?) test._Instance;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public OperatorTestProxy(ProxyInterfaceSourceGeneratorTests.Source.OperatorTest instance)
|
||||
{
|
||||
_Instance = instance;
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
#nullable disable
|
||||
+2
@@ -114,6 +114,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public PersonExtendsProxy(ProxyInterfaceSourceGeneratorTests.Source.PersonExtends instance)
|
||||
{
|
||||
_Instance = instance;
|
||||
|
||||
+2
@@ -168,6 +168,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public PersonProxy(ProxyInterfaceSourceGeneratorTests.Source.Person instance) : base(instance)
|
||||
{
|
||||
_Instance = instance;
|
||||
|
||||
@@ -88,6 +88,63 @@ namespace ProxyInterfaceSourceGeneratorTests
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GenerateFiles_ForClassWithOperator_Should_GenerateCorrectFiles()
|
||||
{
|
||||
// Arrange
|
||||
var fileNames = new[]
|
||||
{
|
||||
"ProxyInterfaceSourceGeneratorTests.Source.IOperatorTest.g.cs",
|
||||
"ProxyInterfaceSourceGeneratorTests.Source.OperatorTestProxy.g.cs"
|
||||
};
|
||||
|
||||
var path = "./Source/IOperatorTest.cs";
|
||||
var sourceFile = new SourceFile
|
||||
{
|
||||
Path = path,
|
||||
Text = File.ReadAllText(path),
|
||||
AttributeToAddToInterface = new ExtraAttribute
|
||||
{
|
||||
Name = "ProxyInterfaceGenerator.Proxy",
|
||||
ArgumentList = "typeof(ProxyInterfaceSourceGeneratorTests.Source.OperatorTest)"
|
||||
}
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _sut.Execute(new[]
|
||||
{
|
||||
sourceFile
|
||||
});
|
||||
|
||||
// Assert
|
||||
result.Valid.Should().BeTrue();
|
||||
result.Files.Should().HaveCount(fileNames.Length + 1);
|
||||
|
||||
foreach (var fileName in fileNames.Select((fileName, index) => new { fileName, index }))
|
||||
{
|
||||
var builder = result.Files[fileName.index + 1]; // +1 means skip the attribute
|
||||
builder.Path.Should().EndWith(fileName.fileName);
|
||||
|
||||
if (Write) File.WriteAllText($"../../../Destination/{fileName.fileName}", builder.Text);
|
||||
builder.Text.Should().Be(File.ReadAllText($"../../../Destination/{fileName.fileName}"));
|
||||
}
|
||||
|
||||
var name = "stef";
|
||||
var operatorTest = new OperatorTest
|
||||
{
|
||||
Name = name
|
||||
};
|
||||
string name1 = (string) operatorTest;
|
||||
name1.Should().Be(name);
|
||||
|
||||
var p = new OperatorTestProxy(operatorTest);
|
||||
string name2 = (string)p;
|
||||
name2.Should().Be(name);
|
||||
|
||||
var p2 = (OperatorTestProxy)name;
|
||||
p2.Should().BeEquivalentTo(new OperatorTestProxy(operatorTest));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GenerateFiles_When_NoNamespace_Should_GenerateCorrectFiles()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
// file-scoped namespace !
|
||||
namespace ProxyInterfaceSourceGeneratorTests.Source;
|
||||
|
||||
public partial interface IOperatorTest
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
namespace ProxyInterfaceSourceGeneratorTests.Source
|
||||
{
|
||||
public class OperatorTest
|
||||
{
|
||||
public string Name { get; set; } = null!;
|
||||
|
||||
public int? Id { get; set; }
|
||||
|
||||
// Operator : implicit
|
||||
public static implicit operator OperatorTest(string name)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Name = name
|
||||
};
|
||||
}
|
||||
|
||||
public static implicit operator OperatorTest(int? id)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Id = id
|
||||
};
|
||||
}
|
||||
|
||||
// Operator : explicit
|
||||
public static explicit operator string(OperatorTest test)
|
||||
{
|
||||
return test.Name;
|
||||
}
|
||||
|
||||
public static explicit operator int?(OperatorTest test)
|
||||
{
|
||||
return test.Id;
|
||||
}
|
||||
}
|
||||
|
||||
public class X
|
||||
{
|
||||
public X()
|
||||
{
|
||||
OperatorTest operatorTest = "stef";
|
||||
var s = (string)operatorTest;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user