コード例 #1
0
        public void EmptyArray()
        {
            IScript script = parser.Parse("[]");
            Array   array  = script.Execute <Array>();

            Assert.NotNull(array);
            Assert.AreEqual(0, array.Length);
        }
コード例 #2
0
        public void CreateSingleParameterLeavingOutDefault()
        {
            IScript script = parser.Parse(
                "$var=new variable(\"test\")\n" +
                "$var.name"
                );

            Assert.AreEqual("test", script.Execute());
        }
コード例 #3
0
        public void SingleLineComment()
        {
            IScript script = parser.Parse(
                "// this is a comment\n" +
                "$result=4"
                );

            Assert.AreEqual(4, script.Execute());
        }
コード例 #4
0
        public void DisposeSingle()
        {
            IScript script = parser.Parse(
                "$data=new disposable()\n" +
                "using($data)\n" +
                "\"weird statement\"\n" +
                "$data.disposed"
                );

            Assert.AreEqual(true, script.Execute());
        }
コード例 #5
0
        public void ExecuteExpressionInTask()
        {
            IScript script = parser.Parse(ScriptCode.Create(
                                              "$result=0",
                                              "$t=new task($result=5)",
                                              "$t.start()",
                                              "$t.wait()",
                                              "$result"
                                              ));

            Assert.AreEqual(5, script.Execute());
        }
コード例 #6
0
        public void AddValues()
        {
            IScript script = parser.Parse(
                "$list=new list()" +
                "$list.add(1)" +
                "$list.add(7)" +
                "$list.add(9)" +
                "$list"
                );


            Assert.That(new[] { 1, 7, 9 }.SequenceEqual(script.Execute <IEnumerable>().Cast <int>()));
        }
コード例 #7
0
        public void SimpleAssignmentFailure()
        {
            IScript script = parser.Parse("$lockedvar=4");

            try {
                script.Execute();
                Assert.Fail("Script should fail with runtime exception");
            }
            catch (ScriptRuntimeException e) {
                Console.WriteLine(e.CreateStackTrace());
                Assert.That(e.Token is ICodePositionToken);
                Assert.AreEqual(1, ((ICodePositionToken)e.Token).LineNumber);
            }
        }
コード例 #8
0
        public void DetectOptionalScriptParameter()
        {
            IScript            script    = parser.Parse("parameter($parameter, \"int\", 0) method.call($parameter)", new Variable("method"));
            ParameterExtractor extractor = new ParameterExtractor();

            extractor.Visit(script);
            Assert.AreEqual(1, extractor.Parameters.Count());
            Assert.AreEqual("parameter", extractor.Parameters.First().Name);
            Assert.That(extractor.Parameters.First().IsOptional);
        }
コード例 #9
0
        public void PrecompileAndExecuteWithDifferentArguments(int argument, int expected)
        {
            IScript script = parser.Parse(
                "$result=7\n" +
                "$result+=$argument\n" +
                "$result"
                );

            Assert.AreEqual(expected, script.Execute(new Variable("argument", argument)));
        }
コード例 #10
0
        public void ValidWaitStatement(string timeargument)
        {
            IScript script = parser.Parse($"wait({timeargument})");

            DateTime now = DateTime.Now;

            Assert.DoesNotThrow(() => script.Execute());
            TimeSpan executiontime = DateTime.Now - now;

            Assert.Greater(TimeSpan.FromSeconds(1), executiontime);
        }
コード例 #11
0
        public IScriptToken ExtractToken(string data, int position, bool fulltoken = true, Func <IScriptToken, bool> filter = null)
        {
            position = Math.Min(Math.Max(position, 0), data.Length);
            int endposition   = position;
            int startposition = position;

            if (startposition >= data.Length)
            {
                --startposition;
            }
            while (startposition > 0 && data[startposition] != '\n')
            {
                --startposition;
            }
            if (data[startposition] == '\n')
            {
                ++startposition;
            }

            if (startposition >= data.Length)
            {
                return(null);
            }

            if (fulltoken)
            {
                ScanLiteral(data, ref endposition);
            }
            string line = data.Substring(startposition, endposition - startposition);

            StringBuilder finalline = new StringBuilder();
            int           index     = 0;

            Scan(finalline, line, ref index);

            while (finalline.Length > 0 && (finalline[finalline.Length - 1] == '.' || finalline[finalline.Length - 1] == ',' || finalline[finalline.Length - 1] == ' '))
            {
                --finalline.Length;
            }

            IScript script;

            try {
                script = parser.Parse(finalline.ToString());
            }
            catch (Exception) {
                return(null);
            }

            TokenExtractionVisitor tokenvisitor = new TokenExtractionVisitor(position, filter);

            tokenvisitor.Visit(script);
            return(tokenvisitor.Token);
        }
コード例 #12
0
        /// <summary>
        /// imports an external script method assembly resources
        /// </summary>
        /// <param name="parameters">resource name</param>
        /// <returns>script method stored in resource</returns>
        public IExternalMethod Import(object[] parameters)
        {
            if (parameters.Length == 0)
            {
                throw new ScriptRuntimeException("A resource to import is necessary", null);
            }
            if (parameters.Length > 1)
            {
                throw new ScriptRuntimeException("Too many arguments provided. Only a resource path is necessary.", null);
            }

            using (StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(parameters[0].ToString())))
                return(new ExternalScriptMethod(parameters[0]?.ToString(), parser.Parse(reader.ReadToEnd())));
        }
