Ejemplo n.º 1
0
        public void GetCharIndexFromLineColumnIndex()
        {
            TextBuffer                  text;
            SplitArray <int>            lhi;
            SplitArray <LineDirtyState> lds;

            MakeTestData(out text, out lhi, out lds);

            Assert.AreEqual(0, TextUtil.GetCharIndexFromLineColumnIndex(text, lhi, 0, 0));
            Assert.AreEqual(34, TextUtil.GetCharIndexFromLineColumnIndex(text, lhi, 2, 1));
            Assert.AreEqual(71, TextUtil.GetCharIndexFromLineColumnIndex(text, lhi, 6, 18));

            MyAssert.Throws <AssertException>(delegate {
                TextUtil.GetCharIndexFromLineColumnIndex(text, lhi, 6, 19);
            });
            MyAssert.Throws <AssertException>(delegate {
                TextUtil.GetCharIndexFromLineColumnIndex(text, lhi, 0, 100);
            });
        }
Ejemplo n.º 2
0
        public void NextLineHead()
        {
            TextBuffer text = new TextBuffer(1, 32);

            text.Insert(0, TestData.ToCharArray());

            MyAssert.Throws <AssertException>(delegate {
                TextUtil.NextLineHead(text, -1);
            });

            int i = 0;

            for ( ; i < 32; i++)
            {
                Assert.AreEqual(32, TextUtil.NextLineHead(text, i));
            }
            for ( ; i < 33; i++)
            {
                Assert.AreEqual(33, TextUtil.NextLineHead(text, i));
            }
            for ( ; i < 37; i++)
            {
                Assert.AreEqual(37, TextUtil.NextLineHead(text, i));
            }
            for ( ; i < 38; i++)
            {
                Assert.AreEqual(38, TextUtil.NextLineHead(text, i));
            }
            for ( ; i < 52; i++)
            {
                Assert.AreEqual(52, TextUtil.NextLineHead(text, i));
            }
            for ( ; i < 53; i++)
            {
                Assert.AreEqual(53, TextUtil.NextLineHead(text, i));
            }
            for ( ; i < 71; i++)
            {
                Assert.AreEqual(-1, TextUtil.NextLineHead(text, i));
            }
            Assert.AreEqual(-1, TextUtil.NextLineHead(text, i));
        }
Ejemplo n.º 3
0
        public void LineHeadIndexFromCharIndex()
        {
            TextBuffer       text;
            SplitArray <int> lhi;

            MakeTestData(out text, out lhi);

            int i = 0;

            for ( ; i < 32; i++)
            {
                Assert.AreEqual(0, TextUtil.GetLineHeadIndexFromCharIndex(text, lhi, i));
            }
            for ( ; i < 33; i++)
            {
                Assert.AreEqual(32, TextUtil.GetLineHeadIndexFromCharIndex(text, lhi, i));
            }
            for ( ; i < 37; i++)
            {
                Assert.AreEqual(33, TextUtil.GetLineHeadIndexFromCharIndex(text, lhi, i));
            }
            for ( ; i < 38; i++)
            {
                Assert.AreEqual(37, TextUtil.GetLineHeadIndexFromCharIndex(text, lhi, i));
            }
            for ( ; i < 52; i++)
            {
                Assert.AreEqual(38, TextUtil.GetLineHeadIndexFromCharIndex(text, lhi, i));
            }
            for ( ; i < 53; i++)
            {
                Assert.AreEqual(52, TextUtil.GetLineHeadIndexFromCharIndex(text, lhi, i));
            }
            for ( ; i <= 71; i++)
            {
                Assert.AreEqual(53, TextUtil.GetLineHeadIndexFromCharIndex(text, lhi, i));
            }
            MyAssert.Throws <AssertException>(delegate {
                TextUtil.GetLineHeadIndexFromCharIndex(text, lhi, i);
            });
        }
Ejemplo n.º 4
0
        public void GetLineColumnIndexFromCharIndex()
        {
            // keep it as simple as possible\r\n
            // but\n
            // not simpler.\r\n
            Document doc = new Document();
            int      line, column;
            int      i = 0;

            doc.Text = "keep it as simple as possible\r\nbut\nnot simpler.\r\n";

            i = 0;
            for ( ; i < 31; i++)
            {
                doc.GetLineColumnIndexFromCharIndex(i, out line, out column);
                Assert.AreEqual(0, line);
                Assert.AreEqual(i, column);
            }
            for ( ; i < 35; i++)
            {
                doc.GetLineColumnIndexFromCharIndex(i, out line, out column);
                Assert.AreEqual(1, line);
                Assert.AreEqual(i - 31, column);
            }
            for ( ; i < 49; i++)
            {
                doc.GetLineColumnIndexFromCharIndex(i, out line, out column);
                Assert.AreEqual(2, line);
                Assert.AreEqual(i - 35, column);
            }
            doc.GetLineColumnIndexFromCharIndex(49, out line, out column);
            Assert.AreEqual(3, line);
            Assert.AreEqual(i - 49, column);

            // out of range
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                doc.GetLineColumnIndexFromCharIndex(50, out line, out column);
            });
        }
Ejemplo n.º 5
0
        public void GetLineColumnIndexFromCharIndex()
        {
            TextBuffer       text;
            SplitArray <int> lhi;
            int l, c;

            MakeTestData(out text, out lhi);

            TextUtil.GetLineColumnIndexFromCharIndex(text, lhi, 0, out l, out c);
            Assert.AreEqual(0, l);
            Assert.AreEqual(0, c);
            TextUtil.GetLineColumnIndexFromCharIndex(text, lhi, 2, out l, out c);
            Assert.AreEqual(0, l);
            Assert.AreEqual(2, c);
            TextUtil.GetLineColumnIndexFromCharIndex(text, lhi, 40, out l, out c);
            Assert.AreEqual(4, l);
            Assert.AreEqual(2, c);
            TextUtil.GetLineColumnIndexFromCharIndex(text, lhi, 71, out l, out c);               // 71 --> EOF
            Assert.AreEqual(6, l);
            Assert.AreEqual(18, c);
            MyAssert.Throws <AssertException>(delegate {
                TextUtil.GetLineColumnIndexFromCharIndex(text, lhi, 72, out l, out c);
            });
        }
Ejemplo n.º 6
0
        public void GetLineLength()
        {
            // 0 keep it\r
            // 1 \r
            // 2 as simple as possible\r\n
            // 3 \n
            // 4 but\n
            // 5 \r\n
            // 6 not simpler.
            Document doc = new Document();

            doc.Text = "keep it\r\ras simple as possible\r\n\nbut\n\r\nnot simpler.";

            Assert.AreEqual(7, doc.GetLineLength(0));
            Assert.AreEqual(0, doc.GetLineLength(1));
            Assert.AreEqual(21, doc.GetLineLength(2));
            Assert.AreEqual(0, doc.GetLineLength(3));
            Assert.AreEqual(3, doc.GetLineLength(4));
            Assert.AreEqual(0, doc.GetLineLength(5));
            Assert.AreEqual(12, doc.GetLineLength(6));
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                doc.GetLineLength(7);
            });
        }
