|
| 1 | +// Copyright Subatomix Research Inc. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +namespace DependencyQueue; |
| 5 | + |
| 6 | +[TestFixture] |
| 7 | +public class ExampleTests |
| 8 | +{ |
| 9 | + [Test] |
| 10 | + public void TestExample() |
| 11 | + { |
| 12 | + var burgerAssembler = new Step(); |
| 13 | + var fridgeRaider = new Step(); |
| 14 | + var griller = new Step(); |
| 15 | + var toaster = new Step(); |
| 16 | + |
| 17 | + // Create a queue |
| 18 | + using var queue = new DependencyQueue<Step>(); |
| 19 | + |
| 20 | + // Create a builder for queue entries |
| 21 | + var builder = queue.CreateEntryBuilder(); |
| 22 | + |
| 23 | + // Add entries in any order |
| 24 | + // First, we know we have to assemble the burger |
| 25 | + builder |
| 26 | + .NewEntry("Assembly", burgerAssembler) |
| 27 | + .AddRequires("GrilledPatty", "ToastedBun", "Condiments", "Sauce") |
| 28 | + .Enqueue(); |
| 29 | + |
| 30 | + // And we have to get the ingredients somewhere |
| 31 | + builder |
| 32 | + .NewEntry("Gathering", fridgeRaider) |
| 33 | + .AddProvides("Patty", "Bun", "Condiments", "Sauce") |
| 34 | + .Enqueue(); |
| 35 | + |
| 36 | + // Gotta cook the patty |
| 37 | + builder |
| 38 | + .NewEntry("Grilling", griller) |
| 39 | + .AddRequires("Patty") |
| 40 | + .AddProvides("GrilledPatty") |
| 41 | + .Enqueue(); |
| 42 | + |
| 43 | + // Gotta toast the bun, too |
| 44 | + builder |
| 45 | + .NewEntry("Toasting", toaster) |
| 46 | + .AddRequires("Bun") |
| 47 | + .AddProvides("ToastedBun") |
| 48 | + .Enqueue(); |
| 49 | + |
| 50 | + // Validate the queue |
| 51 | + var errors = queue.Validate(); |
| 52 | + if (errors.Any()) |
| 53 | + throw new InvalidBurgerException(errors); |
| 54 | + |
| 55 | + // Now build the burger |
| 56 | + while (queue.TryDequeue() is { } entry) |
| 57 | + { |
| 58 | + // Commented out to reduce test output noise |
| 59 | + //Console.WriteLine("Executing: " + entry.Name); |
| 60 | + entry.Value.Execute(); |
| 61 | + queue.Complete(entry); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private class Step |
| 66 | + { |
| 67 | + public void Execute() { } |
| 68 | + } |
| 69 | + |
| 70 | + private class InvalidBurgerException : Exception |
| 71 | + { |
| 72 | + public InvalidBurgerException(IReadOnlyList<DependencyQueueError> errors) { } |
| 73 | + } |
| 74 | +} |
0 commit comments