コード例 #13
0
        public async Task Deploy()
        {
            var dict = new Dictionary <PhoneModel, string>
            {
                { PhoneModel.Talkman, Path.Combine("Scripts", "950.txt") },
                { PhoneModel.Cityman, Path.Combine("Scripts", "950xl.txt") },
            };

            var phoneModel = await phone.GetModel();

            var path = dict[phoneModel];

            await scriptRunner.Run(parser.Parse(File.ReadAllText(path)));
        }
コード例 #14
0
        public async Task Deploy()
        {
            var dict = new Dictionary <PhoneModel, string>
            {
                { PhoneModel.Talkman, Path.Combine("Scripts", "950.txt") },
                { PhoneModel.Cityman, Path.Combine("Scripts", "950xl.txt") },
            };

            var phoneModel = await phone.GetModel();

            Log.Verbose("{Model} detected", phoneModel);
            var path = dict[phoneModel];

            await scriptRunner.Run(parser.Parse(File.ReadAllText(path)));

            await PreparePhoneDiskForSafeRemoval();
        }
コード例 #15
0
        public IScript CompileCode(string code, ScriptLanguage language)
        {
            if (string.IsNullOrEmpty(code))
            {
                return(null);
            }

            switch (language)
            {
            case ScriptLanguage.NCScript:
                return(parser.Parse(code));

            case ScriptLanguage.JavaScript:
                return(new JavaScript(code, new ScriptImportService(null)));

            default:
                throw new ArgumentException($"Unsupported script language '{language}'");
            }
        }
コード例 #16
0
        /// <summary>
        /// loads a script from a file to provide an external method
        /// </summary>
        /// <param name="parameters">path to scriptfile to load and compile</param>
        /// <returns>compiled script as a external method</returns>
        public IExternalMethod Import(object[] parameters)
        {
            if (parameters.Length == 0)
            {
                throw new ScriptRuntimeException("A script file to import is necessary", null);
            }
            if (parameters.Length > 1)
            {
                throw new ScriptRuntimeException("Too many arguments provided. Only a filename is necessary.", null);
            }

            string fullpath = Path.Combine(Path.GetDirectoryName((Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly()).Location), parameters[0].ToString());

            if (!File.Exists(fullpath))
            {
                throw new FileNotFoundException("External script not found", fullpath);
            }

            return(new ExternalScriptMethod(parameters[0]?.ToString(), scriptparser.Parse(File.ReadAllText(fullpath))));
        }
コード例 #17
0
        public void AssignVariable()
        {
            IScript script = globalparser.Parse(
                "$number=7\n" +
                "$number"
                );

            Assert.AreEqual(7, script.Execute());
        }
コード例 #18
0
 public void UnterminatedForLoop()
 {
     Assert.Throws <ScriptParserException>(() => parser.Parse("for(i="));
 }
コード例 #19
0
        public async Task Deploy()
        {
            var scriptPath = Path.Combine("Scripts", "Deployment.txt");

            await scriptRunner.Run(parser.Parse(File.ReadAllText(scriptPath)));
        }
コード例 #20
0
 public void Equals(string data)
 {
     Assert.AreEqual(true, parser.Parse(data).Execute());
 }
コード例 #21
0
 public void ReturnHost()
 {
     Assert.DoesNotThrow(() => parser.Parse("return (host)"));
 }
コード例 #22
0
        public void CallMethodWithArrayParameters()
        {
            IScript script = parser.Parse("$test.methodwitharrayparameters(\"success\", [$test.parameter(\"n\", \"1\"),$test.parameter(\"m\", \"7\")])", new Variable("test", this));

            Assert.AreEqual("success:n=1,m=7", script.Execute());
        }
コード例 #23
0
        public void EnumerationParameterCall()
        {
            IScript script = parser.Parse("test.enumeration([\"hello\",\"world\"])");

            Assert.AreEqual("hello;world", script.Execute());
        }
コード例 #24
0
        public void PutFloatingPointToIntegerType()
        {
            IScript script = parser.Parse("parameter($input, \"int\") return($input)");

            Assert.AreEqual(12, script.Execute(new Variable("input", 12.23)));
        }
コード例 #25
0
 public void BitwiseAnd()
 {
     Assert.AreEqual(27 & 13, parser.Parse("27&13").Execute());
 }
コード例 #26
0
        public void InvalidStringOperations(string data)
        {
            IScript script = parser.Parse(data);

            Assert.Throws <ScriptRuntimeException>(() => script.Execute());
        }
コード例 #27
0
 public void Decimal()
 {
     Assert.AreEqual(722m, parser.Parse("decimal(\"722\")").Execute());
 }
コード例 #28
0
        public void CompareWithNegativeNumber()
        {
            IScript script = parser.Parse("-1==-1");

            Assert.AreEqual(true, script.Execute());
        }