Ejemplo n.º 7
0
        public void LHI_Delete()
        {
            // TEST DATA:
            // --------------------
            // "keep it as simple as possible\r\n (head: 0, len:31)
            // \n                                 (head:32, len: 1)
            // but\n                              (head:33, len: 4)
            // \r                                 (head:37, len: 1)
            // not simpler."\r                    (head:38, len:14)
            // \r                                 (head:52, len: 1)
            //  - Albert Einstein                 (head:53, len:18)
            // --------------------
            const string                TestData = "\"keep it as simple as possible\r\n\nbut\n\rnot simpler.\"\r\r - Albert Einstein";
            TextBuffer                  text     = new TextBuffer(1, 32);
            SplitArray <int>            lhi      = new SplitArray <int>(1, 8);
            SplitArray <LineDirtyState> lds      = new SplitArray <LineDirtyState>(1, 8);

            lds.Add(LineDirtyState.Clean);

            // prepare
            lhi.Add(0);
            TextUtil.LHI_Insert(lhi, lds, text, TestData, 0);
            text.Add(TestData.ToCharArray());
            Assert.AreEqual("0 32 33 37 38 52 53", lhi.ToString());
            Assert.AreEqual("DDDDDDD", MakeLdsText(lds));
            for (int i = 0; i < lds.Count; i++)
            {
                lds[i] = LineDirtyState.Clean;
            }

            //--- delete range in line ---
            // valid range
            TextUtil.LHI_Delete(lhi, lds, text, 2, 5);
            text.RemoveRange(2, 5);
            Assert.AreEqual("0 29 30 34 35 49 50", lhi.ToString());
            Assert.AreEqual("DCCCCCC", MakeLdsText(lds));

            // invalid range (before begin to middle)
            MyAssert.Throws <AssertException>(delegate {
                TextUtil.LHI_Delete(lhi, lds, text, -1, 5);
            });

            //--- delete range between different lines ---
            text.Clear(); lhi.Clear(); lhi.Add(0); lds.Clear(); lds.Add(LineDirtyState.Clean);
            TextUtil.LHI_Insert(lhi, lds, text, TestData, 0);
            text.Add(TestData.ToCharArray());
            // "keep it as simple as possible\r\n (head: 0, len:31)
            // \n                                 (head:32, len: 1)
            // but\n                              (head:33, len: 4)
            // \r                                 (head:37, len: 1)
            // not simpler."\r                    (head:38, len:14)
            // \r                                 (head:52, len: 1)
            //  - Albert Einstein[EOF]            (head:53, len:18)

            // delete only one EOL code
            //----
            // "keep it as simple as possible\r\n (head: 0, len:31)
            // but\n                              (head:32, len: 4)
            // \r                                 (head:36, len: 1)
            // not simpler."\r                    (head:37, len:14)
            // \r                                 (head:51, len: 1)
            //  - Albert Einstein[EOF]            (head:52, len:18)
            //----
            for (int i = 0; i < lds.Count; i++)
            {
                lds[i] = LineDirtyState.Clean;
            }
            TextUtil.LHI_Delete(lhi, lds, text, 32, 33);
            text.RemoveAt(32);
            Assert.AreEqual("0 32 36 37 51 52", lhi.ToString());
            Assert.AreEqual("CDCCCC", MakeLdsText(lds));

            // delete middle of the first line to not EOF pos
            //----
            // "keep it as simple as not simpler."\r (head: 0, len:35)
            // \r                                    (head:36, len: 1)
            //  - Albert Einstein[EOF]               (head:37, len:18)
            //----
            for (int i = 0; i < lds.Count; i++)
            {
                lds[i] = LineDirtyState.Clean;
            }
            TextUtil.LHI_Delete(lhi, lds, text, 22, 37);
            text.RemoveRange(22, 37);
            Assert.AreEqual("0 36 37", lhi.ToString());
            Assert.AreEqual("DCC", MakeLdsText(lds));

            // delete all
            //----
            // [EOF] (head:0, len:0)
            //----
            for (int i = 0; i < lds.Count; i++)
            {
                lds[i] = LineDirtyState.Clean;
            }
            TextUtil.LHI_Delete(lhi, lds, text, 0, 55);
            text.RemoveRange(0, 55);
            Assert.AreEqual("0", lhi.ToString());
            Assert.AreEqual("D", MakeLdsText(lds));

            //--- special care about CR+LF ---
            // (1) deletion creates a new CR+LF
            // (2) deletion breaks a CR+LF at the left side of the deletion range
            // (3) deletion breaks a CR+LF at the left side of the deletion range
            //--------------------------------
            // (1)
            {
                text.Clear(); lhi.Clear(); lhi.Add(0); lds.Clear(); lds.Add(LineDirtyState.Clean);
                TextUtil.LHI_Insert(lhi, lds, text, "foo\rx\nbar", 0);
                text.Add("foo\rx\nbar".ToCharArray());

                for (int i = 0; i < lds.Count; i++)
                {
                    lds[i] = LineDirtyState.Clean;
                }
                TextUtil.LHI_Delete(lhi, lds, text, 4, 5);
                text.RemoveRange(4, 5);
                Assert.AreEqual("0 5", lhi.ToString());
                Assert.AreEqual("DC", MakeLdsText(lds));
            }

            // (2)
            {
                text.Clear(); lhi.Clear(); lhi.Add(0); lds.Clear(); lds.Add(LineDirtyState.Clean);
                TextUtil.LHI_Insert(lhi, lds, text, "foo\r\nbar", 0);
                text.Add("foo\r\nbar".ToCharArray());

                for (int i = 0; i < lds.Count; i++)
                {
                    lds[i] = LineDirtyState.Clean;
                }
                TextUtil.LHI_Delete(lhi, lds, text, 4, 6);
                text.RemoveRange(4, 6);
                Assert.AreEqual("0 4", lhi.ToString());
                Assert.AreEqual("DD", MakeLdsText(lds));
            }

            // (3)
            {
                text.Clear(); lhi.Clear(); lhi.Add(0); lds.Clear(); lds.Add(LineDirtyState.Clean);
                TextUtil.LHI_Insert(lhi, lds, text, "foo\r\nbar", 0);
                text.Add("foo\r\nbar".ToCharArray());

                for (int i = 0; i < lds.Count; i++)
                {
                    lds[i] = LineDirtyState.Clean;
                }
                TextUtil.LHI_Delete(lhi, lds, text, 2, 4);
                text.RemoveRange(2, 4);
                Assert.AreEqual("0 3", lhi.ToString());
                Assert.AreEqual("DC", MakeLdsText(lds));
            }

            // (1)+(2)+(3)
            {
                text.Clear(); lhi.Clear(); lhi.Add(0); lds.Clear(); lds.Add(LineDirtyState.Clean);
                TextUtil.LHI_Insert(lhi, lds, text, "\r\nfoo\r\n", 0);
                text.Add("\r\nfoo\r\n".ToCharArray());

                for (int i = 0; i < lds.Count; i++)
                {
                    lds[i] = LineDirtyState.Clean;
                }
                TextUtil.LHI_Delete(lhi, lds, text, 1, 6);
                text.RemoveRange(1, 6);
                Assert.AreEqual("0 2", lhi.ToString());
                Assert.AreEqual("DC", MakeLdsText(lds));
            }

            //--- misc ---
            // insert "\n" after '\r' at end of document (boundary check for LHI_Insert)
            {
                text.Clear(); lhi.Clear(); lhi.Add(0); lds.Clear(); lds.Add(LineDirtyState.Clean);
                TextUtil.LHI_Insert(lhi, lds, text, "\r", 0);
                text.Add("\r".ToCharArray());

                for (int i = 0; i < lds.Count; i++)
                {
                    lds[i] = LineDirtyState.Clean;
                }
                TextUtil.LHI_Insert(lhi, lds, text, "\n", 1);
                text.Add("\n".ToCharArray());
                Assert.AreEqual("0 2", lhi.ToString());
                Assert.AreEqual("CD", MakeLdsText(lds));
            }

            // insert "\n" after '\r' at end of document (boundary check for LHI_Delete)
            {
                text.Clear(); lhi.Clear(); lhi.Add(0); lds.Clear(); lds.Add(LineDirtyState.Clean);
                TextUtil.LHI_Insert(lhi, lds, text, "\r\n", 0);
                text.Add("\r\n".ToCharArray());

                for (int i = 0; i < lds.Count; i++)
                {
                    lds[i] = LineDirtyState.Clean;
                }
                TextUtil.LHI_Delete(lhi, lds, text, 1, 2);
                text.RemoveRange(1, 2);
                Assert.AreEqual("0 1", lhi.ToString());
                Assert.AreEqual("DD", MakeLdsText(lds));
            }
        }
