public void EmptyArray() { IScript script = parser.Parse("[]"); Array array = script.Execute <Array>(); Assert.NotNull(array); Assert.AreEqual(0, array.Length); }
public void CreateSingleParameterLeavingOutDefault() { IScript script = parser.Parse( "$var=new variable(\"test\")\n" + "$var.name" ); Assert.AreEqual("test", script.Execute()); }
public void SingleLineComment() { IScript script = parser.Parse( "// this is a comment\n" + "$result=4" ); Assert.AreEqual(4, script.Execute()); }
public void DisposeSingle() { IScript script = parser.Parse( "$data=new disposable()\n" + "using($data)\n" + "\"weird statement\"\n" + "$data.disposed" ); Assert.AreEqual(true, script.Execute()); }
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()); }
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>())); }
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); } }
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); }
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))); }
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); }
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); }
/// <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()))); }
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))); }
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(); }
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}'"); } }
/// <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)))); }
public void AssignVariable() { IScript script = globalparser.Parse( "$number=7\n" + "$number" ); Assert.AreEqual(7, script.Execute()); }
public void UnterminatedForLoop() { Assert.Throws <ScriptParserException>(() => parser.Parse("for(i=")); }
public async Task Deploy() { var scriptPath = Path.Combine("Scripts", "Deployment.txt"); await scriptRunner.Run(parser.Parse(File.ReadAllText(scriptPath))); }
public void Equals(string data) { Assert.AreEqual(true, parser.Parse(data).Execute()); }
public void ReturnHost() { Assert.DoesNotThrow(() => parser.Parse("return (host)")); }
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()); }
public void EnumerationParameterCall() { IScript script = parser.Parse("test.enumeration([\"hello\",\"world\"])"); Assert.AreEqual("hello;world", script.Execute()); }
public void PutFloatingPointToIntegerType() { IScript script = parser.Parse("parameter($input, \"int\") return($input)"); Assert.AreEqual(12, script.Execute(new Variable("input", 12.23))); }
public void BitwiseAnd() { Assert.AreEqual(27 & 13, parser.Parse("27&13").Execute()); }
public void InvalidStringOperations(string data) { IScript script = parser.Parse(data); Assert.Throws <ScriptRuntimeException>(() => script.Execute()); }
public void Decimal() { Assert.AreEqual(722m, parser.Parse("decimal(\"722\")").Execute()); }
public void CompareWithNegativeNumber() { IScript script = parser.Parse("-1==-1"); Assert.AreEqual(true, script.Execute()); }