Skip to content

Commit b4370f4

Browse files
committed
MapToInstance
1 parent d3215ea commit b4370f4

8 files changed

+190
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//----------------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//----------------------------------------------------------------------------------------
9+
10+
#nullable enable
11+
using System;
12+
13+
namespace ProxyInterfaceSourceGeneratorTests.Source
14+
{
15+
public partial class BarProxy : global::ProxyInterfaceSourceGeneratorTests.Source.IBar
16+
{
17+
18+
19+
public global::ProxyInterfaceSourceGeneratorTests.Source.Bar _Instance { get; }
20+
21+
22+
public BarProxy(global::ProxyInterfaceSourceGeneratorTests.Source.Bar instance)
23+
{
24+
_Instance = instance;
25+
26+
}
27+
}
28+
}
29+
#nullable restore
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//----------------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//----------------------------------------------------------------------------------------
9+
10+
#nullable enable
11+
using System;
12+
13+
namespace ProxyInterfaceSourceGeneratorTests.Source
14+
{
15+
public partial interface IBar
16+
{
17+
global::ProxyInterfaceSourceGeneratorTests.Source.Bar _Instance { get; }
18+
19+
20+
}
21+
}
22+
#nullable restore
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//----------------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//----------------------------------------------------------------------------------------
9+
10+
#nullable enable
11+
using System;
12+
13+
namespace ProxyInterfaceSourceGeneratorTests.Source
14+
{
15+
public partial interface INoSetter
16+
{
17+
global::ProxyInterfaceSourceGeneratorTests.Source.NoSetter _Instance { get; }
18+
19+
global::ProxyInterfaceSourceGeneratorTests.Source.IBar? Bar { get; }
20+
21+
22+
}
23+
}
24+
#nullable restore
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//----------------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//----------------------------------------------------------------------------------------
9+
10+
#nullable enable
11+
using System;
12+
13+
namespace ProxyInterfaceSourceGeneratorTests.Source
14+
{
15+
public partial class NoSetterProxy : global::ProxyInterfaceSourceGeneratorTests.Source.INoSetter
16+
{
17+
18+
private static global::ProxyInterfaceSourceGeneratorTests.Source.IBar MapToInterface(global::ProxyInterfaceSourceGeneratorTests.Source.Bar value)
19+
{
20+
return new global::ProxyInterfaceSourceGeneratorTests.Source.BarProxy(value);
21+
}
22+
23+
private static global::ProxyInterfaceSourceGeneratorTests.Source.Bar MapToInstance(global::ProxyInterfaceSourceGeneratorTests.Source.IBar value)
24+
{
25+
return value._Instance;
26+
}
27+
28+
29+
30+
public global::ProxyInterfaceSourceGeneratorTests.Source.NoSetter _Instance { get; }
31+
32+
public global::ProxyInterfaceSourceGeneratorTests.Source.IBar? Bar { get => _Instance.Bar != null ? MapToInterface(_Instance.Bar) : null; }
33+
34+
35+
public NoSetterProxy(global::ProxyInterfaceSourceGeneratorTests.Source.NoSetter instance)
36+
{
37+
_Instance = instance;
38+
39+
}
40+
}
41+
}
42+
#nullable restore

tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,63 @@ public void GenerateFiles_When_MixedVisibility_Should_GenerateCorrectFiles()
310310
}
311311
}
312312

313+
[Fact]
314+
public void GenerateFiles_When_NoSetter_Should_GenerateCorrectFiles()
315+
{
316+
// Arrange
317+
var fileNames = new[]
318+
{
319+
"ProxyInterfaceSourceGeneratorTests.Source.IBar.g.cs",
320+
"ProxyInterfaceSourceGeneratorTests.Source.INoSetter.g.cs",
321+
"ProxyInterfaceSourceGeneratorTests.Source.BarProxy.g.cs",
322+
"ProxyInterfaceSourceGeneratorTests.Source.NoSetterProxy.g.cs"
323+
};
324+
325+
var pathNoSetter = Path.Combine(_basePath, "Source/INoSetter.cs");
326+
var sourceFileNoSetter = new SourceFile
327+
{
328+
Path = pathNoSetter,
329+
Text = File.ReadAllText(pathNoSetter),
330+
AttributeToAddToInterface = new ExtraAttribute
331+
{
332+
Name = "ProxyInterfaceGenerator.Proxy",
333+
ArgumentList = "typeof(ProxyInterfaceSourceGeneratorTests.Source.NoSetter)"
334+
}
335+
};
336+
337+
var pathBar = Path.Combine(_basePath, "Source/IBar.cs");
338+
var sourceFileBar = new SourceFile
339+
{
340+
Path = pathBar,
341+
Text = File.ReadAllText(pathBar),
342+
AttributeToAddToInterface = new ExtraAttribute
343+
{
344+
Name = "ProxyInterfaceGenerator.Proxy",
345+
ArgumentList = "typeof(ProxyInterfaceSourceGeneratorTests.Source.Bar)"
346+
}
347+
};
348+
349+
// Act
350+
var result = _sut.Execute([
351+
sourceFileBar,
352+
sourceFileNoSetter,
353+
]);
354+
355+
// Assert
356+
result.Valid.Should().BeTrue();
357+
result.Files.Should().HaveCount(fileNames.Length + 1);
358+
359+
foreach (var fileName in fileNames.Select((fileName, index) => new { fileName, index }))
360+
{
361+
var builder = result.Files[fileName.index + 1]; // +1 means skip the attribute
362+
builder.Path.Should().EndWith(fileName.fileName);
363+
364+
var destinationFilename = Path.Combine(_basePath, $"Destination/{fileName.fileName}");
365+
if (Write) File.WriteAllText(destinationFilename, builder.Text);
366+
builder.Text.Should().Be(File.ReadAllText(destinationFilename));
367+
}
368+
}
369+
313370
[Fact]
314371
public void GenerateFiles_ForSingleClass_Should_GenerateCorrectFiles()
315372
{
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace ProxyInterfaceSourceGeneratorTests.Source;
2+
3+
public partial interface IBar
4+
{
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace ProxyInterfaceSourceGeneratorTests.Source;
2+
3+
public partial interface INoSetter
4+
{
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ProxyInterfaceSourceGeneratorTests.Source;
2+
3+
public class NoSetter
4+
{
5+
public Bar? Bar { get; }
6+
}

0 commit comments

Comments
 (0)