public void GetsFileIfItIsAnEmbeddedResource()
        {
            var table       = new Dictionary <int, string>();
            var xmlDocument = new XmlDocument();

            using (var stream = streamLoader.LoadFor("TestTable.xml"))
                xmlDocument.Load(stream);

            var objects = xmlDocument.DocumentElement.ChildNodes;

            Assert.That(objects[0].SelectSingleNode("value").InnerText, Is.EqualTo("Value 1"));
            Assert.That(objects[1].SelectSingleNode("value").InnerText, Is.EqualTo("Value 2"));
            Assert.That(objects[0].ChildNodes.Count, Is.EqualTo(1));
            Assert.That(objects[1].ChildNodes.Count, Is.EqualTo(3));
            Assert.That(objects.Count, Is.EqualTo(2));

            var subvalues = objects[1].SelectNodes("subvalue");

            Assert.That(subvalues[0].InnerText, Is.EqualTo("Subvalue 1"));
            Assert.That(subvalues[1].InnerText, Is.EqualTo("Subvalue 2"));
            Assert.That(subvalues.Count, Is.EqualTo(2));
        }
        public Dictionary <string, IEnumerable <string> > Map(string tableName)
        {
            var filename    = tableName + ".xml";
            var mappedTable = new Dictionary <string, IEnumerable <string> >();
            var xmlDocument = new XmlDocument();

            using (var stream = streamLoader.LoadFor(filename))
                xmlDocument.Load(stream);

            if (xmlDocument.DocumentElement.LocalName != "collections")
            {
                throw new Exception($"Table {tableName} is not a collection table (does not have \"collections\" as root node)");
            }

            var collectionNodes = xmlDocument.DocumentElement.ChildNodes;

            foreach (XmlNode node in collectionNodes)
            {
                if (node.LocalName != "collection")
                {
                    throw new Exception($"Collection table {tableName} contains an invalid collection node called \"{node.LocalName}\"");
                }

                var nameNodes  = node.SelectNodes("name");
                var entryNodes = node.SelectNodes("entry");

                if (nameNodes.Count != 1 || nameNodes.Count + entryNodes.Count != node.ChildNodes.Count)
                {
                    throw new Exception($"Collection table {tableName} has a malformed collection node");
                }

                var name = node.SelectSingleNode("name").InnerText;
                if (mappedTable.ContainsKey(name))
                {
                    throw new Exception($"Collection table {tableName} has duplicate collections of {name}");
                }

                var entries = new List <string>();

                foreach (XmlNode entryNode in entryNodes)
                {
                    entries.Add(entryNode.InnerText);
                }

                mappedTable.Add(name, entries);
            }

            return(mappedTable);
        }
Beispiel #3
0
        public Dictionary <int, string> Map(string tableName)
        {
            var filename    = tableName + ".xml";
            var mappedTable = new Dictionary <int, string>();
            var xmlDocument = new XmlDocument();

            using (var stream = streamLoader.LoadFor(filename))
                xmlDocument.Load(stream);

            if (xmlDocument.DocumentElement.LocalName != "percentiles")
            {
                throw new Exception($"Table {tableName} is not a percentile table (does not have \"percentiles\" as root node)");
            }

            var percentileNodes = xmlDocument.DocumentElement.ChildNodes;

            if (percentileNodes.Count == 0)
            {
                throw new Exception($"Percentile table {tableName} is empty");
            }

            foreach (XmlNode node in percentileNodes)
            {
                if (node.LocalName != "percentile")
                {
                    throw new Exception($"Percentile table {tableName} contains an invalid percentile node called \"{node.LocalName}\"");
                }

                var lowerNodes   = node.SelectNodes("lower");
                var contentNodes = node.SelectNodes("content");
                var upperNodes   = node.SelectNodes("upper");

                if (lowerNodes.Count != 1 || contentNodes.Count != 1 || upperNodes.Count != 1 || node.ChildNodes.Count != 3)
                {
                    throw new Exception($"Percentile table {tableName} has a malformed percentile node");
                }

                var lowerLimit = Convert.ToInt32(lowerNodes[0].InnerText);
                var content    = contentNodes[0].InnerText;
                var upperLimit = Convert.ToInt32(upperNodes[0].InnerText);

                for (var roll = lowerLimit; roll <= upperLimit; roll++)
                {
                    if (mappedTable.ContainsKey(roll))
                    {
                        throw new Exception($"Percentile table {tableName} has duplicate results for {roll}");
                    }

                    mappedTable.Add(roll, content);
                }
            }

            var rolls   = Enumerable.Range(1, 100);
            var missing = rolls.Except(mappedTable.Keys);

            if (missing.Any())
            {
                throw new Exception($"Percentile table {tableName} is missing results for {string.Join(", ", missing)}");
            }

            var extras = mappedTable.Keys.Except(rolls);

            if (extras.Any())
            {
                throw new Exception($"Percentile table {tableName} has extra results for {string.Join(", ", extras)}");
            }

            return(mappedTable);
        }