Ejemplo n.º 1
0
        public void CurrentLine()
        {
            FormattedCode data = Format(
                "line 0\r\nline 1\r\nline 2\r\nline 3\r\nline 4\r\nline 5\r\nline 6\r\nline 7\r\n", "");

            _box        = SetupCodeBox(data, new SizeF(200, 400));
            _box.Height = 30;

            // CurrentLine: 0

            _mockRenderer.LineIndexToYCoordinate(0, _box.RenderingContext.Graphics, _box.RenderingContext.Font).Returns(0f);

            _box.CurrentLine = 0;

            Assert.That(_box.CurrentLine, Is.EqualTo(0));
            Assert.That(_box.AutoScrollPosition, Is.EqualTo(new Point(0, 0)));

            // CurrentLine: 7

            _mockRenderer.LineIndexToYCoordinate(7, _box.RenderingContext.Graphics, _box.RenderingContext.Font).Returns(390f);

            _box.CurrentLine = 7;

            Assert.That(_box.CurrentLine, Is.EqualTo(7));
            Assert.That(_box.AutoScrollPosition, Is.EqualTo(new Point(0, -375)));

            return;
        }
Ejemplo n.º 2
0
        public PaintLineLocation[] ViewportLines(FormattedCode code, RectangleF viewport, float fontHeight)
        {
            List <PaintLineLocation> list = new List <PaintLineLocation>();
            int visibles = CountVisibleLines(viewport.Height, fontHeight);
//            int topIndex = YCoordinateToLineIndex(viewport.Top, fontHeight);
            int lineIndex = YCoordinateToLineIndex(viewport.Top, fontHeight);
            int i;

            for (i = 0; i < visibles; ++i, lineIndex++)
            {
                if (lineIndex < 0)
                {
                    continue;
                }

                if (lineIndex >= code.LineCount)
                {
                    break;
                }

                list.Add(
                    new PaintLineLocation(lineIndex, code.GetTextAt(lineIndex),
                                          new PointF(-viewport.Left,
                                                     LineIndexToYCoordinate(lineIndex, fontHeight) -
                                                     viewport.Top)));
            }

            return(list.ToArray());
        }
Ejemplo n.º 3
0
        public void Format_Pick_Best_Formatter()
        {
            ErrorItem      itemHelloTxt;
            ErrorItem      itemBasicCs;
            ICodeFormatter txtFormatter;
            ICodeFormatter csFormatter;
            FormattedCode  exp;

            using (TestResource resource = new TestResource("HelloWorld.txt"))
            {
                itemHelloTxt = new ErrorItem(resource.Path, 1);
                txtFormatter = new PlainTextCodeFormatter();
                exp          = txtFormatter.Format(itemHelloTxt.ReadFile());
                Assert.That(
                    _formatter.FormatFromExtension(itemHelloTxt.ReadFile(), itemHelloTxt.FileExtension),
                    Is.EqualTo(exp));
                FormattedCode.CheckData(exp);
            }

            using (TestResource resource = new TestResource("Basic.cs"))
            {
                itemBasicCs = new ErrorItem(resource.Path, 1);
                csFormatter = new CSharpCodeFormatter();
                exp         = csFormatter.Format(itemBasicCs.ReadFile());
                Assert.That(
                    _formatter.FormatFromExtension(itemBasicCs.ReadFile(), itemBasicCs.FileExtension),
                    Is.EqualTo(exp));
                FormattedCode.CheckData(exp);
            }

            return;
        }
