diff --git a/src/Chapter05.Tests/Listing05.31.Tests.cs b/src/Chapter05.Tests/Listing05.31.Tests.cs index 81a2451d2..129042df3 100644 --- a/src/Chapter05.Tests/Listing05.31.Tests.cs +++ b/src/Chapter05.Tests/Listing05.31.Tests.cs @@ -5,4 +5,22 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_31.Tests; [TestClass] public class ProgramTests { + [TestMethod] + public void Main_NoArgs_ExpectErrors() + { + string expected = "Value cannot be null. (Parameter \'httpsUrl\')"; + + IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected, + () => + { + try + { + Program.Main(); + }catch(Exception ex) + { + Console.WriteLine(ex.Message); + } + }); + } + } diff --git a/src/Chapter05.Tests/Listing05.32.Tests.cs b/src/Chapter05.Tests/Listing05.32.Tests.cs index 7d4d627b1..1e19b25d6 100644 --- a/src/Chapter05.Tests/Listing05.32.Tests.cs +++ b/src/Chapter05.Tests/Listing05.32.Tests.cs @@ -5,5 +5,22 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_32.Tests; [TestClass] public class ProgramTests { + [TestMethod] + public void Main_NoArgs_ExpectErrors() + { + string expected = "Value cannot be null. (Parameter \'httpsUrl\')"; + IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected, + () => + { + try + { + Program.Main(); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + }); + } } diff --git a/src/Chapter05.Tests/Listing05.33.Tests.cs b/src/Chapter05.Tests/Listing05.33.Tests.cs index 8f18bbcfb..b7262f444 100644 --- a/src/Chapter05.Tests/Listing05.33.Tests.cs +++ b/src/Chapter05.Tests/Listing05.33.Tests.cs @@ -5,5 +5,180 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_33.Tests; [TestClass] public class ProgramTests { + [TestMethod] + public void Main_NoArgs_ExpectErrors() + { + string[] args = Array.Empty(); + string expected = """ + ERROR: You must specify the URL and the file name + Usage: Downloader.exe + """; + IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected, + () => + { + Program.Main(args); + }); + } + +#if !NET7_0_OR_GREATER +[TestMethod] + public void Main_NoArgs_ExpectFirstNullError() + { + string[] args = {null! , "Montoya"}; + string expected = "Value cannot be null. (Parameter \'httpsUrl\')"; + + IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected, + () => + { + try + { + Program.Main(args); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + }); + } + + [TestMethod] + public void Main_FirstArg_ExpectSecondNullError() + { + string[] args = { "Inigo", null! }; + string expected = "Value cannot be null. (Parameter \'fileName\')"; + + IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected, + () => + { + try + { + Program.Main(args); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + }); + } + + [TestMethod] + public void Main_InvalidSecondArg_ExpectWhitespaceError() + { + string[] args = { "Inigo", " " }; + string expected = "fileName cannot be empty or only whitespace"; + + IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected, + () => + { + try + { + Program.Main(args); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + }); + } +#else + [TestMethod] + public void Main_NoArgs_ExpectFirstNullError() + { + string[] args = { null!, "Montoya" }; + string expected = "Object reference not set to an instance of an object."; + + IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected, + () => + { + try + { + Program.Main(args); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + }); + } + + [TestMethod] + public void Main_FirstArg_ExpectSecondNullError() + { + string[] args = { "Inigo", null! }; + string expected = "Value cannot be null. (Parameter \'fileName?.Trim()\')"; + + IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected, + () => + { + try + { + Program.Main(args); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + }); + } + + [TestMethod] + public void Main_InvalidSecondArg_ExpectWhitespaceError() + { + string[] args = { "Inigo", " " }; + string expected = "The value cannot be an empty string. (Parameter \'fileName?.Trim()\')"; + + IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected, + () => + { + try + { + Program.Main(args); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + }); + } +#endif + + [TestMethod] + public void Main_InvalidSecondArg_ExpectHTTPError() + { + string[] args = { "Inigo", "Montoya" }; + string expected = "URL must start with \'HTTPS\'."; + + IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected, + () => + { + try + { + Program.Main(args); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + }); + } + + [TestMethod] + public void Main_IntelliTectIndexHtmlArgs_DownloadFile() + { + try + { + string fileName = "about"; + string[] args = { "https://IntelliTect.com", fileName }; + string expected = $"Downloaded '{args[1]}' from '{args[0]}'."; + + int? result = null; + IntelliTect.TestTools.Console.ConsoleAssert.Expect( + expected, () => result = Program.Main(args)); + } + catch (AggregateException exception) when (exception.InnerException is System.Net.Http.HttpRequestException) + { + Assert.Inconclusive("Unable to download the file. Check your internet connection."); + } + } }