public void reftype_equal() { //Arrange var tree1 = new nutility.Tree <string>(); var tree2 = new nutility.Tree <string>(); //Act tree1.Value = "2"; tree1.Add(new nutility.Tree <string> { Value = "1" }); tree1.Add(new nutility.Tree <string> { Value = "3" }); tree1.Add(new nutility.Tree <string> { Value = null }); tree2.Value = "2"; tree2.Add(new nutility.Tree <string> { Value = "1" }); tree2.Add(new nutility.Tree <string> { Value = "3" }); tree2.Add(new nutility.Tree <string> { Value = null }); //Assert Assert.AreEqual(tree1, tree2); }
public void valuetype_nullable_not_equal() { //Arrange var tree1 = new nutility.Tree <int, int?>(); var tree2 = new nutility.Tree <int, int?>(); //Act tree1.Value = 1; tree1[1] = new nutility.Tree <int, int?> { Value = 11 }; tree1.Add(2, new nutility.Tree <int, int?> { Value = 12 }); tree1[3] = new nutility.Tree <int, int?> { Value = null }; tree2.Value = 1; tree2.Add(1, new nutility.Tree <int, int?> { Value = 11 }); tree2[2] = new nutility.Tree <int, int?> { Value = 13 }; tree2[3] = new nutility.Tree <int, int?> { Value = null }; //Assert Assert.AreNotEqual(tree1, tree2); }
public void valuetype_not_equal() { //Arrange var tree1 = new nutility.Tree <int>(); var tree2 = new nutility.Tree <int>(); //Act tree1.Value = 2; tree1.Add(new nutility.Tree <int> { Value = 1 }); tree1.Add(new nutility.Tree <int> { Value = 3 }); tree1.Add(new nutility.Tree <int> { Value = 0 }); tree2.Value = 2; tree2.Add(new nutility.Tree <int> { Value = 1 }); tree2.Add(new nutility.Tree <int> { Value = 3 }); tree2.Add(new nutility.Tree <int> { Value = 2 }); //Assert Assert.AreNotEqual(tree1, tree2); Assert.AreEqual("<tree value=\"2\">\r\n <tree value=\"1\" />\r\n <tree value=\"3\" />\r\n <tree value=\"0\" />\r\n</tree>", $"{tree1}"); Assert.AreEqual("<tree value=\"2\">\r\n <tree value=\"1\" />\r\n <tree value=\"3\" />\r\n <tree value=\"2\" />\r\n</tree>", $"{tree2}"); }
public void ref_ref_equal() { //Arrange var tree1 = new nutility.Tree <string, string>(); var tree2 = new nutility.Tree <string, string>(); //Act tree1.Value = "R"; tree1["1"] = new nutility.Tree <string, string> { Value = "2" }; tree1.Add("2", new nutility.Tree <string, string> { Value = "3" }); tree1["3"] = new nutility.Tree <string, string> { Value = null }; tree2.Value = "R"; tree2.Add("1", new nutility.Tree <string, string> { Value = "2" }); tree2["2"] = new nutility.Tree <string, string> { Value = "3" }; tree2["3"] = new nutility.Tree <string, string> { Value = null }; //Assert Assert.AreEqual(tree1, tree2); }
public void nullable_char_not_equal() { //Arrange var tree1 = new nutility.Tree <char?>(); var tree2 = new nutility.Tree <char?>(); //Act tree1.Value = '2'; tree1.Add(new nutility.Tree <char?> { Value = '1' }); tree1.Add(new nutility.Tree <char?> { Value = '3' }); tree1.Add(new nutility.Tree <char?> { Value = null }); tree2.Value = '2'; tree2.Add(new nutility.Tree <char?> { Value = '1' }); tree2.Add(new nutility.Tree <char?> { Value = '3' }); tree2.Add(new nutility.Tree <char?> { Value = '1' }); //Assert Assert.AreNotEqual(tree1.GetHashCode(), tree2.GetHashCode()); Assert.AreNotEqual(tree1, tree2); Assert.IsFalse(tree1.Equals(tree2)); }
private void @new(string key, nutility.Tree <string, InputReplLevel> tree) { if (reserved.ContainsKey(key) || key.StartsWith("?")) { Writer.WriteLine($"For clarity, please choose another id because '{key}' is reserved."); } else if (tree.ContainsKey(key)) { Writer.WriteLine($"{key} already identifies an existing input class or input instance."); } else { InputClassReplLevel current_class = tree.Value as InputClassReplLevel; if (current_class == null) { Writer.WriteLine($"A new input instance is currently supported only on input class levels."); } else { var new_instance = Activator.CreateInstance(current_class.InputClass); var new_level = new nutility.InputInstanceReplLevel(key, new_instance); tree[new_level.ID] = new nutility.Tree <string, nutility.InputReplLevel> { Value = new_level, Parent = tree }; Writer.WriteLine($"\t{key} ({current_class.InputClass.FullName})"); } } }
public void capture_standard_output() { //Arrange var asFound = Console.Out; try { var current_level = new nutility.InputInstanceReplLevel("UC-2", new cli1.RootInput()); var tree = new nutility.Tree <string, nutility.InputReplLevel> { Value = current_level }; var input_lines = new[] { "-f1 -n=-132" }; var reader = new StringReader(Common.asTextContent(input_lines)); var writer = new StringWriter(); Console.SetOut(writer); var repl = new nutility.REPL { Reader = reader, Writer = writer }; //Act repl.Loop(tree); //Assert Assert.IsTrue($"{writer}".Contains("N: -132")); } finally { Console.SetOut(asFound); } }
public void valuetype_equal() { //Arrange var tree1 = new nutility.Tree <int>(); var tree2 = new nutility.Tree <int>(); //Act tree1.Value = 2; tree1.Add(new nutility.Tree <int> { Value = 1 }); tree1.Add(new nutility.Tree <int> { Value = 3 }); tree1.Add(new nutility.Tree <int> { Value = 0 }); tree2.Value = 2; tree2.Add(new nutility.Tree <int> { Value = 1 }); tree2.Add(new nutility.Tree <int> { Value = 3 }); tree2.Add(new nutility.Tree <int> { Value = 0 }); //Assert Assert.AreEqual(tree1, tree2); }
public void value_ref_equal() { //Arrange var tree1 = new nutility.Tree <int, string>(); var tree2 = new nutility.Tree <int, string>(); //Act tree1.Value = "R"; tree1[1] = new nutility.Tree <int, string> { Value = "2" }; tree1.Add(2, new nutility.Tree <int, string> { Value = "3" }); tree1[3] = new nutility.Tree <int, string> { Value = null }; tree2.Value = "R"; tree2.Add(1, new nutility.Tree <int, string> { Value = "2" }); tree2[2] = new nutility.Tree <int, string> { Value = "3" }; tree2[3] = new nutility.Tree <int, string> { Value = null }; //Assert Assert.AreEqual(tree1, tree2); }
public void nullable_equal() { //Arrange var tree1 = new nutility.Tree <int?>(); var tree2 = new nutility.Tree <int?>(); //Act tree1.Value = 2; tree1.Add(new nutility.Tree <int?> { Value = 1 }); tree1.Add(new nutility.Tree <int?> { Value = 3 }); tree1.Add(new nutility.Tree <int?> { Value = null }); tree2.Value = 2; tree2.Add(new nutility.Tree <int?> { Value = 1 }); tree2.Add(new nutility.Tree <int?> { Value = 3 }); tree2.Add(new nutility.Tree <int?> { Value = null }); //Assert Assert.AreEqual(tree1, tree2); }
private void ShowCurrentLevel(string input, nutility.Tree <string, InputReplLevel> tree) { var current_instance_level = tree.Value as InputInstanceReplLevel; var current_class_level = tree.Value as InputClassReplLevel; var level = current_instance_level == null ? "class" : "instance"; var type = current_instance_level == null ? current_class_level.InputClass : current_instance_level.InputInstance.GetType(); Writer.WriteLine($"Current input {level} type: {type.FullName}"); if (input.Contains(HelpExpandCommand)) { ShowUsage(type); } var childs_classes = tree.Where(t => t.Value.Value is InputClassReplLevel); Writer.WriteLine($"\r\nCurrent input child classes ({childs_classes.Count()}):"); foreach (var child in childs_classes) { var child_class_level = child.Value.Value as InputClassReplLevel; Writer.WriteLine($"\r\n\t{child.Key} ({child_class_level?.InputClass.FullName})"); if (input.Contains(HelpDetailCommand)) { ShowUsage(child_class_level.InputClass, 2); } } var childs_instances = tree.Where(t => t.Value.Value is InputInstanceReplLevel); Writer.WriteLine($"\r\nCurrent input instances ({childs_instances.Count()}):"); foreach (var child in childs_instances) { var child_instance_level = child.Value.Value as InputInstanceReplLevel; Writer.WriteLine($"\r\n\t{child.Key} ({child_instance_level.InputInstance.GetType().FullName})"); } var commands = $"{reserved.Keys.Aggregate(new StringBuilder(" "), (whole, next) => whole.AppendFormat(" {0}", next))}"; Writer.WriteLine($"\r\nGeneral commands: {commands.Substring(1)}"); if (input.Contains(HelpExpandCommand)) { Writer.WriteLine($"\t{HelpCommand} {HelpExpandCommand} {HelpDetailCommand} (this help)"); Writer.WriteLine($"\t{GoUpCommand} (go up one level)"); Writer.WriteLine("\t<input class id | input instance id> (go down to that input class or input instance level)"); Writer.WriteLine($"\t{NewCommand} <id> (new instance of current input class)"); Writer.WriteLine($"\t{DeleteCommand} <id> (remove an existing instance of current input class)"); Writer.WriteLine($"\t{ClearCommand} (clear console)"); } }
public void serialized() { //Arrange var tree = new nutility.Tree <int, string>(); tree.Value = "R"; tree[1] = new nutility.Tree <int, string> { Value = "R1" }; tree.Add(2, new nutility.Tree <int, string> { Value = "R2" }); string expected = "<tree value='R'><tree key='1'><tree value='R1' /></tree><tree key='2'><tree value='R2' /></tree></tree>"; var expected_result = System.Xml.Linq.XDocument.Parse(expected); //Act var actual_result = System.Xml.Linq.XDocument.Parse($"{tree}"); //Assert Assert.AreEqual($"{expected_result}", $"{actual_result}"); }
public void capture_TextWriter() { //Arrange var input_lines = new[] { "x1 -f1 -n=43" }; var reader = new StringReader(Common.asTextContent(input_lines)); var repl = new nutility.REPL { Reader = reader }; var level_writer = new StringWriter(); var current_level = new nutility.InputInstanceReplLevel("x1", new InputY(level_writer)); var tree = new nutility.Tree <string, nutility.InputReplLevel> { Value = current_level }; //Act repl.Loop(tree); //Assert Assert.AreEqual("N: 43\r\n", $"{level_writer}"); }
public void basic() { //Arrange var current_level = new nutility.InputClassReplLevel("UC-1", typeof(InputX)); var tree = new nutility.Tree <string, nutility.InputReplLevel> { Value = current_level }; var input_lines = new[] { "new x1", "?" }; var reader = new StringReader(Common.asTextContent(input_lines)); var writer = new StringWriter(); var repl = new nutility.REPL { Reader = reader, Writer = writer }; //Act repl.Loop(tree); //Assert Assert.IsTrue($"{writer}".Contains($"x1 ({typeof(InputX).FullName})")); }
public void parent() { //Arrange var head = new nutility.Tree <int, string>(); head.Value = "R"; head.Parent = null; head[1] = new nutility.Tree <int, string> { Value = "R1", Parent = head }; var child2 = new nutility.Tree <int, string> { Value = "R2", Parent = head }; head[2] = child2; //Act var parent = child2.Parent; //Assert Assert.AreSame(head, parent); }
public void parent() { //Arrange var head = new nutility.Tree <int>(); head.Value = 1; head.Parent = null; head.Add(new nutility.Tree <int> { Value = 2, Parent = head }); var child2 = new nutility.Tree <int> { Value = 3, Parent = head }; head.Add(child2); //Act var parent = child2.Parent; //Assert Assert.AreSame(head, parent); }
public void serialized() { //Arrange var tree = new nutility.Tree <string> { Value = "2" }; tree.Add(new nutility.Tree <string> { Value = "1" }); tree.Add(new nutility.Tree <string> { Value = "3" }); string expected = "<tree value='2'><tree value='1' /><tree value='3' /></tree>"; var expected_result = System.Xml.Linq.XDocument.Parse(expected); //Act var actual_result = System.Xml.Linq.XDocument.Parse($"{tree}"); //Assert Assert.AreEqual($"{expected_result}", $"{actual_result}"); }
private void delete(string key, nutility.Tree <string, InputReplLevel> tree) { var instance_level = tree.ContainsKey(key) ? (tree[key].Value as InputInstanceReplLevel) : null; if (instance_level != null) { var instance = instance_level.InputInstance; var classname = instance?.GetType().FullName; IDisposable disposable = instance as IDisposable; if (disposable != null) { Writer.WriteLine($"\t{key} ({classname}) disposing..."); disposable.Dispose(); Writer.WriteLine($"\t{key} ({classname}) disposed."); } tree.Remove(key); Writer.WriteLine($"\t{key} ({classname}) removed."); } else { Writer.WriteLine($"{key} not found among existing instances at current input level."); } }
public void is_binary_tree() { //Arrange bool is_binary_tree(nutility.Tree <string> _tree) { bool _result = false; if (_tree != null) { _result = _tree.Count == 0 || _tree.Count == 2; var k = _tree.GetEnumerator(); while (_result == true && k.MoveNext()) { _result = is_binary_tree(k.Current); } } return(_result); } var tree = new nutility.Tree <string> { Value = "2" }; tree.Add(new nutility.Tree <string> { Value = "1" }); tree.Add(new nutility.Tree <string> { Value = "3" }); //Act var result = is_binary_tree(tree); //Assert Assert.IsTrue(result); }
public void Loop(nutility.Tree <string, InputReplLevel> tree) { #region Input validation if (tree?.Value == null) { throw new ArgumentNullException($"{nameof(tree)}", $"{nameof(tree)} cannot be null."); } if (Reader == null) { Reader = Console.In; } if (Writer == null) { Writer = Console.Out; } #endregion var current_tree = tree; var current_path = new Stack <string>(); current_path.Push(current_tree.Value.ID); do { Writer.WriteLine(); Writer.Write($"{path(current_path)}: "); var input = Reader.ReadLine()?.Trim(); if (string.IsNullOrWhiteSpace(input)) { break; } var args = nutility.SystemArgumentParser.Parse(input); var opts = new nutility.Switch(args); if (args?.Any() == false || opts?.Any() == false) { continue; } if (input.StartsWith(HelpCommand)) { ShowCurrentLevel(input, current_tree); } else if (opts.IndexedArguments?.Count == 2 && opts.IndexedArguments?.First() == NewCommand && !string.IsNullOrWhiteSpace(opts.IndexedArguments[1])) { @new(opts.IndexedArguments[1], current_tree); } else if (opts.IndexedArguments?.Count == 2 && opts.IndexedArguments?.First() == DeleteCommand && !string.IsNullOrWhiteSpace(opts.IndexedArguments[1])) { delete(opts.IndexedArguments[1], current_tree); } else if (opts.IndexedArguments?.Count == 1 && opts.IndexedArguments?.First() == GoUpCommand && current_tree != tree) { current_tree = current_tree.Parent; current_path.Pop(); } else if (opts.IndexedArguments?.Count == 1 && opts.IndexedArguments?.First() == ClearCommand) { clear(); } else { var childInputInstanceKey = args.Any() && current_tree.Any(t => t.Key == args.First() && (t.Value.Value is InputInstanceReplLevel)) ? args.First() : null; var childInputClassKey = args.Any() && current_tree.Any(t => t.Key == args.First() && (t.Value.Value is InputClassReplLevel)) ? args.First() : null; var target_args = args.SkipWhile((arg, index) => index < 1).ToArray(); if (childInputInstanceKey != null) { var current_level = current_tree[childInputInstanceKey].Value as InputInstanceReplLevel; if (target_args?.Any() == true && current_level?.InputInstance != null) { nutility.Switch.AsType(target_args, current_level.InputInstance); } else { current_tree = current_tree[childInputInstanceKey]; current_path.Push(current_tree.Value.ID); } } else if (childInputClassKey != null) { var current_level = current_tree[childInputClassKey].Value as InputClassReplLevel; if (target_args?.Any() == true && current_level?.InputClass != null) { nutility.Switch.AsType(target_args, current_level.InputClass); } else { current_tree = current_tree[childInputClassKey]; current_path.Push(current_tree.Value.ID); } } else { var current_instance_level = current_tree.Value as InputInstanceReplLevel; var current_class_level = current_tree.Value as InputClassReplLevel; if (current_instance_level != null) { nutility.Switch.AsType(args, current_instance_level.InputInstance); } else { nutility.Switch.AsType(args, current_class_level.InputClass); } } } Writer.WriteLine(new string('-', ConsoleWindowWidth - 1)); } while (true); }
public static void MainEntryPoint(string[] args, TextReader reader = null, TextWriter writer = null, int consoleWindowWidth = 20) { try { if (reader == null) { reader = Console.In; } if (writer == null) { writer = Console.Out; } if (Environment.UserInteractive) { WriteLine($"Hello, {Environment.UserDomainName}\\{Environment.UserName} !!!"); Write($"PID: {System.Diagnostics.Process.GetCurrentProcess().Id} Thread: {System.Threading.Thread.CurrentThread.ManagedThreadId} Culture: {System.Threading.Thread.CurrentThread?.CurrentUICulture?.Name}, {System.Threading.Thread.CurrentThread?.CurrentCulture?.Name}\n"); } /*if (!(args?.Any() == true) || args?.First().Contains("?") == true) * { * WriteLine($"Working with {GetHostProcessName()}:"); * nutility.Switch.ShowUsage(typeof(RootInput)); * } * else*/ { var mvp = new nutility.InputClassReplLevel("MVP", typeof(RootInput)); var use_case_hierarchy_root = new nutility.Tree <string, nutility.InputReplLevel> { Value = mvp }; var uc2 = new nutility.InputClassReplLevel("UC2", typeof(InputUC2)); use_case_hierarchy_root[uc2.ID] = new nutility.Tree <string, nutility.InputReplLevel> { Value = uc2, Parent = use_case_hierarchy_root }; var uc3 = new nutility.InputClassReplLevel("UC3", typeof(InputUC3)); use_case_hierarchy_root[uc3.ID] = new nutility.Tree <string, nutility.InputReplLevel> { Value = uc3, Parent = use_case_hierarchy_root }; var repl = new nutility.REPL { Reader = reader, Writer = writer, ConsoleWindowWidth = consoleWindowWidth }; repl.Loop(use_case_hierarchy_root); } } catch (Exception ex) { for (int level = 0; ex != null; ex = ex.InnerException, ++level) { writer.WriteLine($"\r\n[Level {level}] {ex.GetType().FullName}: {ex.Message} {ex.StackTrace}"); } } string GetHostProcessName() { var result = Environment.GetCommandLineArgs()?.FirstOrDefault(); if (!string.IsNullOrWhiteSpace(result)) { result = System.IO.Path.GetFileNameWithoutExtension(result); } return(result); } }