Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Chapter05.Tests/Listing05.31.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}

}
17 changes: 17 additions & 0 deletions src/Chapter05.Tests/Listing05.32.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
}
175 changes: 175 additions & 0 deletions src/Chapter05.Tests/Listing05.33.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
string expected = """
ERROR: You must specify the URL and the file name
Usage: Downloader.exe <URL> <TargetFileName>
""";

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.");
}
}
}