Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Aug 29, 2024
1 parent 2d0d8e4 commit 9cd0685
Show file tree
Hide file tree
Showing 23 changed files with 927 additions and 851 deletions.
2 changes: 0 additions & 2 deletions Build/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

static class Tools
{
public static bool UnderTeamCity => Environment.GetEnvironmentVariable("TEAMCITY_VERSION") != default;

public static void MergeNuGetPackages(IEnumerable<string> mergingPackages, string targetPackage)
{
var targetDir = Path.GetDirectoryName(targetPackage);
Expand Down
155 changes: 80 additions & 75 deletions Immutype.Tests/Integration/CommentsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,28 @@ public void ShouldGenerateCommentWhenEnumerable()
const string statements = "System.Console.WriteLine(string.Join(',', new Rec(new[] {33}).WithValues(99, 66).values));";

// When
var output = @"
namespace Sample
{
using System;
using System.Collections.Generic;
using Immutype;
[Target]
public record Rec(
// AbcComment
IEnumerable<int> values);
}".Run(out var generatedCode, new RunOptions
var output = """
namespace Sample
{
using System;
using System.Collections.Generic;
using Immutype;
[Target]
public record Rec(
// AbcComment
IEnumerable<int> values);
}
""".Run(out var generatedCode, new RunOptions
{
Statements = statements
});

// Then
output.ShouldBe(new[]
{
output.ShouldBe([
"99,66"
}, generatedCode);
], generatedCode);

generatedCode.Contains("/// Set <c>Values</c>. AbcComment").ShouldBeTrue();
generatedCode.Contains("/// <param name=\"it\">The original instance.</param>").ShouldBeTrue();
Expand All @@ -48,25 +49,26 @@ public void ShouldGenerateComment()
const string statements = "System.Console.WriteLine(new Rec(33).WithVal(99));";

// When
var output = @"
namespace Sample
{
using System;
[Immutype.TargetAttribute()]
public record Rec(
// AbcComment
int val);
}".Run(out var generatedCode, new RunOptions
var output = """
namespace Sample
{
using System;
[Immutype.TargetAttribute()]
public record Rec(
// AbcComment
int val);
}
""".Run(out var generatedCode, new RunOptions
{
Statements = statements
});

// Then
output.ShouldBe(new[]
{
output.ShouldBe([
"Rec { val = 99 }"
}, generatedCode);
], generatedCode);

generatedCode.Contains("/// Set <c>Val</c>. AbcComment").ShouldBeTrue();
generatedCode.Contains("<param name=\"it\">The original instance.</param>").ShouldBeTrue();
Expand All @@ -81,26 +83,27 @@ public void ShouldGenerateCommentWhenSeveralLines()
const string statements = "System.Console.WriteLine(new Rec(33).WithVal(99));";

// When
var output = @"
namespace Sample
{
using System;
[Immutype.TargetAttribute()]
public record Rec(
// AbcComment
// Xyz
int val);
}".Run(out var generatedCode, new RunOptions
var output = """
namespace Sample
{
using System;
[Immutype.TargetAttribute()]
public record Rec(
// AbcComment
// Xyz
int val);
}
""".Run(out var generatedCode, new RunOptions
{
Statements = statements
});

// Then
output.ShouldBe(new[]
{
output.ShouldBe([
"Rec { val = 99 }"
}, generatedCode);
], generatedCode);

generatedCode.Contains("/// Set <c>Val</c>. AbcComment").ShouldBeTrue();
generatedCode.Contains("/// Xyz").ShouldBeTrue();
Expand All @@ -116,28 +119,29 @@ public void ShouldGenerateCommentFromDoc()
const string statements = "System.Console.WriteLine(new Rec(33).WithVal(99));";

// When
var output = @"
namespace Sample
{
using System;
[Immutype.TargetAttribute()]
/// <summary>
/// Som text
/// </summary>
/// <param name=""val"">AbcComment</param>
public record Rec(
int val);
}".Run(out var generatedCode, new RunOptions
var output = """
namespace Sample
{
using System;
[Immutype.TargetAttribute()]
/// <summary>
/// Som text
/// </summary>
/// <param name="val">AbcComment</param>
public record Rec(
int val);
}
""".Run(out var generatedCode, new RunOptions
{
Statements = statements
});

// Then
output.ShouldBe(new[]
{
output.ShouldBe([
"Rec { val = 99 }"
}, generatedCode);
], generatedCode);

generatedCode.Contains("/// Set <c>Val</c>. AbcComment").ShouldBeTrue();
generatedCode.Contains("<param name=\"it\">The original instance.</param>").ShouldBeTrue();
Expand All @@ -152,29 +156,30 @@ public void ShouldGenerateCommentFromDocAndSimple()
const string statements = "System.Console.WriteLine(new Rec(33).WithVal(99));";

// When
var output = @"
namespace Sample
{
using System;
[Immutype.TargetAttribute()]
/// <summary>
/// Som text
/// </summary>
/// <param name=""val"">AbcComment</param>
public record Rec(
// Xyz
int val);
}".Run(out var generatedCode, new RunOptions
var output = """
namespace Sample
{
using System;
[Immutype.TargetAttribute()]
/// <summary>
/// Som text
/// </summary>
/// <param name="val">AbcComment</param>
public record Rec(
// Xyz
int val);
}
""".Run(out var generatedCode, new RunOptions
{
Statements = statements
});

// Then
output.ShouldBe(new[]
{
output.ShouldBe([
"Rec { val = 99 }"
}, generatedCode);
], generatedCode);

generatedCode.Contains("/// Set <c>Val</c>. AbcComment").ShouldBeTrue();
generatedCode.Contains("/// Xyz").ShouldBeTrue();
Expand Down
56 changes: 29 additions & 27 deletions Immutype.Tests/Integration/GenericTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@ public void ShouldSupportGenerics()
const string statements = "System.Console.WriteLine(string.Join(',', new Rec<int, string>(new List<int>{33}).WithVals(55).AddVals(99, 44).vals));";

// When
var output = @"
namespace Sample
{
using System;
using System.Collections.Generic;
using Immutype;
[Target]
public record Rec<T, T2>(IList<T> vals);
}".Run(out var generatedCode, new RunOptions
var output = """
namespace Sample
{
using System;
using System.Collections.Generic;
using Immutype;
[Target]
public record Rec<T, T2>(IList<T> vals);
}
""".Run(out var generatedCode, new RunOptions
{
Statements = statements
});

// Then
output.ShouldBe(new[]
{
output.ShouldBe([
"55,99,44"
}, generatedCode);
], generatedCode);
}

[Fact]
Expand All @@ -41,25 +42,26 @@ public void ShouldSupportGenericConstraints()
const string statements = "System.Console.WriteLine(string.Join(',', new Rec<int, string>(new List<int>{33}).WithVals(55).AddVals(99, 44).vals));";

// When
var output = @"
namespace Sample
{
using System;
using System.Collections.Generic;
using Immutype;
[Target]
public record Rec<T, T2>(IList<T> vals)
where T: struct;
}".Run(out var generatedCode, new RunOptions
var output = """
namespace Sample
{
using System;
using System.Collections.Generic;
using Immutype;
[Target]
public record Rec<T, T2>(IList<T> vals)
where T: struct;
}
""".Run(out var generatedCode, new RunOptions
{
Statements = statements
});

// Then
output.ShouldBe(new[]
{
output.ShouldBe([
"55,99,44"
}, generatedCode);
], generatedCode);
}
}
58 changes: 31 additions & 27 deletions Immutype.Tests/Integration/TestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ public static IReadOnlyList<string> Run(this string setupCode, out string genera
var curOptions = options ?? new RunOptions();
var parseOptions = CSharpParseOptions.Default.WithLanguageVersion(curOptions.LanguageVersion);

var hostCode = @"
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Immutype;
using Sample;
namespace Sample { public class Program { public static void Main() {" + curOptions.Statements + "} } }";
var hostCode = """
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Immutype;
using Sample;
namespace Sample { public class Program { public static void Main() {
""" + curOptions.Statements + "} } }";

var additionalCode = curOptions.AdditionalCode.Select(code => CSharpSyntaxTree.ParseText(code, parseOptions)).ToArray();

Expand Down Expand Up @@ -78,16 +80,18 @@ namespace Sample { public class Program { public static void Main() {" + curOpti
var configPath = Path.ChangeExtension(tempFileName, "runtimeconfig.json");
var runtime = RuntimeInformation.FrameworkDescription.Split(" ")[1];
var dotnetVersion = $"{Environment.Version.Major}.{Environment.Version.Minor}";
var config = @"
{
""runtimeOptions"": {
""tfm"": ""netV.V"",
""framework"": {
""name"": ""Microsoft.NETCore.App"",
""version"": ""RUNTIME""
}
}
}".Replace("V.V", dotnetVersion).Replace("RUNTIME", runtime);
var config = """
{
"runtimeOptions": {
"tfm": "netV.V",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "RUNTIME"
}
}
}
""".Replace("V.V", dotnetVersion).Replace("RUNTIME", runtime);

try
{
Expand All @@ -96,14 +100,6 @@ namespace Sample { public class Program { public static void Main() {" + curOpti
var result = compilation.Emit(assemblyPath);
Assert.True(result.Success);

void OnOutputDataReceived(object sender, DataReceivedEventArgs args)
{
if (args.Data != null)
{
output.Add(args.Data);
}
}

var process = new Process
{
StartInfo = new ProcessStartInfo
Expand Down Expand Up @@ -134,6 +130,14 @@ void OnOutputDataReceived(object sender, DataReceivedEventArgs args)
}

return output;

void OnOutputDataReceived(object sender, DataReceivedEventArgs args)
{
if (args.Data != null)
{
output.Add(args.Data);
}
}
}
finally
{
Expand Down
Loading

0 comments on commit 9cd0685

Please sign in to comment.