Skip to content

Commit

Permalink
Modernize VB ExceptionUtils add Test Coverage, fix Time Tests REVIEW …
Browse files Browse the repository at this point in the history
…1st (#11982)

* Modernize ExceptionUtils

* Fix broken Time Tests

* Fix broken Time Tests
Add Tests for ExceptionUtils

* Fix GetArgumentExceptionWithArgNameTest_Succeed

* Apply using to FileLogTraceListener

* Remove usage  of CompilerServices.Utils
Remove unneeded Paentheses
Cleanup spacing
Sort UnsafeNativeMethods

* Sort Imports in Interaction.VB

* Remove space on blank lines in  MissingPropertySerializationConfigurationAnalyzerTest

* Add Collection("Sequential") Attribute to ControlTests

* Remove Collection("Sequential") from ControlTests

* Rename ControlTests.vb to  ControlInvokeTests.vb

* Fix casing in MissingPropertySerializationConfigurationAnalyzerTest

* Fix comment is ControlInvokeTests

* Fix Screen to be System.Windows.Forms.Screen

* Clean up Computer Screen

* Correct StackFrame in Comments

* Respond to PR feedback

* Correct Test Name

* Improve Time tests

* Wrap long lines

* Shorted long lines in FileSystemProxy

* Sort FileSystemProxy

* Minor cleanup

* Update src/Microsoft.VisualBasic.Forms/tests/UnitTests/System/Windows/TestUtilities/TestData/DualTimeZones.TimeZoneNames.vb

Co-authored-by: Loni Tra <[email protected]>

* Respond to PR Feedback

* Fix Merge Conflict

* Revert TimeTestData based on PR Feedback

---------

Co-authored-by: Loni Tra <[email protected]>
  • Loading branch information
paul1956 and lonitra authored Sep 11, 2024
1 parent c44316a commit ce8b46f
Show file tree
Hide file tree
Showing 30 changed files with 437 additions and 240 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.VisualBasic.Forms/src/GlobalSuppressions.vb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

Imports System.Diagnostics.CodeAnalysis

<Assembly: SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification:="Compat", Scope:="member", Target:="~M:Microsoft.VisualBasic.CompilerServices.ExceptionUtils.BuildException(System.Int32,System.String,System.Boolean@)~System.Exception")>
<Assembly: SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification:="Compat", Scope:="member", Target:="~M:Microsoft.VisualBasic.CompilerServices.ExceptionUtils.VbMakeException(System.Int32)~System.Exception")>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
' Licensed to the .NET Foundation under one or more agreements.
' The .NET Foundation licenses this file to you under the MIT license.

Imports System.Runtime.CompilerServices

<Assembly: InternalsVisibleTo("Microsoft.VisualBasic.Forms.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010007d1fa57c4aed9f0a32e84aa0faefd0de9e8fd6aec8f87fb03766c834c99921eb23be79ad9d5dcc1dd9ad236132102900b723cf980957fc4e177108fc607774f29e8320e92ea05ece4e821c0a5efe8f1645c4c0c93c1ab99285d622caa652c1dfad63d745d6f2de5f17e5eaf0fc4963d261c8a12436518206dc093344d5ad293")>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

Imports System.ComponentModel

Imports VbUtils = Microsoft.VisualBasic.CompilerServices.Utils
Imports VbUtils = Microsoft.VisualBasic.CompilerServices.ExceptionUtils

Namespace Microsoft.VisualBasic.ApplicationServices

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

Imports System.ComponentModel

Imports VbUtils = Microsoft.VisualBasic.CompilerServices.Utils
Imports VbUtils = Microsoft.VisualBasic.CompilerServices.ExceptionUtils

Namespace Microsoft.VisualBasic.ApplicationServices

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Imports System.Threading
Imports System.Windows.Forms
Imports System.Windows.Forms.Analyzers.Diagnostics

Imports Utils = Microsoft.VisualBasic.CompilerServices.Utils
Imports VbUtils = Microsoft.VisualBasic.CompilerServices.ExceptionUtils

Namespace Microsoft.VisualBasic.ApplicationServices
Expand Down Expand Up @@ -310,7 +309,7 @@ Namespace Microsoft.VisualBasic.ApplicationServices