Ejemplo n.º 8
0
        public void Replace()
        {
            const string      InitData = "hogepiyo";
            SplitArray <char> sary     = new SplitArray <char>(5, 8);

            // replace position
            {
                // before head
                sary.Clear();
                sary.Add(InitData.ToCharArray());
                MyAssert.Throws <AssertException>(delegate {
                    sary.Replace(-1, "000".ToCharArray(), 0, 2);
                });

                // head
                sary.Clear();
                sary.Add(InitData.ToCharArray());
                sary.Replace(0, "000".ToCharArray(), 0, 2);
                Assert.AreEqual("00gepiyo", ToString(sary));

                // middle
                sary.Clear();
                sary.Add(InitData.ToCharArray());
                sary.Replace(4, "000".ToCharArray(), 0, 2);
                Assert.AreEqual("hoge00yo", ToString(sary));

                // end
                sary.Clear();
                sary.Add(InitData.ToCharArray());
                sary.Replace(6, "000".ToCharArray(), 0, 2);
                Assert.AreEqual("hogepi00", ToString(sary));

                // after end
                sary.Clear();
                sary.Add(InitData.ToCharArray());
                MyAssert.Throws <AssertException>(delegate {
                    sary.Replace(7, "000".ToCharArray(), 0, 2);
                });
                MyAssert.Throws <AssertException>(delegate {
                    sary.Replace(8, "000".ToCharArray(), 0, 2);
                });
            }

            // value array
            {
                // giving null
                MyAssert.Throws <AssertException>(delegate {
                    sary.Replace(0, null, 0, 1);
                });

                // empty array
                sary.Replace(0, "".ToCharArray(), 0, 0);
                Assert.AreEqual("hogepiyo", ToString(sary));

                // empty range
                sary.Replace(0, "000".ToCharArray(), 0, 0);
                Assert.AreEqual("hogepiyo", ToString(sary));

                // invalid range (reversed)
                MyAssert.Throws <AssertException>(delegate {
                    sary.Replace(0, "000".ToCharArray(), 1, 0);
                });

                // invalid range (before head)
                MyAssert.Throws <AssertException>(delegate {
                    sary.Replace(0, "000".ToCharArray(), -1, 0);
                });

                // invalid range (after head)
                MyAssert.Throws <AssertException>(delegate {
                    sary.Replace(0, "000".ToCharArray(), 3, 4);
                });
            }
        }
Ejemplo n.º 9
0
        public void GetSet()
        {
            RleArray <char> chars;

            // get
            chars = new RleArray <char>();
            Set(chars, "abc");
            Assert.AreEqual(3, chars.Count);
            Assert.AreEqual('a', chars[0]);
            Assert.AreEqual('b', chars[1]);
            Assert.AreEqual('c', chars[2]);
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                chars[-1].ToString();
            });
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                chars[5].ToString();
            });

            // set - out of bounds
            chars = new RleArray <char>();
            Set(chars, "abc");
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                chars[-1] = 'z';
            });
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                chars[4] = 'd';
            });

            {
                // set - same value
                chars = new RleArray <char>();
                Set(chars, "aabbbcc");
                chars[2] = 'b';
                Assert.AreEqual("a a b b b c c", chars.ToString());
                Assert.AreEqual("[2|a] [3|b] [2|c]", chars._Nodes.ToString());

                // set - new value on left boundary
                chars = new RleArray <char>();
                Set(chars, "aabbbcc");
                chars[2] = 'X';
                Assert.AreEqual("a a X b b c c", chars.ToString());
                Assert.AreEqual("[2|a] [1|X] [2|b] [2|c]", chars._Nodes.ToString());

                // set - same value as the previous node on left boundary
                chars = new RleArray <char>();
                Set(chars, "aabbbcc");
                chars[2] = 'a';
                Assert.AreEqual("a a a b b c c", chars.ToString());
                Assert.AreEqual("[3|a] [2|b] [2|c]", chars._Nodes.ToString());

                // set - new value in the middle of a node
                chars = new RleArray <char>();
                Set(chars, "aabbbcc");
                chars[3] = 'a';
                Assert.AreEqual("a a b a b c c", chars.ToString());
                Assert.AreEqual("[2|a] [1|b] [1|a] [1|b] [2|c]", chars._Nodes.ToString());

                // set - new value on right boundary
                chars = new RleArray <char>();
                Set(chars, "aabbbcc");
                chars[4] = 'X';
                Assert.AreEqual("a a b b X c c", chars.ToString());
                Assert.AreEqual("[2|a] [2|b] [1|X] [2|c]", chars._Nodes.ToString());

                // set - same value as the next node on right boundary
                chars = new RleArray <char>();
                Set(chars, "aabbbcc");
                chars[4] = 'c';
                Assert.AreEqual("a a b b c c c", chars.ToString());
                Assert.AreEqual("[2|a] [2|b] [3|c]", chars._Nodes.ToString());
            }

            {
                // set - combines with previous node and the node disappears
                chars = new RleArray <char>();
                Set(chars, "aabcc");
                chars[2] = 'a';
                Assert.AreEqual("a a a c c", chars.ToString());
                Assert.AreEqual("[3|a] [2|c]", chars._Nodes.ToString());

                // set - combines with next node and the node disappears
                chars = new RleArray <char>();
                Set(chars, "aabcc");
                chars[2] = 'c';
                Assert.AreEqual("a a c c c", chars.ToString());
                Assert.AreEqual("[2|a] [3|c]", chars._Nodes.ToString());

                // set - combines with next and previous node, and the node disappears
                chars = new RleArray <char>();
                Set(chars, "aabaa");
                chars[2] = 'a';
                Assert.AreEqual("a a a a a", chars.ToString());
                Assert.AreEqual("[5|a]", chars._Nodes.ToString());

                // set - combines with previous node and the node disappears
                chars = new RleArray <char>();
                Set(chars, "aabcc");
                chars[2] = 'B';
                Assert.AreEqual("a a B c c", chars.ToString());
                Assert.AreEqual("[2|a] [1|B] [2|c]", chars._Nodes.ToString());
            }
        }
