// Test various operations on the dictionary. static bool TestTags(PowerPoint.Tags tags) { bool passed = true; TagDictionary d1 = new TagDictionary(tags, true, "|Bob_"); TestTagsDump(d1); Console.WriteLine("Test clear..."); d1.Add("test", ""); TestTagsDump(d1); d1.Clear(); if (d1.Count == 0) { Console.WriteLine("Passed."); } else { passed = false; Console.WriteLine("Failed."); } TestTagsDump(d1); Console.WriteLine("Testing access to missing element."); try { string s = d1["test"]; if (s == "") { Console.WriteLine("Passed."); } else { passed = false; Console.WriteLine("Failed."); } } catch (ArgumentException) { passed = false; Console.WriteLine("Failed."); } Console.WriteLine("Testing adding and accessing elements..."); // Alternate entries are keys. Must be at least two pairs. string[] strings = new string[] { "hello", "Yes!", "FOO BAR!", "BaZ", "no", "D'oh!", "Farnsworth", "Zeke" }; bool passedStrings = true; for (int i = 0; i < strings.Length; i += 2) { d1.Add(strings[i], strings[i + 1]); } TestTagsDump(d1); for (int i = strings.Length - 2; i >= 0; i -= 2) { if (d1[strings[i]] != strings[i + 1]) { passedStrings = false; } } if (passedStrings) { Console.WriteLine("Passed."); } else { passed = false; Console.WriteLine("Failed."); } Console.WriteLine("Testing removal..."); d1.Remove(strings[2]); if (d1.Contains(strings[2])) { passed = false; Console.WriteLine("Failed."); } else { Console.WriteLine("Passed."); } Console.WriteLine("Testing overwrite..."); d1[strings[0]] = strings[3]; if (d1[strings[0]] == strings[3]) { Console.WriteLine("Passed."); } else { passed = false; Console.WriteLine("Failed."); } Console.WriteLine("Testing null key..."); try { d1.Add(null, "five"); passed = false; Console.WriteLine("Failed."); } catch (ArgumentNullException) { Console.WriteLine("Passed."); } Console.WriteLine("Testing null value..."); try { d1.Add("five", null); passed = false; Console.WriteLine("Failed."); } catch (ArgumentNullException) { Console.WriteLine("Passed."); } Console.WriteLine("Testing empty value..."); try { d1.Add("five", ""); if (d1["five"] == "") { Console.WriteLine("Passed."); } else { passed = false; Console.WriteLine("Failed."); } } catch (ArgumentOutOfRangeException) { passed = false; Console.WriteLine("Failed."); } Console.WriteLine("Testing coexisting dictionaries separation..."); TagDictionary d2 = new TagDictionary(tags, true, ""); TagDictionary d3 = new TagDictionary(tags, true, "sam"); d1["test"] = "one"; d2["test"] = "two"; d3["test"] = "three"; if (d1["test"] != "one" || d2["test"] != "two" || d3["test"] != "three") { Console.WriteLine("Failed."); passed = false; } else { Console.WriteLine("Passed."); } Console.WriteLine("Testing coexisting dictionaries clearing..."); d1.Clear(); if (d1.Contains("test") || d2["test"] != "two" || d3["test"] != "three") { Console.WriteLine("Failed."); passed = false; } else { Console.WriteLine("Passed."); } Console.WriteLine("d1:"); TestTagsDump(d1); Console.WriteLine("d2:"); TestTagsDump(d2); Console.WriteLine("d3:"); TestTagsDump(d3); return(passed); }
/// <summary> /// Creates a dictionary wrapping a tags object. Name defaults to "". /// </summary> /// <param name="tags">the tags object to wrap</param> /// <param name="allowLower">should keys be allowed to use lower-case letters</param> /// <remarks>if keys are not allowed to use lower-case, lower-case letters will cause an exception; otherwise, they will be transformed to upper-case</remarks> public TagDictionary(PowerPoint.Tags tags, bool allowLower) : this(tags, allowLower, "") { }
/// <summary> /// Creates a dictionary wrapping a tags object /// </summary> /// <param name="tags">the tags object to wrap</param> /// <param name="allowLower">should keys be allowed to use lower-case letters</param> /// <param name="name">a name to use for this tag dictionary; should be all uppercase (will cause /// an exception if allowLower is false and otherwise will be uppercased); only /// entries under this name will be affected by operations on this tag dictionary</param> /// <remarks>if keys are not allowed to use lower-case, lower-case letters will cause an exception; otherwise, they will be transformed to upper-case</remarks> public TagDictionary(PowerPoint.Tags tags, bool allowLower, string name) { this.myTags = tags; this.myAllowLower = allowLower; this.myName = this.CheckOrMakeUpper(name); }
/// <summary> /// Creates a dictionary wrapping a tags object. Defaults to allow lower-case. Name defaults to "". /// </summary> /// <param name="tags">the tags object to wrap</param> /// <remarks>if keys are not allowed to use lower-case, lower-case letters will cause an exception; otherwise, they will be transformed to upper-case</remarks> public TagDictionary(PowerPoint.Tags tags) : this(tags, true) { }