Пример #1
0
        private HtmlTagInstruction PopulateHtmlTag(ScopePrototype scope, HtmlTag htmlTag)
        {
            var htmlTagInstruction = new HtmlTagInstruction(scope, htmlTag);

            PopulateInstructions(htmlTag.Instructions, htmlTagInstruction.Block, scope);
            return(htmlTagInstruction);
        }
Пример #2
0
        public void StringValueWithValueOfScopeWithStringOnly_BuildString_StringBuildCorrectly()
        {
            // prepare
            var scope_prototype = new ScopePrototype
            {
                Variables = new HashSet <string> {
                    "model "
                },
            };
            var scope = new Scope(scope_prototype)
            {
                VariableValues = new Dictionary <string, AssignedValue> {
                    { "model", new AssignedValue(JToken.Parse("\"black\"")) }
                }
            };

            var stringValue = new StringValue
            {
                StringComponents = new List <IStringComponent> {
                    new ValueOf {
                        VariableName = "model"
                    },
                }
            };

            // act
            var result = StringValueBuilder.Build(stringValue, scope);

            // validate
            Assert.Equal("black", result);
        }
Пример #3
0
        public void ScopeWithObjectWithArray_VariableIsCalled_CorrectValueIsReturned()
        {
            // prepare
            var scope_prototype = new ScopePrototype
            {
                Variables = new HashSet <string> {
                    "array"
                },
            };
            var scope = new Scope(scope_prototype)
            {
                VariableValues = new Dictionary <string, AssignedValue> {
                    { "array", new AssignedValue(JToken.Parse("{'object': [1,3,6]}")) }
                }
            };
            var valueOf = new ValueOf
            {
                VariableName = "array",
                NestedValue  = new ValueOf
                {
                    VariableName = "object",
                    Index        = 2
                }
            };

            // act
            var value = scope.FindValueOfValueOf(valueOf);

            // validate
            Assert.Equal("6", value);
        }
Пример #4
0
        private FunctionCallInstruction PopulateFunctionCallInstruction(ScopePrototype scope, FunctionCall functionCall)
        {
            // check if the function has been declared in the scope
            Block function_block;

            if (!_functions.TryGetValue(functionCall.FunctionName, out function_block))
            {
                throw new SemanticsException($"Function {functionCall.FunctionName} has not been declared in the scope of the function {scope.FunctionName}.");
            }

            // check if called arguments exist in the scope
            foreach (var argument in functionCall.ArgumentValues)
            {
                if (argument.GetType() == typeof(ValueOf))
                {
                    if (!scope.Variables.Contains(((ValueOf)argument).VariableName))
                    {
                        throw new SemanticsException($"Variable {((ValueOf) argument).VariableName} has not been declared in the scope of the function {scope.FunctionName}.");
                    }
                }
            }

            var funCallInstruction = new FunctionCallInstruction(scope, functionCall);

            return(funCallInstruction);
        }
Пример #5
0
        public void ScopeWithVariable_VariableIsCalled_CorrectValueIsReturned()
        {
            // prepare
            var scope_prototype = new ScopePrototype
            {
                Variables = new HashSet <string> {
                    "arg"
                },
            };
            var scope = new Scope(scope_prototype)
            {
                VariableValues = new Dictionary <string, AssignedValue> {
                    { "arg", new AssignedValue(JToken.Parse("'value'")) }
                }
            };
            var valueOf = new ValueOf
            {
                VariableName = "arg"
            };

            // act
            var value = scope.FindValueOfValueOf(valueOf);

            // validate
            Assert.Equal("value", value);
        }
