Ejemplo n.º 1
0
        public void TestReplaceMessageReferences()
        {
            I18n.Add("STRING_REF", "test string");

            var resultString = Utils.ReplaceMessageReferences("");

            Assert.IsTrue(string.Equals("", resultString), "Empty string produces empty string");
            resultString = Utils.ReplaceMessageReferences("%{bky_string_ref}");
            Assert.IsTrue(string.Equals("test string", resultString), "Message ref dereferenced.");
            resultString = Utils.ReplaceMessageReferences("before %{bky_string_ref} after");
            Assert.IsTrue(string.Equals("before test string after", resultString), "Message ref dereferenced.");

            resultString = Utils.ReplaceMessageReferences("%1");
            Assert.IsTrue(string.Equals("%1", resultString), "Interpolation tokens ignored.");
            resultString = Utils.ReplaceMessageReferences("%1 %2");
            Assert.IsTrue(string.Equals("%1 %2", resultString), "Interpolation tokens ignored.");
            resultString = Utils.ReplaceMessageReferences("before %1 after");
            Assert.IsTrue(string.Equals("before %1 after", resultString), "Interpolation tokens ignored.");

            resultString = Utils.ReplaceMessageReferences("%%");
            Assert.IsTrue(string.Equals("%", resultString), "Ecaped %");
            resultString = Utils.ReplaceMessageReferences("%%{bky_string_ref}");
            Assert.IsTrue(string.Equals("%{bky_string_ref}", resultString), "Escaped %");

            resultString = Utils.ReplaceMessageReferences("%a");
            Assert.IsTrue(string.Equals("%a", resultString), "Unrecognized % escape code treated as literal");
        }
Ejemplo n.º 2
0
        public void TestTokenizeInterpolation()
        {
            var tokens = Utils.TokenizeInterpolation("");

            Assert.IsTrue(tokens.Count == 0, "Null interpolation");

            tokens = Utils.TokenizeInterpolation("Hello");
            Assert.IsTrue(tokens.Contains("Hello"), "No interpolation");

            tokens = Utils.TokenizeInterpolation("Hello%World");
            Assert.IsTrue(tokens.Contains("Hello%World"), "Unescaped %.");

            tokens = Utils.TokenizeInterpolation("Hello%%World");
            Assert.IsTrue(tokens.Contains("Hello%World"), "Escaped %.");

            tokens = Utils.TokenizeInterpolation("Hello %1 World");
            Assert.IsTrue(string.Equals(tokens[0], "Hello ") && string.Equals(tokens[1], "1") && string.Equals(tokens[2], " World"), "Interpolation.");

            tokens = Utils.TokenizeInterpolation("%123Hello%456World%789");
            Assert.IsTrue(string.Equals(tokens[0], "123") &&
                          string.Equals(tokens[1], "Hello") &&
                          string.Equals(tokens[2], "456") &&
                          string.Equals(tokens[3], "World") &&
                          string.Equals(tokens[4], "789"), "Interpolations.");

            tokens = Utils.TokenizeInterpolation("%%%x%%0%00%01%");

            Assert.IsTrue(string.Equals(tokens[0], "%%x%0") &&
                          string.Equals(tokens[1], "0") &&
                          string.Equals(tokens[2], "1") &&
                          string.Equals(tokens[3], "%"), "Tortune interpolations.");

            I18n.Add("STRING_REF", "test string");

            tokens = Utils.TokenizeInterpolation("%{bky_string_ref}");
            Assert.IsTrue(string.Equals(tokens[0], "test string"), "String table reference,lowercase");

            tokens = Utils.TokenizeInterpolation("%{BKY_STRING_REF}");
            Assert.IsTrue(string.Equals(tokens[0], "test string"), "String table reference,uppercase");

            I18n.Add("WITH_PARAM", "before %1 after");

            tokens = Utils.TokenizeInterpolation("%{bky_with_param}");
            Assert.IsTrue(string.Equals(tokens[0], "before ") &&
                          string.Equals(tokens[1], "1") &&
                          string.Equals(tokens[2], " after"),
                          "String table refrence,with subreference");

            AddMsgToBlocklyMsg("RECURSE", "before %{bky_string_ref} after");
            tokens = Utils.TokenizeInterpolation("%{bky_recurse}");
            Assert.IsTrue(string.Equals(tokens[0], "before test string after"), "String table reference,with subreference");

            // Error cases...
            tokens = Utils.TokenizeInterpolation("%{bky_undefined}");
            Assert.IsTrue(string.Equals(tokens[0], "%{bky_undefined}"), "Undefined string table reference");

            AddMsgToBlocklyMsg("1", "Will not match");
            tokens = Utils.TokenizeInterpolation("before %{1} after");
            Assert.IsTrue(string.Equals("before %{1} after", tokens[0]), "Invalid initial digit in string table reference");

            AddMsgToBlocklyMsg("TWO WORDS", "Will not match");
            tokens = Utils.TokenizeInterpolation("before %{two words} after");
            Assert.IsTrue(string.Equals(tokens[0], "before %{two words} after"), "Invalid character in string table reference: space");

            AddMsgToBlocklyMsg("TWO.WORDS", "Will not match");
            tokens = Utils.TokenizeInterpolation("before %{two.words} after");
            Assert.IsTrue(string.Equals(tokens[0], "before %{two.words} after"), "Invalid character in string table reference:period");

            AddMsgToBlocklyMsg("AB&C", "Will not match");
            tokens = Utils.TokenizeInterpolation("before %{ab&c} after");
            Assert.IsTrue(string.Equals(tokens[0], "before %{ab&c} after"), "Invalid character in string table reference: &");

            AddMsgToBlocklyMsg("UNCLOSED", "Will not match");
            tokens = Utils.TokenizeInterpolation("before %{unclosed");
            Assert.IsTrue(string.Equals(tokens[0], "before %{unclosed"), "String table reference,with parameter");
        }
Ejemplo n.º 3
0
 void AddMsgToBlocklyMsg(string key, string content)
 {
     I18n.Add(key, content);
 }