Skip to content

Commit

Permalink
Option.Some method should not allow null value (#445)
Browse files Browse the repository at this point in the history
* add validation to ensure null should not be allowed when calling Option.Some method.
  • Loading branch information
philipktlin authored Oct 17, 2018
1 parent 1217b83 commit a3ec19e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,7 @@ public async void ReportShutdown()
// build current module set
ModuleSet currentModuleSet = ModuleSet.Create(
edgeAgent,
edgeHub,
new TestRuntimeModule(
"mod1", "1.0", RestartPolicy.OnUnhealthy, "test", ModuleStatus.Running,
new TestConfig("image1"), 0, string.Empty, DateTime.MinValue, DateTime.MinValue,
Expand Down Expand Up @@ -1202,6 +1203,13 @@ public async void ReportShutdown()
{
runtimeStatus = "unknown"
}
},
{
edgeHub.Name,
new
{
runtimeStatus = "unknown"
}
}
},
modules = new Dictionary<string, object>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task SmokeTest()
{
await executor.Invoke(msg);
}
await Task.Delay(20);
await Task.Delay(30);
await executor.CloseAsync();
Assert.Equal(3, endpoint.N);
Assert.Equal(expected, endpoint.Processed);
Expand Down
14 changes: 9 additions & 5 deletions edge-util/src/Microsoft.Azure.Devices.Edge.Util/Option.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ public void ForEach(Action<T> action)

/// <summary>
/// If this option has a value then it transforms it into a new option instance by
/// calling the <paramref name="mapping"/> callback. Returns <see cref="Option.None{T}"/>
/// if there is no value.
/// calling the <paramref name="mapping"/> callback. It will follow exception if callback returns null.
/// Returns <see cref="Option.None{T}"/> if there is no value.
/// </summary>
[Pure]
public Option<TResult> Map<TResult>(Func<T, TResult> mapping)
Expand Down Expand Up @@ -203,15 +203,19 @@ public static class Option
/// Creates an <c>Option &lt;T&gt;</c> with <paramref name="value"/> and marks
/// the option object as having a value, i.e., <c>Option&lt;T&gt;.HasValue == true</c>.
/// </summary>
public static Option<T> Some<T>(T value) => new Option<T>(value, true);
public static Option<T> Some<T>(T value)
{
Preconditions.CheckNotNull(value, nameof(value));

return new Option<T>(value, true);
}

/// <summary>
/// Creates an <c>Option &lt;T&gt;</c> with a default value (<c>default(T)</c>) and marks
/// the option object as having no value, i.e., <c>Option&lt;T&gt;.HasValue == false</c>.
/// </summary>
public static Option<T> None<T>() => new Option<T>(default(T), false);

public static Option<T> Maybe<T>(T value) where T : class =>
value == null ? None<T>() : Some(value);
public static Option<T> Maybe<T>(T value) where T : class => value == null ? None<T>() : Some(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public void TestHashCode()
{
Option<int> some1 = Option.Some(1);
Option<int> some2 = Option.Some(2);
Option<object> some3 = Option.Some<object>(null);
Option<object> some3 = Option.None<object>();
Option<int> none = Option.None<int>();

Assert.Equal(0, none.GetHashCode());
Assert.Equal(1, some3.GetHashCode());
Assert.Equal(0, some3.GetHashCode());
Assert.NotEqual(some1.GetHashCode(), some2.GetHashCode());
}

Expand All @@ -71,11 +71,11 @@ public void TestHashCode()
public void TestToString()
{
Option<int> some1 = Option.Some(1);
Option<object> some2 = Option.Some<object>(null);
Option<object> some2 = Option.Some<object>(new object());
Option<int> none = Option.None<int>();

Assert.Equal("Some(1)", some1.ToString());
Assert.Equal("Some(null)", some2.ToString());
Assert.Equal("Some(System.Object)", some2.ToString());
Assert.Equal("None", none.ToString());
}

Expand Down Expand Up @@ -111,12 +111,13 @@ public void TestContains()
{
Option<int> some = Option.Some(3);
Option<int> none = Option.None<int>();
Option<object> some2 = Option.Some<object>(null);
var value = new object();
Option<object> some2 = Option.Some<object>(value);

Assert.True(some.Contains(3));
Assert.False(some.Contains(2));
Assert.False(none.Contains(3));
Assert.True(some2.Contains(null));
Assert.True(some2.Contains(value));
}

[Fact]
Expand Down

0 comments on commit a3ec19e

Please sign in to comment.