Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions EasySourceGenerators.GeneratorTests/CSharpAccessibilityKeywordTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using EasySourceGenerators.Generators.SourceEmitting;
using Microsoft.CodeAnalysis;

namespace EasySourceGenerators.GeneratorTests;

[TestFixture]
public class CSharpAccessibilityKeywordTests
{
// -----------------------------------------------------------------------
// From (returns "private" as default)
// -----------------------------------------------------------------------

[Test]
public void From_Public_ReturnsPublic()
{
string result = CSharpAccessibilityKeyword.From(Accessibility.Public);

Assert.That(result, Is.EqualTo("public"));
}

[Test]
public void From_Protected_ReturnsProtected()
{
string result = CSharpAccessibilityKeyword.From(Accessibility.Protected);

Assert.That(result, Is.EqualTo("protected"));
}

[Test]
public void From_Internal_ReturnsInternal()
{
string result = CSharpAccessibilityKeyword.From(Accessibility.Internal);

Assert.That(result, Is.EqualTo("internal"));
}

[Test]
public void From_ProtectedOrInternal_ReturnsProtectedInternal()
{
string result = CSharpAccessibilityKeyword.From(Accessibility.ProtectedOrInternal);

Assert.That(result, Is.EqualTo("protected internal"));
}

[Test]
public void From_ProtectedAndInternal_ReturnsPrivateProtected()
{
string result = CSharpAccessibilityKeyword.From(Accessibility.ProtectedAndInternal);

Assert.That(result, Is.EqualTo("private protected"));
}

[Test]
public void From_Private_ReturnsPrivate()
{
string result = CSharpAccessibilityKeyword.From(Accessibility.Private);

Assert.That(result, Is.EqualTo("private"));
}

[Test]
public void From_NotApplicable_ReturnsPrivate()
{
string result = CSharpAccessibilityKeyword.From(Accessibility.NotApplicable);

Assert.That(result, Is.EqualTo("private"));
}

// -----------------------------------------------------------------------
// FromOrEmpty (returns "" as default)
// -----------------------------------------------------------------------

[Test]
public void FromOrEmpty_Public_ReturnsPublic()
{
string result = CSharpAccessibilityKeyword.FromOrEmpty(Accessibility.Public);

Assert.That(result, Is.EqualTo("public"));
}

[Test]
public void FromOrEmpty_Protected_ReturnsProtected()
{
string result = CSharpAccessibilityKeyword.FromOrEmpty(Accessibility.Protected);

Assert.That(result, Is.EqualTo("protected"));
}

[Test]
public void FromOrEmpty_Internal_ReturnsInternal()
{
string result = CSharpAccessibilityKeyword.FromOrEmpty(Accessibility.Internal);

Assert.That(result, Is.EqualTo("internal"));
}

[Test]
public void FromOrEmpty_Private_ReturnsEmptyString()
{
string result = CSharpAccessibilityKeyword.FromOrEmpty(Accessibility.Private);

Assert.That(result, Is.EqualTo(""));
}

[Test]
public void FromOrEmpty_NotApplicable_ReturnsEmptyString()
{
string result = CSharpAccessibilityKeyword.FromOrEmpty(Accessibility.NotApplicable);

Assert.That(result, Is.EqualTo(""));
}
}
137 changes: 137 additions & 0 deletions EasySourceGenerators.GeneratorTests/CSharpLiteralFormatterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
using EasySourceGenerators.Generators.SourceEmitting;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace EasySourceGenerators.GeneratorTests;

