public void ConsoleSingleQuery() { using StringWriter sw = new StringWriter(); Console.SetOut(sw); Console.SetError(sw); var timeoutTask = Task.Delay(MAX_WAIT_TIME); // This task will end once Slapp completes as --keepOpen is not specified var mainTask = Task.Run(async() => { await ConsoleMain.Main("--query Slate".Split(" ")).ConfigureAwait(false); await Task.Delay(500).ConfigureAwait(false); // Let any logging finish. }); Task.WaitAny(timeoutTask, mainTask); Console.SetOut(consoleOut); Console.SetError(consoleError); string actual = sw.ToString(); Console.WriteLine("=================================================="); Console.WriteLine(actual); Console.WriteLine("=================================================="); Console.WriteLine(actual = actual.Base64DecodeByLines()); Console.WriteLine("=================================================="); Assert.IsTrue(actual.Contains(MESSAGE_OK)); Assert.IsTrue(actual.Contains("kjhf1273")); Assert.IsTrue(actual.Contains("Inkology")); Assert.IsTrue(actual.Contains("Revitalize")); Assert.IsTrue(actual.Contains("2019-03-25-LUTI-S8")); // Check sources populated Assert.IsTrue(actual.Count("UNLINKED") == 1); // Check no unlinked teams (except the one specified in Additional Sources) Assert.IsTrue(actual.Count("Built-in") >= 1); // Check for built-in sources (one specified in Additional Sources) }
public void ConsoleNoArguments() { using StringWriter sw = new StringWriter(); Console.SetOut(sw); Console.SetError(sw); string expected = CHOOSE_A_FUNCTION; var timeoutTask = Task.Delay(MAX_WAIT_TIME); var mainTask = Task.Run(() => ConsoleMain.Main(Array.Empty <string>())); var successfulCheck = Task.Run(() => { for (; ;) { string actual = sw.ToString().Base64DecodeByLines(); if (actual.Contains(expected)) { break; } Thread.Sleep(250); } }); Task.WaitAny(timeoutTask, mainTask, successfulCheck); Console.SetOut(consoleOut); Console.SetError(consoleError); string actual = sw.ToString(); Console.WriteLine("=================================================="); Console.WriteLine(actual); Console.WriteLine("=================================================="); Console.WriteLine(actual = actual.Base64DecodeByLines()); Console.WriteLine("=================================================="); Assert.IsTrue(actual.Contains(expected)); }
public void ConsoleCaseSensitiveQuery() { Stopwatch stopwatch = new Stopwatch(); using StringWriter sw = new StringWriter(); Console.SetOut(sw); Console.SetError(sw); stopwatch.Start(); var timeoutTask = Task.Delay(MAX_WAIT_TIME); // This task will end once Slapp completes as --keepOpen is not specified var mainTask = Task.Run(async() => { await ConsoleMain.Main("--query slAte --exactCase".Split(" ")).ConfigureAwait(false); await Task.Delay(500).ConfigureAwait(false); // Let any logging finish. }); Task.WaitAny(timeoutTask, mainTask); stopwatch.Stop(); Console.SetOut(consoleOut); Console.SetError(consoleError); string actual = sw.ToString(); Console.WriteLine("=================================================="); Console.WriteLine(actual); Console.WriteLine("=================================================="); Console.WriteLine(actual = actual.Base64DecodeByLines()); Console.WriteLine("=================================================="); // Should NOT contain the result because the name is "Slate" Assert.IsFalse(actual.Contains("kjhf1273")); Assert.IsTrue(actual.Contains(MESSAGE_OK)); if (stopwatch.ElapsedMilliseconds > 3000) { Assert.Inconclusive($"Test passed but it took {stopwatch.ElapsedMilliseconds}ms which is unacceptable for a Console query. Aim for < 3 seconds."); } }
public void ConsolePerist() { using (StringWriter sw = new StringWriter()) { Console.SetOut(sw); Console.SetError(sw); Console.SetIn(new StringReader(string.Empty)); using StringReader tr = new StringReader("--b64 " + "ig manny".Base64Encode() + "\r\n--b64 " + "Slate".Base64Encode() + "\r\n"); // a TextReader -- also tests spaces. using StringReader tr2 = new StringReader("--b64 " + "thatsrb2dude".Base64Encode() + "\r\n"); var timeoutTask = Task.Delay(MAX_WAIT_TIME); var mainTask = Task.Run(async() => { Console.WriteLine($"[{DateTime.Now:HH:mm:ss.ff}] mainTask: Beginning."); await ConsoleMain.Main("--keepOpen".Split(" ")).ConfigureAwait(false); Console.WriteLine($"[{DateTime.Now:HH:mm:ss.ff}] mainTask: Finishing."); await Task.Delay(500).ConfigureAwait(false); // Let any logging finish. }); var externalInputTask = Task.Run(async() => { Console.WriteLine($"[{DateTime.Now:HH:mm:ss.ff}] externalInputTask: Waiting 3 seconds."); await Task.Delay(3000).ConfigureAwait(false); Console.WriteLine($"[{DateTime.Now:HH:mm:ss.ff}] externalInputTask: Setting first input."); Console.SetIn(tr); // Only move on when the input stream has been consumed. while (tr.Peek() != -1) { await Task.Delay(250).ConfigureAwait(false); } Console.WriteLine($"[{DateTime.Now:HH:mm:ss.ff}] externalInputTask: Setting second input in 2 seconds."); await Task.Delay(2000).ConfigureAwait(false); Console.SetIn(tr2); Console.WriteLine($"[{DateTime.Now:HH:mm:ss.ff}] externalInputTask: Input set, finishing."); }); var successfulCheck = Task.Run(async() => { for (; ;) { string actual = sw.ToString().Base64DecodeByLines(); if (actual.Contains("Slate") && actual.Contains("ig manny") && actual.Contains("thatsrb2dude")) { Console.WriteLine($"[{DateTime.Now:HH:mm:ss.ff}] successfulCheck: Passed."); break; } await Task.Delay(250).ConfigureAwait(false); } Console.WriteLine($"[{DateTime.Now:HH:mm:ss.ff}] successfulCheck: Finishing."); }); bool timedOut = Task.WaitAny(timeoutTask, externalInputTask, mainTask) == 0; Console.WriteLine($"[{DateTime.Now:HH:mm:ss.ff}] timedOut={timedOut}"); bool wasSuccessfulCheck = Task.WaitAny(timeoutTask, mainTask, successfulCheck) == 2; Console.WriteLine($"[{DateTime.Now:HH:mm:ss.ff}] wasSuccessfulCheck={wasSuccessfulCheck}"); Console.WriteLine($"mainTask.IsCompleted={mainTask.IsCompleted}"); Console.WriteLine($"externalInputTask.IsCompleted={externalInputTask.IsCompleted}"); Console.SetOut(consoleOut); Console.SetError(consoleError); string actual = sw.ToString(); Console.WriteLine("=================================================="); Console.WriteLine(actual); Console.WriteLine("=================================================="); Console.WriteLine(actual = actual.Base64DecodeByLines()); Console.WriteLine("=================================================="); Assert.IsTrue(actual.Contains(MESSAGE_OK), "Unexpected message result"); if (actual.Contains(EMPTY_PLAYERS) && !actual.Contains(POPULATED_PLAYERS)) { Assert.Inconclusive("The test returned an empty players list. This may be because the database has not been populated. Do so and re-run the test."); } else { Assert.IsTrue(actual.Contains(POPULATED_PLAYERS), "Doesn't contain matched players"); Assert.IsTrue(actual.Contains(SLATE), "Doesn't contain Slate"); Assert.IsTrue(actual.Contains("ig manny"), "Doesn't contain ig manny query"); Assert.IsTrue(actual.Contains("thatsrb2dude"), "Doesn't contain thatsrb2dude query"); Assert.IsTrue(actual.Contains("kjhf1273"), "Doesn't contain kjhf1273"); Assert.IsTrue(actual.Contains("Inkology"), "Doesn't contain Inkology"); Assert.IsTrue(actual.Contains("Revitalize"), "Doesn't contain Revitalize"); Assert.IsFalse(mainTask.IsCompleted, "The main task should not have completed because keepOpen is specified."); // Test last } } }
public void EndToEndBuild() { using StringWriter sw = new StringWriter(); Console.SetOut(sw); Console.SetError(sw); var mainTask = Task.Run(async() => { await ConsoleMain.Main($"--verbose --rebuild {sourcesPath}".Split(" ")).ConfigureAwait(false); await Task.Delay(500).ConfigureAwait(false); // Let any logging finish. }); Task.WaitAny(mainTask); Console.SetOut(consoleOut); Console.SetError(consoleError); string actual = sw.ToString(); Console.WriteLine("=================================================="); Console.WriteLine(actual = actual.Base64DecodeByLines()); Console.WriteLine("=================================================="); Assert.IsTrue(actual.Contains(MESSAGE_REBUILT), "Database rebuilt message not found."); // Contains the snapshots? Assert.AreEqual(3, SplatTagJsonSnapshotDatabase.GetSnapshots(current).Count()); // Load. SplatTagJsonSnapshotDatabase splatTagJsonSnapshotDatabase = new SplatTagJsonSnapshotDatabase(current); (var players, var teams, var sources) = splatTagJsonSnapshotDatabase.LoadInline(); Assert.IsNotNull(players, "Not expecting null return for players"); Assert.IsNotNull(teams, "Not expecting null return for teams"); Assert.IsNotNull(sources, "Not expecting null return for sources"); // Assertions. Assert.AreEqual(2, players.Count(p => p.Name.Value.Contains("Slate")), "Incorrect number of Slates:\n - " + new StringBuilder() .AppendJoin("\n - ", players .Where(p => p.Name.Value.Contains("Slate")) .Select(p => new StringBuilder() .Append('[') .AppendJoin(", ", p.NamesInformation) .Append(']') .Append(" -- Sourced from [") .AppendJoin(", ", p.Sources) .Append("] and teams [") .AppendJoin(", ", p.TeamInformation.GetAllTeamsUnordered()) .Append("] and Battlefy Slugs [") .AppendJoin(", ", p.Battlefy.Slugs ?? Array.Empty <BattlefyUserSocial>()) .Append("] top500=") .Append(p.Top500) .AppendLine(".") ) )); var splatarians = teams.Where(p => p.Name.Value == "Splatarians"); Assert.AreEqual(1, splatarians.Count(), $"Incorrect number of Splatarians:\n - { string.Join("\n - ", splatarians)}"); Assert.AreEqual("SX", splatarians.First().CurrentDiv.Season); Assert.IsTrue(splatarians.First().DivisionInformation.GetDivisionsUnordered().Any(d => d.Season == "S9")); Assert.AreEqual(4, sources.Count, "Unexpected number of sources loaded."); Assert.IsTrue(players.Where(p => p.Name.Value.Contains("Slate")).All(p => !p.Top500), "Expected Slate top 500 flag to be false."); }
public void EndToEndPatch() { // First run the Rebuild function. EndToEndBuild(); // Now we can patch using StringWriter sw = new StringWriter(); Console.SetOut(sw); Console.SetError(sw); var mainTask = Task.Run(async() => { await ConsoleMain.Main($"--verbose --patch {patchPath}".Split(" ")).ConfigureAwait(false); await Task.Delay(500).ConfigureAwait(false); // Let any logging finish. }); Task.WaitAny(mainTask); Console.SetOut(consoleOut); Console.SetError(consoleError); string actual = sw.ToString(); Console.WriteLine("=================================================="); Console.WriteLine(actual = actual.Base64DecodeByLines()); Console.WriteLine("=================================================="); Assert.IsTrue(actual.Contains(MESSAGE_PATCHED), "Database patched message not found."); SplatTagJsonSnapshotDatabase splatTagJsonSnapshotDatabase = new SplatTagJsonSnapshotDatabase(current); (var players, var teams, var sources) = splatTagJsonSnapshotDatabase.LoadInline(); var slates = players.Where(p => p.AllKnownNames.NamesMatch(new[] { new Name("Slate", Builtins.ManualSource) })); // Assertions. StringBuilder slateFailMessage = new StringBuilder() .AppendJoin("\n - ", slates .Select(p => new StringBuilder() .Append('[') .AppendJoin(", ", p.AllKnownNames) .Append(']') .Append(" -- Sourced from [") .AppendJoin(", ", p.Sources) .Append("] and teams [") .AppendJoin(", ", p.TeamInformation.GetAllTeamsUnordered()) .Append("] and Battlefy Slugs [") .AppendJoin(", ", p.Battlefy.Slugs ?? Array.Empty <BattlefyUserSocial>()) .Append("] top500=") .Append(p.Top500) .AppendLine(".") ) ); Assert.AreEqual(3, slates.Count(), "Incorrect number of Slates:\n - " + slateFailMessage.ToString()); var splatarians = teams.Where(p => p.Name.Value == "Splatarians"); if (splatarians.Count() != 1) { StringBuilder failMessage = new StringBuilder(); failMessage.Append("Incorrect number of Splatarians (").Append(splatarians.Count()).Append("):\n - ").AppendJoin("\n - ", splatarians).AppendLine(); failMessage.Append("Sources (").Append(sources.Count).Append("):\n - ").AppendJoin("\n - ", sources).AppendLine(); failMessage.Append("From:\n - ").AppendJoin("\n - ", splatarians.Select(t => string.Join(", ", t.Sources))).AppendLine(); Assert.Fail(failMessage.ToString()); } Assert.AreEqual("SX", splatarians.First().CurrentDiv.Season); Assert.IsTrue(splatarians.First().DivisionInformation.GetDivisionsUnordered().Any(d => d.Season == "S9")); // Check that the patching has happened Assert.AreEqual(5, sources.Count, "Unexpected number of sources loaded."); Assert.IsTrue(slates.Any(p => p.Top500), "Expected the patching to Slate to set top 500 flag -- " + slateFailMessage.ToString()); }