evgenyigumnov / rustsn

Code snippets generator via LLMs and compiler/tester via build tools
GNU General Public License v3.0
18 stars 5 forks source link

Add C# support #15

Open evgenyigumnov opened 2 days ago

evgenyigumnov commented 2 days ago

Launch example

rustsn --lang=cs

Example query:

Take 2 parameters, add them, and return the result.

Example generation:

csproj File (Solution.csproj)

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

</Project>

Source Code (Program.cs)

using System;

namespace Solution
{
    public class Calculator
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var calculator = new Calculator();
            if (args.Length >= 2 &&
                int.TryParse(args[0], out int a) &&
                int.TryParse(args[1], out int b))
            {
                Console.WriteLine(calculator.Add(a, b));
            }
            else
            {
                Console.WriteLine("Please provide two integer arguments.");
            }
        }
    }
}

Test Code (CalculatorTests.cs)

using Xunit;
using Solution;

namespace Solution.Tests
{
    public class CalculatorTests
    {
        private readonly Calculator _calculator = new Calculator();

        [Fact]
        public void Add_WithPositiveNumbers_ReturnsSum()
        {
            Assert.Equal(3, _calculator.Add(1, 2));
        }

        [Fact]
        public void Add_WithNegativeNumbers_ReturnsSum()
        {
            Assert.Equal(-3, _calculator.Add(-1, -2));
        }

        [Fact]
        public void Add_WithStringInput_ThrowsFormatException()
        {
            // Since the Add method expects integers, handling of strings should be done in the Program.cs
            // Here we can test the Calculator directly which doesn't handle strings
            // So this test might not be applicable unless input parsing is included in the Calculator
            // Alternatively, you can test the Program.Main method with string inputs using a different approach
            Assert.True(true); // Placeholder
        }
    }
}

Example Install Dependencies

dotnet restore

Example Launch Compilation

dotnet build

Example Launch Test

dotnet test