Example #1
0
        private void WriteHeaderFileContent(TextWriter writer, string suitePath)
        {
            Stream stream =
                Assembly.GetExecutingAssembly().GetManifestResourceStream(
                    NewTestSuiteTemplateResourcePath);

            StringTemplateGroup templateGroup = new StringTemplateGroup(
                new StreamReader(stream), typeof(AngleBracketTemplateLexer));

            templateGroup.RegisterAttributeRenderer(typeof(string),
                                                    new NewTestSuiteStringRenderer(suitePath));

            StringTemplate template =
                templateGroup.GetInstanceOf("newSuiteFile");

            // Initialize the options that will be passed into the template.

            Hashtable options = new Hashtable();

            options["suiteName"]       = suiteName;
            options["superclass"]      = superclass;
            options["headerUnderTest"] = headerUnderTest.CanonicalName;
            options["createSetUp"]     = createSetUp;
            options["createTearDown"]  = createTearDown;

            template.SetAttribute("options", options);
            template.SetAttribute("testCases", new List <string>(stubNames));

            template.Write(new AutoIndentWriter(writer));
        }
Example #2
0
        protected virtual int WriteSubTemplate(StringTemplate self,
                                               IStringTemplateWriter @out,
                                               StringTemplate subtemplate)
        {
            /* To evaluate the IF chunk, make a new instance whose enclosingInstance
             * points at 'self' so get attribute works.  Otherwise, enclosingInstance
             * points at the template used to make the precompiled code.  We need a
             * new template instance every time we exec this chunk to get the new
             * "enclosing instance" pointer.
             */
            StringTemplate s = subtemplate.GetInstanceOf();

            s.EnclosingInstance = self;
            // make sure we evaluate in context of enclosing template's
            // group so polymorphism works. :)
            s.Group       = self.Group;
            s.NativeGroup = self.NativeGroup;
            return(s.Write(@out));
        }
Example #3
0
        /** <summary>
         *  To write out the value of a condition expr, invoke the evaluator in eval.g
         *  to walk the condition tree computing the boolean value.  If result
         *  is true, then write subtemplate.
         *  </summary>
         */
        public override int Write(StringTemplate self, IStringTemplateWriter @out)
        {
            if (AST == null || self == null || @out == null)
            {
                return(0);
            }
            //System.Console.Out.WriteLine( "evaluating conditional tree: " + AST.ToStringTree() );
#if !COMPILE_EXPRESSIONS
            ActionEvaluator eval = null;
#endif
            int n = 0;
            try
            {
                bool testedTrue = false;
                // get conditional from tree and compute result
#if COMPILE_EXPRESSIONS
                if (EvaluateCondition == null)
                {
                    EvaluateCondition = GetEvaluator(this, AST.GetChild(0));
                }
                bool includeSubtemplate = EvaluateCondition(self, @out);   // eval and write out tree
#else
                ITree cond = AST.GetChild(0);
                eval = new ActionEvaluator(self, this, @out, cond);
                // eval and write out trees
                bool includeSubtemplate = eval.ifCondition();
#endif
                //System.Console.Out.WriteLine( "subtemplate " + _subtemplate );
                // IF
                if (includeSubtemplate)
                {
                    n          = WriteSubTemplate(self, @out, _subtemplate);
                    testedTrue = true;
                }
                // ELSEIF
                else if (_elseIfSubtemplates != null && _elseIfSubtemplates.Count > 0)
                {
                    for (int i = 0; i < _elseIfSubtemplates.Count; i++)
                    {
                        ElseIfClauseData elseIfClause = _elseIfSubtemplates[i];
#if COMPILE_EXPRESSIONS
                        if (elseIfClause.EvaluateCondition == null)
                        {
                            elseIfClause.EvaluateCondition = GetEvaluator(this, elseIfClause.expr.AST);
                        }
                        includeSubtemplate = elseIfClause.EvaluateCondition(self, @out);
#else
                        eval = new ActionEvaluator(self, this, @out, elseIfClause.expr.AST);
                        includeSubtemplate = eval.ifCondition();
#endif
                        if (includeSubtemplate)
                        {
                            WriteSubTemplate(self, @out, elseIfClause.st);
                            testedTrue = true;
                            break;
                        }
                    }
                }
                // ELSE
                if (!testedTrue && _elseSubtemplate != null)
                {
                    // evaluate ELSE clause if present and IF condition failed
                    StringTemplate s = _elseSubtemplate.GetInstanceOf();
                    s.EnclosingInstance = self;
                    s.Group             = self.Group;
                    s.NativeGroup       = self.NativeGroup;
                    n = s.Write(@out);
                }
                // cond==false and no else => Missing output not empty
                if (!testedTrue && _elseSubtemplate == null)
                {
                    n = Missing;
                }
            }
            catch (RecognitionException re)
            {
                self.Error("can't evaluate tree: " + AST.ToStringTree(), re);
            }
            return(n);
        }
 public void TestNewlineNormalizationInTemplateStringPC()
 {
     StringTemplate st = new StringTemplate(
             "Foo\r\n" +
             "Bar\n",
             typeof( AngleBracketTemplateLexer )
             );
     StringWriter sw = new StringWriter();
     st.Write( new AutoIndentWriter( sw, "\r\n" ) ); // force \r\n as newline
     string result = sw.ToString();
     string expecting = "Foo\r\nBar\r\n";     // expect \r\n in output
     Assert.AreEqual( expecting, result );
 }
        public void TestCharLiterals()
        {
            StringTemplate st = new StringTemplate(
                    "Foo <\\r\\n><\\n><\\t> bar" + newline,
                    typeof( AngleBracketTemplateLexer )
                    );
            StringWriter sw = new StringWriter();
            st.Write( new AutoIndentWriter( sw, "\n" ) ); // force \n as newline
            string result = sw.ToString();
            string expecting = "Foo \n\n\t bar\n";     // expect \n in output
            Assert.AreEqual( expecting, result );

            st = new StringTemplate(
                    "Foo $\\n$$\\t$ bar" + newline );
            sw = new StringWriter();
            st.Write( new AutoIndentWriter( sw, "\n" ) ); // force \n as newline
            expecting = "Foo \n\t bar\n";     // expect \n in output
            result = sw.ToString();
            Assert.AreEqual( expecting, result );

            st = new StringTemplate(
                    "Foo$\\ $bar$\\n$" );
            sw = new StringWriter();
            st.Write( new AutoIndentWriter( sw, "\n" ) ); // force \n as newline
            result = sw.ToString();
            expecting = "Foo bar\n"; // force \n
            Assert.AreEqual( expecting, result );
        }
 public void TestNewlineNormalizationInAttribute()
 {
     StringTemplate st = new StringTemplate(
             "Foo\r\n" +
             "<name>\n",
             typeof( AngleBracketTemplateLexer )
             );
     st.SetAttribute( "name", "a\nb\r\nc" );
     StringWriter sw = new StringWriter();
     st.Write( new AutoIndentWriter( sw, "\n" ) ); // force \n as newline
     string result = sw.ToString();
     string expecting = "Foo\na\nb\nc\n";     // expect \n in output
     Assert.AreEqual( expecting, result );
 }
 public void TestLineBreakNoWhiteSpaceDollar()
 {
     StringTemplate st = new StringTemplate(
             "Foo $\\\\$" + newline +
             "bar" + newline,
             typeof(DefaultTemplateLexer)
             );
     StringWriter sw = new StringWriter();
     st.Write(new AutoIndentWriter(sw, "\n")); // force \n as newline
     string result = sw.ToString();
     string expecting = "Foo bar\n";     // expect \n in output
     Assert.AreEqual(expecting, result);
 }
 public void TestLineBreak2()
 {
     StringTemplate st = new StringTemplate(
             "Foo <\\\\>       " + newline +
             "  \t  bar" + newline,
             typeof(AngleBracketTemplateLexer)
             );
     StringWriter sw = new StringWriter();
     st.Write(new AutoIndentWriter(sw, "\n")); // force \n as newline
     string result = sw.ToString();
     string expecting = "Foo bar\n";     // expect \n in output
     Assert.AreEqual(expecting, result);
 }
 public void TestAlternativeWriter()
 {
     StringBuilder buf = new StringBuilder();
     IStringTemplateWriter w = new AlternativeWriter( buf );
     StringTemplateGroup group =
             new StringTemplateGroup( "test" );
     group.DefineTemplate( "bold", "<b>$x$</b>" );
     StringTemplate name = new StringTemplate( group, "$name:bold(x=name)$" );
     name.SetAttribute( "name", "Terence" );
     name.Write( w );
     Assert.AreEqual( "<b>Terence</b>", buf.ToString() );
 }
