Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added OclEnumDefaultValueAttribute #134

Merged
merged 1 commit into from
Apr 22, 2024
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
17 changes: 16 additions & 1 deletion source/Ocl/Converters/EnumAttributeOclConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,22 @@ public OclDocument ToDocument(OclConversionContext context, object obj)

public IEnumerable<IOclElement> ToElements(OclConversionContext context, PropertyInfo? propertyInfo, object obj)
{
var isDefault = Activator.CreateInstance(obj.GetType()).Equals(obj);
var isDefault = false;
var defaultValueAttributeType = typeof(OclDefaultEnumValueAttribute);
var defaultValueAttribute = propertyInfo?.GetCustomAttribute(defaultValueAttributeType);

if (defaultValueAttribute != null)
{
var defaultValueProperty = defaultValueAttributeType.GetProperty("DefaultValue");
var defaultValue = defaultValueProperty?.GetValue(defaultValueAttribute);

isDefault = Equals(obj, defaultValue);
}
else
{
isDefault = Activator.CreateInstance(obj.GetType()).Equals(obj);
}

if (!isDefault)
yield return new OclAttribute(context.Namer.GetName(propertyInfo!), obj.ToString());
}
Expand Down
20 changes: 20 additions & 0 deletions source/Ocl/Converters/OclEnumDefaultValueAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace Octopus.Ocl.Converters
{
[AttributeUsage(AttributeTargets.Property)]
public class OclDefaultEnumValueAttribute : Attribute
{
public OclDefaultEnumValueAttribute(object defaultValue)
{
if (!(defaultValue is Enum))
{
throw new ArgumentException("defaultValue must be an enum", nameof(defaultValue));
}

DefaultValue = defaultValue;
}

public object DefaultValue { get; }
}
}
83 changes: 83 additions & 0 deletions source/Tests/Converters/EnumAttributeConverterFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Linq;
using FluentAssertions;
using NUnit.Framework;
using Octopus.Ocl;
using Octopus.Ocl.Converters;

namespace Tests.Converters
{
internal class EnumAttributeConverterFixture
{
[Test]
public void WhenConvertingEnum_ValueIsEnumDefault_ReturnsNull()
{
var context = new OclConversionContext(new OclSerializerOptions());

var result = (OclAttribute?)new EnumAttributeOclConverter().ToElements(context, typeof(Dummy).GetProperty(nameof(Dummy.TestProperty))!, Test.Value1).SingleOrDefault();

result.Should().BeNull();
}

[Test]
public void WhenConvertingEnum_ValueIsNotEnumDefault_ReturnsValueOfProperty()
{
var context = new OclConversionContext(new OclSerializerOptions());

var result = (OclAttribute?)new EnumAttributeOclConverter().ToElements(context, typeof(Dummy).GetProperty(nameof(Dummy.TestProperty))!, Test.Value2).SingleOrDefault();

result.Should().NotBeNull();
result!.Value.Should().Be("Value2");
}

[Test]
public void WhenConvertingEnumWithDefaultAttribute_ValueIsEnumDefault_ReturnsValueOfProperty()
{
var context = new OclConversionContext(new OclSerializerOptions());

var result = (OclAttribute?)new EnumAttributeOclConverter().ToElements(context, typeof(DummyWithDefault).GetProperty(nameof(DummyWithDefault.TestProperty))!, Test.Value1).SingleOrDefault();

result.Should().NotBeNull();
result!.Value.Should().Be("Value1");
}

[Test]
public void WhenConvertingEnumWithDefaultAttribute_ValueIsNotADefault_ReturnsValueOfProperty()
{
var context = new OclConversionContext(new OclSerializerOptions());

var result = (OclAttribute?)new EnumAttributeOclConverter().ToElements(context, typeof(DummyWithDefault).GetProperty(nameof(DummyWithDefault.TestProperty))!, Test.Value2).SingleOrDefault();

result.Should().NotBeNull();
result!.Value.Should().Be("Value2");
}

[Test]
public void WhenConvertingEnumWithDefaultAttribute_ValueIstAttributedDefault_ReturnsNull()
{
var context = new OclConversionContext(new OclSerializerOptions());

var result = (OclAttribute?)new EnumAttributeOclConverter().ToElements(context, typeof(DummyWithDefault).GetProperty(nameof(DummyWithDefault.TestProperty))!, Test.Value3).SingleOrDefault();

result.Should().BeNull();
}

enum Test
{
Value1,
Value2,
Value3,
}

class Dummy
{
public Test TestProperty { get; } = Test.Value1;
}

class DummyWithDefault
{
[OclDefaultEnumValue(Test.Value3)]
public Test TestProperty { get; } = Test.Value1;
}
}
}