Ejemplo n.º 10
0
        public void FindPrevR()
        {
            Document doc = new Document();

            doc.Replace("abcdabcaba");

            // black box test (interface test)
            {
                // null target
                MyAssert.Throws <ArgumentNullException>(delegate {
                    doc.FindPrev((Regex)null, 0, 10);
                });

                // negative index
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.FindPrev(new Regex("a", RegexOptions.RightToLeft), -1, 10);
                });

                // invalid regex option
                MyAssert.Throws <ArgumentException>(delegate {
                    doc.FindPrev(new Regex("a", RegexOptions.None), 0, doc.Length);
                });

                // end index at out of range
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.FindPrev(new Regex("a", RegexOptions.RightToLeft), 0, doc.Length + 1);
                });

                // inverted range
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.FindPrev(new Regex("a", RegexOptions.RightToLeft), 1, 0);
                });

                // empty range
                Assert.AreEqual(null, doc.FindPrev(new Regex("a", RegexOptions.RightToLeft), 0, 0));

                // find in valid range
                Assert.AreEqual(9, doc.FindPrev(new Regex("a", RegexOptions.RightToLeft), 0, 10).Begin);
                Assert.AreEqual(7, doc.FindPrev(new Regex("ab", RegexOptions.RightToLeft), 0, 10).Begin);
                Assert.AreEqual(4, doc.FindPrev(new Regex("abc", RegexOptions.RightToLeft), 0, 10).Begin);
                Assert.AreEqual(0, doc.FindPrev(new Regex("abcd", RegexOptions.RightToLeft), 0, 10).Begin);
                Assert.AreEqual(null, doc.FindPrev(new Regex("abcde", RegexOptions.RightToLeft), 0, 10));

                // empty pattern (returns end index)
                Assert.AreEqual(10, doc.FindPrev(new Regex("", RegexOptions.RightToLeft), 0, 10).Begin);

                // comp. options
                Assert.AreEqual(null, doc.FindPrev(new Regex("aBcD", RegexOptions.RightToLeft), 0, 10));
                Assert.AreEqual(0, doc.FindPrev(new Regex("aBcD", RegexOptions.RightToLeft | RegexOptions.IgnoreCase), 0, 10).Begin);
            }

            // white box test (test of the gap condition. test only result.)
            {
                // (buf: abcda......bcaba)

                // gap < begin
                MoveGap(doc, 5);
                Assert.AreEqual(7, doc.FindPrev(new Regex("ab", RegexOptions.RightToLeft), 7, 10).Begin);

                // gap == begin
                {
                    MoveGap(doc, 5);
                    Assert.AreEqual(7, doc.FindPrev(new Regex("ab", RegexOptions.RightToLeft), 5, 10).Begin);

                    // word at the begin
                    MoveGap(doc, 5);
                    Assert.AreEqual(5, doc.FindPrev(new Regex("bc", RegexOptions.RightToLeft), 5, 10).Begin);

                    // partially matched word but overruning boundary
                    MoveGap(doc, 5);
                    Assert.AreEqual(null, doc.FindPrev(new Regex("abca", RegexOptions.RightToLeft), 5, 10));
                }

                // begin < gap < end
                {
                    // word before the gap
                    MoveGap(doc, 5);
                    Assert.AreEqual(3, doc.FindPrev(new Regex("da", RegexOptions.RightToLeft), 0, 10).Begin);

                    // word crossing the gap
                    MoveGap(doc, 5);
                    Assert.AreEqual(4, doc.FindPrev(new Regex("abc", RegexOptions.RightToLeft), 0, 10).Begin);

                    // word after the gap
                    MoveGap(doc, 5);
                    Assert.AreEqual(5, doc.FindPrev(new Regex("bca", RegexOptions.RightToLeft), 0, 10).Begin);
                }

                // gap == end
                MoveGap(doc, 5);
                Assert.AreEqual(0, doc.FindPrev(new Regex("ab", RegexOptions.RightToLeft), 0, 5).Begin);

                // end <= gap
                MoveGap(doc, 5);
                Assert.AreEqual(0, doc.FindPrev(new Regex("ab", RegexOptions.RightToLeft), 0, 4).Begin);
            }
        }
Ejemplo n.º 11
0
        public void FindNextR()
        {
            Document    doc = new Document();
            TextSegment result;

            doc.Replace("aababcabcd");

            // black box test
            {
                // null argument
                MyAssert.Throws <ArgumentNullException>(delegate {
                    doc.FindNext((Regex)null, 1, 2);
                });

                // negative index
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.FindNext(new Regex("a[^b]+"), -1, 2);
                });

                // inverted range
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.FindNext(new Regex("a[^b]+"), 2, 1);
                });

                // empty range
                result = doc.FindNext(new Regex("a[^b]+"), 0, 0);
                Assert.AreEqual(null, result);

                // range exceeding text length
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.FindNext(new Regex("a[^b]+"), 1, 9999);
                });

                // invalid Regex option
                MyAssert.Throws <ArgumentException>(delegate {
                    doc.FindNext(new Regex("a[^b]+", RegexOptions.RightToLeft), 1, 4);
                });

                // pattern ord at begin
                result = doc.FindNext(new Regex("a[^b]+"), 0, 2);
                Assert.AreEqual(0, result.Begin);
                Assert.AreEqual(2, result.End);

                // pattern in the range
                result = doc.FindNext(new Regex("a[^a]+"), 0, 3);
                Assert.AreEqual(1, result.Begin);
                Assert.AreEqual(3, result.End);

                // pattern which ends at end
                result = doc.FindNext(new Regex("[ab]+"), 0, 5);
                Assert.AreEqual(0, result.Begin);
                Assert.AreEqual(5, result.End);

                // pattern... well, pretty hard to describe in English for me...
                result = doc.FindNext(new Regex("[abc]+"), 0, 5);
                Assert.AreEqual(0, result.Begin);
                Assert.AreEqual(5, result.End);
                result = doc.FindNext(new Regex("[abc]+"), 0, 10);
                Assert.AreEqual(0, result.Begin);
                Assert.AreEqual(9, result.End);

                // empty pattern
                result = doc.FindNext(new Regex(""), 0, 10);
                Assert.AreEqual(0, result.Begin);
                Assert.AreEqual(0, result.End);

                // comp. options
                result = doc.FindNext(new Regex("aBcD"), 0, doc.Length);
                Assert.AreEqual(null, result);
                result = doc.FindNext(new Regex("aBcD", RegexOptions.IgnoreCase), 0, doc.Length);
                Assert.AreEqual(6, result.Begin);
                Assert.AreEqual(10, result.End);
            }

            // white box test (test of the gap condition. test only result.)
            {
                // (buf: aaba......bcabcd)

                // gap < begin
                MoveGap(doc, 4);
                Assert.AreEqual(6, doc.FindNext(new Regex("ab"), 5, doc.Length).Begin);

                // gap == begin
                MoveGap(doc, 4);
                Assert.AreEqual(6, doc.FindNext(new Regex("ab"), 4, doc.Length).Begin);

                // begin < gap < end
                {
                    // word before the gap
                    MoveGap(doc, 4);
                    Assert.AreEqual(2, doc.FindNext(new Regex("ba"), 2, doc.Length).Begin);

                    // word crossing the gap
                    MoveGap(doc, 4);
                    Assert.AreEqual(3, doc.FindNext(new Regex("ab"), 2, doc.Length).Begin);

                    // word after the gap
                    MoveGap(doc, 4);
                    Assert.AreEqual(5, doc.FindNext(new Regex("cab"), 2, doc.Length).Begin);
                }

                // gap == end
                {
                    MoveGap(doc, 4);
                    Assert.AreEqual(1, doc.FindNext(new Regex("ab"), 0, 4).Begin);

                    // word at the end
                    MoveGap(doc, 4);
                    Assert.AreEqual(2, doc.FindNext(new Regex("ba"), 0, 4).Begin);

                    // partially matched word but overruning boundary
                    MoveGap(doc, 4);
                    Assert.AreEqual(null, doc.FindNext(new Regex("abc"), 0, 4));
                }

                // end <= gap
                MoveGap(doc, 4);
                Assert.AreEqual(1, doc.FindNext(new Regex("ab"), 0, 4).Begin);
            }
        }
