PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/source/library/Interlace.Tests/PropertyLists/TestParser.cs

https://bitbucket.org/VahidN/interlace
C# | 201 lines | 126 code | 38 blank | 37 comment | 4 complexity | d427eae289ac21bf9b0c55f2d14f733f MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Text;
  31. using MbUnit.Framework;
  32. using Interlace.Collections;
  33. using Interlace.PropertyLists;
  34. using Interlace.Utilities;
  35. #endregion
  36. namespace Interlace.Tests.PropertyLists
  37. {
  38. [TestFixture]
  39. public class TestParser
  40. {
  41. [Test]
  42. public void TestParsing()
  43. {
  44. // Empty dictionaries:
  45. PropertyDictionary empty =
  46. PropertyDictionary.FromString("{}");
  47. // Small, unquoted strings:
  48. PropertyDictionary smallDictionary =
  49. PropertyDictionary.FromString("{ foo = bar }");
  50. Assert.AreEqual("bar", smallDictionary.StringFor("foo"));
  51. Assert.AreEqual("moo", smallDictionary.StringFor("foo2", "moo"));
  52. // Extra characters:
  53. PropertyDictionary symbolDictionary =
  54. PropertyDictionary.FromString("{ _foo = baa-she_ep }");
  55. Assert.AreEqual("baa-she_ep", symbolDictionary.StringFor("_foo"));
  56. // Quoted strings, escaping and numbers:
  57. PropertyDictionary bigDictionary =
  58. PropertyDictionary.FromString("{ foo = \"The quick brown\\nfox!\\\"\"; bar = 342 }");
  59. Assert.AreEqual("The quick brown\nfox!\"", bigDictionary.StringFor("foo"));
  60. Assert.AreEqual(342, bigDictionary.IntegerFor("bar"));
  61. // Dictionaries in dictionaries:
  62. PropertyDictionary nestedDictionary =
  63. PropertyDictionary.FromString("{ foo = { 1 = 3; 2 = 9 } }");
  64. PropertyDictionary innerDictionary = nestedDictionary.DictionaryFor("foo");
  65. Assert.AreEqual(3, innerDictionary.IntegerFor(1));
  66. Assert.AreEqual(9, innerDictionary.IntegerFor(2));
  67. // Arrays in dictionaries:
  68. PropertyDictionary listInDictionary =
  69. PropertyDictionary.FromString("\n{ a = (1, 2, 3, \"moo\") }");
  70. PropertyArray array = listInDictionary.ArrayFor("a");
  71. Assert.AreEqual(4, array.Count);
  72. Assert.AreEqual(1, array.IntegerAt(0));
  73. Assert.AreEqual(2, array.IntegerAt(1));
  74. Assert.AreEqual(3, array.IntegerAt(2));
  75. Assert.AreEqual("moo", array.StringAt(3));
  76. // Comments in dictionaries:
  77. PropertyDictionary commentedDictionary =
  78. PropertyDictionary.FromString("{ foo = // MEOW! // !! \" \n 3; bar = 1 }");
  79. Assert.AreEqual(3, commentedDictionary.IntegerFor("foo"));
  80. Assert.AreEqual(1, commentedDictionary.IntegerFor("bar"));
  81. // Booleans in dictionaries:
  82. PropertyDictionary booleanDictionary =
  83. PropertyDictionary.FromString("{ hello = true; \r\nstuff = false;\r\nother-thing = \"true\"; }");
  84. Assert.AreEqual(true, booleanDictionary.BooleanFor("hello"));
  85. Assert.AreEqual(false, booleanDictionary.BooleanFor("stuff"));
  86. Assert.AreEqual("true", booleanDictionary.StringFor("other-thing"));
  87. // Doubles in dictionaries:
  88. PropertyDictionary doubleDictionary =
  89. PropertyDictionary.FromString("{ a = 1.2; b = -1.3; c = 1.3e10; d = 2; e = -3}");
  90. Assert.AreEqual(double.Parse("1.2"), doubleDictionary.DoubleFor("a"));
  91. Assert.AreEqual(double.Parse("-1.3"), doubleDictionary.DoubleFor("b"));
  92. Assert.AreEqual(double.Parse("1.3e10"), doubleDictionary.DoubleFor("c"));
  93. Assert.AreEqual(double.Parse("2.0"), doubleDictionary.DoubleFor("d"));
  94. Assert.AreEqual(double.Parse("-3.0"), doubleDictionary.DoubleFor("e"));
  95. }
  96. void AssertPropertyValuesAreEqual(object leftValue, object rightValue)
  97. {
  98. if (leftValue is PropertyDictionary)
  99. {
  100. Assert.IsInstanceOfType(typeof(PropertyDictionary), rightValue);
  101. AssertDictionariesEqual(leftValue as PropertyDictionary, rightValue as PropertyDictionary);
  102. }
  103. else if (leftValue is PropertyArray)
  104. {
  105. Assert.IsInstanceOfType(typeof(PropertyArray), rightValue);
  106. AssertArraysEqual(leftValue as PropertyArray, rightValue as PropertyArray);
  107. }
  108. else
  109. {
  110. Assert.AreEqual(leftValue, rightValue);
  111. }
  112. }
  113. void AssertArraysEqual(PropertyArray left, PropertyArray right)
  114. {
  115. Assert.AreEqual(left.Count, right.Count);
  116. for (int i = 0; i < left.Count; i++)
  117. {
  118. AssertPropertyValuesAreEqual(left[i], right[i]);
  119. }
  120. }
  121. void AssertDictionariesEqual(PropertyDictionary left, PropertyDictionary right)
  122. {
  123. Set<object> leftKeys = new Set<object>(left.Keys);
  124. Set<object> rightKeys = new Set<object>(right.Keys);
  125. Assert.AreEqual(leftKeys, rightKeys);
  126. foreach (object key in leftKeys)
  127. {
  128. object leftValue = left.ValueFor(key);
  129. object rightValue = right.ValueFor(key);
  130. }
  131. }
  132. void AssertRoundTrip(string originalDictionary)
  133. {
  134. // Get a property dictionary from a string (this is really just a convenience for the tests):
  135. PropertyDictionary firstStep = PropertyDictionary.FromString(originalDictionary);
  136. // Round trip it:
  137. string secondStep = firstStep.PersistToString();
  138. PropertyDictionary thirdStep;
  139. try
  140. {
  141. thirdStep = PropertyDictionary.FromString(secondStep);
  142. }
  143. catch (PropertyListException e)
  144. {
  145. throw new ApplicationException(string.Format(
  146. "AssertRoundTrip failing with: {0}", secondStep), e);
  147. }
  148. // Test it:
  149. AssertDictionariesEqual(firstStep, thirdStep);
  150. }
  151. [Test]
  152. public void TestMarshalling()
  153. {
  154. AssertRoundTrip("{}");
  155. AssertRoundTrip("{ foo = bar }");
  156. AssertRoundTrip("{ foo = bar; bar = foo }");
  157. AssertRoundTrip("{ \"f=oo\" = bar; bar = 53 }");
  158. AssertRoundTrip("{ \"f=oo\" = bar; bar = { a = 1; b = 2 } }");
  159. AssertRoundTrip("{ numbers = (1, 2, 3) }");
  160. AssertRoundTrip("{ others = ({}, (), { a = b }, (\"foo\", bar)) }");
  161. }
  162. }
  163. }