public void Add_AddsCorrectly() { //arrange object objectToAdd = new object(); var expected = objectToAdd.GetHashCode(); OList <object> ListToTest = new OList <object>(); //act ListToTest.Add(objectToAdd); //assert var actual = ListToTest.GetItem(item => item.GetHashCode() == expected); Assert.AreEqual(expected, actual, "Item not added or retrieved correctly"); }
/// <summary> /// Business logic for calculate weather type /// </summary> /// <param name="weatherData">Weather data from openweathermap </param> /// <returns>Weather type</returns> private WeatherTypes MapWeatherType(OList weatherData) { if (weatherData.Weather[0].Main.ToLower().Contains("rain")) { return(WeatherTypes.Rainy); } if (weatherData.Weather[0].Main.ToLower().Contains("snow")) { return(WeatherTypes.Snowy); } if (weatherData.Clouds.All > 70) { return(WeatherTypes.Cloudy); } if (weatherData.Wind.Speed > 8) { return(WeatherTypes.Windy); } return(WeatherTypes.Sunny); }
public void CompileAction(ParserAction action, OList <ParserAction> actions) { if (action.Consumed) { return; } switch (action.Token) { case ParserToken.Import: { var expr = (ImportExpression)action.Related.Single(); var type = expr.Type; var alias = expr.As; if (File.Exists(type)) { Assembly.LoadFile(type); Debug.WriteLine($"{type} was loaded successfully."); break; } Type foundtype; foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) { foundtype = asm.GetType(type); if (foundtype == null) { continue; } goto _found; } throw new ExpressionCompileException($"Unable to find type: {type}"); _found: Debug.WriteLine($"{type} was loaded successfully."); if (alias != null) { Context.Imports.AddType(foundtype, alias); } else { Context.Imports.AddType(foundtype); } break; } case ParserToken.Declaration: { var expr = (VariableDeclarationExpression)action.Related.Single(); var name = expr.Name.AsString(); //validate name { if (InterpreterOptions.BuiltinKeywords.Any(w => w.Equals(name, StringComparison.Ordinal))) { throw new ExpressionCompileException($"Variable named '{name}' is taken by the interpreter."); } } var right = expr.Right; var evaluation = EvaluateExpression(right); Context.Variables[name] = Data.Create(evaluation); break; } case ParserToken.Expression: { var line = action.RelatedLines.Single(); if (line.Metadata.Contains("ParserToken.Expression")) { break; } line.Metadata.Add("ParserToken.Expression"); line.MarkedForDeletion = false; //they are all true by default, well all lines that were found relevant to ParserAction var copy = line.Content; var ew = new ExpressionWalker(ExpressionLexer.Tokenize(copy)); var vars = Context.Variables; bool changed = false; int last_access_index = 0; //we reparse the line and handle all expressions. if (ew.HasNext) { do { _restart: if (changed) { changed = false; var cleanedCopy = new string(' ', last_access_index) + copy.Substring(last_access_index); ew = new ExpressionWalker(ExpressionLexer.Tokenize(cleanedCopy)); if (ew.Count == 0) { break; } } var current = ew.Current; //iterate all tokens of that line if (current.Token != ExpressionToken.Mod || !ew.HasNext) { continue; } var mod = ew.Current; current = ew.NextToken(); switch (current.Token) { case ExpressionToken.LeftParen: { //it is an expression. ew.NextOrThrow(); var expression = Expression.ParseExpression(ew); object val = EvaluateObject(expression, line); if (val is ReferenceData rd) //make sure references are unpacked { val = rd.UnpackReference(Context); } ew.IsCurrentOrThrow(ExpressionToken.RightParen); var emit = val is Data d?d.Emit() : val.ToString(); copy = copy .Remove(mod.Match.Index, ew.Current.Match.Index + 1 - mod.Match.Index) .Insert(mod.Match.Index, emit); last_access_index = mod.Match.Index + emit.Length; changed = true; goto _restart; } default: continue; } } while (ew.Next()); } line.Replace(copy + (copy.EndsWith("\n") ? "" : "\n")); break; } case ParserToken.ForeachLoop: { _compileForeach(action); break; } case ParserToken.Template: break; default: throw new ArgumentOutOfRangeException(); } }
public static ParsedCode Parse(string code, Dictionary <string, object> variables = null, InterpreterOptions opts = null) { code = code.Replace("\r", ""); //todo this might cause throws in osx. StringSpan output_sb; var output = new LineBuilder(output_sb = StringSpan.Create(code)); variables = variables ?? new Dictionary <string, object>(); opts = opts ?? new InterpreterOptions(); // Define the context of our expression var ew = new ExpressionWalker(ExpressionLexer.Tokenize(code).Where(t => t.Token != ExpressionToken.UnixNewLine).ToList()); //if no tokens detected if (ew.Count == 0) { return(new ParsedCode() { OriginalCode = code, Output = output, Variables = variables, Options = opts, ParseActions = new OList <ParserAction>(0) }); } var parserTokens = new OList <ParserAction>(); do { var current = ew.Current; switch (ew.Current.Token) { case ExpressionToken.Mod: { //copypastes.Add(sb.Substring(from, ew.Current.Match.Index + ew.Current.Match.Length - 1)); current = ew.NextToken(); if (current == null) { break; } switch (current.Token) { case ExpressionToken.Template: { //this is import %import namespace.type as aliasnmae var template = TemplateExpression.Parse(ew); parserTokens += new ParserAction(ParserToken.Template, output.MarkDeleteLinesRelated(template.Matches()), template); break; } case ExpressionToken.Import: { //this is import %import namespace.type as aliasnmae var import = ImportExpression.Parse(ew); parserTokens += new ParserAction(ParserToken.Import, output.MarkDeleteLinesRelated(import.Matches()), import); break; } case ExpressionToken.Literal: { //this is variable declaration %varname = expr var peak = ew.PeakNext.Token; if (peak == ExpressionToken.Equal) { var expr = VariableDeclarationExpression.Parse(ew); parserTokens += new ParserAction(ParserToken.Declaration, output.MarkDeleteLinesRelated(expr.Matches()), expr); } else { break; } break; } case ExpressionToken.LeftParen: { //it is an expression block %(expr) ew.NextOrThrow(); var expr = Expression.ParseExpression(ew); parserTokens += new ParserAction(ParserToken.Expression, output.MarkDeleteLinesRelated(expr.Matches()), expr); ew.IsCurrentOrThrow(ExpressionToken.RightParen); ew.Next(); break; } case ExpressionToken.Foreach: { parserTokens += ForeachExpression.Parse(ew, code, output); break; } case ExpressionToken.CommentRow: //skip untill we hit newline ew.SkipForwardWhile(t => t.Token != ExpressionToken.NewLine); //todo test break; default: { var precentageLine = output.GetLineAt(ew.PeakBack.Match.Index); if (precentageLine.CleanContent() != "%") { throw new UnexpectedTokenException(current.Token, $"The given token was not expected at line {precentageLine.LineNumber}, offset: {current.Match.Index - precentageLine.StartIndex}"); } break; } } break; } default: break; } } while (ew.Next()); return(new ParsedCode() { OriginalCode = code, Output = output, Variables = variables, ETokens = (List <TokenMatch>)ew.Walking, ParseActions = parserTokens, Options = opts }); }