Ejemplo n.º 12
0
        public void FindNext()
        {
            Document doc = new Document();

            doc.Replace("aababcabcd");

            // black box test (interface test)
            {
                // null target
                MyAssert.Throws <ArgumentNullException>(delegate {
                    doc.FindNext((string)null, 0);
                });

                // negative index
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.FindNext("a", -1);
                });

                // end index at out of range
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.FindNext("a", 0, doc.Length + 1, true);
                });

                // inverted range
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.FindNext("a", 1, 0, true);
                });

                // empty range
                Assert.AreEqual(null, doc.FindNext("a", 0, 0, true));

                // find in valid range
                Assert.AreEqual(0, doc.FindNext("a", 0, 1, true).Begin);
                Assert.AreEqual(1, doc.FindNext("ab", 0).Begin);
                Assert.AreEqual(3, doc.FindNext("abc", 0).Begin);
                Assert.AreEqual(6, doc.FindNext("abcd", 0).Begin);
                Assert.AreEqual(null, doc.FindNext("abcde", 0));

                // empty pattern (returns begin index)
                Assert.AreEqual(1, doc.FindNext("", 1).Begin);

                // comp. options
                Assert.AreEqual(null, doc.FindNext("aBcD", 0, doc.Length, true));
                Assert.AreEqual(6, doc.FindNext("aBcD", 0, doc.Length, false).Begin);
            }

            // white box test (test of the gap condition. test only result.)
            {
                // (buf: aaba......bcabcd)

                // gap < begin
                MoveGap(doc, 4);
                Assert.AreEqual(6, doc.FindNext("ab", 5, doc.Length, true).Begin);

                // gap == begin
                MoveGap(doc, 4);
                Assert.AreEqual(6, doc.FindNext("ab", 4, doc.Length, true).Begin);

                // begin < gap < end
                {
                    // word before the gap
                    MoveGap(doc, 4);
                    Assert.AreEqual(2, doc.FindNext("ba", 2, doc.Length, true).Begin);

                    // word crossing the gap
                    MoveGap(doc, 4);
                    Assert.AreEqual(3, doc.FindNext("ab", 2, doc.Length, true).Begin);

                    // word after the gap
                    MoveGap(doc, 4);
                    Assert.AreEqual(5, doc.FindNext("cab", 2, doc.Length, true).Begin);
                }

                // gap == end
                {
                    MoveGap(doc, 4);
                    Assert.AreEqual(1, doc.FindNext("ab", 0, 4, true).Begin);

                    // word at the end
                    MoveGap(doc, 4);
                    Assert.AreEqual(2, doc.FindNext("ba", 0, 4, true).Begin);

                    // partially matched word but overruning boundary
                    MoveGap(doc, 4);
                    Assert.AreEqual(null, doc.FindNext("abc", 0, 4, true));
                }

                // end <= gap
                MoveGap(doc, 4);
                Assert.AreEqual(1, doc.FindNext("ab", 0, 4, true).Begin);
            }
        }
Ejemplo n.º 13
0
        public void Selection()
        {
            Document doc = new Document();
            int      begin, end;

            doc.Text = "臼と似た形をした文字「\xd85a\xdd51」は、UCS 文字空間の第2面に位置する";

            // before head to head
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                doc.SetSelection(-1, 0);
            });

            // head to head
            doc.SetSelection(0, 0);
            doc.GetSelection(out begin, out end);
            Assert.AreEqual(begin, 0);
            Assert.AreEqual(end, 0);

            // head to before hi-surrogate
            doc.SetSelection(0, 4);
            doc.GetSelection(out begin, out end);
            Assert.AreEqual(begin, 0);
            Assert.AreEqual(end, 4);

            // before hi-surrogate to lo-surrogate
            doc.SetSelection(4, 11);
            doc.GetSelection(out begin, out end);
            Assert.AreEqual(begin, 4);
            Assert.AreEqual(end, 11);

            // hi-surrogate to lo-surrogate
            doc.SetSelection(11, 12);
            doc.GetSelection(out begin, out end);
            Assert.AreEqual(begin, 11);
            Assert.AreEqual(end, 13);               // surely expanded?

            // hi-surrogate to after lo-surrogate
            doc.SetSelection(11, 13);
            doc.GetSelection(out begin, out end);
            Assert.AreEqual(begin, 11);
            Assert.AreEqual(end, 13);

            // middle of the surrogate pair
            // ('moving caret between the pair' must be treated as 'moving caret to the *char*')
            doc.SetSelection(12, 12);
            doc.GetSelection(out begin, out end);
            Assert.AreEqual(begin, 11);
            Assert.AreEqual(end, 11);

            // lo-surrogate to after lo-surrogate
            doc.SetSelection(12, 13);
            doc.GetSelection(out begin, out end);
            Assert.AreEqual(begin, 11);
            Assert.AreEqual(end, 13);

            // after lo-surrogate to end
            doc.SetSelection(13, 33);
            doc.GetSelection(out begin, out end);
            Assert.AreEqual(begin, 13);
            Assert.AreEqual(end, 33);

            // end to after end
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                doc.SetSelection(33, 36);
            });
        }
