Ejemplo n.º 1
0
        public void ReplacementStringsJS()
        {
            // reuse the same parser object
            var parser = new JSParser();

            // default should leave tokens intact
            var settings = new CodeSettings();
            var source   = "var a = 'He said, %My.Token:foo%'";
            var actual   = Parse(parser, settings, source);

            Assert.AreEqual("var a=\"He said, %My.Token:foo%\"", actual);

            settings.ReplacementTokensApplyDefaults(new Dictionary <string, string> {
                { "my.token", "\"Now he's done it!\"" },
                { "num_token", "123" },
                { "my-json", "{\"a\": 1, \"b\": 2, \"c\": [ 1, 2, 3 ] }" },
            });
            settings.ReplacementFallbacks.Add("zero", "0");

            actual = Parse(parser, settings, source);
            Assert.AreEqual("var a='He said, \"Now he\\'s done it!\"'", actual);

            actual = Parse(parser, settings, "var b = '%Num_Token%';");
            Assert.AreEqual("var b=\"123\"", actual);

            actual = Parse(parser, settings, "var c = '%My-JSON%';");
            Assert.AreEqual("var c='{\"a\": 1, \"b\": 2, \"c\": [ 1, 2, 3 ] }'", actual);
        }
Ejemplo n.º 2
0
        public void ReplacementFallbacksJS()
        {
            // reuse the same parser object
            var parser = new JSParser();

            // default should leave tokens intact
            var settings = new CodeSettings();

            settings.ReplacementTokensApplyDefaults(new Dictionary <string, string> {
                { "my.token", "\"Now he's done it!\"" },
                { "num_token", "123" },
                { "my-json", "{\"a\": 1, \"b\": 2, \"c\": [ 1, 2, 3 ] }" },
            });
            settings.ReplacementFallbacks.Add("zero", "0");

            var actual = Parse(parser, settings, "var a = %MissingToken:zero%;");

            Assert.AreEqual("var a=0", actual);

            actual = Parse(parser, settings, "var b = %MissingToken:ack% + 0;");
            Assert.AreEqual("var b=+0", actual);

            actual = Parse(parser, settings, "var c = %MissingToken:% + 0;");
            Assert.AreEqual("var c=+0", actual);

            actual = Parse(parser, settings, "var d = %MissingToken:%;debugger;throw 'why?';");
            Assert.AreEqual("var d=;throw\"why?\";", actual);
        }
Ejemplo n.º 3
0
        public void ReplacementNodesJS()
        {
            // reuse the same parser object
            var parser = new JSParser();

            // default should leave tokens intact
            var settings = new CodeSettings();
            var source   = "var a = %My.Token:foo%;";
            var actual   = Parse(parser, settings, source);

            Assert.AreEqual("var a=%My.Token:foo%", actual);

            settings.ReplacementTokensApplyDefaults(new Dictionary <string, string> {
                { "my.token", "\"Now he's done it!\"" },
                { "num_token", "123" },
                { "my-json", "{\"a\": 1, \"b\": 2, \"c\": [ 1, 2, 3 ] }" },
            });
            settings.ReplacementFallbacks.Add("zero", "0");

            actual = Parse(parser, settings, source);
            Assert.AreEqual("var a=\"Now he's done it!\"", actual);

            actual = Parse(parser, settings, "var b = %Num_Token%;");
            Assert.AreEqual("var b=123", actual);

            actual = Parse(parser, settings, "var c = %My-JSON%;");
            Assert.AreEqual("var c={\"a\":1,\"b\":2,\"c\":[1,2,3]}", actual);

            actual = Parse(parser, settings, "var d = '*%MissingToken:zero%*';");
            Assert.AreEqual("var d=\"*0*\"", actual);

            actual = Parse(parser, settings, "var e = '*%MissingToken:ack%*';");
            Assert.AreEqual("var e=\"**\"", actual);

            actual = Parse(parser, settings, "var f = '*%MissingToken:%*';");
            Assert.AreEqual("var f=\"**\"", actual);
        }