Ejemplo n.º 4
0
        //[Test]
        //[ExpectedException(typeof(ArgumentNullException),
        //    ExpectedMessage = "code",
        //    MatchType = MessageMatch.Contains)]
        //public void FormatFromExtension_Can_Throw_CodeNullException()
        //{
        //    _formatter.FormatFromExtension(null, "cs"); // throws exception
        //}

        //[Test]
        //[ExpectedException(typeof(ArgumentNullException),
        //    ExpectedMessage = "extension",
        //    MatchType = MessageMatch.Contains)]
        //public void FormatFromExtension_Can_Throw_ExtensionNullException()
        //{
        //    _formatter.FormatFromExtension("test", null); // throws exception
        //}

        //[Test]
        //[ExpectedException(typeof(ArgumentNullException),
        //    ExpectedMessage = "code",
        //    MatchType = MessageMatch.Contains)]
        //public void Format_Can_Throw_CodeNullException()
        //{
        //    _formatter.Format(null, "C#"); // throws exception
        //}

        //[Test]
        //[ExpectedException(typeof(ArgumentNullException),
        //    ExpectedMessage = "language",
        //    MatchType = MessageMatch.Contains)]
        //public void Format_Can_Throw_LanguageNameNullException()
        //{
        //    _formatter.Format("test", null); // throws exception
        //}

        public void FormatResource(TestResource res)
        {
            ErrorItem             error;
            FormattedCode         code;
            List <ICodeFormatter> array = GetAllFormatters();

            foreach (ICodeFormatter item in array)
            {
                error = new ErrorItem(res.Path, 1);
                code  = item.Format(error.ReadFile());

                Assert.That(code, Is.Not.Null,
                            "Formatter: " + item + " failed to format resource.");

                try
                {
                    FormattedCode.CheckData(code);
                }
                catch (Exception e)
                {
                    Assert.Fail("Formatter: " + item + " has created an ill-formed data. Error: " + e.Message);
                }
            }

            return;
        }
Ejemplo n.º 5
0
        public void Test_Format_3()
        {
            FormattedCode exp;
            FormattedCode res;

            // Ensure that escaping sequences are
            // handled correctly
            //                    0  2           14   17    21
            res = _parser.Format("s=\"<font class=\\\"cls\\\">hi, there!</font>");

            exp = new FormattedCode(
                "s=\"<font class=\\\"cls\\\">hi, there!</font>",
                new int[] { 0, 2 },
                new byte[] { 0, 3 },
                new int[] { 0 });

            Assert.That(res, Is.EqualTo(exp));

            _parser = new TestingCSharpCodeFormatter();

            //                   0  2
            res = _parser.Format("s=\"<font class=\\\\\"cls\\\">hi, there!</font>");
            exp = new FormattedCode(
                "s=\"<font class=\\\\\"cls\\\">hi, there!</font>",
                new int[] { 0, 2, 18, 22 },
                new byte[] { 0, 3, 0, 3 },
                new int[] { 0 });

            Assert.That(res, Is.EqualTo(exp));

            return;
        }
Ejemplo n.º 6
0
 public void CheckData_LineArray_Values_Must_Always_Grow_Up()
 {
     FormattedCode.CheckData(
         new FormattedCode("hi\r\nthere\r\n",
                           new int[] { 0, 3 },
                           new byte[] { 0, 0 },
                           new int[] { 0, 0 })); // throws exception
 }
Ejemplo n.º 7
0
        TestingCodeBox SetupCodeBox(FormattedCode code, SizeF size)
        {
            TestingCodeBox box;

            box = new TestingCodeBox(_mockFormatter, _mockRenderer);

            _mockFormatter.Format(code.Text, "").Returns(code);
            _mockRenderer.GetDocumentSize(code, box.RenderingContext.Graphics, box.RenderingContext.Font).Returns(size);

            box.Text = code.Text;
            Assert.That(box.Text, Is.EqualTo(code.Text));

            return(box);
        }
Ejemplo n.º 8
0
        public void SetUp()
        {
            _mockFormatter = Substitute.For <IFormatterCatalog>();
            _mockRenderer  = Substitute.For <ICodeRenderer>();

            _box        = new TestingCodeBox(_mockFormatter, _mockRenderer);
            _box.Width  = 150;
            _box.Height = 150;

            _someText = Format("some C# code", "");
            _someCode = Format("some C# code", "C#");

            return;
        }
