Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Added tests for Microsoft.VisualBasic.CompilerServices.Conversions #1… #26744

Merged
merged 4 commits into from
Feb 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: blank line.

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>