Skip to content

Commit

Permalink
Changing generations of names with underscore (#320)
Browse files Browse the repository at this point in the history
Co-authored-by: developer <developer@local>
Co-authored-by: Anthony Shaw <anthony.p.shaw@gmail.com>
3 people authored Jan 8, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 9b58edb commit 6686c74
Showing 5 changed files with 135 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/CSnakes.SourceGeneration/CaseHelper.cs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ public static class CaseHelper
{
public static string ToPascalCase(this string snakeCase)
{
return string.Join("", snakeCase.Split('_').Select(s => s.Length > 1 ? char.ToUpperInvariant(s[0]) + s.Substring(1) : "_"));
return string.Join("", snakeCase.Split('_').Select(s => s.Length > 1 ? char.ToUpperInvariant(s[0]) + s.Substring(1) : s.Length > 0 ? s : "_"));
}

public static string ToLowerPascalCase(this string snakeCase)
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// <auto-generated/>
#nullable enable

using CSnakes.Runtime;
using CSnakes.Runtime.Python;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection.Metadata;

using Microsoft.Extensions.Logging;

[assembly: MetadataUpdateHandler(typeof(Python.Generated.Tests.TestClassExtensions))]

namespace Python.Generated.Tests;

public static class TestClassExtensions
{
private static ITestClass? instance;

private static ReadOnlySpan<byte> HotReloadHash => "b689cac043d82a1e3830504a67fe21c6"u8;

public static ITestClass TestClass(this IPythonEnvironment env)
{
if (instance is null)
{
instance = new TestClassInternal(env.Logger);
}
Debug.Assert(!env.IsDisposed());
return instance;
}

public static void UpdateApplication(Type[]? updatedTypes)
{
instance?.ReloadModule();
}

private class TestClassInternal : ITestClass
{
private PyObject module;
private readonly ILogger<IPythonEnvironment> logger;

private PyObject __func_test_with_underscore;

internal TestClassInternal(ILogger<IPythonEnvironment> logger)
{
this.logger = logger;
using (GIL.Acquire())
{
logger.LogDebug("Importing module {ModuleName}", "test");
module = Import.ImportModule("test");
this.__func_test_with_underscore = module.GetAttr("test_with_underscore");
}
}

void IReloadableModuleImport.ReloadModule()
{
logger.LogDebug("Reloading module {ModuleName}", "test");
using (GIL.Acquire())
{
Import.ReloadModule(ref module);
// Dispose old functions
this.__func_test_with_underscore.Dispose();
// Bind to new functions
this.__func_test_with_underscore = module.GetAttr("test_with_underscore");
}
}

public void Dispose()
{
logger.LogDebug("Disposing module {ModuleName}", "test");
this.__func_test_with_underscore.Dispose();
module.Dispose();
}

public void TestWithUnderscore(long testx, long testy)
{
using (GIL.Acquire())
{
logger.LogDebug("Invoking Python function: {FunctionName}", "test_with_underscore");
PyObject __underlyingPythonFunc = this.__func_test_with_underscore;
using PyObject testx_pyObject = PyObject.From(testx)!;
using PyObject testy_pyObject = PyObject.From(testy)!;
using PyObject __result_pyObject = __underlyingPythonFunc.Call(testx_pyObject, testy_pyObject);
return;
}
}
}
}

/// <summary>
/// Represents functions of the Python module <c>test</c>.
/// </summary>
public interface ITestClass : IReloadableModuleImport
{
/// <summary>
/// Invokes the Python function <c>test_with_underscore</c>:
/// <code><![CDATA[
/// def test_with_underscore(test_x: int, test_y: int) -> None: ...
/// ]]></code>
/// </summary>
void TestWithUnderscore(long testx, long testy);
}
Original file line number Diff line number Diff line change
@@ -154,7 +154,7 @@ public static class TestClassExtensions
module.Dispose();
}

public ValueTuple<string> Tuple_(ValueTuple<string> a)
public ValueTuple<string> Tuple1(ValueTuple<string> a)
{
using (GIL.Acquire())
{
@@ -166,7 +166,7 @@ public static class TestClassExtensions
}
}

public (string, string) Tuple_((string, string) a)
public (string, string) Tuple2((string, string) a)
{
using (GIL.Acquire())
{
@@ -178,7 +178,7 @@ public static class TestClassExtensions
}
}

public (string, string, string) Tuple_((string, string, string) a)
public (string, string, string) Tuple3((string, string, string) a)
{
using (GIL.Acquire())
{
@@ -190,7 +190,7 @@ public static class TestClassExtensions
}
}

public (string, string, string, string) Tuple_((string, string, string, string) a)
public (string, string, string, string) Tuple4((string, string, string, string) a)
{
using (GIL.Acquire())
{
@@ -202,7 +202,7 @@ public static class TestClassExtensions
}
}

public (string, string, string, string, string) Tuple_((string, string, string, string, string) a)
public (string, string, string, string, string) Tuple5((string, string, string, string, string) a)
{
using (GIL.Acquire())
{
@@ -214,7 +214,7 @@ public static class TestClassExtensions
}
}

public (string, string, string, string, string, string) Tuple_((string, string, string, string, string, string) a)
public (string, string, string, string, string, string) Tuple6((string, string, string, string, string, string) a)
{
using (GIL.Acquire())
{
@@ -226,7 +226,7 @@ public static class TestClassExtensions
}
}

public (string, string, string, string, string, string, string) Tuple_((string, string, string, string, string, string, string) a)
public (string, string, string, string, string, string, string) Tuple7((string, string, string, string, string, string, string) a)
{
using (GIL.Acquire())
{
@@ -238,7 +238,7 @@ public static class TestClassExtensions
}
}

public (string, string, string, string, string, string, string, string) Tuple_((string, string, string, string, string, string, string, string) a)
public (string, string, string, string, string, string, string, string) Tuple8((string, string, string, string, string, string, string, string) a)
{
using (GIL.Acquire())
{
@@ -250,7 +250,7 @@ public static class TestClassExtensions
}
}

public (string, string, string, string, string, string, string, string, string) Tuple_((string, string, string, string, string, string, string, string, string) a)
public (string, string, string, string, string, string, string, string, string) Tuple9((string, string, string, string, string, string, string, string, string) a)
{
using (GIL.Acquire())
{
@@ -371,71 +371,71 @@ public interface ITestClass : IReloadableModuleImport
/// def tuple_1(a: tuple[str]) -> tuple[str]: ...
/// ]]></code>
/// </summary>
ValueTuple<string> Tuple_(ValueTuple<string> a);
ValueTuple<string> Tuple1(ValueTuple<string> a);

/// <summary>
/// Invokes the Python function <c>tuple_2</c>:
/// <code><![CDATA[
/// def tuple_2(a: tuple[str, str]) -> tuple[str, str]: ...
/// ]]></code>
/// </summary>
(string, string) Tuple_((string, string) a);
(string, string) Tuple2((string, string) a);

/// <summary>
/// Invokes the Python function <c>tuple_3</c>:
/// <code><![CDATA[
/// def tuple_3(a: tuple[str, str, str]) -> tuple[str, str, str]: ...
/// ]]></code>
/// </summary>
(string, string, string) Tuple_((string, string, string) a);
(string, string, string) Tuple3((string, string, string) a);

/// <summary>
/// Invokes the Python function <c>tuple_4</c>:
/// <code><![CDATA[
/// def tuple_4(a: tuple[str, str, str, str]) -> tuple[str, str, str, str]: ...
/// ]]></code>
/// </summary>
(string, string, string, string) Tuple_((string, string, string, string) a);
(string, string, string, string) Tuple4((string, string, string, string) a);

/// <summary>
/// Invokes the Python function <c>tuple_5</c>:
/// <code><![CDATA[
/// def tuple_5(a: tuple[str, str, str, str, str]) -> tuple[str, str, str, str, str]: ...
/// ]]></code>
/// </summary>
(string, string, string, string, string) Tuple_((string, string, string, string, string) a);
(string, string, string, string, string) Tuple5((string, string, string, string, string) a);

/// <summary>
/// Invokes the Python function <c>tuple_6</c>:
/// <code><![CDATA[
/// def tuple_6(a: tuple[str, str, str, str, str, str]) -> tuple[str, str, str, str, str, str]: ...
/// ]]></code>
/// </summary>
(string, string, string, string, string, string) Tuple_((string, string, string, string, string, string) a);
(string, string, string, string, string, string) Tuple6((string, string, string, string, string, string) a);

/// <summary>
/// Invokes the Python function <c>tuple_7</c>:
/// <code><![CDATA[
/// def tuple_7(a: tuple[str, str, str, str, str, str, str]) -> tuple[str, str, str, str, str, str, str]: ...
/// ]]></code>
/// </summary>
(string, string, string, string, string, string, string) Tuple_((string, string, string, string, string, string, string) a);
(string, string, string, string, string, string, string) Tuple7((string, string, string, string, string, string, string) a);

/// <summary>
/// Invokes the Python function <c>tuple_8</c>:
/// <code><![CDATA[
/// def tuple_8(a: tuple[str, str, str, str, str, str, str, str]) -> tuple[str, str, str, str, str, str, str, str]: ...
/// ]]></code>
/// </summary>
(string, string, string, string, string, string, string, string) Tuple_((string, string, string, string, string, string, string, string) a);
(string, string, string, string, string, string, string, string) Tuple8((string, string, string, string, string, string, string, string) a);

/// <summary>
/// Invokes the Python function <c>tuple_9</c>:
/// <code><![CDATA[
/// def tuple_9(a: tuple[str, str, str, str, str, str, str, str, str]) -> tuple[str, str, str, str, str, str, str, str, str]: ...
/// ]]></code>
/// </summary>
(string, string, string, string, string, string, string, string, string) Tuple_((string, string, string, string, string, string, string, string, string) a);
(string, string, string, string, string, string, string, string, string) Tuple9((string, string, string, string, string, string, string, string, string) a);

/// <summary>
/// Invokes the Python function <c>tuple_10</c>:
20 changes: 10 additions & 10 deletions src/Integration.Tests/TupleTests.cs
Original file line number Diff line number Diff line change
@@ -7,23 +7,23 @@ public class TupleTests(PythonEnvironmentFixture fixture) : IntegrationTestBase(
[Fact]
public void SingleTupleValue()
{
var x = TestTuples.Tuple_(ValueTuple.Create("a"));
var x = TestTuples.Tuple1(ValueTuple.Create("a"));

Assert.Equal("a", x.Item1);
}

[Fact]
public void TwoTupleValues()
{
(string a, string b) = TestTuples.Tuple_(("a", "b"));
(string a, string b) = TestTuples.Tuple2(("a", "b"));
Assert.Equal("a", a);
Assert.Equal("b", b);
}

[Fact]
public void ThreeTupleValues()
{
(string a, string b, string c) = TestTuples.Tuple_(("a", "b", "c"));
(string a, string b, string c) = TestTuples.Tuple3(("a", "b", "c"));
Assert.Equal("a", a);
Assert.Equal("b", b);
Assert.Equal("c", c);
@@ -32,7 +32,7 @@ public void ThreeTupleValues()
[Fact]
public void FourTupleValues()
{
(string a, string b, string c, string d) = TestTuples.Tuple_(("a", "b", "c", "d"));
(string a, string b, string c, string d) = TestTuples.Tuple4(("a", "b", "c", "d"));
Assert.Equal("a", a);
Assert.Equal("b", b);
Assert.Equal("c", c);
@@ -42,7 +42,7 @@ public void FourTupleValues()
[Fact]
public void FiveTupleValues()
{
(string a, string b, string c, string d, string e) = TestTuples.Tuple_(("a", "b", "c", "d", "e"));
(string a, string b, string c, string d, string e) = TestTuples.Tuple5(("a", "b", "c", "d", "e"));
Assert.Equal("a", a);
Assert.Equal("b", b);
Assert.Equal("c", c);
@@ -53,7 +53,7 @@ public void FiveTupleValues()
[Fact]
public void SixTupleValues()
{
(string a, string b, string c, string d, string e, string f) = TestTuples.Tuple_(("a", "b", "c", "d", "e", "f"));
(string a, string b, string c, string d, string e, string f) = TestTuples.Tuple6(("a", "b", "c", "d", "e", "f"));
Assert.Equal("a", a);
Assert.Equal("b", b);
Assert.Equal("c", c);
@@ -65,7 +65,7 @@ public void SixTupleValues()
[Fact]
public void SevenTupleValues()
{
(string a, string b, string c, string d, string e, string f, string g) = TestTuples.Tuple_(("a", "b", "c", "d", "e", "f", "g"));
(string a, string b, string c, string d, string e, string f, string g) = TestTuples.Tuple7(("a", "b", "c", "d", "e", "f", "g"));
Assert.Equal("a", a);
Assert.Equal("b", b);
Assert.Equal("c", c);
@@ -78,7 +78,7 @@ public void SevenTupleValues()
[Fact]
public void EightTupleValues()
{
(string a, string b, string c, string d, string e, string f, string g, string h) = TestTuples.Tuple_(("a", "b", "c", "d", "e", "f", "g", "h"));
(string a, string b, string c, string d, string e, string f, string g, string h) = TestTuples.Tuple8(("a", "b", "c", "d", "e", "f", "g", "h"));
Assert.Equal("a", a);
Assert.Equal("b", b);
Assert.Equal("c", c);
@@ -92,7 +92,7 @@ public void EightTupleValues()
[Fact]
public void NineTupleValues()
{
(string a, string b, string c, string d, string e, string f, string g, string h, string i) = TestTuples.Tuple_(("a", "b", "c", "d", "e", "f", "g", "h", "i"));
(string a, string b, string c, string d, string e, string f, string g, string h, string i) = TestTuples.Tuple9(("a", "b", "c", "d", "e", "f", "g", "h", "i"));
Assert.Equal("a", a);
Assert.Equal("b", b);
Assert.Equal("c", c);
@@ -259,4 +259,4 @@ public void SeventeenTupleValues()
Assert.Equal("p", p);
Assert.Equal("q", q);
}
}
}
2 changes: 2 additions & 0 deletions src/Integration.Tests/python/test_args_underscore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test_with_underscore(test_x: int, test_y: int) -> None:
return None

0 comments on commit 6686c74

Please sign in to comment.