-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #134 from OctopusDeploy/sjc/add-enum-default-attri…
…bute
- Loading branch information
Showing
3 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |