Skip to content

Commit

Permalink
Added tests for Microsoft.VisualBasic.CompilerServices.Conversions do…
Browse files Browse the repository at this point in the history
…tnet#1… (dotnet#26744)

* Added tests for Microsoft.VisualBasic.CompilerServices.Conversions #14344

* added tests for ToBoolean fro Object
  • Loading branch information
Oswald Maskens authored and A-And committed Feb 20, 2018
1 parent c76c004 commit cd69c2d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
81 changes: 81 additions & 0 deletions src/Microsoft.VisualBasic/tests/ConversionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using Microsoft.VisualBasic.CompilerServices;
using Xunit;

namespace Microsoft.VisualBasic.Tests
{
public class ConversionsTests
{
[Theory]
[InlineData("true", true)]
[InlineData("false", false)]
[InlineData("5", true)]
[InlineData("0", false)]
[InlineData("5.5", true)]
[InlineData("0.0", false)]
[InlineData("&h5", true)]
[InlineData("&h0", false)]
[InlineData("&o5", true)]
[InlineData("&o0", false)]
public void ToBoolean_String_ReturnsExpected(string str, bool expected)
{
Assert.Equal(expected, Conversions.ToBoolean(str));
}

[Theory]
[InlineData(null)]
[InlineData("yes")]
[InlineData("contoso")]
public void ToBoolean_String_ThrowsOnInvalidFormat(string str)
{
Assert.Throws<InvalidCastException>(() => Conversions.ToBoolean(str));
}

public static IEnumerable<object[]> ToBoolean_Object_ReturnsExpected_TestData()
{
yield return new object[] { null, false };
yield return new object[] { false, false };
yield return new object[] { true, true };
yield return new object[] { (sbyte)0, false };
yield return new object[] { (sbyte)42, true };
yield return new object[] { (byte)0, false };
yield return new object[] { (byte)42, true };
yield return new object[] { (System.Int16)0, false };
yield return new object[] { (System.Int16)42, true };
yield return new object[] { (System.UInt16)0, false };
yield return new object[] { (System.UInt16)42, true };
yield return new object[] { (System.Int32)0, false };
yield return new object[] { (System.Int32)42, true };
yield return new object[] { (System.UInt32)0, false };
yield return new object[] { (System.UInt32)42, true };
yield return new object[] { (System.Int64)0, false };
yield return new object[] { (System.Int64)42, true };
yield return new object[] { (System.UInt64)0, false };
yield return new object[] { (System.UInt64)42, true };
yield return new object[] { 0.0m, false };
yield return new object[] { 0.42m, true };
yield return new object[] { (float)0.0, false };
yield return new object[] { (float)0.42, true };
yield return new object[] { (double)0.0, false };
yield return new object[] { (double)0.42, true };
}

[Theory]
[MemberData(nameof(ToBoolean_Object_ReturnsExpected_TestData))]
public void ToBoolean_Object_ReturnsExpected(object obj, bool expected)
{
Assert.Equal(expected, Conversions.ToBoolean(obj));
}

[Fact]
public void ToBoolean_Object_ThrowsOn_List()
{
Assert.Throws<InvalidCastException>(() => Conversions.ToBoolean(new List<string>()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netstandard-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netstandard-Release|AnyCPU'" />
<ItemGroup>
<Compile Include="ConversionsTests.cs" />
<Compile Include="OperatorsTests.cs" />
<Compile Include="UtilsTests.cs" />
<Compile Include="StringsTests.cs" />
Expand All @@ -18,4 +19,4 @@
<EmbeddedResource Include="Resources\$(AssemblyName).rd.xml" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>
</Project>

0 comments on commit cd69c2d

Please sign in to comment.