' Allow for the case where they set splash screen = nothing and mainForm is currently nothing.
If value IsNot Nothing AndAlso value Is _appContext.MainForm Then
Throw New ArgumentException(Utils.GetResourceString(SR.AppModel_SplashAndMainFormTheSame))
Throw New ArgumentException(VbUtils.GetResourceString(SR.AppModel_SplashAndMainFormTheSame))
End If

_splashScreen = value
Expand All @@ -329,7 +328,7 @@ Namespace Microsoft.VisualBasic.ApplicationServices
Throw VbUtils.GetArgumentNullException("MainForm", SR.General_PropertyNothing, "MainForm")
End If
If value Is _splashScreen Then
Throw New ArgumentException(Utils.GetResourceString(SR.AppModel_SplashAndMainFormTheSame))
Throw New ArgumentException(VbUtils.GetResourceString(SR.AppModel_SplashAndMainFormTheSame))
End If
_appContext.MainForm = value
End Set
Expand Down Expand Up @@ -803,7 +802,7 @@ Namespace Microsoft.VisualBasic.ApplicationServices

RaiseEvent ApplyApplicationDefaults(Me, applicationDefaultsEventArgs)

If (applicationDefaultsEventArgs.Font IsNot Nothing) Then
If applicationDefaultsEventArgs.Font IsNot Nothing Then
Application.SetDefaultFont(applicationDefaultsEventArgs.Font)
End If

Expand All @@ -828,7 +827,7 @@ Namespace Microsoft.VisualBasic.ApplicationServices
Debug.Assert(dpiSetResult, "We could net set the HighDpiMode.")

' Now, let's set VisualStyles and ColorMode:
If (_enableVisualStyles) Then
If _enableVisualStyles Then
Application.EnableVisualStyles()
End If

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,13 @@
' The .NET Foundation licenses this file to you under the MIT license.

Imports System.Runtime.InteropServices
Imports VbUtils = Microsoft.VisualBasic.CompilerServices.Utils
Imports System.Threading

Namespace Microsoft.VisualBasic.CompilerServices

' Implements error utilities for Basic
Friend Module ExceptionUtils

Friend Function BuildException(
resourceId As Integer,
description As String,
ByRef vbDefinedError As Boolean) As Exception

vbDefinedError = True

Select Case resourceId

Case VbErrors.None

Case VbErrors.FileNotFound
Return New IO.FileNotFoundException(description)

Case VbErrors.PermissionDenied
Return New IO.IOException(description)

Case Else
'Fall below to default
vbDefinedError = False
Return New Exception(description)
End Select

vbDefinedError = False
Return New Exception(description)

End Function

''' <summary>
''' Returns a new instance of <see cref="ArgumentException"/> with the message from resource file and the Exception.ArgumentName property set.
''' </summary>
Expand All @@ -50,7 +22,7 @@ Namespace Microsoft.VisualBasic.CompilerServices
resourceID As String,
ParamArray placeHolders() As String) As ArgumentException

Return New ArgumentException(VbUtils.GetResourceString(resourceID, placeHolders), argumentName)
Return New ArgumentException(GetResourceString(resourceID, placeHolders), argumentName)
End Function

''' <summary>
Expand All @@ -60,7 +32,7 @@ Namespace Microsoft.VisualBasic.CompilerServices
''' <returns>A new instance of <see cref="ArgumentNullException"/>.</returns>
Friend Function GetArgumentNullException(argumentName As String) As ArgumentNullException

Return New ArgumentNullException(argumentName, VbUtils.GetResourceString(SR.General_ArgumentNullException))
Return New ArgumentNullException(argumentName, GetResourceString(SR.General_ArgumentNullException))
End Function

''' <summary>
Expand All @@ -75,7 +47,7 @@ Namespace Microsoft.VisualBasic.CompilerServices
resourceID As String,
ParamArray placeHolders() As String) As ArgumentNullException

Return New ArgumentNullException(argumentName, VbUtils.GetResourceString(resourceID, placeHolders))
Return New ArgumentNullException(argumentName, GetResourceString(resourceID, placeHolders))
End Function

''' <summary>
Expand All @@ -88,7 +60,7 @@ Namespace Microsoft.VisualBasic.CompilerServices
resourceID As String,
ParamArray placeHolders() As String) As IO.DirectoryNotFoundException

Return New IO.DirectoryNotFoundException(VbUtils.GetResourceString(resourceID, placeHolders))
Return New IO.DirectoryNotFoundException(GetResourceString(resourceID, placeHolders))
End Function

''' <summary>
Expand All @@ -103,7 +75,7 @@ Namespace Microsoft.VisualBasic.CompilerServices
resourceID As String,
ParamArray placeHolders() As String) As IO.FileNotFoundException