Ejemplo n.º 9
0
        public void SetUp()
        {
            _mockFormatter = new DynamicMock(typeof(IFormatterCatalog));
            _mockRenderer  = new DynamicMock(typeof(ICodeRenderer));

            _box = new TestingCodeBox((IFormatterCatalog)_mockFormatter.MockInstance,
                                      (ICodeRenderer)_mockRenderer.MockInstance);
            _box.Width  = 150;
            _box.Height = 150;

//            _emptyText = Format("", "");
            _someText = Format("some C# code", "");
            _someCode = Format("some C# code", "C#");

            return;
        }
Ejemplo n.º 10
0
        public void Format_HelloWorld()
        {
            FormattedCode res;
            FormattedCode exp;

            res = _formatter.Format("Hello world!");

            exp = new FormattedCode(
                "Hello world!",
                new int[] { 0 },
                new byte[] { 0 },
                new int[] { 0 });

            Assert.That(res, Is.EqualTo(exp));

            return;
        }
Ejemplo n.º 11
0
        public void Test_Format()
        {
            FormattedCode exp;
            FormattedCode res;

            res = _parser.Format("line 1\n  line 2\nline 3\n");

            exp = new FormattedCode(
                "line 1\n  line 2\nline 3\n",
                new int[] { 0, 7, 16 },
                new byte[] { 0, 0, 0 },
                new int[] { 0, 1, 2 }
                );

            Assert.That(res, Is.EqualTo(exp));

            return;
        }
Ejemplo n.º 12
0
        public void Test_Equals()
        {
            _code = new TestingCSCode(
                "line",
                new int[] { 0 },
                new byte[] { 0 },
                new int[] { 0 }
                );

            // Tests to fail

            Assert.That(_code.Equals(null), Is.False);
            Assert.That(_code.Equals("hello"), Is.False);
            Assert.That(_code.Equals(
                            new TestingCSCode("a", new int[] { 0 }, new byte[] { 0 }, new int[] { 0 })),
                        Is.False);
            Assert.That(_code.Equals(
                            new TestingCSCode("line", new int[] { 1 }, new byte[] { 0 }, new int[] { 0 })),
                        Is.False);
            Assert.That(_code.Equals(
                            new TestingCSCode("line", new int[] { 0 }, new byte[] { 1 }, new int[] { 0 })),
                        Is.False);
            Assert.That(_code.Equals(
                            new TestingCSCode("line", new int[] { 0 }, new byte[] { 0 }, new int[] { 1 })),
                        Is.False);

            Assert.That(_code.Equals(
                            new TestingCSCode("line", new int[] { 0, 0 }, new byte[] { 0 }, new int[] { 0 })),
                        Is.False);
            Assert.That(_code.Equals(
                            new TestingCSCode("line", new int[] { 0 }, new byte[] { 0, 0 }, new int[] { 0 })),
                        Is.False);
            Assert.That(_code.Equals(
                            new TestingCSCode("line", new int[] { 0 }, new byte[] { 0 }, new int[] { 0, 0 })),
                        Is.False);

            // NUnit.UiException.Tests to pass

            Assert.That(_code.Equals(
                            new TestingCSCode("line", new int[] { 0 }, new byte[] { 0 }, new int[] { 0 })),
                        Is.True);

            return;
        }
        public void SetUp()
        {
            _renderer = new DefaultCodeRenderer();

            ICodeFormatter formatter = new PlainTextCodeFormatter();

            _empty = formatter.Format("");

//            _code = formatter.Format(
//                "line 1\r\n" +
//                "line 2\r\n" +
//                "line 3\r\n" +
//                "line 4\r\n");

            _text3x7 = formatter.Format(
                "111\r\n" +
                "222\r\n" +
                "333\r\n" +
                "444\r\n" +
                "555\r\n" +
                "666\r\n" +
                "777\r\n");

            _loremIpsum = formatter.Format(
                "Lorem ipsum dolor sit\r\n" +
                "amet, consectetur adipiscing elit.\r\n" +
                "Maecenas a nisi. In imperdiet, orci in\r\n" +
                "porta facilisis,\r\n" +
                "odio purus iaculis est, non varius urna turpis in mi.\r\n" + // longest line
                "Nullam dictum. Ut iaculis dignissim nulla.\r\n" +
                "Nullam condimentum porttitor leo.\r\n" +
                "Integer a odio et\r\n" +
                "velit suscipit pulvinar.");

            Image img = new Bitmap(100, 100);

            _args          = new CodeRenderingContext();
            _args.Graphics = Graphics.FromImage(img);
            _args.Font     = new Font("Courier New", 12);

            return;
        }
