Skip to content

Commit 3907d2f

Browse files
committed
merged tests
2 parents 87e8412 + 096ae8b commit 3907d2f

File tree

6 files changed

+154
-8
lines changed

6 files changed

+154
-8
lines changed

Enum.Ext/Enum.Ext.Tests/Enum.Ext.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
10+
<PackageReference Include="FluentAssertions" Version="5.7.0" />
1111
<PackageReference Include="nunit" Version="3.12.0" />
1212
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
1313
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" />
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using NUnit.Framework;
1+
using FluentAssertions;
2+
using NUnit.Framework;
23
using System;
34

45
namespace Enum.Ext.Tests
56
{
6-
public class BasicTests
7+
public class WeekdayTests
78
{
89
[SetUp]
910
public void InitWeekday()
@@ -16,21 +17,23 @@ public void Test_ConvertToInt()
1617
{
1718
int value = Weekday.Monday;
1819

19-
Assert.AreEqual(1, value);
20+
value.Should().Be(1);
2021
}
2122

2223
[Test]
2324
public void Test_ConvertFromInt()
2425
{
2526
Weekday value = (Weekday)1;
2627

27-
Assert.AreEqual(Weekday.Monday, value);
28+
value.Should().Be(Weekday.Monday);
2829
}
2930

3031
[Test]
3132
public void Test_ThrowsWhenSameId()
3233
{
33-
Assert.Throws<TypeInitializationException>(() => Initialize.InitStaticFields<WrongEnum>());
34+
Action secondInitialize = () => Initialize.InitStaticFields<WrongEnum>();
35+
36+
secondInitialize.Should().Throw<TypeInitializationException>();
3437
}
3538
}
3639
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Enum.Ext.Tests
7+
{
8+
public sealed class YearlyPrice : TypeSafeEnum<YearlyPrice, int>
9+
{
10+
public decimal Price { get; private set; }
11+
12+
public DateTime ValidFrom { get; private set; }
13+
14+
public DateTime ValidTo { get; private set; }
15+
16+
public static readonly YearlyPrice Price_2018 =
17+
new YearlyPrice(1, 15.99m, new DateTime(2018, 1, 1), new DateTime(2018, 12, 31));
18+
19+
public static readonly YearlyPrice Price_2019 =
20+
new YearlyPrice(2, 16.99m, new DateTime(2019, 1, 1), new DateTime(2019, 12, 31));
21+
22+
public YearlyPrice(int id, decimal price, DateTime validFrom, DateTime validTo) : base(id)
23+
{
24+
ValidFrom = validFrom;
25+
ValidTo = validTo;
26+
Price = price;
27+
}
28+
29+
public static YearlyPrice GetPriceByDate(DateTime date)
30+
{
31+
return List.FirstOrDefault(x => x.ValidFrom <= date && date <= x.ValidTo);
32+
}
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using FluentAssertions;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
7+
namespace Enum.Ext.Tests
8+
{
9+
[TestFixture]
10+
public class YearlyPriceTests
11+
{
12+
[SetUp]
13+
public void InitYearlyPrice()
14+
{
15+
Initialize.InitStaticFields<YearlyPrice>();
16+
}
17+
18+
[Test]
19+
public void QueryByDate_ShouldReturnCorrectEnum()
20+
{
21+
DateTime year_2018 = new DateTime(2018, 5, 3);
22+
23+
YearlyPrice.GetPriceByDate(year_2018).Should().Be(YearlyPrice.Price_2018);
24+
}
25+
26+
[Test]
27+
public void Enum_ShouldHoldCorrectAdditionalInformation()
28+
{
29+
var priceEnum = YearlyPrice.Price_2018;
30+
31+
priceEnum.Price.Should().Be(15.99m);
32+
}
33+
}
34+
}

README.md

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# Enum.Ext
22

3-
[![Build Status](https://mauracher.visualstudio.com/enum_ext/_apis/build/status/simonmau.enum_ext?branchName=master)](https://mauracher.visualstudio.com/enum_ext/_build/latest?definitionId=20&branchName=master)
3+
[![Build Status](https://mauracher.visualstudio.com/enum_ext/_apis/build/status/simonmau.enum_ext?branchName=master)](https://mauracher.visualstudio.com/enum_ext/_build/latest?definitionId=20&branchName=master) [![Version](https://img.shields.io/nuget/v/Enum.Ext.svg)](https://www.nuget.org/packages/Enum.Ext)
44

5-
Enum.Ext provides a TypeSafeEnum that has many advantages compared to the normal .NET Enum value type
5+
Enum.Ext provides a `TypeSafeEnum` that has a bunch of advantages compared to the normal .NET `Enum` value type.
6+
7+
For example is it possible, to store additional information directly with the enum. You are also able to
8+
query an enum based on the information stored with it.
9+
10+
There is also a Json-Serializer Implemented, so you dont have to cast from DTOs manually.
611

712
### Installation
813
https://www.nuget.org/packages/Enum.Ext/
@@ -13,3 +18,73 @@ Enum.Ext can be installed using the following command via the NuGet package mana
1318

1419

1520
### How to use
21+
22+
Simply inherit your class from `TypeSafeEnum` or `TypeSafeNameEnum` and adjust everything to your needs.
23+
24+
25+
26+
27+
28+
#### Want a weekday enum with a special string representation for each day?
29+
```C#
30+
public sealed class Weekday : TypeSafeNameEnum<Weekday, int>
31+
{
32+
public static readonly Weekday Monday = new Weekday(1, "--Monday--");
33+
public static readonly Weekday Tuesday = new Weekday(2, "--Tuesday--");
34+
public static readonly Weekday Wednesday = new Weekday(3, "--Wednesday--");
35+
....
36+
37+
public Weekday(int id, string name) : base(id, name)
38+
{
39+
}
40+
}
41+
```
42+
43+
Use the enum just like the native one
44+
45+
```C#
46+
var day = Weekday.Monday;
47+
48+
// Assigns Tuesday
49+
day = (Weekday)2;
50+
```
51+
52+
and access the additional information easily
53+
54+
```C#
55+
var day = Weekday.Monday;
56+
57+
// Prints out '--Monday--'
58+
Console.WriteLine(day.Name);
59+
```
60+
61+
#### A fixed price that is valid for a certain time period
62+
```C#
63+
public sealed class YearlyPrice : TypeSafeEnum<YearlyPrice, int>
64+
{
65+
public decimal Price { get; private set; }
66+
67+
public DateTime ValidFrom { get; private set; }
68+
69+
public DateTime ValidTo { get; private set; }
70+
71+
public static readonly YearlyPrice Price_2018 =
72+
new YearlyPrice(1, 15.99m, new DateTime(2018, 1, 1), new DateTime(2018, 12, 31));
73+
74+
public static readonly YearlyPrice Price_2019 =
75+
new YearlyPrice(2, 16.99m, new DateTime(2019, 1, 1), new DateTime(2019, 12, 31));
76+
77+
public YearlyPrice(int id, decimal price, DateTime validFrom, DateTime validTo) : base(id)
78+
{
79+
ValidFrom = validFrom;
80+
ValidTo = validTo;
81+
Price = price;
82+
}
83+
84+
public static YearlyPrice GetPriceByDate(DateTime date)
85+
{
86+
return List.FirstOrDefault(x => x.ValidFrom <= date && date <= x.ValidTo);
87+
}
88+
}
89+
```
90+

Resources/Logo/EXT_64.png

1.65 KB
Loading

0 commit comments

Comments
 (0)