Return New IO.FileNotFoundException(VbUtils.GetResourceString(resourceID, placeHolders), fileName)
Return New IO.FileNotFoundException(GetResourceString(resourceID, placeHolders), fileName)
End Function

''' <summary>
Expand All @@ -116,7 +88,7 @@ Namespace Microsoft.VisualBasic.CompilerServices
resourceID As String,
ParamArray placeHolders() As String) As InvalidOperationException

Return New InvalidOperationException(VbUtils.GetResourceString(resourceID, placeHolders))
Return New InvalidOperationException(GetResourceString(resourceID, placeHolders))
End Function

''' <summary>
Expand All @@ -129,7 +101,19 @@ Namespace Microsoft.VisualBasic.CompilerServices
resourceID As String,
ParamArray placeHolders() As String) As IO.IOException

Return New IO.IOException(VbUtils.GetResourceString(resourceID, placeHolders))
Return New IO.IOException(GetResourceString(resourceID, placeHolders))
End Function

Friend Function GetResourceString(
resourceKey As String,
ParamArray args() As String) As String

Return String.Format(Thread.CurrentThread.CurrentCulture, resourceKey, args)
End Function

Friend Function GetResourceString(resourceId As VbErrors) As String
Dim id As String = $"ID{CStr(resourceId)}"
Return SR.GetResourceString(id, id)
End Function

''' <summary>
Expand All @@ -143,29 +127,27 @@ Namespace Microsoft.VisualBasic.CompilerServices
resourceID As String,
ParamArray placeHolders() As String) As ComponentModel.Win32Exception

Return New ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), VbUtils.GetResourceString(resourceID, placeHolders))
Return New ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), GetResourceString(resourceID, placeHolders))
End Function

Friend Function VbMakeException(hr As Integer) As Exception
Dim sMsg As String
Friend Function VbMakeException(resourceId As Integer) As Exception
Dim description As String = String.Empty

If hr > 0 AndAlso hr <= &HFFFFI Then
sMsg = VbUtils.GetResourceString(CType(hr, VbErrors))
Else
sMsg = String.Empty
If resourceId > 0 AndAlso resourceId <= &HFFFFI Then
description = GetResourceString(DirectCast(resourceId, VbErrors))
End If
VbMakeException = VbMakeExceptionEx(hr, sMsg)
End Function

Friend Function VbMakeExceptionEx(number As Integer, sMsg As String) As Exception
Dim vBDefinedError As Boolean
Select Case resourceId

VbMakeExceptionEx = BuildException(number, sMsg, vBDefinedError)
Case VbErrors.FileNotFound
Return New IO.FileNotFoundException(description)

If vBDefinedError Then
' .NET Framework implementation calls:
' Err().SetUnmappedError(number)
End If
Case VbErrors.PermissionDenied
Return New IO.IOException(description)

Case Else
Return New Exception(description)
End Select

End Function

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
' The .NET Foundation licenses this file to you under the MIT license.

Namespace Microsoft.VisualBasic

''' <summary>
''' Enum for three ways to play a .wav file.
''' </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Namespace Microsoft.VisualBasic.Devices
Private _sound As Media.SoundPlayer

''' <summary>
''' Creates a new Audio object.
''' Creates a new <see cref="Audio"/> object.
''' </summary>
Public Sub New()
End Sub
Expand All @@ -32,10 +32,13 @@ Namespace Microsoft.VisualBasic.Devices
End Sub