Ejemplo n.º 14
0
        public SizeF GetDocumentSize(FormattedCode code, Graphics g, Font font)
        {
            UiExceptionHelper.CheckNotNull(code, "code");
            UiExceptionHelper.CheckNotNull(g, "g");
            UiExceptionHelper.CheckNotNull(font, "font");

            StringBuilder sample;
            SizeF         measure;
            int           i;

            sample = new StringBuilder();
            for (i = code.MaxLength; i > 0; --i)
            {
                sample.Append("m");
            }

            measure = g.MeasureString(sample.ToString(), font);

            return(new SizeF(measure.Width, measure.Height * code.LineCount));
        }
Ejemplo n.º 15
0
        public void Format_Lines()
        {
            FormattedCode res;
            FormattedCode exp;

            res = _formatter.Format(
                "line 1\r\n" +
                "line 2\r\n" +
                "line 3\r\n");

            exp = new FormattedCode(
                res.Text,
                new int[] { 0, 8, 16 },
                new byte[] { 0, 0, 0 },
                new int[] { 0, 1, 2 });

            Assert.That(res, Is.EqualTo(exp));

            return;
        }
Ejemplo n.º 16
0
        public void Test_ComplexCollection()
        {
            _code = new TestingCSCode(
                "int i; //comment\n" +
                "char c='a';\n",
                new int[] { 0, 4, 7, 17, 22, 24, 27 },
                new byte[] { 1, 0, 2, 1, 0, 3, 0 },
                new int[] { 0, 3 }
                );

            Assert.That(_code.Text, Is.EqualTo("int i; //comment\nchar c='a';\n"));
            Assert.That(_code.LineCount, Is.EqualTo(2));

            Assert.That(_code[0], Is.Not.Null);
            Assert.That(_code[0].Text, Is.EqualTo("int i; //comment"));

            Assert.That(_code[1], Is.Not.Null);
            Assert.That(_code[1].Text, Is.EqualTo("char c='a';"));

            // check internal data

            Assert.That(_code[0].Count, Is.EqualTo(3));
            Assert.That(_code[0][0].Text, Is.EqualTo("int "));
            Assert.That(_code[0][0].Tag, Is.EqualTo(ClassificationTag.Keyword));
            Assert.That(_code[0][1].Text, Is.EqualTo("i; "));
            Assert.That(_code[0][1].Tag, Is.EqualTo(ClassificationTag.Code));
            Assert.That(_code[0][2].Text, Is.EqualTo("//comment"));
            Assert.That(_code[0][2].Tag, Is.EqualTo(ClassificationTag.Comment));

            Assert.That(_code[1].Count, Is.EqualTo(4));
            Assert.That(_code[1][0].Text, Is.EqualTo("char "));
            Assert.That(_code[1][0].Tag, Is.EqualTo(ClassificationTag.Keyword));
            Assert.That(_code[1][1].Text, Is.EqualTo("c="));
            Assert.That(_code[1][1].Tag, Is.EqualTo(ClassificationTag.Code));
            Assert.That(_code[1][2].Text, Is.EqualTo("'a'"));
            Assert.That(_code[1][2].Tag, Is.EqualTo(ClassificationTag.String));
            Assert.That(_code[1][3].Text, Is.EqualTo(";"));
            Assert.That(_code[1][3].Tag, Is.EqualTo(ClassificationTag.Code));

            return;
        }