Ejemplo n.º 14
0
        public void Replace()
        {
            Document doc = new Document();

            doc.Text = "keep it as simple as possible\r\nbut\nnot simpler.";

            // empty range (1)
            doc.Replace("n\r", 8, 8);
            Assert.AreEqual("keep it n\ras simple as possible\r\nbut\nnot simpler.", doc.Text);
            Assert.AreEqual(0, doc.AnchorIndex);
            Assert.AreEqual(0, doc.CaretIndex);

            // empty range (2)
            doc.SetSelection(9, 9);
            doc.Replace("ot");
            Assert.AreEqual("keep it not\ras simple as possible\r\nbut\nnot simpler.", doc.Text);
            Assert.AreEqual(11, doc.AnchorIndex);
            Assert.AreEqual(11, doc.CaretIndex);

            // invalid range (before begin to middle)
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                doc.Replace("FOO", -1, 10);
            });

            // valid range (begin to middle)
            {
                // replace to same length (1)
                doc.SetSelection(0, 4);
                doc.Replace("KEP");
                Assert.AreEqual("KEP it not\ras simple as possible\r\nbut\nnot simpler.", doc.Text);
                Assert.AreEqual(3, doc.AnchorIndex);
                Assert.AreEqual(3, doc.CaretIndex);

                // replace to same length (2)
                doc.Replace("Keep", 0, 3);
                Assert.AreEqual("Keep it not\ras simple as possible\r\nbut\nnot simpler.", doc.Text);
                Assert.AreEqual(4, doc.AnchorIndex);
                Assert.AreEqual(4, doc.CaretIndex);

                // replace to longer (1)
                doc.SetSelection(0, 3);
                doc.Replace("KEEP!");
                Assert.AreEqual("KEEP!p it not\ras simple as possible\r\nbut\nnot simpler.", doc.Text);
                Assert.AreEqual(5, doc.AnchorIndex);
                Assert.AreEqual(5, doc.CaretIndex);

                // replace to longer (2)
                doc.Replace("Keeeeep", 0, 6);
                Assert.AreEqual("Keeeeep it not\ras simple as possible\r\nbut\nnot simpler.", doc.Text);
                Assert.AreEqual(7, doc.AnchorIndex);
                Assert.AreEqual(7, doc.CaretIndex);

                // replace to shorter (1)
                doc.SetSelection(0, 7);
                doc.Replace("KEEEP");
                Assert.AreEqual("KEEEP it not\ras simple as possible\r\nbut\nnot simpler.", doc.Text);
                Assert.AreEqual(5, doc.AnchorIndex);
                Assert.AreEqual(5, doc.CaretIndex);

                // replace to shorter (2)
                doc.Replace("Keep", 0, 5);
                Assert.AreEqual("Keep it not\ras simple as possible\r\nbut\nnot simpler.", doc.Text);
                Assert.AreEqual(4, doc.AnchorIndex);
                Assert.AreEqual(4, doc.CaretIndex);
            }

            // valid range (middle to middle)
            {
                // replace to same length (1)
                doc.Replace("ZIMPLE", 15, 21);
                Assert.AreEqual("Keep it not\ras ZIMPLE as possible\r\nbut\nnot simpler.", doc.Text);
                Assert.AreEqual(4, doc.AnchorIndex);
                Assert.AreEqual(4, doc.CaretIndex);

                // replace to same length (2)
                doc.SetSelection(15, 21);
                doc.Replace("SIMPLE");
                Assert.AreEqual("Keep it not\ras SIMPLE as possible\r\nbut\nnot simpler.", doc.Text);
                Assert.AreEqual(21, doc.AnchorIndex);
                Assert.AreEqual(21, doc.CaretIndex);

                // replace to longer (1)
                doc.SetSelection(14, 15);
                doc.Replace("COMPLEX", 15, 21);
                Assert.AreEqual("Keep it not\ras COMPLEX as possible\r\nbut\nnot simpler.", doc.Text);
                Assert.AreEqual(14, doc.AnchorIndex);
                Assert.AreEqual(22, doc.CaretIndex);

                // replace to longer (2)
                doc.SetSelection(19, 22);
                doc.Replace("LEX!");
                Assert.AreEqual("Keep it not\ras COMPLEX! as possible\r\nbut\nnot simpler.", doc.Text);
                Assert.AreEqual(23, doc.AnchorIndex);
                Assert.AreEqual(23, doc.CaretIndex);

                // replace to shorter (1)
                doc.Replace("simple!", 15, 23);
                Assert.AreEqual("Keep it not\ras simple! as possible\r\nbut\nnot simpler.", doc.Text);
                Assert.AreEqual(22, doc.AnchorIndex);
                Assert.AreEqual(22, doc.CaretIndex);

                // replace to shorter (2)
                doc.SetSelection(15, 22);
                doc.Replace("simple");
                Assert.AreEqual("Keep it not\ras simple as possible\r\nbut\nnot simpler.", doc.Text);
                Assert.AreEqual(21, doc.AnchorIndex);
                Assert.AreEqual(21, doc.CaretIndex);
            }

            // valid range (middle to end)
            {
                // replace to same length (1)
                doc.Replace("?", 50, 51);
                Assert.AreEqual("Keep it not\ras simple as possible\r\nbut\nnot simpler?", doc.Text);
                Assert.AreEqual(21, doc.AnchorIndex);
                Assert.AreEqual(21, doc.CaretIndex);

                // replace to same length (2)
                doc.SetSelection(50, 51);
                doc.Replace("!");
                Assert.AreEqual("Keep it not\ras simple as possible\r\nbut\nnot simpler!", doc.Text);
                Assert.AreEqual(51, doc.AnchorIndex);
                Assert.AreEqual(51, doc.CaretIndex);

                // replace to longer (1)
                doc.Replace("??", 50, 51);
                Assert.AreEqual("Keep it not\ras simple as possible\r\nbut\nnot simpler??", doc.Text);
                Assert.AreEqual(52, doc.AnchorIndex);
                Assert.AreEqual(52, doc.CaretIndex);

                // replace to longer (2)
                doc.Replace("!!!", 50, 52);
                Assert.AreEqual("Keep it not\ras simple as possible\r\nbut\nnot simpler!!!", doc.Text);
                Assert.AreEqual(53, doc.AnchorIndex);
                Assert.AreEqual(53, doc.CaretIndex);

                // replace to shorter (1)
                doc.Replace("..", 50, 53);
                Assert.AreEqual("Keep it not\ras simple as possible\r\nbut\nnot simpler..", doc.Text);
                Assert.AreEqual(52, doc.AnchorIndex);
                Assert.AreEqual(52, doc.CaretIndex);

                // replace to shorter (2)
                doc.Replace(".", 50, 52);
                Assert.AreEqual("Keep it not\ras simple as possible\r\nbut\nnot simpler.", doc.Text);
                Assert.AreEqual(51, doc.AnchorIndex);
                Assert.AreEqual(51, doc.CaretIndex);
            }

            // invalid range (middle to after end)
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                doc.Replace("PIYO", 51, 53);
            });
        }
Ejemplo n.º 15
0
        public void DocumentDirtyState()
        {
            // dirty state
            {
                Document doc = new Document();
                Assert.AreEqual(false, doc.IsDirty);

                doc.Replace("a", 0, 0);
                Assert.AreEqual(true, doc.IsDirty);
                Assert.AreEqual("a", doc.Text);

                doc.IsDirty = false;
                Assert.AreEqual(false, doc.IsDirty);
                Assert.AreEqual("a", doc.Text);

                doc.BeginUndo();
                Assert.AreEqual(false, doc.IsDirty);
                Assert.AreEqual("a", doc.Text);

                doc.Replace("b", 1, 1);
                Assert.AreEqual(true, doc.IsDirty);
                Assert.AreEqual("ab", doc.Text);

                doc.Replace("c", 2, 2);
                Assert.AreEqual(true, doc.IsDirty);
                Assert.AreEqual("abc", doc.Text);

                doc.EndUndo();
                Assert.AreEqual(true, doc.IsDirty);
                Assert.AreEqual("abc", doc.Text);

                doc.Undo();
                Assert.AreEqual(false, doc.IsDirty);
                Assert.AreEqual("a", doc.Text);

                doc.Undo();
                Assert.AreEqual(true, doc.IsDirty);
                Assert.AreEqual("", doc.Text);

                doc.Redo();
                Assert.AreEqual(false, doc.IsDirty);
                Assert.AreEqual("a", doc.Text);

                doc.Undo();
                doc.Replace("a", 0, 0);
                Assert.AreEqual(true, doc.IsDirty);
                Assert.AreEqual("a", doc.Text);
            }

            // change IsDirty flag while recording group UNDO
            {
                Document doc = new Document();
                doc.BeginUndo();
                MyAssert.Throws <InvalidOperationException>(delegate {
                    doc.IsDirty = true;
                });
            }

            // special case; BeginUndo at initial state
            // ([*] goes exceptional 'if' code in EditHistory.IsSavedState)
            {
                Document doc = new Document();
                Assert.AreEqual(false, doc.IsDirty);
                doc.BeginUndo();
                Assert.AreEqual(false, doc.IsDirty);
                doc.Replace("a", 0, 0);
                Assert.AreEqual(true, doc.IsDirty);                   // [*]
                doc.EndUndo();
                Assert.AreEqual(true, doc.IsDirty);
            }
        }
