Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
{
// -----------------------------------------------------------------------
// ToKeyword with defaultToPrivate = true (default)
// -----------------------------------------------------------------------

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// -----------------------------------------------------------------------
// ToKeyword with defaultToPrivate = false
// -----------------------------------------------------------------------

[Test]
public void ToKeyword_DefaultToPrivateFalse_Public_ReturnsPublic()
{
string result = CSharpAccessibilityKeyword.ToKeyword(Accessibility.Public, defaultToPrivate: false);

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

[Test]
public void ToKeyword_DefaultToPrivateFalse_Protected_ReturnsProtected()
{
string result = CSharpAccessibilityKeyword.ToKeyword(Accessibility.Protected, defaultToPrivate: false);

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

[Test]
public void ToKeyword_DefaultToPrivateFalse_Internal_ReturnsInternal()
{
string result = CSharpAccessibilityKeyword.ToKeyword(Accessibility.Internal, defaultToPrivate: false);

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

[Test]
public void ToKeyword_DefaultToPrivateFalse_Private_ReturnsEmptyString()
{
string result = CSharpAccessibilityKeyword.ToKeyword(Accessibility.Private, defaultToPrivate: false);

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

[Test]
public void ToKeyword_DefaultToPrivateFalse_NotApplicable_ReturnsEmptyString()
{
string result = CSharpAccessibilityKeyword.ToKeyword(Accessibility.NotApplicable, defaultToPrivate: false);

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"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using EasySourceGenerators.Generators.IncrementalGenerators;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace EasySourceGenerators.GeneratorTests;

[TestFixture]
public class DiagnosticMessageHelperTests
{
[Test]
public void JoinErrorDiagnostics_EmptyList_ReturnsEmptyString()
{
string result = DiagnosticMessageHelper.JoinErrorDiagnostics(Array.Empty<Diagnostic>());

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

[Test]
public void JoinErrorDiagnostics_OnlyWarnings_ReturnsEmptyString()
{
Diagnostic[] diagnostics = new[]
{
CreateDiagnostic(DiagnosticSeverity.Warning, "This is a warning")
};

string result = DiagnosticMessageHelper.JoinErrorDiagnostics(diagnostics);

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

[Test]
public void JoinErrorDiagnostics_SingleError_ReturnsSingleMessage()
{
Diagnostic[] diagnostics = new[]
{
CreateDiagnostic(DiagnosticSeverity.Error, "Something went wrong")
};

string result = DiagnosticMessageHelper.JoinErrorDiagnostics(diagnostics);

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

[Test]
public void JoinErrorDiagnostics_MultipleErrors_JoinsWithSemicolon()
{
Diagnostic[] diagnostics = new[]
{
CreateDiagnostic(DiagnosticSeverity.Error, "First error"),
CreateDiagnostic(DiagnosticSeverity.Error, "Second error")
};

string result = DiagnosticMessageHelper.JoinErrorDiagnostics(diagnostics);

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

[Test]
public void JoinErrorDiagnostics_MixedSeverities_ReturnsOnlyErrors()
{
Diagnostic[] diagnostics = new[]
{
CreateDiagnostic(DiagnosticSeverity.Warning, "A warning"),
CreateDiagnostic(DiagnosticSeverity.Error, "An error"),
CreateDiagnostic(DiagnosticSeverity.Info, "An info")
};

string result = DiagnosticMessageHelper.JoinErrorDiagnostics(diagnostics);

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

private static Diagnostic CreateDiagnostic(DiagnosticSeverity severity, string message)
{
DiagnosticDescriptor descriptor = new(
id: "TEST001",
title: "Test",
messageFormat: message,
category: "Test",
defaultSeverity: severity,
isEnabledByDefault: true);

return Diagnostic.Create(descriptor, Location.None);
}
}
Loading
Loading