From 93faaeefc8cecbbfc132ee89689ca2e725df1dbf Mon Sep 17 00:00:00 2001 From: Charles Stoner Date: Fri, 19 Apr 2019 23:31:00 -0700 Subject: [PATCH] Port Microsoft.VisualBasic.Conversion class (dotnet/corefx#37047) Commit migrated from https://github.com/dotnet/corefx/commit/c608ddaa2a024dde2510f3c1122c89ba07b4325d --- .../ref/Microsoft.VisualBasic.Core.cs | 63 ++ .../ref/Microsoft.VisualBasic.Core.csproj | 1 + .../src/Microsoft.VisualBasic.Core.vbproj | 1 + .../CompilerServices/LateBinding.vb | 1 - .../CompilerServices/ObjectType.vb | 3 - .../CompilerServices/Utils.LateBinder.vb | 127 ++- .../VisualBasic/CompilerServices/Versioned.vb | 43 + .../src/Microsoft/VisualBasic/Conversion.vb | 960 ++++++++++++++++++ .../src/Microsoft/VisualBasic/Information.vb | 119 +++ .../src/Microsoft/VisualBasic/Interaction.vb | 2 +- .../src/Microsoft/VisualBasic/Strings.vb | 96 +- .../tests/CompilerServices/VersionedTests.cs | 98 ++ .../tests/ConversionTests.cs | 793 +++++++++++++++ .../tests/InformationTests.cs | 34 + .../tests/InteractionTests.cs | 77 ++ .../Microsoft.VisualBasic.Core.Tests.csproj | 2 + .../tests/StringsTests.cs | 12 + 17 files changed, 2425 insertions(+), 7 deletions(-) create mode 100644 src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Conversion.vb create mode 100644 src/libraries/Microsoft.VisualBasic.Core/tests/ConversionTests.cs create mode 100644 src/libraries/Microsoft.VisualBasic.Core/tests/InteractionTests.cs diff --git a/src/libraries/Microsoft.VisualBasic.Core/ref/Microsoft.VisualBasic.Core.cs b/src/libraries/Microsoft.VisualBasic.Core/ref/Microsoft.VisualBasic.Core.cs index 5942c757cb7..2016691b8a5 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/ref/Microsoft.VisualBasic.Core.cs +++ b/src/libraries/Microsoft.VisualBasic.Core/ref/Microsoft.VisualBasic.Core.cs @@ -94,6 +94,59 @@ namespace Microsoft.VisualBasic public ControlChars() { } } [Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute] + public sealed partial class Conversion + { + internal Conversion() { } + public static TargetType CTypeDynamic(object Expression) { throw null; } + public static object CTypeDynamic(object Expression, System.Type TargetType) { throw null; } + public static string ErrorToString() { throw null; } + public static string ErrorToString(int ErrorNumber) { throw null; } + public static int Fix(int Number) { throw null; } + public static long Fix(long Number) { throw null; } + public static double Fix(double Number) { throw null; } + public static float Fix(float Number) { throw null; } + public static decimal Fix(decimal Number) { throw null; } + public static object Fix(object Number) { throw null; } + public static short Fix(short Number) { throw null; } + public static string Hex(object Number) { throw null; } + [System.CLSCompliant(false)] + public static string Hex(ulong Number) { throw null; } + public static string Hex(long Number) { throw null; } + public static string Hex(int Number) { throw null; } + public static string Hex(short Number) { throw null; } + public static string Hex(byte Number) { throw null; } + [System.CLSCompliant(false)] + public static string Hex(uint Number) { throw null; } + [System.CLSCompliant(false)] + public static string Hex(ushort Number) { throw null; } + [System.CLSCompliant(false)] + public static string Hex(sbyte Number) { throw null; } + public static short Int(short Number) { throw null; } + public static int Int(int Number) { throw null; } + public static object Int(object Number) { throw null; } + public static double Int(double Number) { throw null; } + public static float Int(float Number) { throw null; } + public static long Int(long Number) { throw null; } + public static decimal Int(decimal Number) { throw null; } + public static string Oct(object Number) { throw null; } + [System.CLSCompliant(false)] + public static string Oct(ulong Number) { throw null; } + [System.CLSCompliant(false)] + public static string Oct(uint Number) { throw null; } + public static string Oct(long Number) { throw null; } + public static string Oct(short Number) { throw null; } + public static string Oct(byte Number) { throw null; } + [System.CLSCompliant(false)] + public static string Oct(sbyte Number) { throw null; } + public static string Oct(int Number) { throw null; } + [System.CLSCompliant(false)] + public static string Oct(ushort Number) { throw null; } + public static string Str(object Number) { throw null; } + public static double Val(string InputStr) { throw null; } + public static int Val(char Expression) { throw null; } + public static double Val(object Expression) { throw null; } + } + [Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute] public sealed partial class DateAndTime { internal DateAndTime() { } @@ -133,10 +186,17 @@ namespace Microsoft.VisualBasic public static int QBColor(int Color) { throw null; } public static int RGB(int Red, int Green, int Blue) { throw null; } public static string SystemTypeName(string VbName) { throw null; } + public static string TypeName(object VarName) { throw null; } public static int UBound(System.Array Array, int Rank = 1) { throw null; } public static Microsoft.VisualBasic.VariantType VarType(object VarName) { throw null; } public static string VbTypeName(string UrtName) { throw null; } } + [Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute] + public sealed partial class Interaction + { + internal Interaction() { } + public static object CallByName(object ObjectRef, string ProcName, CallType UseCallType, params object[] Args) { throw null; } + } [System.AttributeUsageAttribute(System.AttributeTargets.Class, AllowMultiple=false, Inherited=false)] [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] public sealed partial class MyGroupCollectionAttribute : System.Attribute @@ -189,6 +249,7 @@ namespace Microsoft.VisualBasic public static string Right(string str, int Length) { throw null; } public static string RTrim(string str) { throw null; } public static int StrComp(string String1, string String2, CompareMethod Compare = CompareMethod.Binary) { throw null; } + public static string StrReverse(string Expression) { throw null; } public static string Trim(string str) { throw null; } } public enum VariantType @@ -647,8 +708,10 @@ namespace Microsoft.VisualBasic.CompilerServices public sealed partial class Versioned { internal Versioned() { } + public static object CallByName(object Instance, string MethodName, CallType UseCallType, params object[] Arguments) { throw null; } public static bool IsNumeric(object Expression) { throw null; } public static string SystemTypeName(string VbName) { throw null; } + public static string TypeName(object Expression) { throw null; } public static string VbTypeName(string SystemName) { throw null; } } } diff --git a/src/libraries/Microsoft.VisualBasic.Core/ref/Microsoft.VisualBasic.Core.csproj b/src/libraries/Microsoft.VisualBasic.Core/ref/Microsoft.VisualBasic.Core.csproj index 676ad92b7d9..82a69235e1a 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/ref/Microsoft.VisualBasic.Core.csproj +++ b/src/libraries/Microsoft.VisualBasic.Core/ref/Microsoft.VisualBasic.Core.csproj @@ -14,6 +14,7 @@ + diff --git a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft.VisualBasic.Core.vbproj b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft.VisualBasic.Core.vbproj index 5b5b2b87243..73da487de0e 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft.VisualBasic.Core.vbproj +++ b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft.VisualBasic.Core.vbproj @@ -73,6 +73,7 @@ + diff --git a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/LateBinding.vb b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/LateBinding.vb index b0357fcb37c..f2e008ee4d4 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/LateBinding.vb +++ b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/LateBinding.vb @@ -290,7 +290,6 @@ Namespace Microsoft.VisualBasic.CompilerServices Dim NewValue As Object If fi.IsInitOnly Then - 'REVIEW: Should this be MissingMember? Throw New MissingMemberException(GetResourceString(SR.MissingMember_ReadOnlyField2, name, VBFriendlyName(objType, o))) End If diff --git a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/ObjectType.vb b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/ObjectType.vb index b40bf24a881..f13747083f2 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/ObjectType.vb +++ b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/ObjectType.vb @@ -3541,8 +3541,6 @@ DecimalExit: fromType = obj.GetType() - 'REVIEW: - Should we handle ByRef in this manner? - ' what happens when it's returned as non-byref? If toType.IsByRef Then toType = toType.GetElementType() IsToByRef = True @@ -3561,7 +3559,6 @@ DecimalExit: Return obj End If End If - 'END REVIEW Dim toTypeCode As TypeCode = Type.GetTypeCode(toType) diff --git a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/Utils.LateBinder.vb b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/Utils.LateBinder.vb index daf1338fe48..37b6adee699 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/Utils.LateBinder.vb +++ b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/Utils.LateBinder.vb @@ -88,6 +88,111 @@ Namespace Microsoft.VisualBasic.CompilerServices Return SR.Format(resourceKey, args) End Function + Friend Shared Function StdFormat(ByVal s As String) As String + Dim nfi As NumberFormatInfo + Dim iIndex As Integer + Dim c0, c1, c2 As Char + Dim sb As StringBuilder + + nfi = Threading.Thread.CurrentThread.CurrentCulture.NumberFormat + iIndex = s.IndexOf(nfi.NumberDecimalSeparator) + + If iIndex = -1 Then + Return s + End If + + Try + c0 = s.Chars(0) + c1 = s.Chars(1) + c2 = s.Chars(2) + Catch ex As StackOverflowException + Throw ex + Catch ex As OutOfMemoryException + Throw ex + Catch ex As System.Threading.ThreadAbortException + Throw ex + Catch + 'Ignore, should default to 0 values + End Try + + If s.Chars(iIndex) = chPeriod Then + 'Optimization: no period replacement needed + 'avoids creating stringbuilder and copying string + + 'If format is "0.xxxx" then replace 0 with space + If c0 = chZero AndAlso c1 = chPeriod Then + Return s.Substring(1) + + 'If format is "-0.xxxx", "+0.xxxx", " 0.xxxx" then shift everything down over the zero + ElseIf (c0 = chHyphen OrElse c0 = chPlus OrElse c0 = chSpace) AndAlso c1 = chZero AndAlso c2 = chPeriod Then + 'Fall down below and use a stringbuilder + Else + 'No change + Return s + End If + End If + + sb = New StringBuilder(s) + sb.Chars(iIndex) = chPeriod ' change decimal separator to "." + + 'If format is "0.xxxx" then replace 0 with space + If (c0 = chZero AndAlso c1 = chPeriod) Then + StdFormat = sb.ToString(1, sb.Length - 1) + 'If format is "-0.xxxx", "+0.xxxx", " 0.xxxx" then shift everything down over the zero + ElseIf (c0 = chHyphen OrElse c0 = chPlus OrElse c0 = chSpace) AndAlso c1 = chZero AndAlso c2 = chPeriod Then + sb.Remove(1, 1) + StdFormat = sb.ToString() + Else + StdFormat = sb.ToString() + End If + End Function + + Friend Shared Function OctFromLong(ByVal Val As Long) As String + 'System.Radix is being removed from the .NET platform, so compute this locally. + Dim Buffer As String = "" + Dim ModVal As Integer + Dim CharZero As Integer = Convert.ToInt32(chZero) + Dim Negative As Boolean + + If Val < 0 Then + Val = Int64.MaxValue + Val + 1 + Negative = True + End If + + 'Pull apart the number and put the digits (in reverse order) into the buffer. + Do + ModVal = CInt(Val Mod 8) + Val = Val >> 3 + Buffer = Buffer & ChrW(ModVal + CharZero) + Loop While Val > 0 + + Buffer = StrReverse(Buffer) + + If Negative Then + Buffer = "1" & Buffer + End If + + Return Buffer + End Function + + Friend Shared Function OctFromULong(ByVal Val As ULong) As String + 'System.Radix is being removed from the .NET platform, so compute this locally. + Dim Buffer As String = "" + Dim ModVal As Integer + Dim CharZero As Integer = Convert.ToInt32(chZero) + + 'Pull apart the number and put the digits (in reverse order) into the buffer. + Do + ModVal = CInt(Val Mod 8UL) + Val = Val >> 3 + Buffer = Buffer & ChrW(ModVal + CharZero) + Loop While Val <> 0UL + + Buffer = StrReverse(Buffer) + + Return Buffer + End Function + Friend Shared Function GetCultureInfo() As CultureInfo Return CultureInfo.CurrentCulture End Function @@ -188,7 +293,6 @@ GetSpecialValue: Return True End Function - Friend Shared Function VBFriendlyName(ByVal obj As Object) As String If obj Is Nothing Then Return "Nothing" @@ -541,6 +645,27 @@ GetSpecialValue: Return resultString End Function + Friend Shared Function AdjustArraySuffix(ByVal sRank As String) As String + Dim OneChar As Char + Dim RevResult As String = Nothing + Dim length As Integer = sRank.Length + While length > 0 + OneChar = sRank.Chars(length - 1) + Select Case OneChar + Case ")"c + RevResult = RevResult + "("c + Case "("c + RevResult = RevResult + ")"c + Case ","c + RevResult = RevResult + OneChar + Case Else + RevResult = OneChar + RevResult + End Select + length = length - 1 + End While + Return RevResult + End Function + Friend Shared Function MemberToString(ByVal member As MemberInfo) As String Select Case member.MemberType Case MemberTypes.Method, MemberTypes.Constructor diff --git a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/Versioned.vb b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/Versioned.vb index 34ae2209f3a..ce48f6a80ba 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/Versioned.vb +++ b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/Versioned.vb @@ -14,6 +14,35 @@ Namespace Microsoft.VisualBasic.CompilerServices Private Sub New() End Sub + Public Shared Function CallByName(ByVal Instance As System.Object, ByVal MethodName As String, ByVal UseCallType As CallType, ByVal ParamArray Arguments() As Object) As Object + + Select Case UseCallType + + Case CallType.Method + 'Need to use LateGet, because we are returning a value + Return CompilerServices.NewLateBinding.LateCall(Instance, Nothing, MethodName, Arguments, Nothing, Nothing, Nothing, False) + + Case CallType.Get + Return CompilerServices.NewLateBinding.LateGet(Instance, Nothing, MethodName, Arguments, Nothing, Nothing, Nothing) + + Case CallType.Let, + CallType.Set + Dim idmop As IDynamicMetaObjectProvider = IDOUtils.TryCastToIDMOP(Instance) + If idmop IsNot Nothing Then + ' UseCallType is used in the late binder to affect the binding behavior for COM Object, but COM Objects + ' don't implement IDynamicMetaObjectProvider. Therefore it is safe not to pass on UseCallType here. + IDOBinder.IDOSet(idmop, MethodName, Nothing, Arguments) + Else + CompilerServices.NewLateBinding.LateSet(Instance, Nothing, MethodName, Arguments, Nothing, Nothing, False, False, UseCallType) + End If + Return Nothing + + Case Else + Throw New ArgumentException(GetResourceString(SR.Argument_InvalidValue1, "CallType")) + End Select + + End Function + Public Shared Function IsNumeric(ByVal Expression As Object) As Boolean Dim valueInterface As IConvertible = TryCast(Expression, IConvertible) @@ -72,6 +101,20 @@ Namespace Microsoft.VisualBasic.CompilerServices End Function + Public Shared Function TypeName(ByVal Expression As Object) As String + + Dim Result As String + Dim typ As System.Type + + If Expression Is Nothing Then + Return "Nothing" + End If + + typ = Expression.GetType() + Result = VBFriendlyNameOfType(typ) + Return Result + End Function + Public Shared Function SystemTypeName(ByVal VbName As String) As String Select Case Trim(VbName).ToUpperInvariant() Case "BOOLEAN" : Return "System.Boolean" diff --git a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Conversion.vb b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Conversion.vb new file mode 100644 index 00000000000..8935b974c4e --- /dev/null +++ b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Conversion.vb @@ -0,0 +1,960 @@ +' 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. + +Imports System +Imports System.Runtime.Versioning + +Imports Microsoft.VisualBasic.CompilerServices +Imports Microsoft.VisualBasic.CompilerServices.ExceptionUtils +Imports Microsoft.VisualBasic.CompilerServices.Utils + +Namespace Microsoft.VisualBasic + + Public Module Conversion + + Private Const NUMPRS_LEADING_WHITE As Integer = &H1I + Private Const NUMPRS_TRAILING_WHITE As Integer = &H2I + Private Const NUMPRS_LEADING_PLUS As Integer = &H4I + Private Const NUMPRS_TRAILING_PLUS As Integer = &H8I + Private Const NUMPRS_LEADING_MINUS As Integer = &H10I + Private Const NUMPRS_TRAILING_MINUS As Integer = &H20I + Private Const NUMPRS_HEX_OCT As Integer = &H40I + Private Const NUMPRS_PARENS As Integer = &H80I + Private Const NUMPRS_DECIMAL As Integer = &H100I + Private Const NUMPRS_THOUSANDS As Integer = &H200I + Private Const NUMPRS_CURRENCY As Integer = &H400I + Private Const NUMPRS_EXPONENT As Integer = &H800I + Private Const NUMPRS_USE_ALL As Integer = &H1000I + Private Const NUMPRS_STD As Integer = &H1FFFI + + ' flags used by dwOutFlags only: + ' + Private Const NUMPRS_NEG As Integer = &H10000I + Private Const NUMPRS_INEXACT As Integer = &H20000I + + ' flags used by VarNumFromParseNum to indicate acceptable result types: + ' + Private Const VTBIT_EMPTY As Integer = &H0 + Private Const VTBIT_NULL As Integer = &H2 + Private Const VTBIT_I2 As Integer = &H4 + Private Const VTBIT_I4 As Integer = &H8 + Private Const VTBIT_R4 As Integer = &H10 + Private Const VTBIT_R8 As Integer = &H20 + Private Const VTBIT_CY As Integer = &H40 + Private Const VTBIT_DATE As Integer = &H80 + Private Const VTBIT_BSTR As Integer = &H100 + Private Const VTBIT_OBJECT As Integer = &H200 + Private Const VTBIT_ERROR As Integer = &H400 + Private Const VTBIT_BOOL As Integer = &H800 + Private Const VTBIT_VARIANT As Integer = &H1000 + Private Const VTBIT_DATAOBJECT As Integer = &H2000 + Private Const VTBIT_DECIMAL As Integer = &H4000 + Private Const VTBIT_BYTE As Integer = &H20000 + Private Const VTBIT_CHAR As Integer = &H40000 + Private Const VTBIT_LONG As Integer = &H100000 + + Private Const MAX_ERR_NUMBER As Integer = 65535 + Private Const LOCALE_NOUSEROVERRIDE As Integer = &H80000000I + Private Const LCID_US_ENGLISH As Integer = &H409I + Private Const PRSFLAGS As Integer _ + = (NUMPRS_LEADING_PLUS Or NUMPRS_LEADING_MINUS Or NUMPRS_HEX_OCT Or NUMPRS_DECIMAL Or NUMPRS_EXPONENT) + 'Private Const VTBITS As Integer = (VTBIT_I2 Or VTBIT_I4 Or VTBIT_R8 Or VTBIT_CY Or VTBIT_DECIMAL) + Private Const VTBITS As Integer = (VTBIT_I2 Or VTBIT_I4 Or VTBIT_R8 Or VTBIT_DECIMAL) + + Private Const TYPE_INDICATOR_INT16 As Char = "%"c + Private Const TYPE_INDICATOR_INT32 As Char = "&"c + Private Const TYPE_INDICATOR_SINGLE As Char = "!"c + Private Const TYPE_INDICATOR_DECIMAL As Char = "@"c + + '============================================================================ + ' Error message functions. + '============================================================================ + Public Function ErrorToString() As String + Return Information.Err().Description + End Function + + Public Function ErrorToString(ByVal ErrorNumber As Integer) As String + If ErrorNumber >= MAX_ERR_NUMBER Then + Throw New ArgumentException(GetResourceString(SR.MaxErrNumber)) + End If + + If ErrorNumber > 0 Then + ErrorNumber = (SEVERITY_ERROR Or FACILITY_CONTROL Or ErrorNumber) + End If + + If (ErrorNumber And SCODE_FACILITY) = FACILITY_CONTROL Then + ErrorNumber = ErrorNumber And &HFFFFI + Return GetResourceString(CType(ErrorNumber, vbErrors)) + ElseIf ErrorNumber <> 0 Then + Return GetResourceString(vbErrors.UserDefined) + Else + Return "" + End If + End Function + + '============================================================================ + ' Numeric functions. + '============================================================================ + + Public Function Fix(ByVal Number As Short) As Short + Return Number + End Function + + Public Function Fix(ByVal Number As Integer) As Integer + Return Number + End Function + + Public Function Fix(ByVal Number As Long) As Long + Return Number + End Function + + Public Function Fix(ByVal Number As Double) As Double + If Number >= 0 Then + Return System.Math.Floor(Number) + Else + Return -System.Math.Floor(-Number) + End If + End Function + + Public Function Fix(ByVal Number As Single) As Single + If Number >= 0 Then + Return CSng(System.Math.Floor(CDbl(Number))) + Else + Return CSng(-System.Math.Floor(CDbl(-Number))) + End If + End Function + + Public Function Fix(ByVal Number As Decimal) As Decimal + If System.Decimal.op_LessThan(Number, System.Decimal.Zero) Then + Return System.Decimal.Negate(System.Decimal.Floor(System.Decimal.Negate(Number))) + Else + Return System.Decimal.Floor(Number) + End If + End Function + + Public Function Fix(ByVal Number As Object) As Object + If Number Is Nothing Then + Throw New ArgumentNullException(GetResourceString(SR.Argument_InvalidNullValue1, "Number")) + End If + + Dim ValueInterface As IConvertible + + ValueInterface = TryCast(Number, IConvertible) + + If Not ValueInterface Is Nothing Then + + Select Case ValueInterface.GetTypeCode() + + Case TypeCode.SByte, + TypeCode.Byte, + TypeCode.Int16, + TypeCode.UInt16, + TypeCode.Int32, + TypeCode.UInt32, + TypeCode.Int64, + TypeCode.UInt64 + + Return Number + + Case TypeCode.Single + Return Fix(ValueInterface.ToSingle(Nothing)) + + Case TypeCode.Double + Return Fix(ValueInterface.ToDouble(Nothing)) + + Case TypeCode.Decimal + Return Fix(ValueInterface.ToDecimal(Nothing)) + + Case TypeCode.Boolean + Return ValueInterface.ToInt32(Nothing) + + Case TypeCode.String + Return Fix(CDbl(ValueInterface.ToString(Nothing))) + + Case Else + 'TypeCode.Char + 'TypeCode.DateTime + ' Fall through to error + + End Select + + End If + + Throw VbMakeException(New ArgumentException(GetResourceString(SR.Argument_NotNumericType2, "Number", Number.GetType().FullName)), vbErrors.TypeMismatch) + End Function + + Public Function Int(ByVal Number As Short) As Short + Return Number + End Function + + Public Function Int(ByVal Number As Integer) As Integer + Return Number + End Function + + Public Function Int(ByVal Number As Long) As Long + Return Number + End Function + + Public Function Int(ByVal Number As Double) As Double + Return System.Math.Floor(Number) + End Function + + Public Function Int(ByVal Number As Single) As Single + Return CSng(System.Math.Floor(CDbl(Number))) + End Function + + Public Function Int(ByVal Number As Decimal) As Decimal + Return System.Decimal.Floor(Number) + End Function + + Public Function Int(ByVal Number As Object) As Object + If Number Is Nothing Then + Throw New ArgumentNullException(GetResourceString(SR.Argument_InvalidNullValue1, "Number")) + End If + + Dim ValueInterface As IConvertible + + ValueInterface = TryCast(Number, IConvertible) + + If Not ValueInterface Is Nothing Then + + Select Case ValueInterface.GetTypeCode() + + Case TypeCode.SByte, + TypeCode.Byte, + TypeCode.Int16, + TypeCode.UInt16, + TypeCode.Int32, + TypeCode.UInt32, + TypeCode.Int64, + TypeCode.UInt64 + + Return Number + + Case TypeCode.Single + Return Int(ValueInterface.ToSingle(Nothing)) + + Case TypeCode.Double + Return Int(ValueInterface.ToDouble(Nothing)) + + Case TypeCode.Decimal + Return Int(ValueInterface.ToDecimal(Nothing)) + + Case TypeCode.Boolean + Return ValueInterface.ToInt32(Nothing) + + Case TypeCode.String + Return Int(CDbl(ValueInterface.ToString(Nothing))) + + Case Else + 'TypeCode.Char + 'TypeCode.DateTime + ' Fall through to error + End Select + End If + + Throw VbMakeException(New ArgumentException(GetResourceString(SR.Argument_NotNumericType2, "Number", Number.GetType().FullName)), vbErrors.TypeMismatch) + End Function + + '============================================================================ + ' Number to string conversion + '============================================================================ + + Public Function Hex(ByVal Number As SByte) As String + Return Number.ToString("X") + End Function + + Public Function Hex(ByVal Number As Byte) As String + Return Number.ToString("X") + End Function + + Public Function Hex(ByVal Number As Short) As String + Return Number.ToString("X") + End Function + + + Public Function Hex(ByVal Number As UShort) As String + Return Number.ToString("X") + End Function + + Public Function Hex(ByVal Number As Integer) As String + Return Number.ToString("X") + End Function + + + Public Function Hex(ByVal Number As UInteger) As String + Return Number.ToString("X") + End Function + + Public Function Hex(ByVal Number As Long) As String + Return Number.ToString("X") + End Function + + + Public Function Hex(ByVal Number As ULong) As String + Return Number.ToString("X") + End Function + + Public Function Hex(ByVal Number As Object) As String + Dim LongValue As Long + + If Number Is Nothing Then + Throw New ArgumentNullException(GetResourceString(SR.Argument_InvalidNullValue1, "Number")) + End If + + Dim ValueInterface As IConvertible + + ValueInterface = TryCast(Number, IConvertible) + + If Not ValueInterface Is Nothing Then + + Select Case ValueInterface.GetTypeCode() + + Case TypeCode.SByte + Return Hex(ValueInterface.ToSByte(Nothing)) + + Case TypeCode.Byte + Return Hex(ValueInterface.ToByte(Nothing)) + + Case TypeCode.Int16 + Return Hex(ValueInterface.ToInt16(Nothing)) + + Case TypeCode.UInt16 + Return Hex(ValueInterface.ToUInt16(Nothing)) + + Case TypeCode.Int32 + Return Hex(ValueInterface.ToInt32(Nothing)) + + Case TypeCode.UInt32 + Return Hex(ValueInterface.ToUInt32(Nothing)) + + Case TypeCode.Int64, + TypeCode.Single, + TypeCode.Double, + TypeCode.Decimal + LongValue = ValueInterface.ToInt64(Nothing) + GoTo RangeCheck + + Case TypeCode.UInt64 + Return Hex(ValueInterface.ToUInt64(Nothing)) + + Case TypeCode.String + Try + LongValue = CLng(ValueInterface.ToString(Nothing)) + Catch ex As OverflowException + 'If the conversion to Long overflows, we can try ULong. + Return Hex(CULng(ValueInterface.ToString(Nothing))) + End Try +RangeCheck: + 'Optimization case + If LongValue = 0 Then + Return "0" + End If + + If (LongValue > 0) Then + Return Hex(LongValue) + Else + 'For VB6 compatability, format as Int32 value + ' unless it overflows into an Int64 + If (LongValue >= System.Int32.MinValue) Then + Return Hex(CInt(LongValue)) + End If + Return Hex(LongValue) + End If + + Case TypeCode.Boolean, + TypeCode.Char, + TypeCode.DateTime + ' Fall through to error + + Case Else + ' Fall through to error + End Select + End If + + Throw New ArgumentException(GetResourceString(SR.Argument_InvalidValueType2, "Number", VBFriendlyName(Number))) + End Function + + + Public Function Oct(ByVal Number As SByte) As String + Return OctFromLong(CLng(Number) And &HFFL) + End Function + + Public Function Oct(ByVal Number As Byte) As String + Return OctFromULong(CULng(Number)) + End Function + + Public Function Oct(ByVal Number As Short) As String + Return OctFromLong(CLng(Number) And &HFFFFL) + End Function + + + Public Function Oct(ByVal Number As UShort) As String + Return OctFromULong(CULng(Number)) + End Function + + Public Function Oct(ByVal Number As Integer) As String + Return OctFromLong(CLng(Number) And &HFFFFFFFFL) + End Function + + + Public Function Oct(ByVal Number As UInteger) As String + Return OctFromULong(CULng(Number)) + End Function + + Public Function Oct(ByVal Number As Long) As String + Return OctFromLong(Number) + End Function + + + Public Function Oct(ByVal Number As ULong) As String + Return OctFromULong(Number) + End Function + + Public Function Oct(ByVal Number As Object) As String + Dim LongValue As Long + + If Number Is Nothing Then + Throw New ArgumentNullException(GetResourceString(SR.Argument_InvalidNullValue1, "Number")) + End If + + Dim ValueInterface As IConvertible + + ValueInterface = TryCast(Number, IConvertible) + + If Not ValueInterface Is Nothing Then + + Select Case ValueInterface.GetTypeCode() + + Case TypeCode.SByte + Return Oct(ValueInterface.ToSByte(Nothing)) + Case TypeCode.Byte + Return Oct(ValueInterface.ToByte(Nothing)) + Case TypeCode.Int16 + Return Oct(ValueInterface.ToInt16(Nothing)) + Case TypeCode.UInt16 + Return Oct(ValueInterface.ToUInt16(Nothing)) + Case TypeCode.Int32 + Return Oct(ValueInterface.ToInt32(Nothing)) + Case TypeCode.UInt32 + Return Oct(ValueInterface.ToUInt32(Nothing)) + + Case TypeCode.Int64, + TypeCode.Single, + TypeCode.Double, + TypeCode.Decimal + LongValue = ValueInterface.ToInt64(Nothing) + GoTo RangeCheck + + Case TypeCode.UInt64 + Return Oct(ValueInterface.ToUInt64(Nothing)) + + Case TypeCode.String + Try + LongValue = CLng(ValueInterface.ToString(Nothing)) + Catch ex As OverflowException + 'If the conversion to Long overflows, we can try ULong. + Return Oct(CULng(ValueInterface.ToString(Nothing))) + End Try +RangeCheck: + 'Optimization case + If LongValue = 0 Then + Return "0" + End If + + If (LongValue > 0) Then + Return Oct(LongValue) + Else + 'For VB6 compatability, format as Int32 value + ' unless it overflows into an Int64 + If (LongValue >= System.Int32.MinValue) Then + Return Oct(CInt(LongValue)) + End If + Return Oct(LongValue) + End If + + Case TypeCode.Boolean, + TypeCode.Char, + TypeCode.DateTime + ' Fall through to error + + Case Else + ' Fall through to error + End Select + End If + + Throw New ArgumentException(GetResourceString(SR.Argument_InvalidValueType2, "Number", VBFriendlyName(Number))) + End Function + + Public Function Str(ByVal Number As Object) As String + Dim s As String + + If Number Is Nothing Then + Throw New ArgumentNullException(GetResourceString(SR.Argument_InvalidNullValue1, "Number")) + End If + + Dim ValueInterface As IConvertible + Dim ValueTypeCode As TypeCode + + ValueInterface = TryCast(Number, IConvertible) + + If ValueInterface Is Nothing Then + Throw New InvalidCastException(GetResourceString(SR.ArgumentNotNumeric1, "Number")) + End If + + ValueTypeCode = ValueInterface.GetTypeCode() + Select Case ValueTypeCode + + Case TypeCode.DBNull + Return "Null" + + Case TypeCode.Boolean + If ValueInterface.ToBoolean(Nothing) Then + Return "True" + Else + Return "False" + End If + + Case TypeCode.SByte, + TypeCode.Byte, + TypeCode.Int16, + TypeCode.UInt16, + TypeCode.Int32, + TypeCode.UInt32, + TypeCode.Int64, + TypeCode.UInt64, + TypeCode.Single, + TypeCode.Double, + TypeCode.Decimal + s = CStr(Number) + + Case Else + If ValueTypeCode = TypeCode.String Then + Try + s = CStr(CDbl(ValueInterface.ToString(Nothing))) + GoTo FormatAndExit + Catch ex As StackOverflowException + Throw ex + Catch ex As OutOfMemoryException + Throw ex + Catch ex As System.Threading.ThreadAbortException + Throw ex + Catch + 'Throw our own exception below + End Try + End If + Throw New InvalidCastException(GetResourceString(SR.ArgumentNotNumeric1, "Number")) + End Select + +FormatAndExit: + If s.Length > 0 AndAlso s.Chars(0) <> "-"c Then + Return " " & StdFormat(s) + Else + Return StdFormat(s) + End If + End Function + + Private Function HexOrOctValue(ByVal InputStr As String, ByVal i As Integer) As Double + Dim digits As Integer = 0 + Dim ch As Char + Dim iLen As Integer + Dim ivalue As Long + Dim digitValue As Integer + + Const asc0 As Integer = AscW("0"c) + Const ascUpperAoffset As Integer = AscW("A"c) - 10 + Const ascLowerAoffset As Integer = AscW("a"c) - 10 + + iLen = InputStr.Length + + ch = InputStr.Chars(i) + i += 1 + + If ch = "H"c OrElse ch = "h"c Then + 'Loop for octal + Do While (i < iLen AndAlso digits < 17) + ch = InputStr.Chars(i) + i += 1 + Select Case ch + Case ControlChars.Tab, ControlChars.Lf, ControlChars.Cr, ChrW(32), ChrW(&H3000S) + GoTo NextHexCharacter + + Case "0"c + If digits = 0 Then + 'leading zeros do not affect type + GoTo NextHexCharacter + End If + digitValue = 0 + + Case "1"c To "9"c + digitValue = AscW(ch) - asc0 + + Case "A"c To "F"c + digitValue = AscW(ch) - ascUpperAoffset + + Case "a"c To "f"c + digitValue = AscW(ch) - ascLowerAoffset + + Case Else + Exit Do + End Select +AddHexDigit: + ' If digits = 15 AndAlso ivalue >= &H800000000000000L Then + If digits = 15 AndAlso ivalue > &H7FFFFFFFFFFFFFFL Then + 'This will overflow because we don't have a shift operator + 'and must do multiplication + ivalue = (ivalue And &H7FFFFFFFFFFFFFFL) * 16 + ivalue = ivalue Or &H8000000000000000L + Else + ivalue = ivalue * 16 + End If + ivalue += digitValue + digits += 1 +NextHexCharacter: + + Loop + + If digits = 16 Then + i += 1 + If i < iLen Then + 'We fell out of the loop before getting the typechar + ch = InputStr.Chars(i) + End If + End If + + If digits > 8 Then + 'leave ivalue unchanged + + ElseIf digits > 4 OrElse ch = TYPE_INDICATOR_INT32 Then + If ivalue > &H7FFFFFFFL Then + ivalue = Int32.MinValue + (ivalue And &H7FFFFFFFL) + End If + + ElseIf digits > 2 OrElse ch = TYPE_INDICATOR_INT16 Then + If ivalue > &H7FFFL Then + ivalue = Int16.MinValue + (ivalue And &H7FFFL) + End If + + End If + + If ch = TYPE_INDICATOR_INT16 Then + ivalue = CShort(ivalue) + ElseIf ch = TYPE_INDICATOR_INT32 Then + ivalue = CInt(ivalue) + End If + Return ivalue + + ElseIf ch = "O"c OrElse ch = "o"c Then + + 'Loop for octal + Do While (i < iLen AndAlso digits < 22) + ch = InputStr.Chars(i) + i += 1 + + Select Case ch + Case ControlChars.Tab, ControlChars.Lf, ControlChars.Cr, ChrW(32), ChrW(&H3000S) + GoTo NextOctCharacter + + Case "0"c + If digits = 0 Then + 'leading zeros do not affect type + GoTo NextOctCharacter + End If + digitValue = 0 + + Case "1"c To "7"c + digitValue = AscW(ch) - asc0 + + Case Else + Exit Do + + End Select + +AddOctDigit: + If ivalue >= &O100000000000000000000L Then + 'This will overflow because we don't have a shift operator + 'and must do multiplication + ivalue = (ivalue And &O77777777777777777777L) * 8 + ivalue = ivalue Or &O100000000000000000000L + Else + ivalue = ivalue * 8 + End If + ivalue += digitValue + digits += 1 +NextOctCharacter: + + Loop + + If digits = 22 Then + i += 1 + If i < iLen Then + 'We fell out of the loop before getting the typechar + ch = InputStr.Chars(i) + End If + End If + + If ivalue > &H100000000L Then + 'leave ivalue unchanged + + ElseIf ivalue > &HFFFFL OrElse ch = TYPE_INDICATOR_INT32 Then + If ivalue > &H7FFFFFFFL Then + ivalue = Int32.MinValue + (ivalue And &H7FFFFFFFL) + End If + + ElseIf ivalue > &HFFL OrElse ch = TYPE_INDICATOR_INT16 Then + If ivalue > &H7FFFL Then + ivalue = Int16.MinValue + (ivalue And &H7FFFL) + End If + + End If + + If ch = TYPE_INDICATOR_INT16 Then + ivalue = CShort(ivalue) + ElseIf ch = TYPE_INDICATOR_INT32 Then + ivalue = CInt(ivalue) + End If + Return ivalue + Else + 'input is invalid + Return 0 + End If + + End Function + + Public Function Val(ByVal InputStr As String) As Double + + Dim ch As Char + Dim i As Integer + Dim iLen As Integer + Dim digits As Integer + Dim digitsAfterDecimal, digitsBeforeDecimal As Integer + + Const asc0 As Integer = AscW("0"c) + + If InputStr Is Nothing Then + iLen = 0 + Else + iLen = InputStr.Length + End If + + i = 0 + 'Skip over leading whitespace + Do While (i < iLen) + ch = InputStr.Chars(i) + Select Case ch + Case ControlChars.Tab, ControlChars.Lf, ControlChars.Cr, ChrW(32), ChrW(&H3000S) + i += 1 + Case Else + Exit Do + End Select + Loop + + If i >= iLen Then + Return 0 + End If + + ch = InputStr.Chars(i) + If ch = "&"c Then 'We are dealing with hex or octal numbers + Return HexOrOctValue(InputStr, i + 1) + + Else 'we are dealing with base 10 decimal + Dim value As Double + Dim afterdecimal As Boolean = False + Dim aftere As Boolean = False + Dim negative As Boolean = False + Dim eval As Double = 0 + + 'Check for negative + ch = InputStr.Chars(i) + If ch = "-"c Then + negative = True + i += 1 + ElseIf ch = "+"c Then + i += 1 + End If + + 'check for numbers before a decimal or E + Do While (i < iLen) + ch = InputStr.Chars(i) + Select Case ch + Case ControlChars.Tab, ControlChars.Lf, ControlChars.Cr, ChrW(32), ChrW(&H3000S) + i += 1 + + Case "0"c + If digits <> 0 OrElse afterdecimal Then + value = value * 10 + AscW(ch) - asc0 + i += 1 + digits += 1 + Else + i += 1 + 'don't count as digit + End If + + Case "1"c To "9"c + value = value * 10 + AscW(ch) - asc0 + i += 1 + digits += 1 + + Case "."c + i += 1 + If afterdecimal = False Then + afterdecimal = True + digitsBeforeDecimal = digits + Else + 'handle "1..1" or "1.2.1" + Exit Do + End If + + Case "e"c, "E"c, "d"c, "D"c + aftere = True + i += 1 + Exit Do + + Case Else + Exit Do + End Select + Loop + + If afterdecimal Then + digitsAfterDecimal = digits - digitsBeforeDecimal + End If + + If aftere Then + Dim afterplusminus As Boolean = False + Dim enegative As Boolean = False + Do While (i < iLen) + ch = InputStr.Chars(i) + Select Case ch + Case ControlChars.Tab, ControlChars.Lf, ControlChars.Cr, ChrW(32), ChrW(&H3000S) + i += 1 + + Case "0"c To "9"c + eval = eval * 10 + AscW(ch) - asc0 + i += 1 + + Case "+"c + If Not afterplusminus Then + afterplusminus = True + i += 1 + Else + Exit Do + End If + + Case "-"c + If Not afterplusminus Then + afterplusminus = True + enegative = True + i += 1 + Else + Exit Do + End If + + Case Else + Exit Do + End Select + Loop + + If enegative Then + eval += digitsAfterDecimal + value = value * (10 ^ (-eval)) + Else + eval -= digitsAfterDecimal + value = value * (10 ^ (eval)) + End If + Else + If afterdecimal AndAlso digitsAfterDecimal <> 0 Then + 'Need to adjust for decimal + value = value / (10 ^ digitsAfterDecimal) + End If + End If + + If System.Double.IsInfinity(value) Then + Throw VbMakeException(vbErrors.Overflow) + End If + + If negative Then + value = -value + End If + + Select Case ch + + Case TYPE_INDICATOR_INT16 + If digitsAfterDecimal > 0 Then + Throw VbMakeException(vbErrors.TypeMismatch) + End If + value = CShort(value) + + Case TYPE_INDICATOR_INT32 + If digitsAfterDecimal > 0 Then + Throw VbMakeException(vbErrors.TypeMismatch) + End If + value = CInt(value) + + Case TYPE_INDICATOR_SINGLE + value = CSng(value) + + Case TYPE_INDICATOR_DECIMAL + value = CDec(value) + + Case Else + + End Select + + Return value + End If + End Function + + Public Function Val(ByVal Expression As Char) As Integer + 'Val only handles Ascii decimal chars '0' to '9' + Dim CharValue As Integer + + CharValue = AscW(Expression) 'CType(Expression, IConvertible).ToInt32(Nothing) + If CharValue >= AscW("1"c) AndAlso CharValue <= AscW("9"c) Then + Return CharValue - AscW("0") + End If + Return 0 + End Function + + Public Function Val(ByVal Expression As Object) As Double + + Dim StringExpression As String = TryCast(Expression, String) + + If StringExpression IsNot Nothing Then + Return Val(StringExpression) + + ElseIf TypeOf Expression Is Char Then + Return Val(DirectCast(Expression, Char)) + + ElseIf CompilerServices.Versioned.IsNumeric(Expression) Then + Return CDbl(Expression) + + Else + Dim sValue As String + Try + sValue = CStr(Expression) + Catch ex As StackOverflowException + Throw ex + Catch ex As OutOfMemoryException + Throw ex + Catch ex As System.Threading.ThreadAbortException + Throw ex + Catch + Throw VbMakeException(New ArgumentException(GetResourceString(SR.Argument_InvalidValueType2, "Expression", VBFriendlyName(Expression))), vbErrors.OLENoPropOrMethod) + End Try + + Return Val(sValue) + End If + + End Function + + Public Function CTypeDynamic(ByVal Expression As Object, ByVal TargetType As System.Type) As Object + Return Conversions.ChangeType(Expression, TargetType, True) + End Function + + Public Function CTypeDynamic(Of TargetType)(ByVal Expression As Object) As TargetType + return DirectCast(Conversions.ChangeType(Expression, GetType(TargetType), True), TargetType) + End Function + End Module +End Namespace + diff --git a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Information.vb b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Information.vb index 78d18821366..5fe3e6dd06f 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Information.vb +++ b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Information.vb @@ -333,6 +333,114 @@ Namespace Microsoft.VisualBasic End Function + Friend Function OldVBFriendlyNameOfTypeName(ByVal typename As String) As String + Dim ArraySuffix As String = Nothing + Dim Name As String + Dim LastChar As Integer = typename.Length - 1 + + If typename.Chars(LastChar) = "]"c Then + Dim pos As Integer + pos = typename.IndexOf("["c) + If pos + 1 = LastChar Then + ArraySuffix = "()" + Else + ArraySuffix = typename.Substring(pos, LastChar - pos + 1).Replace("["c, "("c).Replace("]"c, ")"c) + End If + typename = typename.Substring(0, pos) + End If + + Name = OldVbTypeName(typename) + If Name Is Nothing Then + Name = typename + End If + + If ArraySuffix Is Nothing Then + Return Name + End If + Return Name & AdjustArraySuffix(ArraySuffix) + + End Function + + Public Function TypeName(ByVal VarName As Object) As String + + Dim Result As String + Dim bIsArray As Boolean + Dim typ As System.Type + Dim ArrayType As System.Type + + If VarName Is Nothing Then + Return "Nothing" + End If + + typ = VarName.GetType() + + If typ.IsArray Then + bIsArray = True + ArrayType = typ + typ = ArrayType.GetElementType() + End If + + If typ.IsEnum() Then + + Result = typ.Name + GoTo UnmangleName + + Else + Dim tc As TypeCode + + tc = Type.GetTypeCode(typ) + + Select Case tc + + Case TypeCode.DBNull : Result = "DBNull" + Case TypeCode.Int16 : Result = "Short" + Case TypeCode.Int32 : Result = "Integer" + Case TypeCode.Single : Result = "Single" + Case TypeCode.Double : Result = "Double" + Case TypeCode.DateTime : Result = "Date" + Case TypeCode.String : Result = "String" + Case TypeCode.Boolean : Result = "Boolean" + Case TypeCode.Decimal : Result = "Decimal" + Case TypeCode.Byte : Result = "Byte" + Case TypeCode.Char : Result = "Char" + Case TypeCode.Int64 : Result = "Long" + + Case Else + + Result = typ.Name + + If (typ.IsCOMObject AndAlso (System.String.CompareOrdinal(Result, COMObjectName) = 0)) Then + Result = LegacyTypeNameOfCOMObject(VarName, True) + End If + +UnmangleName: + Dim i As Integer + i = Result.IndexOf("+"c) + If i >= 0 Then + Result = Result.Substring(i + 1) + End If + + End Select + + End If + + If bIsArray Then + + Dim ary As Array + ary = CType(VarName, Array) + If ary.Rank = 1 Then + Result = Result & "[]" + Else + Result = Result & "[" & (New String(","c, ary.Rank - 1)) & "]" + End If + + Result = OldVBFriendlyNameOfTypeName(Result) + + End If + + Return Result + End Function + Public Function SystemTypeName(ByVal VbName As String) As String Select Case Trim(VbName).ToUpperInvariant() @@ -383,6 +491,17 @@ Namespace Microsoft.VisualBasic End Function + Friend Function LegacyTypeNameOfCOMObject(ByVal VarName As Object, ByVal bThrowException As Boolean) As String + + Dim Result As String = COMObjectName + + If Result.Chars(0) = "_"c Then + Result = Result.Substring(1) + End If + + Return Result + End Function + End Module End Namespace diff --git a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Interaction.vb b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Interaction.vb index acd730cd820..97d8c5e5008 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Interaction.vb +++ b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Interaction.vb @@ -7,7 +7,7 @@ Imports Microsoft.VisualBasic.CompilerServices.Utils Namespace Microsoft.VisualBasic - Friend Module Interaction + Public Module Interaction Friend Function IIf(Of T)(ByVal condition As Boolean, ByVal truePart As T, ByVal falsePart As T) As T If condition Then diff --git a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Strings.vb b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Strings.vb index e6be2a40078..fb50cc1db87 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Strings.vb +++ b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Strings.vb @@ -451,7 +451,6 @@ EmptyMatchString: Dim CharArray As Char() = TryCast(Expression, Char()) If CharArray IsNot Nothing Then - 'REVIEW: Should this be char length or byte length? Return CharArray.Length End If End If @@ -464,6 +463,101 @@ EmptyMatchString: Throw VbMakeException(vbErrors.TypeMismatch) End Function + '============================================================================ + ' Fixed-length string functions. + '============================================================================ + + Public Function StrReverse(ByVal Expression As String) As String + + If (Expression Is Nothing) Then + Return "" + End If + + Dim chars As Char() + Dim uc As UnicodeCategory + Dim ch As Char + Dim SrcIndex, Length As Integer + + Length = Expression.Length + If Length = 0 Then + Return "" + End If + + 'Detect if there are any graphemes that need special handling + For SrcIndex = 0 To Length - 1 + ch = Expression.Chars(SrcIndex) + uc = Char.GetUnicodeCategory(ch) + If uc = UnicodeCategory.Surrogate OrElse + uc = UnicodeCategory.NonSpacingMark OrElse + uc = UnicodeCategory.SpacingCombiningMark OrElse + uc = UnicodeCategory.EnclosingMark Then + 'Need to use special handling + Return InternalStrReverse(Expression, SrcIndex, Length) + End If + Next SrcIndex + + chars = Expression.ToCharArray() + System.Array.Reverse(chars) + Return New String(chars) + + End Function + + 'This routine handles reversing Strings containing graphemes + ' GRAPHEME: a text element that is displayed as a single character + ' + Private Function InternalStrReverse(ByVal Expression As String, ByVal SrcIndex As Integer, ByVal Length As Integer) As String + + Dim TextEnum As TextElementEnumerator + Dim DestIndex, LastSrcIndex, NextSrcIndex As Integer + Dim sb As StringBuilder + + 'This code can only be hit one time + sb = New StringBuilder(Length) + sb.Length = Length + + TextEnum = StringInfo.GetTextElementEnumerator(Expression, SrcIndex) + + 'Init enumerator position + If Not TextEnum.MoveNext() Then + Return "" + End If + + LastSrcIndex = 0 + DestIndex = Length - 1 + + 'Copy up the first surrogate found + Do While LastSrcIndex < SrcIndex + sb.Chars(DestIndex) = Expression.Chars(LastSrcIndex) + DestIndex -= 1 + LastSrcIndex += 1 + Loop + + 'Now iterate through the text elements and copy them to the reversed string + NextSrcIndex = TextEnum.ElementIndex + + Do While DestIndex >= 0 + SrcIndex = NextSrcIndex + + 'Move to next element + If (TextEnum.MoveNext()) Then + NextSrcIndex = TextEnum.ElementIndex + Else + 'Point NextSrcIndex to end of string + NextSrcIndex = Length + End If + LastSrcIndex = NextSrcIndex - 1 + + Do While LastSrcIndex >= SrcIndex + sb.Chars(DestIndex) = Expression.Chars(LastSrcIndex) + DestIndex -= 1 + LastSrcIndex -= 1 + Loop + Loop + + Return sb.ToString() + + End Function + '============================================================================ ' Left/Right/Mid/Trim functions. '============================================================================ diff --git a/src/libraries/Microsoft.VisualBasic.Core/tests/CompilerServices/VersionedTests.cs b/src/libraries/Microsoft.VisualBasic.Core/tests/CompilerServices/VersionedTests.cs index c9b1e86a1e3..a3a157c4435 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/tests/CompilerServices/VersionedTests.cs +++ b/src/libraries/Microsoft.VisualBasic.Core/tests/CompilerServices/VersionedTests.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections.Generic; using Microsoft.VisualBasic.CompilerServices; using Xunit; @@ -10,6 +11,70 @@ namespace Microsoft.VisualBasic.Tests { public class VersionedTests { + [Theory] + [MemberData(nameof(CallByName_TestData))] + public void CallByName(object instance, string methodName, CallType useCallType, object[] args, Func getResult, object expected) + { + Assert.Equal(getResult is null ? expected : null, Versioned.CallByName(instance, methodName, useCallType, args)); + if (getResult != null) + { + Assert.Equal(expected, getResult(instance)); + } + } + + [Theory] + [MemberData(nameof(CallByName_ArgumentException_TestData))] + public void CallByName_ArgumentException(object instance, string methodName, CallType useCallType, object[] args) + { + Assert.Throws(() => Versioned.CallByName(instance, methodName, useCallType, args)); + } + + [Theory] + [MemberData(nameof(CallByName_MissingMemberException_TestData))] + public void CallByName_MissingMemberException(object instance, string methodName, CallType useCallType, object[] args) + { + Assert.Throws(() => Versioned.CallByName(instance, methodName, useCallType, args)); + } + + private static IEnumerable CallByName_TestData() + { + yield return new object[] { new Class(), "Method", CallType.Method, new object[] { 1, 2 }, null, 3 }; + yield return new object[] { new Class(), "Method", CallType.Get, new object[] { 2, 3 }, null, 5 }; + yield return new object[] { new Class(), "P", CallType.Get, new object[0], null, 0 }; + yield return new object[] { new Class(), "Item", CallType.Get, new object[] { 2 }, null, 2 }; + yield return new object[] { new Class(), "P", CallType.Set, new object[] { 3 }, new Func(obj => ((Class)obj).Value), 3 }; + yield return new object[] { new Class(), "Item", CallType.Let, new object[] { 4, 5 }, new Func(obj => ((Class)obj).Value), 9 }; + } + + private static IEnumerable CallByName_ArgumentException_TestData() + { + yield return new object[] { null, null, default(CallType), new object[0] }; + yield return new object[] { new Class(), "Method", default(CallType), new object[] { 1, 2 } }; + yield return new object[] { new Class(), "Method", (CallType)int.MaxValue, new object[] { 1, 2 } }; + } + + private static IEnumerable CallByName_MissingMemberException_TestData() + { + yield return new object[] { new Class(), "Method", CallType.Method, new object[0] }; + yield return new object[] { new Class(), "Q", CallType.Get, new object[0] }; + } + + private sealed class Class + { + public int Value; + public int Method(int x, int y) => x + y; + public int P + { + get { return Value; } + set { Value = value; } + } + public object this[object index] + { + get { return Value + (int)index; } + set { Value = (int)value + (int)index; } + } + } + [Theory] [InlineData(null, false)] [InlineData('a', false)] @@ -36,6 +101,39 @@ namespace Microsoft.VisualBasic.Tests Assert.Equal(expected, Versioned.SystemTypeName(value)); } + [Theory] + [MemberData(nameof(TypeName_TestData))] + public void TypeName(object expression, string expected) + { + Assert.Equal(expected, Versioned.TypeName(expression)); + } + + private static IEnumerable TypeName_TestData() + { + yield return new object[] { null, "Nothing" }; + yield return new object[] { new object(), "Object" }; + yield return new object[] { default(bool), "Boolean" }; + yield return new object[] { default(char), "Char" }; + yield return new object[] { default(sbyte), "SByte" }; + yield return new object[] { default(byte), "Byte" }; + yield return new object[] { default(short), "Short" }; + yield return new object[] { default(ushort), "UShort" }; + yield return new object[] { default(int), "Integer" }; + yield return new object[] { default(uint), "UInteger" }; + yield return new object[] { default(long), "Long" }; + yield return new object[] { default(ulong), "ULong" }; + yield return new object[] { default(float), "Single" }; + yield return new object[] { default(double), "Double" }; + yield return new object[] { default(decimal), "Decimal" }; + yield return new object[] { default(DateTime), "Date" }; + yield return new object[] { "", "String" }; + yield return new object[] { default(object[]), "Nothing" }; + yield return new object[] { new object[0], "Object()" }; + yield return new object[] { new char[0, 0], "Char(,)" }; + yield return new object[] { default(int?), "Nothing" }; + yield return new object[] { (int?)0, "Integer" }; + } + [Theory] [InlineData(null, null)] [InlineData("System.Object", "Object")] diff --git a/src/libraries/Microsoft.VisualBasic.Core/tests/ConversionTests.cs b/src/libraries/Microsoft.VisualBasic.Core/tests/ConversionTests.cs new file mode 100644 index 00000000000..4e641c08604 --- /dev/null +++ b/src/libraries/Microsoft.VisualBasic.Core/tests/ConversionTests.cs @@ -0,0 +1,793 @@ +// 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 Xunit; + +namespace Microsoft.VisualBasic.Tests +{ + public class ConversionTests + { + [Theory] + [MemberData(nameof(CTypeDynamic_Byte_TestData))] + public void CTypeDynamic_Byte(object expression, object expected) + { + CTypeDynamic(expression, expected); + } + + [Theory] + [MemberData(nameof(CTypeDynamic_SByte_TestData))] + public void CTypeDynamic_SByte(object expression, object expected) + { + CTypeDynamic(expression, expected); + } + + [Theory] + [MemberData(nameof(CTypeDynamic_UShort_TestData))] + public void CTypeDynamic_UShort(object expression, object expected) + { + CTypeDynamic(expression, expected); + } + + [Theory] + [MemberData(nameof(CTypeDynamic_Short_TestData))] + public void CTypeDynamic_Short(object expression, object expected) + { + CTypeDynamic(expression, expected); + } + + [Theory] + [MemberData(nameof(CTypeDynamic_UInteger_TestData))] + public void CTypeDynamic_UInteger(object expression, object expected) + { + CTypeDynamic(expression, expected); + } + + [Theory] + [MemberData(nameof(CTypeDynamic_Integer_TestData))] + public void CTypeDynamic_Integer(object expression, object expected) + { + CTypeDynamic(expression, expected); + } + + [Theory] + [MemberData(nameof(CTypeDynamic_ULong_TestData))] + public void CTypeDynamic_ULong(object expression, object expected) + { + CTypeDynamic(expression, expected); + } + + [Theory] + [MemberData(nameof(CTypeDynamic_Long_TestData))] + public void CTypeDynamic_Long(object expression, object expected) + { + CTypeDynamic(expression, expected); + } + + [Theory] + [MemberData(nameof(CTypeDynamic_Single_TestData))] + public void CTypeDynamic_Single(object expression, object expected) + { + CTypeDynamic(expression, expected); + } + + [Theory] + [MemberData(nameof(CTypeDynamic_Double_TestData))] + public void CTypeDynamic_Double(object expression, object expected) + { + CTypeDynamic(expression, expected); + } + + [Theory] + [MemberData(nameof(CTypeDynamic_Decimal_TestData))] + public void CTypeDynamic_Decimal(object expression, object expected) + { + CTypeDynamic(expression, expected); + } + + [Theory] + [MemberData(nameof(CTypeDynamic_Char_TestData))] + public void CTypeDynamic_Char(object expression, object expected) + { + CTypeDynamic(expression, expected); + } + + [Theory] + [MemberData(nameof(CTypeDynamic_String_TestData))] + public void CTypeDynamic_String(object expression, object expected) + { + CTypeDynamic(expression, expected); + } + + private static void CTypeDynamic(object expression, object expected) + { + Assert.Equal(expected, Conversion.CTypeDynamic(expression, typeof(T))); + Assert.Equal(expected, Conversion.CTypeDynamic(expression)); + } + + private static IEnumerable CTypeDynamic_Byte_TestData() => ConversionsTests.ToByte_Object_TestData(); + private static IEnumerable CTypeDynamic_SByte_TestData() => ConversionsTests.ToSByte_Object_TestData(); + private static IEnumerable CTypeDynamic_UShort_TestData() => ConversionsTests.ToUShort_Object_TestData(); + private static IEnumerable CTypeDynamic_Short_TestData() => ConversionsTests.ToShort_Object_TestData(); + private static IEnumerable CTypeDynamic_UInteger_TestData() => ConversionsTests.ToUInteger_Object_TestData(); + private static IEnumerable CTypeDynamic_Integer_TestData() => ConversionsTests.ToInteger_Object_TestData(); + private static IEnumerable CTypeDynamic_ULong_TestData() => ConversionsTests.ToULong_Object_TestData(); + private static IEnumerable CTypeDynamic_Long_TestData() => ConversionsTests.ToLong_Object_TestData(); + private static IEnumerable CTypeDynamic_Single_TestData() => ConversionsTests.ToSingle_Object_TestData(); + private static IEnumerable CTypeDynamic_Double_TestData() => ConversionsTests.ToDouble_Object_TestData(); + private static IEnumerable CTypeDynamic_Decimal_TestData() => ConversionsTests.ToDecimal_Object_TestData(); + private static IEnumerable CTypeDynamic_Char_TestData() => ConversionsTests.ToChar_Object_TestData(); + private static IEnumerable CTypeDynamic_String_TestData() => ConversionsTests.ToString_Object_TestData(); + + [Theory] + [InlineData(null, null)] + [InlineData("", null)] + public void CTypeDynamic_ArgumentException(object expression, Type targetType) + { + Assert.Throws(() => Conversion.CTypeDynamic(expression, targetType)); + } + + [Fact] + public void ErrorToString() + { + Microsoft.VisualBasic.CompilerServices.ProjectData.SetProjectError(new System.IO.FileNotFoundException()); + Assert.NotNull(Conversion.ErrorToString()); + } + + [Theory] + [InlineData(0)] + [InlineData(-1)] + [InlineData(7)] + [InlineData(int.MinValue)] + public void ErrorToString(int errorNumber) + { + Assert.NotNull(Conversion.ErrorToString(errorNumber)); + } + + [Theory] + [InlineData(int.MaxValue)] + public void ErrorToString_ArgumentException(int errorNumber) + { + Assert.Throws(() => Conversion.ErrorToString(errorNumber)); + } + + [Theory] + [MemberData(nameof(Fix_Short_TestData))] + [MemberData(nameof(Fix_Integer_TestData))] + [MemberData(nameof(Fix_Long_TestData))] + [MemberData(nameof(Fix_Single_TestData))] + [MemberData(nameof(Fix_Double_TestData))] + [MemberData(nameof(Fix_Decimal_TestData))] + public void Fix(object value, object expected) + { + Assert.Equal(expected, Conversion.Fix(value)); + } + + [Theory] + [MemberData(nameof(Fix_Short_TestData))] + public void Fix(short value, short expected) + { + Assert.Equal(expected, Conversion.Fix(value)); + } + + [Theory] + [MemberData(nameof(Fix_Integer_TestData))] + public void Fix(int value, int expected) + { + Assert.Equal(expected, Conversion.Fix(value)); + } + + [Theory] + [MemberData(nameof(Fix_Long_TestData))] + public void Fix(long value, long expected) + { + Assert.Equal(expected, Conversion.Fix(value)); + } + + [Theory] + [MemberData(nameof(Fix_Single_TestData))] + public void Fix(float value, float expected) + { + Assert.Equal(expected, Conversion.Fix(value)); + } + + [Theory] + [MemberData(nameof(Fix_Double_TestData))] + public void Fix(double value, double expected) + { + Assert.Equal(expected, Conversion.Fix(value)); + } + + [Theory] + [MemberData(nameof(Fix_Decimal_TestData))] + public void Fix(decimal value, decimal expected) + { + Assert.Equal(expected, Conversion.Fix(value)); + } + + private static IEnumerable Fix_Short_TestData() + { + yield return new object[] { (short)0, (short)0 }; + // Add more... + } + + private static IEnumerable Fix_Integer_TestData() + { + yield return new object[] { 0, 0 }; + // Add more... + } + + private static IEnumerable Fix_Long_TestData() + { + yield return new object[] { 0L, 0L }; + // Add more... + } + + private static IEnumerable Fix_Single_TestData() + { + yield return new object[] { 0.0f, 0.0f }; + // Add more... + } + + private static IEnumerable Fix_Double_TestData() + { + yield return new object[] { 0.0, 0.0 }; + // Add more... + } + + private static IEnumerable Fix_Decimal_TestData() + { + yield return new object[] { (decimal)0, (decimal)0 }; + // Add more... + } + + [Theory] + [MemberData(nameof(Hex_Byte_TestData))] + [MemberData(nameof(Hex_SByte_TestData))] + [MemberData(nameof(Hex_UShort_TestData))] + [MemberData(nameof(Hex_Short_TestData))] + [MemberData(nameof(Hex_UInteger_TestData))] + [MemberData(nameof(Hex_Integer_TestData))] + [MemberData(nameof(Hex_ULong_TestData))] + [MemberData(nameof(Hex_Long_TestData))] + [MemberData(nameof(Hex_Single_TestData))] + [MemberData(nameof(Hex_Double_TestData))] + [MemberData(nameof(Hex_Decimal_TestData))] + public void Hex(object value, string expected) + { + Assert.Equal(expected, Conversion.Hex(value)); + } + + [Theory] + [MemberData(nameof(Hex_Byte_TestData))] + public void Hex(byte value, string expected) + { + Assert.Equal(expected, Conversion.Hex(value)); + } + + [Theory] + [MemberData(nameof(Hex_SByte_TestData))] + public void Hex(sbyte value, string expected) + { + Assert.Equal(expected, Conversion.Hex(value)); + } + + [Theory] + [MemberData(nameof(Hex_UShort_TestData))] + public void Hex(ushort value, string expected) + { + Assert.Equal(expected, Conversion.Hex(value)); + } + + [Theory] + [MemberData(nameof(Hex_Short_TestData))] + public void Hex(short value, string expected) + { + Assert.Equal(expected, Conversion.Hex(value)); + } + + [Theory] + [MemberData(nameof(Hex_UInteger_TestData))] + public void Hex(uint value, string expected) + { + Assert.Equal(expected, Conversion.Hex(value)); + } + + [Theory] + [MemberData(nameof(Hex_Integer_TestData))] + public void Hex(int value, string expected) + { + Assert.Equal(expected, Conversion.Hex(value)); + } + + [Theory] + [MemberData(nameof(Hex_ULong_TestData))] + public void Hex(ulong value, string expected) + { + Assert.Equal(expected, Conversion.Hex(value)); + } + + [Theory] + [MemberData(nameof(Hex_Long_TestData))] + public void Hex(long value, string expected) + { + Assert.Equal(expected, Conversion.Hex(value)); + } + + [Theory] + [MemberData(nameof(Hex_Single_TestData))] + public void Hex(float value, string expected) + { + Assert.Equal(expected, Conversion.Hex(value)); + } + + [Theory] + [MemberData(nameof(Hex_Double_TestData))] + public void Hex(double value, string expected) + { + Assert.Equal(expected, Conversion.Hex(value)); + } + + [Theory] + [MemberData(nameof(Hex_Decimal_TestData))] + public void Hex(decimal value, string expected) + { + Assert.Equal(expected, Conversion.Hex(value)); + } + + private static IEnumerable Hex_Byte_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Hex_SByte_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Hex_UShort_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Hex_Short_TestData() + { + yield return new object[] { short.MinValue, "8000" }; + yield return new object[] { (short)-1, "FFFF" }; + yield return new object[] { (short)0, "0" }; + yield return new object[] { (short)1, "1" }; + yield return new object[] { short.MaxValue, "7FFF" }; + } + + private static IEnumerable Hex_UInteger_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Hex_Integer_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Hex_ULong_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Hex_Long_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Hex_Single_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Hex_Double_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Hex_Decimal_TestData() + { + yield break; // Add more... + } + + [Theory] + [MemberData(nameof(Int_Short_TestData))] + [MemberData(nameof(Int_Integer_TestData))] + [MemberData(nameof(Int_Long_TestData))] + [MemberData(nameof(Int_Single_TestData))] + [MemberData(nameof(Int_Double_TestData))] + [MemberData(nameof(Int_Decimal_TestData))] + public void Int(object value, object expected) + { + Assert.Equal(expected, Conversion.Int(value)); + } + + [Theory] + [MemberData(nameof(Int_Short_TestData))] + public void Int(short value, short expected) + { + Assert.Equal(expected, Conversion.Int(value)); + } + + [Theory] + [MemberData(nameof(Int_Integer_TestData))] + public void Int(int value, int expected) + { + Assert.Equal(expected, Conversion.Int(value)); + } + + [Theory] + [MemberData(nameof(Int_Long_TestData))] + public void Int(long value, long expected) + { + Assert.Equal(expected, Conversion.Int(value)); + } + + [Theory] + [MemberData(nameof(Int_Single_TestData))] + public void Int(float value, float expected) + { + Assert.Equal(expected, Conversion.Int(value)); + } + + [Theory] + [MemberData(nameof(Int_Double_TestData))] + public void Int(double value, double expected) + { + Assert.Equal(expected, Conversion.Int(value)); + } + + [Theory] + [MemberData(nameof(Int_Decimal_TestData))] + public void Int(decimal value, decimal expected) + { + Assert.Equal(expected, Conversion.Int(value)); + } + + private static IEnumerable Int_Short_TestData() + { + yield return new object[] { (short)0, (short)0 }; + // Add more... + } + + private static IEnumerable Int_Integer_TestData() + { + yield return new object[] { 0, 0 }; + // Add more... + } + + private static IEnumerable Int_Long_TestData() + { + yield return new object[] { 0L, 0L }; + // Add more... + } + + private static IEnumerable Int_Single_TestData() + { + yield return new object[] { 0.0f, 0.0f }; + // Add more... + } + + private static IEnumerable Int_Double_TestData() + { + yield return new object[] { 0.0, 0.0 }; + // Add more... + } + + private static IEnumerable Int_Decimal_TestData() + { + yield return new object[] { (decimal)0, (decimal)0 }; + // Add more... + } + + [Theory] + [MemberData(nameof(Oct_Byte_TestData))] + [MemberData(nameof(Oct_SByte_TestData))] + [MemberData(nameof(Oct_UShort_TestData))] + [MemberData(nameof(Oct_Short_TestData))] + [MemberData(nameof(Oct_UInteger_TestData))] + [MemberData(nameof(Oct_Integer_TestData))] + [MemberData(nameof(Oct_ULong_TestData))] + [MemberData(nameof(Oct_Long_TestData))] + [MemberData(nameof(Oct_Single_TestData))] + [MemberData(nameof(Oct_Double_TestData))] + [MemberData(nameof(Oct_Decimal_TestData))] + public void Oct(object value, string expected) + { + Assert.Equal(expected, Conversion.Oct(value)); + } + + [Theory] + [MemberData(nameof(Oct_Byte_TestData))] + public void Oct(byte value, string expected) + { + Assert.Equal(expected, Conversion.Oct(value)); + } + + [Theory] + [MemberData(nameof(Oct_SByte_TestData))] + public void Oct(sbyte value, string expected) + { + Assert.Equal(expected, Conversion.Oct(value)); + } + + [Theory] + [MemberData(nameof(Oct_UShort_TestData))] + public void Oct(ushort value, string expected) + { + Assert.Equal(expected, Conversion.Oct(value)); + } + + [Theory] + [MemberData(nameof(Oct_Short_TestData))] + public void Oct(short value, string expected) + { + Assert.Equal(expected, Conversion.Oct(value)); + } + + [Theory] + [MemberData(nameof(Oct_UInteger_TestData))] + public void Oct(uint value, string expected) + { + Assert.Equal(expected, Conversion.Oct(value)); + } + + [Theory] + [MemberData(nameof(Oct_Integer_TestData))] + public void Oct(int value, string expected) + { + Assert.Equal(expected, Conversion.Oct(value)); + } + + [Theory] + [MemberData(nameof(Oct_ULong_TestData))] + public void Oct(ulong value, string expected) + { + Assert.Equal(expected, Conversion.Oct(value)); + } + + [Theory] + [MemberData(nameof(Oct_Long_TestData))] + public void Oct(long value, string expected) + { + Assert.Equal(expected, Conversion.Oct(value)); + } + + [Theory] + [MemberData(nameof(Oct_Single_TestData))] + public void Oct(float value, string expected) + { + Assert.Equal(expected, Conversion.Oct(value)); + } + + [Theory] + [MemberData(nameof(Oct_Double_TestData))] + public void Oct(double value, string expected) + { + Assert.Equal(expected, Conversion.Oct(value)); + } + + [Theory] + [MemberData(nameof(Oct_Decimal_TestData))] + public void Oct(decimal value, string expected) + { + Assert.Equal(expected, Conversion.Oct(value)); + } + + private static IEnumerable Oct_Byte_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Oct_SByte_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Oct_UShort_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Oct_Short_TestData() + { + yield return new object[] { short.MinValue, "100000" }; + yield return new object[] { (short)-1, "177777" }; + yield return new object[] { (short)0, "0" }; + yield return new object[] { (short)1, "1" }; + yield return new object[] { short.MaxValue, "77777" }; + } + + private static IEnumerable Oct_UInteger_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Oct_Integer_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Oct_ULong_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Oct_Long_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Oct_Single_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Oct_Double_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Oct_Decimal_TestData() + { + yield break; // Add more... + } + + [Theory] + [MemberData(nameof(Str_TestData))] + public void Str(object value, string expected) + { + Assert.Equal(expected, Conversion.Str(value)); + } + + [Theory] + [InlineData(null)] + public void Str_ArgumentNullException(object value) + { + Assert.Throws(() => Conversion.Str(value)); + } + + private static IEnumerable Str_TestData() + { + yield return new object[] { 0, " 0" }; + yield return new object[] { 1, " 1" }; + yield return new object[] { -1, "-1" }; + // Add more... + } + + [Theory] + [MemberData(nameof(Val_Object_TestData))] + [MemberData(nameof(Val_Char_TestData))] + [MemberData(nameof(Val_String_TestData))] + public void Val(object value, double expected) + { + Assert.Equal(expected, Conversion.Val(value)); + } + + [Theory] + [MemberData(nameof(Val_Object_ArgumentException_TestData))] + public void Val_ArgumentException(object value) + { + Assert.Throws(() => Conversion.Val(value)); + } + + [Theory] + [MemberData(nameof(Val_Object_OverflowException_TestData))] + public void Val_OverflowException(object value) + { + Assert.Throws(() => Conversion.Val(value)); + } + + [Theory] + [MemberData(nameof(Val_Char_TestData))] + public void Val(char value, int expected) + { + Assert.Equal(expected, Conversion.Val(value)); + } + + [Theory] + [MemberData(nameof(Val_Char_ArgumentException_TestData))] + public void Val_ArgumentException(char value) + { + Assert.Throws(() => Conversion.Val(value)); + } + + [Theory] + [MemberData(nameof(Val_Char_OverflowException_TestData))] + public void Val_OverflowException(char value) + { + Assert.Throws(() => Conversion.Val(value)); + } + + [Theory] + [MemberData(nameof(Val_String_TestData))] + public void Val(string value, double expected) + { + Assert.Equal(expected, Conversion.Val(value)); + } + + [Theory] + [MemberData(nameof(Val_String_ArgumentException_TestData))] + public void Val_ArgumentException(string value) + { + Assert.Throws(() => Conversion.Val(value)); + } + + [Theory] + [MemberData(nameof(Val_String_OverflowException_TestData))] + public void Val_OverflowException(string value) + { + Assert.Throws(() => Conversion.Val(value)); + } + + private static IEnumerable Val_Object_TestData() + { + yield return new object[] { 0, "0" }; + yield return new object[] { 1, " 1" }; + // Add more... + } + + private static IEnumerable Val_Object_ArgumentException_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Val_Object_OverflowException_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Val_Char_TestData() + { + yield return new object[] { (char)0, 0 }; + yield return new object[] { '0', 0 }; + yield return new object[] { '1', 1 }; + yield return new object[] { '2', 2 }; + yield return new object[] { '3', 3 }; + yield return new object[] { '4', 4 }; + yield return new object[] { '5', 5 }; + yield return new object[] { '6', 6 }; + yield return new object[] { '7', 7 }; + yield return new object[] { '8', 8 }; + yield return new object[] { '9', 9 }; + yield return new object[] { 'A', 0 }; + yield return new object[] { char.MaxValue, 0 }; + } + + private static IEnumerable Val_Char_ArgumentException_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Val_Char_OverflowException_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Val_String_TestData() + { + yield return new object[] { null, 0.0 }; + yield return new object[] { "", 0.0 }; + yield return new object[] { "0", 0.0 }; + yield return new object[] { "&H7F", 127.0 }; + yield return new object[] { "-1e20", -1.0e20 }; + yield return new object[] { "1e-3", 1.0e-3 }; + } + + private static IEnumerable Val_String_ArgumentException_TestData() + { + yield break; // Add more... + } + + private static IEnumerable Val_String_OverflowException_TestData() + { + yield break; // Add more... + } + } +} diff --git a/src/libraries/Microsoft.VisualBasic.Core/tests/InformationTests.cs b/src/libraries/Microsoft.VisualBasic.Core/tests/InformationTests.cs index eaa3255ae9a..73815b6ea03 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/tests/InformationTests.cs +++ b/src/libraries/Microsoft.VisualBasic.Core/tests/InformationTests.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections.Generic; using Xunit; namespace Microsoft.VisualBasic.Tests @@ -174,6 +175,39 @@ namespace Microsoft.VisualBasic.Tests Assert.Equal(expected, Information.SystemTypeName(value)); } + [Theory] + [MemberData(nameof(TypeName_TestData))] + public void TypeName(object expression, string expected) + { + Assert.Equal(expected, Information.TypeName(expression)); + } + + private static IEnumerable TypeName_TestData() + { + yield return new object[] { null, "Nothing" }; + yield return new object[] { new object(), "Object" }; + yield return new object[] { default(bool), "Boolean" }; + yield return new object[] { default(char), "Char" }; + yield return new object[] { default(sbyte), "SByte" }; + yield return new object[] { default(byte), "Byte" }; + yield return new object[] { default(short), "Short" }; + yield return new object[] { default(ushort), "UInt16" }; + yield return new object[] { default(int), "Integer" }; + yield return new object[] { default(uint), "UInt32" }; + yield return new object[] { default(long), "Long" }; + yield return new object[] { default(ulong), "UInt64" }; + yield return new object[] { default(float), "Single" }; + yield return new object[] { default(double), "Double" }; + yield return new object[] { default(decimal), "Decimal" }; + yield return new object[] { default(DateTime), "Date" }; + yield return new object[] { "", "String" }; + yield return new object[] { default(object[]), "Nothing" }; + yield return new object[] { new object[0], "Object()" }; + yield return new object[] { new char[0, 0], "Char(,)" }; + yield return new object[] { default(int?), "Nothing" }; + yield return new object[] { (int?)0, "Integer" }; + } + [Theory] [InlineData(null, null)] [InlineData("System.Object", "Object")] diff --git a/src/libraries/Microsoft.VisualBasic.Core/tests/InteractionTests.cs b/src/libraries/Microsoft.VisualBasic.Core/tests/InteractionTests.cs new file mode 100644 index 00000000000..b5b09f01098 --- /dev/null +++ b/src/libraries/Microsoft.VisualBasic.Core/tests/InteractionTests.cs @@ -0,0 +1,77 @@ +// 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 Xunit; + +namespace Microsoft.VisualBasic.Tests +{ + public class InteractionTests + { + [Theory] + [MemberData(nameof(CallByName_TestData))] + public void CallByName(object instance, string methodName, CallType useCallType, object[] args, Func getResult, object expected) + { + Assert.Equal(getResult is null ? expected : null, Interaction.CallByName(instance, methodName, useCallType, args)); + if (getResult != null) + { + Assert.Equal(expected, getResult(instance)); + } + } + + [Theory] + [MemberData(nameof(CallByName_ArgumentException_TestData))] + public void CallByName_ArgumentException(object instance, string methodName, CallType useCallType, object[] args) + { + Assert.Throws(() => Interaction.CallByName(instance, methodName, useCallType, args)); + } + + [Theory] + [MemberData(nameof(CallByName_MissingMemberException_TestData))] + public void CallByName_MissingMemberException(object instance, string methodName, CallType useCallType, object[] args) + { + Assert.Throws(() => Interaction.CallByName(instance, methodName, useCallType, args)); + } + + private static IEnumerable CallByName_TestData() + { + yield return new object[] { new Class(), "Method", CallType.Method, new object[] { 1, 2 }, null, 3 }; + yield return new object[] { new Class(), "Method", CallType.Get, new object[] { 2, 3 }, null, 5 }; + yield return new object[] { new Class(), "P", CallType.Get, new object[0], null, 0 }; + yield return new object[] { new Class(), "Item", CallType.Get, new object[] { 2 }, null, 2 }; + yield return new object[] { new Class(), "P", CallType.Set, new object[] { 3 }, new Func(obj => ((Class)obj).Value), 3 }; + yield return new object[] { new Class(), "Item", CallType.Let, new object[] { 4, 5 }, new Func(obj => ((Class)obj).Value), 9 }; + } + + private static IEnumerable CallByName_ArgumentException_TestData() + { + yield return new object[] { null, null, default(CallType), new object[0] }; + yield return new object[] { new Class(), "Method", default(CallType), new object[] { 1, 2 } }; + yield return new object[] { new Class(), "Method", (CallType)int.MaxValue, new object[] { 1, 2 } }; + } + + private static IEnumerable CallByName_MissingMemberException_TestData() + { + yield return new object[] { new Class(), "Method", CallType.Method, new object[0] }; + yield return new object[] { new Class(), "Q", CallType.Get, new object[0] }; + } + + private sealed class Class + { + public int Value; + public int Method(int x, int y) => x + y; + public int P + { + get { return Value; } + set { Value = value; } + } + public object this[object index] + { + get { return Value + (int)index; } + set { Value = (int)value + (int)index; } + } + } + } +} diff --git a/src/libraries/Microsoft.VisualBasic.Core/tests/Microsoft.VisualBasic.Core.Tests.csproj b/src/libraries/Microsoft.VisualBasic.Core/tests/Microsoft.VisualBasic.Core.Tests.csproj index f83344927e0..e8de35a609d 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/tests/Microsoft.VisualBasic.Core.Tests.csproj +++ b/src/libraries/Microsoft.VisualBasic.Core/tests/Microsoft.VisualBasic.Core.Tests.csproj @@ -13,12 +13,14 @@ + + diff --git a/src/libraries/Microsoft.VisualBasic.Core/tests/StringsTests.cs b/src/libraries/Microsoft.VisualBasic.Core/tests/StringsTests.cs index 8f85d9ce344..e099fe79a75 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/tests/StringsTests.cs +++ b/src/libraries/Microsoft.VisualBasic.Core/tests/StringsTests.cs @@ -459,6 +459,18 @@ namespace Microsoft.VisualBasic.Tests Assert.Equal(expectedTextCompare, Strings.StrComp(left, right, CompareMethod.Text)); } + [Theory] + [InlineData(null, "")] + [InlineData("", "")] + [InlineData("\0", "\0")] + [InlineData("ABC", "CBA")] + [InlineData("\ud83c\udfc8", "\ud83c\udfc8")] + [InlineData("A\ud83c\udfc8", "\ud83c\udfc8A")] + public void StrReverse(string str, string expected) + { + Assert.Equal(expected, Strings.StrReverse(str)); + } + public static TheoryData InStr_TestData_NullsAndEmpties() => new TheoryData { {null, null, 0 },