private static int UpdateConsole(ref BaseClass obj) { PropertyInfo[] objProps = obj.GetType().GetProperties(); if (objProps.Length > 0 && obj.GetType() != typeof(BaseClass)) { Console.WriteLine($"\n\t\x1b[1mType the value for each property, empty entries will be skipped\x1b[0m\n"); } for (int i = 0; i < objProps.Length;) { PropertyInfo property = objProps[i]; string propName = objProps[i].Name; if (propName == "id" || propName == "date_created" || propName == "date_updated") { i++; continue; } Console.ForegroundColor = ConsoleColor.Cyan; string propRequestStr = $"\x1b[1m\"{propName}\" ({property.PropertyType.Name}) >>\x1b[0m "; Console.Write(propRequestStr); Console.ResetColor(); string propValue = Console.ReadLine(); Console.SetCursorPosition(propRequestStr.Length + propValue.Length - 7, --Console.CursorTop); propValue = propValue.Trim(); if (propValue.Length == 0) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine($"\x1b[1m(skipped)\x1b[0m"); Console.ResetColor(); i++; continue; } Type propType = property.PropertyType; MethodInfo deserailizeMethod = jsonStorage.GetType().GetMethod("DynamicDeserialize").MakeGenericMethod(propType); try { if (propType == typeof(string)) { if (!propValue.StartsWith("\"") && !propValue.EndsWith("\"")) { propValue = $"\"{propValue}\""; } } Object propValueObj = deserailizeMethod.Invoke(null, new object[] { propValue }); property.SetValue(obj, propValueObj); } catch (System.Exception) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"\x1b[1m(wrong type try again)\x1b[0m"); Console.ResetColor(); continue; } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"\x1b[1m(set)\x1b[0m"); Console.ResetColor(); i++; } Console.WriteLine(); return(0); }
public void JSONStorage_New() { JSONStorage<BaseClass> jsbs = new JSONStorage<BaseClass>(); Assert.IsTrue(jsbs.GetType().GetMethod("New").ReturnType == typeof(void)); BaseClass bs = new BaseClass(); Item it = new Item(); User us = new User(); Inventory inv = new Inventory(); jsbs.New(bs); Assert.IsTrue(jsbs.All().ContainsKey($"{bs.GetType().Name}.{bs.id}")); Assert.IsTrue(jsbs.All().ContainsValue(bs)); jsbs.New(it); Assert.IsTrue(jsbs.All().ContainsKey($"{it.GetType().Name}.{it.id}")); Assert.IsTrue(jsbs.All().ContainsValue(it)); jsbs.New(us); Assert.IsTrue(jsbs.All().ContainsKey($"{us.GetType().Name}.{us.id}")); Assert.IsTrue(jsbs.All().ContainsValue(us)); jsbs.New(inv); Assert.IsTrue(jsbs.All().ContainsKey($"{inv.GetType().Name}.{inv.id}")); Assert.IsTrue(jsbs.All().ContainsValue(inv)); jsbs.All().Remove($"{bs.GetType().Name}.{bs.id}"); jsbs.All().Remove($"{it.GetType().Name}.{it.id}"); jsbs.All().Remove($"{us.GetType().Name}.{us.id}"); jsbs.All().Remove($"{inv.GetType().Name}.{inv.id}"); }
public void JSONStorage_Instance_Properties( [Values("objects")] string prop) { JSONStorage<BaseClass> jsbs = new JSONStorage<BaseClass>(); Type myType = jsbs.GetType(); Assert.IsNotNull(myType.GetProperty(prop)); }
public void JSONStorage_All() { JSONStorage<BaseClass> jsbs = new JSONStorage<BaseClass>(); Assert.IsTrue(jsbs.GetType().GetMethod("All").ReturnType == typeof(Dictionary<string, BaseClass>)); JSONStorage<Item> jsit = new JSONStorage<Item>(); Assert.IsTrue(jsit.GetType().GetMethod("All").ReturnType == typeof(Dictionary<string, Item>)); JSONStorage<User> jsus = new JSONStorage<User>(); Assert.IsTrue(jsus.GetType().GetMethod("All").ReturnType == typeof(Dictionary<string, User>)); JSONStorage<Inventory> jsinv = new JSONStorage<Inventory>(); Assert.IsTrue(jsinv.GetType().GetMethod("All").ReturnType == typeof(Dictionary<string, Inventory>)); }
public void JSONStorage_Property_Objects() { JSONStorage<BaseClass> jsbs = new JSONStorage<BaseClass>(); Assert.IsTrue(jsbs.GetType().GetProperty("objects").PropertyType == typeof(Dictionary<string, BaseClass>)); JSONStorage<Item> jsit = new JSONStorage<Item>(); Assert.IsTrue(jsit.GetType().GetProperty("objects").PropertyType == typeof(Dictionary<string, Item>)); JSONStorage<User> jsus = new JSONStorage<User>(); Assert.IsTrue(jsus.GetType().GetProperty("objects").PropertyType == typeof(Dictionary<string, User>)); JSONStorage<Inventory> jsinv = new JSONStorage<Inventory>(); Assert.IsTrue(jsinv.GetType().GetProperty("objects").PropertyType == typeof(Dictionary<string, Inventory>)); }
public void JSONStorage_Save() { JSONStorage<BaseClass> jsbs = new JSONStorage<BaseClass>(); Assert.IsTrue(jsbs.GetType().GetMethod("Save").ReturnType == typeof(void)); BaseClass bs = new BaseClass(); Item it = new Item(); User us = new User(); Inventory inv = new Inventory(); jsbs.New(bs); jsbs.New(it); jsbs.New(us); jsbs.New(inv); jsbs.Save(); jsbs.All().Remove($"{bs.GetType().Name}.{bs.id}"); jsbs.All().Remove($"{it.GetType().Name}.{it.id}"); jsbs.All().Remove($"{us.GetType().Name}.{us.id}"); jsbs.All().Remove($"{inv.GetType().Name}.{inv.id}"); jsbs.Save(); FileAssert.Exists("storage/inventory_manager.json"); }