public void Preprocess_WhenWriterIsNull_Throws()
        {
            var preprocessor = new XmlPreprocessor();
            var reader       = XmlReader.Create(new StringReader(""));

            Assert.Throws <ArgumentNullException>(() => preprocessor.Preprocess(reader, null));
        }
        public void Preprocess_WhenReaderIsNull_Throws()
        {
            var preprocessor = new XmlPreprocessor();
            var writer       = XmlWriter.Create(new StringBuilder());

            Assert.Throws <ArgumentNullException>(() => preprocessor.Preprocess(null, writer));
        }
        public void Preprocess_WhenBlocksAreNested_RespectsStackOrder(string input, string expectedOutput)
        {
            var preprocessor = new XmlPreprocessor();

            string output = PreprocessString(preprocessor, input);

            Assert.AreEqual(expectedOutput, output);
        }
        public void IsDefined_WhenConstantIsDefined_ReturnsTrue()
        {
            var preprocessor = new XmlPreprocessor();

            preprocessor.Define("CONSTANT");

            Assert.IsTrue(preprocessor.IsDefined("CONSTANT"));
        }
        public void Preprocess_WhenNewDefinesCreatedWithExcessWhitespaceDuringPreprocessing_TrimsAndSavesThem()
        {
            var    preprocessor = new XmlPreprocessor();
            string input        = "<root><?define CONSTANT    ?></root>";

            PreprocessString(preprocessor, input);

            Assert.IsTrue(preprocessor.IsDefined("CONSTANT"));
        }
        public void Preprocess_WhenEndIfExcess_Throws()
        {
            var    preprocessor = new XmlPreprocessor();
            string input        = "<root><?endif?></root>";

            var ex = Assert.Throws <InvalidOperationException>(() => PreprocessString(preprocessor, input));

            Assert.AreEqual("Found <?endif?> instruction without matching <?ifdef?> or <?ifndef?>.", ex.Message);
        }
        public void Preprocess_WhenElseOutsideOfBlock_Throws()
        {
            var    preprocessor = new XmlPreprocessor();
            string input        = "<root><?else?></root>";

            var ex = Assert.Throws <InvalidOperationException>(() => PreprocessString(preprocessor, input));

            Assert.AreEqual("Found <?else?> instruction without enclosing <?ifdef?> or <?ifndef?> block.", ex.Message);
        }
        public void Preprocess_WhenEndIfMissing_Throws()
        {
            var    preprocessor = new XmlPreprocessor();
            string input        = "<root><?ifdef Foo?></root>";

            var ex = Assert.Throws <InvalidOperationException>(() => PreprocessString(preprocessor, input));

            Assert.AreEqual("Missing <?endif?> instruction.", ex.Message);
        }
        public void Preprocess_WhenIfNDefAndElsePresentAndConstantDefined_ExcludesIfBlockIncludesElse()
        {
            var    preprocessor = new XmlPreprocessor();
            string input        = "<root><?define CONSTANT?><?ifndef CONSTANT?>If<?else?>Else<?endif?></root>";

            string output = PreprocessString(preprocessor, input);

            Assert.AreEqual("<root>Else</root>", output);
        }
        public void Preprocess_WhenIfNDefPresentWithExcessWhitespaceAndConstantDefined_ExcludesIfBlock()
        {
            var    preprocessor = new XmlPreprocessor();
            string input        = "<root><?define CONSTANT?><?ifndef CONSTANT    ?>If<?endif?></root>";

            string output = PreprocessString(preprocessor, input);

            Assert.AreEqual("<root></root>", output);
        }
        public void Define_WhenConstantAlreadyDefined_DoesNothing()
        {
            var preprocessor = new XmlPreprocessor();

            preprocessor.Define("CONSTANT");

            preprocessor.Define("CONSTANT");

            Assert.IsTrue(preprocessor.IsDefined("CONSTANT"));
        }
        public object ProcessXmlResource(XmlDocument document)
        {
            XmlPreprocessor preprocessor = new XmlPreprocessor();
            preprocessor.Add("version", new Version(ApplicationEnvironment.ProductVersion));
            preprocessor.Add("language", CultureInfo.CurrentUICulture.TwoLetterISOLanguageName);
            preprocessor.Add("culture", CultureInfo.CurrentUICulture.Name);

            preprocessor.Munge(document);

            return _processor(document);
        }
        public void Preprocess_WhenPresetDefinesBeforeProcessing_UsesThem()
        {
            var preprocessor = new XmlPreprocessor();

            preprocessor.Define("CONSTANT");
            string input = "<root><?ifdef CONSTANT?>Text<?endif?></root>";

            string output = PreprocessString(preprocessor, input);

            Assert.AreEqual("<root>Text</root>", output);
        }
        public object ProcessXmlResource(XmlDocument document)
        {
            XmlPreprocessor preprocessor = new XmlPreprocessor();

            preprocessor.Add("version", new Version(ApplicationEnvironment.ProductVersion));
            preprocessor.Add("language", CultureInfo.CurrentUICulture.TwoLetterISOLanguageName);
            preprocessor.Add("culture", CultureInfo.CurrentUICulture.Name);

            preprocessor.Munge(document);

            return(_processor(document));
        }
        private static string PreprocessString(XmlPreprocessor preprocessor, string xml)
        {
            using (var reader = XmlReader.Create(new StringReader(xml)))
            {
                var xmlOutput = new StringBuilder();
                using (var writer = XmlWriter.Create(xmlOutput, new XmlWriterSettings()
                {
                    OmitXmlDeclaration = true
                }))
                {
                    preprocessor.Preprocess(reader, writer);
                }

                return(xmlOutput.ToString());
            }
        }
Beispiel #16
0
        private Plugin PreprocessAndDeserialize(TextReader reader)
        {
            XmlPreprocessor preprocessor = new XmlPreprocessor();

            foreach (string constant in initialPreprocessorConstants)
            {
                preprocessor.Define(constant);
            }

            StringBuilder preprocessedXml = new StringBuilder();

            using (XmlReader xmlReader = XmlReader.Create(reader))
            {
                using (XmlWriter xmlWriter = XmlWriter.Create(preprocessedXml))
                    preprocessor.Preprocess(xmlReader, xmlWriter);
            }

            var plugin = (Plugin)PluginXmlSerializer.Deserialize(new StringReader(preprocessedXml.ToString()));

            plugin.Validate();
            return(plugin);
        }
        public void IsDefined_WhenConstantIsNull_Throws()
        {
            var preprocessor = new XmlPreprocessor();

            Assert.Throws <ArgumentNullException>(() => preprocessor.IsDefined(null));
        }
        public void IsDefined_WhenConstantIsNotDefined_ReturnsFalse()
        {
            var preprocessor = new XmlPreprocessor();

            Assert.IsFalse(preprocessor.IsDefined("CONSTANT"));
        }