-
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 branch 'feature/TypedMetadata'
Closes #2 * feature/TypedMetadata: More README CodeFactor yelled at me Update README Use ReferenceEquals instead of == Bump version New class SimpleMetadata Visual studio keeps changing this XML back Clean CodeFactor recommendations Add CodeFactor badge to README
- Loading branch information
Showing
10 changed files
with
252 additions
and
9 deletions.
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
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,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace BoringHelpers.Types | ||
{ | ||
public abstract class CaseInsensitiveMetadata : SimpleMetadata<string> | ||
{ | ||
protected override IEqualityComparer<string> EqualityComparer { get { return StringComparer.OrdinalIgnoreCase; } } | ||
protected override IComparer<string> Comparer { get { return StringComparer.OrdinalIgnoreCase; } } | ||
|
||
protected CaseInsensitiveMetadata(string value) | ||
: base(value) { } | ||
} | ||
} |
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.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace BoringHelpers.Types | ||
{ | ||
public abstract class SimpleMetadata<T> : IComparable<SimpleMetadata<T>>, IEquatable<SimpleMetadata<T>>, IComparable where T : IComparable<T>, IComparable | ||
{ | ||
private const int CompareIsEqual = 0; | ||
public readonly T Value; | ||
|
||
protected SimpleMetadata(T value) | ||
{ | ||
this.Value = value; | ||
} | ||
|
||
protected virtual IEqualityComparer<T> EqualityComparer { get { return EqualityComparer<T>.Default; } } | ||
protected virtual IComparer<T> Comparer { get { return Comparer<T>.Default; } } | ||
|
||
public override bool Equals(object obj) => this.Equals(obj as SimpleMetadata<T>); | ||
|
||
public bool Equals(SimpleMetadata<T> other) | ||
{ | ||
if (object.ReferenceEquals(other, null)) return false; | ||
if (this.GetType() != other.GetType()) return false; | ||
return this.CompareTo(other) == CompareIsEqual; | ||
} | ||
|
||
public bool Equals(T other) => Comparer.Compare(this.Value, other) == CompareIsEqual; | ||
|
||
public virtual int CompareTo(object obj) | ||
{ | ||
if (this.GetType() != obj.GetType()) throw new ArgumentException($"{nameof(obj)} not the same type as this instance"); | ||
return this.CompareTo(obj as SimpleMetadata<T>); | ||
} | ||
|
||
public virtual int CompareTo(SimpleMetadata<T> other) => Comparer.Compare(this.Value, other.Value); | ||
|
||
public override int GetHashCode() => EqualityComparer.GetHashCode(this.Value); | ||
|
||
public override string ToString() => this.Value.ToString(); | ||
|
||
public static bool operator ==(SimpleMetadata<T> obj1, SimpleMetadata<T> obj2) | ||
{ | ||
if (object.ReferenceEquals(obj1, null)) | ||
{ | ||
return object.ReferenceEquals(obj2, null); | ||
} | ||
|
||
return obj1.Equals(obj2); | ||
} | ||
|
||
public static bool operator ==(SimpleMetadata<T> obj1, T obj2) | ||
{ | ||
if (object.ReferenceEquals(obj1, null)) | ||
{ | ||
return object.ReferenceEquals(obj2, null); | ||
} | ||
|
||
return obj1.Equals(obj2); | ||
} | ||
|
||
public static bool operator !=(SimpleMetadata<T> obj1, SimpleMetadata<T> obj2) | ||
{ | ||
if (object.ReferenceEquals(obj1, null)) | ||
{ | ||
return !object.ReferenceEquals(obj2, null); | ||
} | ||
|
||
return !obj1.Equals(obj2); | ||
} | ||
|
||
public static bool operator !=(SimpleMetadata<T> obj1, T obj2) | ||
{ | ||
if (object.ReferenceEquals(obj1, null)) | ||
{ | ||
return !object.ReferenceEquals(obj2, null); | ||
} | ||
|
||
return !obj1.Equals(obj2); | ||
} | ||
} | ||
} |
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,74 @@ | ||
using Xunit; | ||
using System.Collections.Generic; | ||
using BoringHelpers.Types; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace BoringHelpersTests.Types | ||
{ | ||
public class SimpleMetadata | ||
{ | ||
[Fact] | ||
public void Simple_Equal() | ||
{ | ||
var value = "foobar"; | ||
var item1 = new SimpleString(value); | ||
var item2 = new SimpleString(value); | ||
Assert.Equal(item1, item2); | ||
} | ||
|
||
[Fact] | ||
public void Simple_Equal_Direct() | ||
{ | ||
var value = "foobar"; | ||
var item1 = new SimpleString("foobar"); | ||
Assert.True(item1 == value); | ||
} | ||
|
||
[Fact] | ||
public void Simple_NotEqual() | ||
{ | ||
var value = "foobar"; | ||
var item1 = new SimpleString(value.ToLower()); | ||
var item2 = new SimpleString(value.ToUpper()); | ||
Assert.NotEqual(item1, item2); | ||
} | ||
|
||
[Fact] | ||
public void Simple_NotEqual_Direct() | ||
{ | ||
var value = "foobar"; | ||
var item1 = new SimpleString(value.ToLower()); | ||
Assert.False(item1 == value.ToUpper()); | ||
} | ||
|
||
[Fact] | ||
public void CaseInsentive_Equal() | ||
{ | ||
var value = "foobar"; | ||
var item1 = new SimpleStringIgnoreCase(value.ToLower()); | ||
var item2 = new SimpleStringIgnoreCase(value.ToUpper()); | ||
Assert.Equal(item1, item2); | ||
} | ||
|
||
[Fact] | ||
public void CaseInsentive_NotEqual() | ||
{ | ||
var value = "foobar"; | ||
var item1 = new SimpleStringIgnoreCase(value); | ||
var item2 = new SimpleStringIgnoreCase(value+"_"); | ||
Assert.NotEqual(item1, item2); | ||
} | ||
|
||
[Fact] | ||
public void SortedSame() | ||
{ | ||
var orignal = Enumerable.Range(0,5).Select(i => i.ToString()); | ||
var metadata = orignal.Select(s => new SimpleString(s)); | ||
|
||
Assert.Equal(orignal.OrderBy(s => s), metadata.OrderBy(m => m).Select(m => m.Value)); | ||
Assert.Equal(orignal.OrderByDescending(s => s), metadata.OrderByDescending(m => m).Select(m => m.Value)); | ||
Assert.NotEqual(orignal.OrderBy(s => s), metadata.OrderByDescending(m => m).Select(m => m.Value)); | ||
} | ||
} | ||
} |
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,12 @@ | ||
using BoringHelpers.Types; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace BoringHelpersTests.Types | ||
{ | ||
public class SimpleString : SimpleMetadata<string> | ||
{ | ||
public SimpleString(string value) | ||
: base(value) { } | ||
} | ||
} |
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,12 @@ | ||
using BoringHelpers.Types; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace BoringHelpersTests.Types | ||
{ | ||
public class SimpleStringIgnoreCase : CaseInsensitiveMetadata | ||
{ | ||
public SimpleStringIgnoreCase(string value) | ||
: base(value) { } | ||
} | ||
} |
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