Ejemplo n.º 17
0
        TestingCodeBox SetupCodeBox(FormattedCode code, SizeF size)
        {
            TestingCodeBox box;

            box = new TestingCodeBox(
                (IFormatterCatalog)_mockFormatter.MockInstance,
                (ICodeRenderer)_mockRenderer.MockInstance);

            _mockFormatter.ExpectAndReturn("Format", code, new object[] { code.Text, "" });

            _mockRenderer.ExpectAndReturn("GetDocumentSize", size,
                                          new object[] { code, box.RenderingContext.Graphics, box.RenderingContext.Font });

            box.Text = code.Text;
            Assert.That(box.Text, Is.EqualTo(code.Text));

            _mockFormatter.Verify();
            _mockRenderer.Verify();

            return(box);
        }
Ejemplo n.º 18
0
        public void Test_Format_2()
        {
            FormattedCode exp;
            FormattedCode res;

            res = _parser.Format(
                "int i; //comment\n" +
                "char c='a';\n");

            exp = new FormattedCode(
                "int i; //comment\n" +
                "char c='a';\n",
                new int[] { 0, 3, 7, 16, 17, 21, 24, 27 },
                new byte[] { 1, 0, 2, 0, 1, 0, 3, 0 },
                new int[] { 0, 4 }
                );

            Assert.That(res, Is.EqualTo(exp));

            return;
        }
Ejemplo n.º 19
0
        public void Test_MaxLength()
        {
            _code = new TestingCSCode(
                "", new int[] { }, new byte[] { }, new int[] { });
            Assert.That(_code.MaxLength, Is.EqualTo(0));

            _code = new TestingCSCode(
                "a\r\nabc\r\nab",
                new int[] { 0, 3, 8 },
                new byte[] { 0, 0, 0 },
                new int[] { 0, 1, 2 });
            Assert.That(_code.MaxLength, Is.EqualTo(3));

            _code = new TestingCSCode(
                "a\r\nab\r\nabc",
                new int[] { 0, 3, 7 },
                new byte[] { 0, 0, 0 },
                new int[] { 0, 1, 2 });
            Assert.That(_code.MaxLength, Is.EqualTo(3));

            return;
        }
Ejemplo n.º 20
0
        public void Test_SimpleCollection()
        {
            _code = new TestingCSCode(
                "line 1\n  line 2\nline 3\n",
                new int[] { 0, 7, 16 },
                new byte[] { 0, 0, 0 },
                new int[] { 0, 1, 2 }
                );

            Assert.That(_code.Text, Is.EqualTo("line 1\n  line 2\nline 3\n"));
            Assert.That(_code.LineCount, Is.EqualTo(3));

            Assert.That(_code[0], Is.Not.Null);
            Assert.That(_code[0].Text, Is.EqualTo("line 1"));

            Assert.That(_code[1], Is.Not.Null);
            Assert.That(_code[1].Text, Is.EqualTo("  line 2"));

            Assert.That(_code[2], Is.Not.Null);
            Assert.That(_code[2].Text, Is.EqualTo("line 3"));

            // check internal data

            Assert.That(_code[0].Count, Is.EqualTo(1));
            Assert.That(_code[0][0].Text, Is.EqualTo("line 1"));
            Assert.That(_code[0][0].Tag, Is.EqualTo(ClassificationTag.Code));

            Assert.That(_code[1].Count, Is.EqualTo(1));
            Assert.That(_code[1][0].Text, Is.EqualTo("  line 2"));
            Assert.That(_code[1][0].Tag, Is.EqualTo(ClassificationTag.Code));

            Assert.That(_code[2].Count, Is.EqualTo(1));
            Assert.That(_code[2][0].Text, Is.EqualTo("line 3"));
            Assert.That(_code[2][0].Tag, Is.EqualTo(ClassificationTag.Code));

            return;
        }
