public static List <LingoWordModel> MergeObjectLists(List <LingoWordModel> objList1, List <LingoWordModel> objList2) { // merge existing List of LingoWordModel instance data with another // List of LingoWordModels returning a new List of LingoWordModels if (objList1.Count < 1 && objList2.Count < 1) { return(new List <LingoWordModel>()); } if (objList1.Count < 1) { return(objList2); } if (objList2.Count < 1) { return(objList1); } LingoWordModel newItem = null; foreach (LingoWordModel lwm in objList2) { newItem = new LingoWordModel(); newItem.Word = lwm.Word; newItem.Category = lwm.Category; objList1.Add(newItem); } return(objList1); }
public static bool AddWordsToCategoryList(List <string> words, string category) { // take args and add to an existing Lingo Word List if (words.Count < 1 || string.IsNullOrEmpty(category)) { return(false); } LingoWordModel temp = null; List <LingoWordModel> wordList = new List <LingoWordModel>(); foreach (string word in words) { temp = new LingoWordModel(); temp.Category = category; temp.Word = word; wordList.Add(temp); } // convert object list to XElement type XElement objWordsToAdd = ConvertToXElement(wordList); // merge XElement LingoWords with existing LingoWords XElement existingLingoWords = GetFileData(); XElement mergedXElements = MergeDocuments(existingLingoWords, objWordsToAdd); if (UpdateFileData(mergedXElements)) { // xml file was updated with new list of words in new category // caller can then use the updated xml file to select a category and create a new LingoBingo board return(true); } return(false); }
public void Test_MergeObjectLists() { LingoWordModel lwm = null; List <LingoWordModel> objList1 = new List <LingoWordModel>(); List <LingoWordModel> objList2 = new List <LingoWordModel>(); // Example: // Category 1, Word 1 // Category 2, Word 2 // Category 3, Word 3 // Category 4, Word 4 for (int count = 1; count <= 4; count++) { lwm = new LingoWordModel(); lwm.Category = $"Category { count }"; lwm.Word = $"Word { count }"; if (count % 2 != 0) { objList1.Add(lwm); } else { objList2.Add(lwm); } } StringBuilder expectedResult = new StringBuilder(); expectedResult.Append("Category 1, "); expectedResult.AppendLine("Word 1"); expectedResult.Append("Category 2, "); expectedResult.AppendLine("Word 2"); expectedResult.Append("Category 3, "); expectedResult.AppendLine("Word 3"); expectedResult.Append("Category 4, "); expectedResult.AppendLine("Word 4"); List <LingoWordModel> mergedLists = FileManagementHelper.MergeObjectLists(objList1, objList2); StringBuilder actualResult = new StringBuilder(); foreach (LingoWordModel lwms in mergedLists) { actualResult.Append($"{ lwms.Category }, "); actualResult.AppendLine($"{ lwms.Word }"); } Console.WriteLine($"Expected\n" + $"Output: { expectedResult.ToString() }\n" + $"Length: { expectedResult.ToString().Length }\n" + $"Actual:\n" + $"Output: { actualResult.ToString() }\n" + $"Length: { actualResult.ToString().Length }\n"); Assert.IsTrue(expectedResult.ToString().Length == actualResult.ToString().Length); }
public void Test_ConvertToObjectList() { List <LingoWordModel> expectedResult = new List <LingoWordModel>(2); LingoWordModel lwm = null; for (int index = 1; index < 3; index++) { lwm = new LingoWordModel(); lwm.Category = $"Convert { index }"; lwm.Word = $"Alpha { index }"; expectedResult.Add(lwm); } XElement xEl = new XElement( new XElement("Root", new XElement("Item", new XElement("Category", "Convert 1"), new XElement("Word", "Alpha 1") ), new XElement("Item", new XElement("Category", "Convert 2"), new XElement("Word", "Alpha 2") ))); int matchCount = 0; int totalCount = 0; List <LingoWordModel> actualResult = FileManagementHelper.ConvertToObjectList(xEl); if (actualResult.Count > 0) { for (int jindex = 0; jindex < actualResult.Count; jindex++) { if (expectedResult[jindex].Category == actualResult[jindex].Category) { matchCount++; } totalCount++; if (expectedResult[jindex].Word == actualResult[jindex].Word) { matchCount++; } totalCount++; } } Assert.AreEqual(expectedResult.Count, actualResult.Count); // if equal than both lists have same number of objects Assert.AreEqual(totalCount, matchCount); // if equal than all if conditionals returned true }
public void Test_ConvertToXElement() { XElement expectedResult = new XElement( new XElement("Words", new XElement("Item", new XElement("Category", "ConvertToXEl 1"), new XElement("Word", "Word 1") ), new XElement("Item", new XElement("Category", "ConvertToXEl 2"), new XElement("Word", "Word 2") ), new XElement("Item", new XElement("Category", "ConvertToXEl 3"), new XElement("Word", "Word 3") ), new XElement("Item", new XElement("Category", "ConvertToXEl 4"), new XElement("Word", "Word 4") ))); List <LingoWordModel> objectList = null; LingoWordModel lwm = null; objectList = new List <LingoWordModel>(); for (int index = 1; index <= 4; index++) { lwm = new LingoWordModel(); lwm.Category = $"ConvertToXEl { index }"; lwm.Word = $"Word { index }"; objectList.Add(lwm); } XElement actualResult = FileManagementHelper.ConvertToXElement(objectList); Console.WriteLine( $"Expected\n" + $"Output: \n{ expectedResult.ToString() }\n" + $"Length: \n{ expectedResult.ToString().Length }\n" + $"\nActual:\n" + $"Output: \n{ actualResult.ToString() }\n" + $"Length: \n{ actualResult.ToString().Length }\n" ); Assert.IsTrue(expectedResult.ToString().Length == actualResult.ToString().Length); }
public static List <LingoWordModel> ConvertToObjectList(XElement xElement) { // takes XElement object and returns a List of LingoWordModel objects with data from input XDocument List <LingoWordModel> lingoWordObjects = new List <LingoWordModel>(); LingoWordModel temp = null; if (null == xElement) { // null in null out throw no exception return(new List <LingoWordModel>()); } IEnumerable <XElement> itemsXml = xElement.Descendants("Item"); foreach (XElement xItem in itemsXml) { temp = new LingoWordModel(); temp.Category = xItem.Element("Category").Value; temp.Word = xItem.Element("Word").Value; lingoWordObjects.Add(temp); } return(lingoWordObjects); }