using System; using Amphora; using UnitTestSharp; namespace Foundations.UnitTests { public class StartupTests : TestFixture { public static readonly Flag Level = new Flag("Sets the level.", defaultValue: 3); public static FlagRegistry Registry() { var returnMe = new FlagRegistry(); returnMe.Register("level", "StartupTests.Level", Level); return returnMe; } public class TryReadQueryTests : TestFixture { public void GoodParameters_GoOnWithNoMessage() { bool goOn = Startup.TryReadQuery(Registry(), "?level=9", out Flags flags, out string message); CheckTrue(goOn); CheckEqual(9, flags.Get(Level)); CheckNull(message); } public void WrongParameters_GoOnWithTheRestAndAWarning() { bool goOn = Startup.TryReadQuery(Registry(), "?wibble&level=9&level=x", out Flags flags, out string message); CheckTrue(goOn); CheckEqual(9, flags.Get(Level)); CheckEqual("Ignored from the page's address:" + Environment.NewLine + "There's no flag --wibble." + Environment.NewLine + "--level is given more than once.", message); } public void Help_StopsWithTheUsageAndHowToWriteIt() { FlagRegistry registry = Registry(); bool goOn = Startup.TryReadQuery(registry, "?wibble&help", out Flags flags, out string message); CheckFalse(goOn); CheckNull(flags); CheckEqual("In the page's address, write ?name=value&name in place of --name=value --name." + Environment.NewLine + registry.Usage(), message); } } public class TryReadTests : TestFixture { public void GoodArguments_GoOn() { bool goOn = Startup.TryRead(Registry(), new[] { "--level=9" }, out Flags flags, out string message, out int exitCode); CheckTrue(goOn); CheckEqual(9, flags.Get(Level)); CheckNull(message); CheckEqual(0, exitCode); } public void NoArguments_GoOnWithDefaults() { Startup.TryRead(Registry(), new string[0], out Flags flags, out string message, out int exitCode); CheckEqual(3, flags.Get(Level)); } public void Help_StopsWithTheUsageAndExitsZero() { FlagRegistry registry = Registry(); bool goOn = Startup.TryRead(registry, new[] { "--help" }, out Flags flags, out string message, out int exitCode); CheckFalse(goOn); CheckNull(flags); CheckEqual(registry.Usage(), message); CheckEqual(0, exitCode); } public void Help_WinsOverAMistake() { FlagRegistry registry = Registry(); bool goOn = Startup.TryRead(registry, new[] { "--wibble", "-h" }, out Flags flags, out string message, out int exitCode); CheckFalse(goOn); CheckEqual(registry.Usage(), message); CheckEqual(0, exitCode); } public void AMistake_StopsWithTheProblemsAndPointsAtHelp() { bool goOn = Startup.TryRead(Registry(), new[] { "--wibble", "--level=x" }, out Flags flags, out string message, out int exitCode); CheckFalse(goOn); CheckNull(flags); CheckEqual("There's no flag --wibble." + Environment.NewLine + "--level needs a whole number, not 'x'." + Environment.NewLine + "Try --help for the list of flags.", message); } public void AMistake_ExitsWithTheBadArgumentsCode() { Startup.TryRead(Registry(), new[] { "--wibble" }, out Flags flags, out string message, out int exitCode); CheckEqual(Startup.BadArgumentsExitCode, exitCode); CheckEqual(2, Startup.BadArgumentsExitCode); } } } }