Exemplo n.º 1
0
        public void AddArgumentWillDoRuntimeCheck()
        {
            var code = "sql v1{#hello<3>}";
            var rt   = new SdmapCompiler();

            rt.AddSourceCode(code);
            rt.AddMacro("hello", new SdmapTypes[0], (context, ns, self, arguments) =>
            {
                return(Result.Ok("Hello World"));
            });
            var result = rt.TryEmit("v1", null);

            Assert.False(result.IsSuccess);
        }
Exemplo n.º 2
0
        public void CanAddArgumentMacro()
        {
            var code = "sql v1{#hello<sql{#val<>}>}";
            var rt   = new SdmapCompiler();

            rt.AddSourceCode(code);
            rt.AddMacro("hello", new[] { SdmapTypes.StringOrSql }, (context, ns, self, arguments) =>
            {
                return(Result.Ok("Hello " + ((EmitFunction)arguments[0])(context.DupNewFragments()).Value));
            });
            var result = rt.Emit("v1", "World");

            Assert.Equal("Hello World", result);
        }
Exemplo n.º 3
0
        public void CanAddMacro()
        {
            var code = "sql v1{#hello<>}";
            var rt   = new SdmapCompiler();

            rt.AddSourceCode(code);
            rt.AddMacro("hello", new SdmapTypes[0], (context, ns, self, arguments) =>
            {
                return(Result.Ok("Hello World"));
            });
            var result = rt.Emit("v1", null);

            Assert.Equal("Hello World", result);
        }
Exemplo n.º 4
0
        public void CanAddArgumentMacro()
        {
            var code = "sql v1{#hello<sql{#val<>}>}";
            var rt   = new SdmapCompiler();

            rt.AddSourceCode(code);
            rt.AddMacro("hello", new[] { SdmapTypes.StringOrSql }, (context, ns, self, arguments) =>
            {
                return(Result.Ok($"Hello " +
                                 MacroUtil.EvalToString(arguments[0], context, self).Value));
            });
            var result = rt.Emit("v1", "World");

            Assert.Equal("Hello World", result);
        }
Exemplo n.º 5
0
        public void OnlyRunOnce()
        {
            var code = "sql v1{#iif<A, sql{#record<>}, sql{#record<>}}";
            var rt   = new SdmapCompiler();

            rt.AddSourceCode(code);
            var times = 0;

            rt.AddMacro("record", null, (ctx, ns, self, args) =>
            {
                ++times;
                return(Result.Ok(string.Empty));
            });
            var result = rt.TryEmit("v1", new { A = true });

            Assert.True(result.IsSuccess);
            Assert.Equal(1, times);
        }
Exemplo n.º 6
0
        public void CanImplementDepInMacro()
        {
            var code = "sql v1{#def<B, 'Nice'>#isNotEmptyWithDeps<A, sql {ABCDEFG}, B>}";
            var rt   = new SdmapCompiler();

            rt.AddSourceCode(code);
            string id = "isNotEmptyWithDeps";

            rt.AddMacro(id, (context, ns, self, arguments) =>
            {
                if (self == null)
                {
                    return(Result.Fail <string>($"Query requires not null in macro '{id}'."));
                }
                ;

                var prop = self.GetType().GetProperty((string)arguments[0]);
                if (prop == null)
                {
                    return(Result.Fail <string>($"Query requires property '{prop}' in macro '{id}'."));
                }

                if (!RuntimeMacros.IsEmpty(RuntimeMacros.GetPropValue(self, (string)arguments[0])))
                {
                    foreach (var dep in arguments.Skip(2).OfType <string>())
                    {
                        context.Deps.Add(dep);
                    }
                    return(((EmitFunction)arguments[1])(context.DupNewFragments()));
                }
                return(Result.Ok(string.Empty));
            });
            var result = rt.Emit("v1", new { A = new[] { 1, 2, 3 } });

            Assert.Equal("NiceABCDEFG", result);
        }