Beispiel #1
0
        public void TypeConversion()
        {
            Mock <IConfigurationSection> typeconfig = new Mock <IConfigurationSection>();

            typeconfig.SetupGet(s => s.Key).Returns("NamedCode");
            typeconfig.SetupGet(s => s.Value).Returns("ScriptService.Dto.NamedCode,ScriptService");

            Mock <IConfigurationSection> typesconfig = new Mock <IConfigurationSection>();

            typesconfig.Setup(s => s.GetChildren()).Returns(new[] { typeconfig.Object });

            Mock <IConfiguration> config = new Mock <IConfiguration>();

            config.Setup(s => s.GetSection("Types")).Returns(typesconfig.Object);

            TypeCreator   creator       = new TypeCreator(new NullLogger <TypeCreator>(), config.Object);
            PythonService pythonservice = new PythonService(new Mock <IScriptImportService>().Object, creator);

            PythonScript script = new PythonScript(pythonservice, "import NamedCode\ncode=NamedCode()\ncode.Name='Test'\ntest.TestMethod(code)");

            Assert.AreEqual("Test", script.Execute(new Dictionary <string, object> {
                ["log"]  = new WorkableLogger(new NullLogger <JavascriptTests>(), null),
                ["test"] = this
            }));
        }
Beispiel #2
0
        public void CallNCScript()
        {
            ScriptParser parser = new ScriptParser();

            parser.Types.AddType <Script>();
            IScript ncscript = parser.Parse("parameter($data, script) return($data.name)");

            Mock <IScriptCompiler> scriptcompiler = new Mock <IScriptCompiler>();

            scriptcompiler.Setup(s => s.CompileScriptAsync(It.IsAny <string>(), It.IsAny <int?>())).ReturnsAsync(new CompiledScript {
                Instance = ncscript
            });

            Mock <IScriptImportService> importservice = new Mock <IScriptImportService>();

            importservice.Setup(s => s.Script(It.IsAny <string>(), It.IsAny <int?>())).Returns(new ScriptExecutor(new WorkableLogger(new NullLogger <JavascriptTests>(), null), scriptcompiler.Object, "Test", 1));
            importservice.Setup(s => s.Clone(It.IsAny <WorkableLogger>())).Returns(() => importservice.Object);

            PythonService pythonservice = new PythonService(importservice.Object, null);
            PythonScript  script        = new PythonScript(pythonservice, "script=load.Script('Test', None)\nscript.Execute({'data':{'Name':'Test'}})");

            Assert.AreEqual("Test", script.Execute(new Dictionary <string, object> {
                ["log"] = null
            }));
        }
Beispiel #3
0
        public void ReturnConstant()
        {
            PythonService pythonservice = new PythonService(new Mock <IScriptImportService>().Object, null);
            PythonScript  script        = new PythonScript(pythonservice, "7");

            Assert.AreEqual(7, script.Execute(new Dictionary <string, object> {
                ["test"] = this
            }));
        }
Beispiel #4
0
        public void AwaitTask()
        {
            PythonService pythonservice = new PythonService(new Mock <IScriptImportService>().Object, null);
            PythonScript  script        = new PythonScript(pythonservice, "await(test.TestTask())");

            Assert.AreEqual("test", script.Execute(new Dictionary <string, object> {
                ["test"] = this
            }));
        }
Beispiel #5
0
        public void IntCall()
        {
            PythonService pythonservice = new PythonService(new Mock <IScriptImportService>().Object, null);
            PythonScript  script        = new PythonScript(pythonservice, "test.TestNumber(7)");

            Assert.AreEqual(7, script.Execute(new Dictionary <string, object> {
                ["test"] = this
            }));
        }
Beispiel #6
0
        public void InvalidImports()
        {
            PythonService pythonservice = new PythonService(new Mock <IScriptImportService>().Object, new Mock <ITypeCreator>().Object);

            Assert.Throws <ArgumentException>(() => new PythonScript(pythonservice, "import clr\n7"));
        }
Beispiel #7
0
        public void InvalidClrCall(string line)
        {
            PythonService pythonservice = new PythonService(new Mock <IScriptImportService>().Object, new Mock <ITypeCreator>().Object);

            Assert.Throws <ArgumentException>(() => new PythonScript(pythonservice, $"{line}\n7"));
        }