Пример #6
0
        public void DefinedFunctionWithValueOfCall_CallFunctionWithArguments_ScopeInitializedCorrectly()
        {
            // prepare
            var variables = new List <string> {
                "arg1"
            };
            var functionBlock = new Block(null, variables, "fun");

            functionBlock.NestedBlocks.Add(new StringComponentInstruction(functionBlock.ScopePrototype, new ValueOf
            {
                VariableName = "arg1"
            }));
            var functionDictionary = new Dictionary <string, Block> {
                { "function", functionBlock }
            };
            var scope_prototype = new ScopePrototype
            {
                Variables = new HashSet <string> {
                    "model"
                },
            };
            var scope = new Scope(scope_prototype)
            {
                VariableValues = new Dictionary <string, AssignedValue> {
                    { "model", new AssignedValue(JToken.Parse("['value1', 'value2']")) }
                }
            };
            var functionCallInstruction = new FunctionCallInstruction(scope_prototype, new FunctionCall
            {
                FunctionName   = "function",
                ArgumentValues = new List <Value> {
                    new ValueOf {
                        VariableName = "model",
                        Index        = 1
                    }
                }
            });

            // act
            var memoryStream = new MemoryStream();
            var streamWriter = new StreamWriter(memoryStream);
            var node         = new Node
            {
                StreamWriter  = streamWriter,
                NewLine       = false,
                NestedLevel   = 0,
                Scope         = scope,
                FunctionsDict = functionDictionary
            };

            functionCallInstruction.Execute(node);
            streamWriter.Flush();

            // validate
            memoryStream.Position = 0;
            var streamReader = new StreamReader(memoryStream);

            //Assert.Equal("\nvalue2", streamReader.ReadToEnd());
            Assert.Equal("value2", streamReader.ReadToEnd());
        }
Пример #7
0
        public void DefinedFunction_CallFunctionWithArgumentsOutOfScope_ThrowRuntimeException()
        {
            // prepare
            var variables = new List <string> {
                "arg1"
            };
            var functionBlock      = new Block(null, variables, "fun");
            var functionDictionary = new Dictionary <string, Block> {
                { "function", functionBlock }
            };
            var scope_prototype = new ScopePrototype
            {
                Variables = new HashSet <string> {
                    "model"
                },
            };
            var scope = new Scope(scope_prototype)
            {
                VariableValues = new Dictionary <string, AssignedValue> {
                    { "model", new AssignedValue(JToken.Parse("['value1', 'value2']")) }
                }
            };
            var functionCallInstruction = new FunctionCallInstruction(scope_prototype, new FunctionCall
            {
                FunctionName   = "function",
                ArgumentValues = new List <Value> {
                    new ValueOf {
                        VariableName = "model",
                        Index        = 2
                    }
                }
            });

            // act
            var streamWriter       = new Mock <StreamWriter>(new MemoryStream());
            var exceptionWasThrown = false;
            var node = new Node
            {
                StreamWriter  = streamWriter.Object,
                NewLine       = false,
                NestedLevel   = 0,
                Scope         = scope,
                FunctionsDict = functionDictionary
            };

            try
            {
                functionCallInstruction.Execute(node);
            }
            catch (RuntimeException)
            {
                exceptionWasThrown = true;
            }
            // validate
            Assert.True(exceptionWasThrown);
        }
Пример #8
0
        private IfInstruction PopulateIfCondition(ScopePrototype scope, IfExpression ifExpression, ElseExpression elseExpression)
        {
            var ifInstruction = new IfInstruction(scope, ifExpression);

            PopulateInstructions(ifExpression.Instructions, ifInstruction.IfBlock, scope);
            if (elseExpression != null)
            {
                PopulateInstructions(elseExpression.Instructions, ifInstruction.ElseBlock, scope);
            }
            return(ifInstruction);
        }
Пример #9
0
        private StringComponentInstruction PopulateValueOf(ScopePrototype scope, ValueOf valueOf)
        {
            // check if value has been declared in the scope
            if (!scope.Variables.Contains(valueOf.VariableName))
            {
                throw new SemanticsException($"Variable {valueOf.VariableName} has not been declared in the scope of function {scope.FunctionName}.");
            }

            var valueOfInstruction = new StringComponentInstruction(scope, valueOf);

            return(valueOfInstruction);
        }
