public void SetAlternative_ThreeDifferentLanguages_LanguageFormsAreSortedbyWritingSystem() { MultiTextBase multiTextBaseToPopulate = new MultiTextBase(); multiTextBaseToPopulate.SetAlternative("fr", "fr Word3"); multiTextBaseToPopulate.SetAlternative("de", "de Word1"); multiTextBaseToPopulate.SetAlternative("en", "en Word2"); Assert.AreEqual(3, multiTextBaseToPopulate.Forms.Length); Assert.AreEqual("de", multiTextBaseToPopulate.Forms[0].WritingSystemId); Assert.AreEqual("en", multiTextBaseToPopulate.Forms[1].WritingSystemId); Assert.AreEqual("fr", multiTextBaseToPopulate.Forms[2].WritingSystemId); }
public void NullConditions() { MultiTextBase text = new MultiTextBase(); Assert.AreSame(string.Empty, text["foo"], "never before heard of alternative should give back an empty string"); Assert.AreSame(string.Empty, text["foo"], "second time"); Assert.AreSame(string.Empty, text.GetBestAlternative("fox")); text.SetAlternative("zox", ""); Assert.AreSame(string.Empty, text["zox"]); text.SetAlternative("zox", null); Assert.AreSame(string.Empty, text["zox"], "should still be empty string after setting to null"); text.SetAlternative("zox", "something"); text.SetAlternative("zox", null); Assert.AreSame(string.Empty, text["zox"], "should still be empty string after setting something and then back to null"); }
public void CompareTo_MultiTextWithMoreForms_ReturnsLess() { MultiTextBase multiTextBase = new MultiTextBase(); MultiTextBase multiTextBaseToCompare = new MultiTextBase(); multiTextBaseToCompare.SetAlternative("de", "Word 1"); Assert.AreEqual(-1, multiTextBase.CompareTo(multiTextBaseToCompare)); }
public void CompareTo_MultiTextWithFewerForms_ReturnsGreater() { MultiTextBase multiTextBase = new MultiTextBase(); multiTextBase.SetAlternative("de", "Word 1"); MultiTextBase multiTextBaseToCompare = new MultiTextBase(); Assert.AreEqual(1, multiTextBase.CompareTo(multiTextBaseToCompare)); }
public void CompareTo_IdenticalMultiText_ReturnsEqual() { MultiTextBase multiTextBase = new MultiTextBase(); multiTextBase.SetAlternative("de", "Word 1"); MultiTextBase multiTextBaseToCompare = new MultiTextBase(); multiTextBaseToCompare.SetAlternative("de", "Word 1"); Assert.AreEqual(0, multiTextBase.CompareTo(multiTextBaseToCompare)); }
public void CompareTo_MultiTextWithNonIdenticalFormsAndFirstNonidenticalformIsAlphabeticallyLater_ReturnsLess() { MultiTextBase multiTextBase = new MultiTextBase(); multiTextBase.SetAlternative("de", "Word 1"); MultiTextBase multiTextBaseToCompare = new MultiTextBase(); multiTextBaseToCompare.SetAlternative("de", "Word 2"); Assert.AreEqual(-1, multiTextBase.CompareTo(multiTextBaseToCompare)); }
public void CompareTo_MultiTextWithNonIdenticalWritingSystemsAndFirstNonidenticalWritingSystemIsAlphabeticallyEarlier_ReturnsGreater() { MultiTextBase multiTextBase = new MultiTextBase(); multiTextBase.SetAlternative("en", "Word 1"); MultiTextBase multiTextBaseToCompare = new MultiTextBase(); multiTextBaseToCompare.SetAlternative("de", "Word 1"); Assert.AreEqual(1, multiTextBase.CompareTo(multiTextBaseToCompare)); }
public void UpdateGenericLanguageString(string key, string value, bool isCollectionValue) { var text = new MultiTextBase(); text.SetAlternative("*", value); if (TextVariables.ContainsKey(key)) { TextVariables.Remove(key); } TextVariables.Add(key, new NamedMutliLingualValue(text, isCollectionValue)); }
/// <summary> /// walk throught the sourceDom, collecting up values from elements that have data-book or data-collection attributes. /// </summary> private void GatherDataItemsFromXElement(DataSet data, XmlNode sourceElement, // can be the whole sourceDom or just a page HashSet <Tuple <string, string> > itemsToDelete = null) // records key, lang pairs for which we found an empty element in the source. { string elementName = "*"; try { string query = String.Format(".//{0}[(@data-book or @data-library or @data-collection)]", elementName); XmlNodeList nodesOfInterest = sourceElement.SafeSelectNodes(query); foreach (XmlElement node in nodesOfInterest) { bool isCollectionValue = false; string key = node.GetAttribute("data-book").Trim(); if (key == String.Empty) { key = node.GetAttribute("data-collection").Trim(); if (key == String.Empty) { key = node.GetAttribute("data-library").Trim(); //the old (pre-version 1) name of collections was 'library' } isCollectionValue = true; } string value = node.InnerXml.Trim(); //may contain formatting if (node.Name.ToLower() == "img") { value = node.GetAttribute("src"); //Make the name of the image safe for showing up in raw html (not just in the relatively safe confines of the src attribut), //becuase it's going to show up between <div> tags. E.g. "Land & Water.png" as the cover page used to kill us. value = WebUtility.HtmlEncode(WebUtility.HtmlDecode(value)); } string lang = node.GetOptionalStringAttribute("lang", "*"); if (lang == "") //the above doesn't stop a "" from getting through { lang = "*"; } if (string.IsNullOrEmpty(value)) { // This is a value we may want to delete if (itemsToDelete != null) { itemsToDelete.Add(Tuple.Create(key, lang)); } } else if (!value.StartsWith("{")) //ignore placeholder stuff like "{Book Title}"; that's not a value we want to collect { if ((elementName.ToLower() == "textarea" || elementName.ToLower() == "input" || node.GetOptionalStringAttribute("contenteditable", "false") == "true") && (lang == "V" || lang == "N1" || lang == "N2")) { throw new ApplicationException( "Editable element (e.g. TextArea) should not have placeholder @lang attributes (V,N1,N2)\r\n\r\n" + node.OuterXml); } //if we don't have a value for this variable and this language, add it if (!data.TextVariables.ContainsKey(key)) { var t = new MultiTextBase(); t.SetAlternative(lang, value); data.TextVariables.Add(key, new NamedMutliLingualValue(t, isCollectionValue)); } else if (!data.TextVariables[key].TextAlternatives.ContainsAlternative(lang)) { MultiTextBase t = data.TextVariables[key].TextAlternatives; t.SetAlternative(lang, value); } } } } catch (Exception error) { throw new ApplicationException( "Error in GatherDataItemsFromDom(," + elementName + "). RawDom was:\r\n" + sourceElement.OuterXml, error); } }