Skip to content

Commit

Permalink
Merge pull request #134 from OctopusDeploy/sjc/add-enum-default-attri…
Browse files Browse the repository at this point in the history
…bute
  • Loading branch information
stevencl840 authored Apr 22, 2024
2 parents 2abf077 + 395af2f commit 2e94969
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
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;
}
}
}

0 comments on commit 2e94969

Please sign in to comment.