Пример #10
0
        public void DefinedFunctionWithValueOfCallWithSimpleValue_CallFunctionWithArguments_ScopeInitializedCorrectly()
        {
            // prepare
            var variables = new List <string> {
                "arg1"
            };
            var functionBlock = new Block(null, variables, "fun");

            functionBlock.NestedBlocks.Add(new StringComponentInstruction(functionBlock.ScopePrototype, new ValueOf
            {
                VariableName = "arg1"
            }));
            var functionDictionary = new Dictionary <string, Block> {
                { "function", functionBlock }
            };
            var scope_prototype = new ScopePrototype
            {
                Variables = new HashSet <string> {
                    "model"
                },
            };
            var scope = new Scope(scope_prototype)
            {
                VariableValues = new Dictionary <string, AssignedValue> {
                    { "model", new AssignedValue(JToken.Parse("'value2'")) }
                }
            };
            var functionCallInstruction = new FunctionCallInstruction(scope_prototype, new FunctionCall
            {
                FunctionName   = "function",
                ArgumentValues = new List <Value> {
                    new ValueOf {
                        VariableName = "model"
                    }
                }
            });

            // act
            var streamWriter = new Mock <StreamWriter>(new MemoryStream());
            var node         = new Node
            {
                StreamWriter  = streamWriter.Object,
                NewLine       = false,
                NestedLevel   = 0,
                Scope         = scope,
                FunctionsDict = functionDictionary
            };

            functionCallInstruction.Execute(node);

            // validate
            streamWriter.Verify(s => s.Write("value2"), Times.Once);
        }
Пример #11
0
        private ForInstruction PopulateForInstruction(ScopePrototype scope, ForExpression forExpression)
        {
            // check if the value to be iterated over exists in the scope
            if (!scope.Variables.Contains(forExpression.Collection.VariableName))
            {
                throw new SemanticsException($"Variable {forExpression.Collection.VariableName} has not been declared in the scope of operation for in function {scope.FunctionName}.");
            }
            var forInstruction = new ForInstruction(scope, forExpression);

            PopulateInstructions(forExpression.Instructions, forInstruction.Block.NestedBlocks, forInstruction.Block.ScopePrototype);
            return(forInstruction);
        }
Пример #12
0
        public void DefinedForInstruction_CallInsideScopeWithArrayOfObjects_NestedScopesInitializedCorrectly()
        {
            // prepare
            var outer_scope_prototype = new ScopePrototype {
                Variables = new HashSet <string> {
                    "model"
                },
            };
            var outer_scope = new Scope(outer_scope_prototype)
            {
                VariableValues = new Dictionary <string, AssignedValue> {
                    { "model", new AssignedValue(JToken.Parse("{'field1':[{'prop': 2},{'prop': 5},{'prop':8}],'field2':'val2'}")) }
                }
            };
            var forInstruction = new ForInstruction(outer_scope_prototype, new ForExpression {
                Collection = new ValueOf {
                    VariableName = "model",
                    NestedValue  = new ValueOf {
                        VariableName = "field1"
                    }
                },
                ElementName = "element"
            });

            forInstruction.Block.NestedBlocks.Add(new StringComponentInstruction(forInstruction.Block.ScopePrototype, new ValueOf {
                VariableName = "element",
                NestedValue  = new ValueOf {
                    VariableName = "prop"
                }
            }));

            // act
            var streamWriter = new Mock <StreamWriter>(new MemoryStream());
            var node         = new Node {
                StreamWriter = streamWriter.Object,
                NewLine      = false,
                NestedLevel  = 0,
                Scope        = outer_scope
            };

            forInstruction.Execute(node);

            // validate
            // Assert.Single(forInstruction.ScopePrototype.Variables);
            // Assert.True(forInstruction.Scope.VariableValues.ContainsKey("model"));
            // Assert.Single(forInstruction.Block.Scope.VariableValues);
            // Assert.Equal("8", forInstruction.Block.Scope.VariableValues["element"].);
            streamWriter.Verify(s => s.Write("2"), Times.Once);
            streamWriter.Verify(s => s.Write("5"), Times.Once);
            streamWriter.Verify(s => s.Write("8"), Times.Once);
        }