Ejemplo n.º 16
0
        public void Keywords()
        {
            Document           doc = new Document();
            KeywordHighlighter h   = new KeywordHighlighter();

            h.AddEnclosure("\"", "\"", CharClass.String, '\\');
            h.AddEnclosure("/*", "*/", CharClass.Comment);
            h.AddKeywordSet(new string[] {
                "for", "if", "int", "interface", "join"
            }, CharClass.Keyword);
            doc.Highlighter = h;
            //---------------------------------------------

            int i;

            // "int" --> "int "
            doc.Text = "int";
            h.Highlight(doc);
            for (i = 0; i < 3; i++)
            {
                Assert.AreEqual(CharClass.Keyword, doc.GetCharClass(i));
            }
            doc.Replace(" ", 3, 3);
            h.Highlight(doc);
            Assert.AreEqual("int ", doc.Text);
            for (i = 0; i < 3; i++)
            {
                Assert.AreEqual(CharClass.Keyword, doc.GetCharClass(i));
            }
            for ( ; i < 4; i++)
            {
                Assert.AreEqual(CharClass.Normal, doc.GetCharClass(i));
            }
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                doc.GetCharClass(4);
            });

            // "int" --> "-int"
            doc.Text = "int";
            h.Highlight(doc);
            for (i = 0; i < 3; i++)
            {
                Assert.AreEqual(CharClass.Keyword, doc.GetCharClass(i));
            }
            doc.Replace("-", 0, 0);
            h.Highlight(doc);
            Assert.AreEqual("-int", doc.Text);
            for (i = 0; i < 1; i++)
            {
                Assert.AreEqual(CharClass.Normal, doc.GetCharClass(i));
            }
            for ( ; i < 4; i++)
            {
                Assert.AreEqual(CharClass.Keyword, doc.GetCharClass(i));
            }
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                doc.GetCharClass(4);
            });

            // in --> int
            doc.Text = "in";
            h.Highlight(doc);
            for (i = 0; i < 2; i++)
            {
                Assert.AreEqual(CharClass.Normal, doc.GetCharClass(i));
            }
            doc.Replace("t", 2, 2);
            h.Highlight(doc);
            Assert.AreEqual("int", doc.Text);
            for (i = 0; i < 3; i++)
            {
                Assert.AreEqual(CharClass.Keyword, doc.GetCharClass(i));
            }
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                doc.GetCharClass(3);
            });

            // it --> int
            doc.Text = "it";
            h.Highlight(doc);
            for (i = 0; i < 2; i++)
            {
                Assert.AreEqual(CharClass.Normal, doc.GetCharClass(i));
            }
            doc.Replace("n", 1, 1);
            h.Highlight(doc);
            Assert.AreEqual("int", doc.Text);
            for (i = 0; i < 3; i++)
            {
                Assert.AreEqual(CharClass.Keyword, doc.GetCharClass(i));
            }
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                doc.GetCharClass(3);
            });

            // nt --> int
            doc.Text = "nt";
            h.Highlight(doc);
            for (i = 0; i < 2; i++)
            {
                Assert.AreEqual(CharClass.Normal, doc.GetCharClass(i));
            }
            doc.Replace("i", 0, 0);
            h.Highlight(doc);
            Assert.AreEqual("int", doc.Text);
            for (i = 0; i < 3; i++)
            {
                Assert.AreEqual(CharClass.Keyword, doc.GetCharClass(i));
            }
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                doc.GetCharClass(3);
            });

            // "insert at" --> int
            doc.Text = "insert at";
            h.Highlight(doc);
            for (i = 0; i < 9; i++)
            {
                Assert.AreEqual(CharClass.Normal, doc.GetCharClass(i));
            }
            doc.Replace("n", 1, 8);
            h.Highlight(doc);
            Assert.AreEqual("int", doc.Text);
            for (i = 0; i < 3; i++)
            {
                Assert.AreEqual(CharClass.Keyword, doc.GetCharClass(i));
            }
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                doc.GetCharClass(3);
            });

            // "hoge" --> "h int e"
            doc.Text = "hoge";
            h.Highlight(doc);
            for (i = 0; i < 4; i++)
            {
                Assert.AreEqual(CharClass.Normal, doc.GetCharClass(i));
            }
            doc.Replace(" int ", 1, 3);
            h.Highlight(doc);
            Assert.AreEqual("h int e", doc.Text);
            for (i = 0; i < 2; i++)
            {
                Assert.AreEqual(CharClass.Normal, doc.GetCharClass(i));
            }
            for ( ; i < 5; i++)
            {
                Assert.AreEqual(CharClass.Keyword, doc.GetCharClass(i));
            }
            for ( ; i < 7; i++)
            {
                Assert.AreEqual(CharClass.Normal, doc.GetCharClass(i));
            }
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                doc.GetCharClass(7);
            });

            // "int" --> "if!"
            doc.Text = "int";
            h.Highlight(doc);
            for (i = 0; i < 3; i++)
            {
                Assert.AreEqual(CharClass.Keyword, doc.GetCharClass(i));
            }
            doc.Replace("f!", 1, 3);
            h.Highlight(doc);
            Assert.AreEqual("if!", doc.Text);
            for (i = 0; i < 2; i++)
            {
                Assert.AreEqual(CharClass.Keyword, doc.GetCharClass(i));
            }
            for ( ; i < 3; i++)
            {
                Assert.AreEqual(CharClass.Normal, doc.GetCharClass(i));
            }
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                doc.GetCharClass(3);
            });

            // "int" --> "inte"
            doc.Text = "int";
            h.Highlight(doc);
            for (i = 0; i < 3; i++)
            {
                Assert.AreEqual(CharClass.Keyword, doc.GetCharClass(i));
            }
            doc.Replace("e", 3, 3);
            h.Highlight(doc);
            Assert.AreEqual("inte", doc.Text);
            for (i = 0; i < 4; i++)
            {
                Assert.AreEqual(CharClass.Normal, doc.GetCharClass(i));
            }
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                doc.GetCharClass(4);
            });

            // "int" --> "interface"
            doc.Text = "int";
            h.Highlight(doc);
            for (i = 0; i < 3; i++)
            {
                Assert.AreEqual(CharClass.Keyword, doc.GetCharClass(i));
            }
            doc.Replace("erface", 3, 3);
            h.Highlight(doc);
            Assert.AreEqual("interface", doc.Text);
            for (i = 0; i < 9; i++)
            {
                Assert.AreEqual(CharClass.Keyword, doc.GetCharClass(i));
            }
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                doc.GetCharClass(10);
            });
        }