Ejemplo n.º 21
0
        protected CodeBox(IFormatterCatalog formatter, ICodeRenderer renderer)
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            DoubleBuffered = true;

            _formatter     = formatter;
            _formattedCode = FormattedCode.Empty;

            _renderer = renderer;

            _currentLine     = -1;
            _showCurrentLine = false;

            _language = "";

            this.Font      = new Font(FontFamily.GenericMonospace, 8);
            this.BackColor = Color.White;

            createGraphics();
            AutoScroll = true;

            return;
        }
Ejemplo n.º 22
0
 public void DrawToGraphics(FormattedCode code, CodeRenderingContext args, Rectangle viewport)
 {
     CURRENTLINE_INDEX = args.CurrentLine;
 }
Ejemplo n.º 23
0
 public void CheckData_LineArray_Values_Must_Be_In_IndexArray_Count()
 {
     FormattedCode.CheckData(
         new FormattedCode("hi there!", new int[] { 0 }, new byte[] { 0 }, new int[] { 1 })); // throws exception
 }
Ejemplo n.º 24
0
 public void CheckData_IndexArray_And_TagArray_Count_Must_Match()
 {
     FormattedCode.CheckData(
         new FormattedCode("hello", new int[] { 0 }, new byte[0], new int[] { 0 })); // throws exception
 }
Ejemplo n.º 25
0
 public void CheckData_Can_Throw_NullDataException()
 {
     FormattedCode.CheckData(null); // throws exception
 }
Ejemplo n.º 26
0
        public void DrawToGraphics(FormattedCode code, CodeRenderingContext args, Rectangle viewport)
        {
            UiExceptionHelper.CheckNotNull(code, "code");
            UiExceptionHelper.CheckNotNull(args, "args");

            ClassifiedTokenCollection line;

            PaintLineLocation[] lines;
            ClassifiedToken     token;
            float  fontHeight;
            string text;
            float  tk_width;
            int    i;
            float  x;

            fontHeight = LineIndexToYCoordinate(1, args.Graphics, args.Font);
            lines      = ViewportLines(code, viewport, fontHeight);

            foreach (PaintLineLocation paintLine in lines)
            {
                // All lines that differ from CurrentLine are displayed
                // in using different styles of Brush to make a distinction
                // between code, keyword, comments.
                if (paintLine.LineIndex != args.CurrentLine)
                {
                    line = code[paintLine.LineIndex];
                    x    = 0;
                    text = line.Text;

                    for (i = 0; i < line.Count; ++i)
                    {
                        token = line[i];

                        args.Graphics.DrawString(token.Text, args.Font, args.GetBrush(token.Tag),
                                                 paintLine.Location.X + x, paintLine.Location.Y);

                        tk_width = measureStringWidth(args.Graphics, args.Font, text,
                                                      token.IndexStart, token.Text.Length);

                        x += tk_width;
                    }

                    continue;
                }

                // The current line is emphasized by using a
                // specific couples of Background & Foreground colors

                args.Graphics.FillRectangle(
                    args.CurrentLineBackBrush,
                    0, paintLine.Location.Y,
                    viewport.Width, fontHeight);

                args.Graphics.DrawString(
                    paintLine.Text, args.Font,
                    args.CurrentLineForeBrush,
                    paintLine.Location.X, paintLine.Location.Y);
            }

            return;
        }
Ejemplo n.º 27
0
 public SizeF GetDocumentSize(FormattedCode code, Graphics g, Font font)
 {
     return(new SizeF(200, 400));
 }
Ejemplo n.º 28
0
 public void CheckData_Can_Throw_NullDataException()
 {
     Assert.Throws <ArgumentNullException>(() => FormattedCode.CheckData(null)); // throws exception
 }