Пример #13
0
        public void ScopeWithoutVariable_VariableIsCalled_RuntimeExceptionIsThrown()
        {
            // prepare
            var outer_scope_prototype = new ScopePrototype
            {
                Variables = new HashSet <string> {
                    "arg"
                },
            };
            var scope_prototype = new ScopePrototype
            {
                Variables = new HashSet <string> {
                    "arg2"
                },
                UpperScopePrototype = outer_scope_prototype
            };
            var scope = new Scope(scope_prototype)
            {
                UpperScope = new Scope(outer_scope_prototype)
                {
                    VariableValues = new Dictionary <string, AssignedValue> {
                        { "arg", new AssignedValue(JToken.Parse("'value'")) }
                    }
                },
                VariableValues = new Dictionary <string, AssignedValue> {
                    { "arg2", new AssignedValue(JToken.Parse("'value2'")) }
                }
            };
            var valueOf = new ValueOf
            {
                VariableName = "arg3"
            };

            // act
            var exceptionWasThrown = false;

            try
            {
                scope.FindValueOfValueOf(valueOf);
            }
            catch (RuntimeException)
            {
                exceptionWasThrown = true;
            }

            // validate
            Assert.True(exceptionWasThrown);
        }
Пример #14
0
        public void StringValueWithValueOfAndStringsAlternating_BuildString_StringBuildCorrectly()
        {
            // prepare
            var scope_prototype = new ScopePrototype
            {
                Variables = new HashSet <string> {
                    "model "
                },
            };
            var scope = new Scope(scope_prototype)
            {
                VariableValues = new Dictionary <string, AssignedValue> {
                    { "model", new AssignedValue(JToken.Parse("{'val1': 'aaa', 'val2': 'bbb'}")) }
                }
            };

            var stringValue = new StringValue
            {
                StringComponents = new List <IStringComponent> {
                    new Literal {
                        Content = "literal_"
                    },
                    new ValueOf {
                        VariableName = "model",
                        NestedValue  = new ValueOf {
                            VariableName = "val1"
                        }
                    },
                    new ValueOf {
                        VariableName = "model",
                        NestedValue  = new ValueOf {
                            VariableName = "val2"
                        }
                    },
                    new Literal {
                        Content = "_end"
                    }
                }
            };

            // act
            var result = StringValueBuilder.Build(stringValue, scope);

            // validate
            Assert.Equal("literal_aaabbb_end", result);
        }
Пример #15
0
        public void DefinedFunctionWithRecursiveFunctionCall_CallFunctionWithArguments_TwoDifferentScopesUsed()
        {
            // prepare
            var scope_prototype = new ScopePrototype
            {
                Variables = new HashSet <string> {
                    "arg1", "stopcondition"
                },
            };
            var variables = new List <string> {
                "arg1", "stopcondition"
            };
            var functionBlock = new Block(null, variables, "function");

            functionBlock.NestedBlocks.Add(new StringComponentInstruction(functionBlock.ScopePrototype, new ValueOf
            {
                VariableName = "arg1"
            }));
            functionBlock.NestedBlocks.Add(new IfInstruction(scope_prototype, new IfExpression
            {
                Condition = new SimpleCondition
                {
                    LeftHandSideVariable = new ValueOf
                    {
                        VariableName = "stopcondition"
                    }
                },
                Negated = true
            })
            {
                IfBlock = new List <Executable> {
                    new FunctionCallInstruction(scope_prototype, new FunctionCall {
                        FunctionName   = "function",
                        ArgumentValues = new List <Value> {
                            new StringValue {
                                StringComponents = new List <IStringComponent> {
                                    new Literal {
                                        Content = "another_value"
                                    }
                                }
                            },
                            new StringValue {
                                StringComponents = new List <IStringComponent> {
                                    new Literal {
                                        Content = "true"
                                    }
                                }
                            },
                        }
                    })
                }
            });
            functionBlock.NestedBlocks.Add(new StringComponentInstruction(functionBlock.ScopePrototype, new ValueOf
            {
                VariableName = "arg1"
            }));
            var functionDictionary = new Dictionary <string, Block> {
                { "function", functionBlock }
            };
            var scope = new Scope(scope_prototype)
            {
                VariableValues = new Dictionary <string, AssignedValue> {
                    { "arg1", new AssignedValue(JToken.Parse("'value2'")) }
                }
            };
            var functionCallInstruction = new FunctionCallInstruction(scope_prototype, new FunctionCall
            {
                FunctionName   = "function",
                ArgumentValues = new List <Value> {
                    new ValueOf {
                        VariableName = "arg1"
                    },
                    new StringValue {
                        StringComponents = new List <IStringComponent> {
                            new Literal {
                                Content = "false"
                            }
                        }
                    }
                }
            });

            // act
            var streamWriter = new Mock <StreamWriter>(new MemoryStream());
            var node         = new Node
            {
                StreamWriter  = streamWriter.Object,
                NewLine       = false,
                NestedLevel   = 0,
                Scope         = scope,
                FunctionsDict = functionDictionary
            };

            functionCallInstruction.Execute(node);

            // validate
            streamWriter.Verify(s => s.Write("value2"), Times.Exactly(2));
            streamWriter.Verify(s => s.Write("another_value"), Times.Exactly(2));
        }