Ejemplo n.º 17
0
        public void WordDetection()
        {
            Document        doc      = new Document();
            DefaultWordProc wordProc = new DefaultWordProc();

            string[] samples = new string[] { "aa", "11", ",,", "\x3042\x3042", "\x963f\x963f", "\n\n", "  " };
            int[]    expected;
            int[]    expectedForSameClass;

            // NextWordStart
            expected             = new int[] { 0, 2, 2, 4, 4 };
            expectedForSameClass = new int[] { 0, 4, 4, 4, 4 };
            for (int left = 0; left < samples.Length; left++)
            {
                for (int right = 0; right < samples.Length; right++)
                {
                    doc.Text = samples[left] + samples[right];
                    for (int i = 0; i < doc.Length; i++)
                    {
                        int actual = wordProc.NextWordStart(doc, i);
                        if (left == right)
                        {
                            Assert.AreEqual(expectedForSameClass[i], actual);
                        }
                        else
                        {
                            Assert.AreEqual(expected[i], actual);
                        }
                    }
                }
            }
            Assert.AreEqual(doc.Length, wordProc.NextWordStart(doc, doc.Length));

            // combining character
            doc.Text = ".a\x0300.";
            Assert.AreEqual(1, wordProc.NextWordStart(doc, 1));
            Assert.AreEqual(3, wordProc.NextWordStart(doc, 2));
            Assert.AreEqual(3, wordProc.NextWordStart(doc, 3));

            // combining character
            doc.Text = "aa\x0300a";
            Assert.AreEqual(4, wordProc.NextWordStart(doc, 1));
            Assert.AreEqual(4, wordProc.NextWordStart(doc, 2));
            Assert.AreEqual(4, wordProc.NextWordStart(doc, 3));
            MyAssert.Throws <ArgumentNullException>(delegate {
                wordProc.NextWordStart(null, 0);
            });
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                wordProc.NextWordStart(doc, -1);
            });
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                wordProc.NextWordStart(doc, doc.Length + 1);
            });

            // NextWordEnd
            expected             = new int[] { 2, 2, 2, 4, 4 };
            expectedForSameClass = new int[] { 4, 4, 4, 4, 4 };
            for (int left = 0; left < samples.Length; left++)
            {
                for (int right = 0; right < samples.Length; right++)
                {
                    doc.Text = samples[left] + samples[right];
                    for (int i = 0; i < doc.Length; i++)
                    {
                        int actual = wordProc.NextWordEnd(doc, i);
                        if (left == right)
                        {
                            Assert.AreEqual(expectedForSameClass[i], actual);
                        }
                        else
                        {
                            Assert.AreEqual(expected[i], actual);
                        }
                    }
                }
            }
            Assert.AreEqual(doc.Length, wordProc.NextWordEnd(doc, doc.Length));

            // combining character
            doc.Text = ".a\x0300.";
            Assert.AreEqual(1, wordProc.NextWordEnd(doc, 1));
            Assert.AreEqual(3, wordProc.NextWordEnd(doc, 2));
            Assert.AreEqual(3, wordProc.NextWordEnd(doc, 3));

            // combining character
            doc.Text = "aa\x0300a";
            Assert.AreEqual(4, wordProc.NextWordEnd(doc, 1));
            Assert.AreEqual(4, wordProc.NextWordEnd(doc, 2));
            Assert.AreEqual(4, wordProc.NextWordEnd(doc, 3));
            MyAssert.Throws <ArgumentNullException>(delegate {
                wordProc.NextWordEnd(null, 0);
            });
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                wordProc.NextWordEnd(doc, -1);
            });
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                wordProc.NextWordEnd(doc, doc.Length + 1);
            });

            // PrevWordStart
            expected             = new int[] { 0, 0, 2, 2, 4 };
            expectedForSameClass = new int[] { 0, 0, 0, 0, 4 };
            for (int left = 0; left < samples.Length; left++)
            {
                for (int right = 0; right < samples.Length; right++)
                {
                    doc.Text = samples[left] + samples[right];
                    for (int i = 0; i < doc.Length; i++)
                    {
                        int actual = wordProc.PrevWordStart(doc, i);
                        if (left == right)
                        {
                            Assert.AreEqual(expectedForSameClass[i], actual);
                        }
                        else
                        {
                            Assert.AreEqual(expected[i], actual);
                        }
                    }
                }
            }
            Assert.AreEqual(doc.Length, wordProc.PrevWordStart(doc, doc.Length));

            // combining character
            doc.Text = ".a\x0300.";
            Assert.AreEqual(1, wordProc.PrevWordStart(doc, 1));
            Assert.AreEqual(1, wordProc.PrevWordStart(doc, 2));
            Assert.AreEqual(3, wordProc.PrevWordStart(doc, 3));

            // combining character
            doc.Text = "aa\x0300a";
            Assert.AreEqual(0, wordProc.PrevWordStart(doc, 1));
            Assert.AreEqual(0, wordProc.PrevWordStart(doc, 2));
            Assert.AreEqual(0, wordProc.PrevWordStart(doc, 3));
            MyAssert.Throws <ArgumentNullException>(delegate {
                wordProc.PrevWordStart(null, 0);
            });
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                wordProc.PrevWordStart(doc, -1);
            });
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                wordProc.PrevWordStart(doc, doc.Length + 1);
            });

            // PrevWordEnd
            expected             = new int[] { 0, 0, 2, 2, 4 };
            expectedForSameClass = new int[] { 0, 0, 0, 0, 4 };
            for (int left = 0; left < samples.Length; left++)
            {
                for (int right = 0; right < samples.Length; right++)
                {
                    doc.Text = samples[left] + samples[right];
                    for (int i = 0; i < doc.Length; i++)
                    {
                        int actual = wordProc.PrevWordEnd(doc, i);
                        if (left == right)
                        {
                            Assert.AreEqual(expectedForSameClass[i], actual);
                        }
                        else
                        {
                            Assert.AreEqual(expected[i], actual);
                        }
                    }
                }
            }
            Assert.AreEqual(doc.Length, wordProc.PrevWordEnd(doc, doc.Length));

            // combining character
            doc.Text = ".a\x0300.";
            Assert.AreEqual(1, wordProc.PrevWordEnd(doc, 1));
            Assert.AreEqual(1, wordProc.PrevWordEnd(doc, 2));
            Assert.AreEqual(3, wordProc.PrevWordEnd(doc, 3));

            // combining character
            doc.Text = "aa\x0300a";
            Assert.AreEqual(0, wordProc.PrevWordEnd(doc, 1));
            Assert.AreEqual(0, wordProc.PrevWordEnd(doc, 2));
            Assert.AreEqual(0, wordProc.PrevWordEnd(doc, 3));
            MyAssert.Throws <ArgumentNullException>(delegate {
                wordProc.PrevWordEnd(null, 0);
            });
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                wordProc.PrevWordEnd(doc, -1);
            });
            MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                wordProc.PrevWordEnd(doc, doc.Length + 1);
            });
        }
Ejemplo n.º 18
0
        public void GetTextInRange()
        {
            // keep it\r
            // as simple as possible\r\n
            // but\n
            // not simpler.
            Document doc = new Document();

            doc.Text = "keep it\ras simple as possible\r\nbut\nnot simpler.";

            // char-index type
            {
                // invalid range (before begin to middle)
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.GetTextInRange(-1, 4);
                });

                // valid range (begin to middle)
                Assert.AreEqual("ke", doc.GetTextInRange(0, 2));

                // valid range (middle to middle)
                Assert.AreEqual("ep it", doc.GetTextInRange(2, 7));

                // valid range (middle to end)
                Assert.AreEqual("simpler.", doc.GetTextInRange(39, 47));

                // invalid range (middle to after end)
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.GetTextInRange(39, 48);
                });

                // invalid range (minus range)
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.GetTextInRange(10, 9);
                });
            }

            // line/column index type
            {
                // invalid range (before begin to middle)
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.GetTextInRange(-1, -1, 1, 1);
                });
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.GetTextInRange(-1, 0, 1, 1);
                });
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.GetTextInRange(1, -1, 1, 1);
                });

                // valid range (empty range)
                Assert.AreEqual("", doc.GetTextInRange(0, 0, 0, 0));
                Assert.AreEqual("", doc.GetTextInRange(1, 1, 1, 1));

                // valid range (begin to middle)
                Assert.AreEqual("ke", doc.GetTextInRange(0, 0, 0, 2));
                Assert.AreEqual("ee", doc.GetTextInRange(0, 1, 0, 3));
                Assert.AreEqual("as", doc.GetTextInRange(1, 0, 1, 2));

                // valid range (middle to middle)
                Assert.AreEqual("s simple as possible\r\nbut\nn", doc.GetTextInRange(1, 1, 3, 1));

                // valid range (middle to end)
                Assert.AreEqual("t\nnot simpler.", doc.GetTextInRange(2, 2, 3, 12));

                // invalid range (middle to after end)
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.GetTextInRange(2, 2, 3, 13);
                });
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.GetTextInRange(2, 2, 4, 0);
                });
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.GetTextInRange(2, 2, 4, 13);
                });

                // invalid range (minus range)
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.GetTextInRange(1, 1, 1, 0);
                });
                MyAssert.Throws <ArgumentOutOfRangeException>(delegate {
                    doc.GetTextInRange(1, 1, 0, 0);
                });
            }
        }