''' <summary>
''' Gets the full name and path for the file. Throws if unable to get full name and path.
''' Gets the full name and path for the file.
''' </summary>
''' <param name="location">The filename being tested.</param>
''' <returns>A full name and path of the file.</returns>
''' <exception cref="ArgumentNullException">
''' If location is <see langword="Nothing"/> or <see cref="String.Empty"/>.
''' </exception>
Private Shared Function ValidateFilename(location As String) As String
If String.IsNullOrEmpty(location) Then
Throw VbUtils.GetArgumentNullException(NameOf(location))
Expand All @@ -45,7 +48,7 @@ Namespace Microsoft.VisualBasic.Devices
End Function

''' <summary>
''' Plays the passed in SoundPlayer in the passed in mode.
''' Plays the passed in <see cref="Media.SoundPlayer"/> in the passed in <see cref="AudioPlayMode"/>.
''' </summary>
''' <param name="sound">The SoundPlayer to play.</param>
''' <param name="mode">The mode in which to play the sound.</param>
Expand Down Expand Up @@ -106,8 +109,9 @@ Namespace Microsoft.VisualBasic.Devices
''' <summary>
''' Plays a Byte array representation of a .wav file in the passed in mode.
''' </summary>
''' <param name="data">The array representing the .wav file.</param>
''' <param name="data">The <see langword="Byte"/> array representing the .wav file.</param>
''' <param name="playMode">The mode in which the array should be played.</param>
''' <exception cref="ArgumentNullException">if data is <see langword="Nothing"/>.</exception>
Public Sub Play(data() As Byte, playMode As AudioPlayMode)
If data Is Nothing Then
Throw VbUtils.GetArgumentNullException(NameOf(data))
Expand All @@ -120,10 +124,11 @@ Namespace Microsoft.VisualBasic.Devices
End Sub

''' <summary>
''' Plays a stream representation of a .wav file in the passed in mode.
''' Plays a <see cref="Stream"/> representation of a .wav file in the passed in <see cref="AudioPlayMode"/>.
''' </summary>
''' <param name="stream">The stream representing the .wav file.</param>
''' <param name="playMode">The mode in which the stream should be played.</param>
''' <exception cref="ArgumentNullException">if stream is <see langword="Nothing"/>.</exception>
Public Sub Play(stream As Stream, playMode As AudioPlayMode)
ValidateAudioPlayModeEnum(playMode, NameOf(playMode))
If stream Is Nothing Then
Expand All @@ -134,10 +139,11 @@ Namespace Microsoft.VisualBasic.Devices
End Sub

''' <summary>
''' Plays a system messageBeep sound.
''' Plays a system sound.
''' </summary>
''' <param name="systemSound">The sound to be played.</param>
''' <remarks>Plays the sound asynchronously.</remarks>
''' <exception cref="ArgumentNullException">if systemSound is <see langword="Nothing"/>.</exception>
Public Sub PlaySystemSound(systemSound As Media.SystemSound)
If systemSound Is Nothing Then
Throw VbUtils.GetArgumentNullException(NameOf(systemSound))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
' Licensed to the .NET Foundation under one or more agreements.
' The .NET Foundation licenses this file to you under the MIT license.

Imports System.Windows.Forms
Imports Microsoft.VisualBasic.MyServices

Namespace Microsoft.VisualBasic.Devices
Expand Down Expand Up @@ -29,10 +28,13 @@ Namespace Microsoft.VisualBasic.Devices

'Lazy initialized cache for the clipboard class. (proxies can be shared - they have no state)
Private Shared s_clipboard As ClipboardProxy

'Lazy initialized cache for the Keyboard class. SHARED because Keyboard behaves as a ReadOnly singleton class
Private Shared s_keyboardInstance As Keyboard

'Lazy initialized cache for the Mouse class. SHARED because Mouse behaves as a ReadOnly singleton class
Private Shared s_mouse As Mouse

'Lazy initialized cache for the Audio class.
Private _audio As Audio

Expand Down Expand Up @@ -92,12 +94,12 @@ Namespace Microsoft.VisualBasic.Devices
''' This property returns the primary display screen.
''' </summary>
''' <value>A <see cref="System.Windows.Forms.Screen"/> object as the primary screen.</value>
Public ReadOnly Property Screen() As Screen
Public ReadOnly Property Screen() As System.Windows.Forms.Screen
Get
'Don't cache this. The Screen class responds to display resolution changes by nulling out AllScreens, which
'PrimaryScreen relies on to find the primary. So we always need to access the latest PrimaryScreen so we
'will get the current resolution reported.
Return Screen.PrimaryScreen
Return System.Windows.Forms.Screen.PrimaryScreen
End Get
End Property

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
' The .NET Foundation licenses this file to you under the MIT license.

Namespace Microsoft.VisualBasic.Devices

Partial Public Class ComputerInfo

''' <summary>
Expand Down
Loading

0 comments on commit ce8b46f

Please sign in to comment.