Пример #16
0
        private HtmlInlineTagInstruction PopulateHtmlInlineTag(ScopePrototype scope, HtmlInlineTag htmlInlineTag)
        {
            var htmlInlineInstruction = new HtmlInlineTagInstruction(scope, htmlInlineTag);

            return(htmlInlineInstruction);
        }
Пример #17
0
        private StringComponentInstruction PopulateLiteral(ScopePrototype scope, Literal literal)
        {
            var literalInstruction = new StringComponentInstruction(scope, literal);

            return(literalInstruction);
        }
Пример #18
0
        private void PopulateInstructions(List <IInstruction> instructions, List <Executable> block, ScopePrototype scope)
        {
            IfExpression encounteredIfExpression = null;

            foreach (var instruction in instructions)
            {
                if (encounteredIfExpression != null)
                {
                    if (instruction.GetType() == typeof(ElseExpression))
                    {
                        block.Add(PopulateIfCondition(scope, encounteredIfExpression, (ElseExpression)instruction));
                        encounteredIfExpression = null;
                        continue;
                    }
                    else
                    {
                        block.Add(PopulateIfCondition(scope, encounteredIfExpression, null));
                        encounteredIfExpression = null;
                    }
                }
                if (instruction.GetType() == typeof(IfExpression))
                {
                    encounteredIfExpression = (IfExpression)instruction;
                }
                else if (instruction.GetType() == typeof(ForExpression))
                {
                    block.Add(PopulateForInstruction(scope, (ForExpression)instruction));
                }
                else if (instruction.GetType() == typeof(ElseExpression))
                {
                    throw new SemanticsException("Else instruction must be preceeded by the if block");
                }
                else if (instruction.GetType() == typeof(FunctionCall))
                {
                    block.Add(PopulateFunctionCallInstruction(scope, (FunctionCall)instruction));
                }
                else if (instruction.GetType() == typeof(HtmlInlineTag))
                {
                    block.Add(PopulateHtmlInlineTag(scope, (HtmlInlineTag)instruction));
                }
                else if (instruction.GetType() == typeof(HtmlTag))
                {
                    block.Add(PopulateHtmlTag(scope, (HtmlTag)instruction));
                }
                else if (instruction.GetType() == typeof(Literal))
                {
                    block.Add(PopulateLiteral(scope, (Literal)instruction));
                }
                else if (instruction.GetType() == typeof(ValueOf))
                {
                    block.Add(PopulateValueOf(scope, (ValueOf)instruction));
                }
                else
                {
                    throw new SemanticsException($"Unknown instruction type: ${instruction.GetType()}");
                }
            }
            if (encounteredIfExpression != null)
            {
                block.Add(PopulateIfCondition(scope, encounteredIfExpression, null));
            }
        }