[TestFixture]
public class CSharpLiteralFormatterTests
{
// -----------------------------------------------------------------------
// FormatValueAsLiteral
// -----------------------------------------------------------------------

[Test]
public void FormatValueAsLiteral_NullValue_ReturnsDefault()
{
string result = CSharpLiteralFormatter.FormatValueAsLiteral(null, SpecialType.System_String, TypeKind.Class, "string");

Assert.That(result, Is.EqualTo("default"));
}

[Test]
public void FormatValueAsLiteral_StringType_ReturnsQuotedLiteral()
{
string result = CSharpLiteralFormatter.FormatValueAsLiteral("hello", SpecialType.System_String, TypeKind.Class, "string");

Assert.That(result, Is.EqualTo("\"hello\""));
}

[Test]
public void FormatValueAsLiteral_StringWithSpecialCharacters_ReturnsEscapedLiteral()
{
string result = CSharpLiteralFormatter.FormatValueAsLiteral("line1\nline2", SpecialType.System_String, TypeKind.Class, "string");

Assert.That(result, Does.Contain("\\n"));
}

[Test]
public void FormatValueAsLiteral_CharType_ReturnsSingleQuotedLiteral()
{
string result = CSharpLiteralFormatter.FormatValueAsLiteral("A", SpecialType.System_Char, TypeKind.Struct, "char");

Assert.That(result, Is.EqualTo("'A'"));
}

[Test]
public void FormatValueAsLiteral_BooleanTrue_ReturnsLowercase()
{
string result = CSharpLiteralFormatter.FormatValueAsLiteral("True", SpecialType.System_Boolean, TypeKind.Struct, "bool");

Assert.That(result, Is.EqualTo("true"));
}

[Test]
public void FormatValueAsLiteral_BooleanFalse_ReturnsLowercase()
{
string result = CSharpLiteralFormatter.FormatValueAsLiteral("False", SpecialType.System_Boolean, TypeKind.Struct, "bool");

Assert.That(result, Is.EqualTo("false"));
}

[Test]
public void FormatValueAsLiteral_EnumType_ReturnsPrefixedValue()
{
string result = CSharpLiteralFormatter.FormatValueAsLiteral("Red", SpecialType.None, TypeKind.Enum, "MyNamespace.Colors");

Assert.That(result, Is.EqualTo("MyNamespace.Colors.Red"));
}

[Test]
public void FormatValueAsLiteral_IntegerType_ReturnsValueAsIs()
{
string result = CSharpLiteralFormatter.FormatValueAsLiteral("42", SpecialType.System_Int32, TypeKind.Struct, "int");

Assert.That(result, Is.EqualTo("42"));
}

[Test]
public void FormatValueAsLiteral_DecimalType_ReturnsValueAsIs()
{
string result = CSharpLiteralFormatter.FormatValueAsLiteral("3.14", SpecialType.System_Double, TypeKind.Struct, "double");

Assert.That(result, Is.EqualTo("3.14"));
}

// -----------------------------------------------------------------------
// FormatKeyAsLiteral
// -----------------------------------------------------------------------

[Test]
public void FormatKeyAsLiteral_EnumType_ReturnsPrefixedValue()
{
string result = CSharpLiteralFormatter.FormatKeyAsLiteral("Green", TypeKind.Enum, "MyNamespace.Colors");

Assert.That(result, Is.EqualTo("MyNamespace.Colors.Green"));
}

[Test]
public void FormatKeyAsLiteral_BoolTrue_ReturnsTrueLiteral()
{
string result = CSharpLiteralFormatter.FormatKeyAsLiteral(true, TypeKind.Struct, "bool");

Assert.That(result, Is.EqualTo("true"));
}

[Test]
public void FormatKeyAsLiteral_BoolFalse_ReturnsFalseLiteral()
{
string result = CSharpLiteralFormatter.FormatKeyAsLiteral(false, TypeKind.Struct, "bool");

Assert.That(result, Is.EqualTo("false"));
}

[Test]
public void FormatKeyAsLiteral_String_ReturnsQuotedLiteral()
{
string result = CSharpLiteralFormatter.FormatKeyAsLiteral("hello", TypeKind.Class, "string");

Assert.That(result, Is.EqualTo("\"hello\""));
}

[Test]
public void FormatKeyAsLiteral_Integer_ReturnsToStringResult()
{
string result = CSharpLiteralFormatter.FormatKeyAsLiteral(42, TypeKind.Struct, "int");

Assert.That(result, Is.EqualTo("42"));
}

[Test]
public void FormatKeyAsLiteral_NullTypeKind_UsesDefaultBehavior()
{
string result = CSharpLiteralFormatter.FormatKeyAsLiteral(99, null, null);

Assert.That(result, Is.EqualTo("99"));
}
}
48 changes: 48 additions & 0 deletions EasySourceGenerators.GeneratorTests/CSharpTypeKeywordTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using EasySourceGenerators.Generators.SourceEmitting;
using Microsoft.CodeAnalysis;

namespace EasySourceGenerators.GeneratorTests;

[TestFixture]
public class CSharpTypeKeywordTests
{
[Test]
public void From_Class_ReturnsClass()
{
string result = CSharpTypeKeyword.From(TypeKind.Class);

Assert.That(result, Is.EqualTo("class"));
}

[Test]
public void From_Struct_ReturnsStruct()
{
string result = CSharpTypeKeyword.From(TypeKind.Struct);

Assert.That(result, Is.EqualTo("struct"));
}

[Test]
public void From_Interface_ReturnsInterface()
{
string result = CSharpTypeKeyword.From(TypeKind.Interface);

Assert.That(result, Is.EqualTo("interface"));
}

[Test]
public void From_Enum_ReturnsClass()
{
string result = CSharpTypeKeyword.From(TypeKind.Enum);

Assert.That(result, Is.EqualTo("class"));
}

[Test]
public void From_Delegate_ReturnsClass()
{
string result = CSharpTypeKeyword.From(TypeKind.Delegate);

Assert.That(result, Is.EqualTo("class"));
}
}
Loading
Loading