Example #10
0
        protected virtual int WriteTemplate(StringTemplate self, StringTemplate stToWrite, IStringTemplateWriter @out)
        {
            int n = 0;
            /* failsafe: perhaps enclosing instance not set
             * Or, it could be set to another context!  This occurs
             * when you store a template instance as an attribute of more
             * than one template (like both a header file and C file when
             * generating C code).  It must execute within the context of
             * the enclosing template.
             */
            stToWrite.EnclosingInstance = self;
            // if self is found up the enclosing instance chain, then infinite recursion
            if (StringTemplate.LintMode && StringTemplate.IsRecursiveEnclosingInstance(stToWrite))
            {
                // throw exception since sometimes eval keeps going even after I ignore this write of o.
                throw new InvalidOperationException("infinite recursion to " +
                        stToWrite.GetTemplateDeclaratorString() + " referenced in " +
                        stToWrite.EnclosingInstance.TemplateDeclaratorString +
                        "; stack trace:" + Environment.NewLine + stToWrite.GetEnclosingInstanceStackTrace());
            }
            else
            {
                // if we have a wrap string, then inform writer it might need to wrap
                if (_wrapString != null)
                {
                    n = @out.WriteWrapSeparator(_wrapString);
                }

                // check if formatting needs to be applied to the stToWrite
                if (_formatString != null)
                {
                    IAttributeRenderer renderer = self.GetAttributeRenderer(typeof(string));
                    if (renderer != null)
                    {
                        /* you pay a penalty for applying format option to a template
                         * because the template must be written to a temp StringWriter so it can
                         * be formatted before being written to the real output.
                         */
                        StringWriter buf = new StringWriter();
                        IStringTemplateWriter sw = self.Group.GetStringTemplateWriter(buf);
                        stToWrite.Write(sw);
                        n = @out.Write(renderer.ToString(buf.ToString(), _formatString));
                        return n;
                    }
                }

                n = stToWrite.Write(@out);
            }

            return n;
        }