Skip to content
Snippets Groups Projects
Tests.cs 19.1 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
using System;
using NUnit.Framework;
using Lexer;

namespace TestLexer
{
    [TestFixture]
    public class TestIntLexer
    {
        [Test]
        public void TestIntParse()
        {
            Lexer.Lexer l = new IntLexer("154216");
            Assert.IsTrue(l.Parse(), "Не разбирает 154216");

            l = new IntLexer("1");
            Assert.IsTrue(l.Parse(), "Не разбирает 1");

            l = new IntLexer("0");
            Assert.IsTrue(l.Parse(), "Не разбирает 0");

            l = new IntLexer("-0");
            Assert.IsTrue(l.Parse(), "Не разбирает -0");

            l = new IntLexer("+0");
            Assert.IsTrue(l.Parse(), "Не разбирает +0");

            l = new IntLexer("+11");
            Assert.IsTrue(l.Parse(), "Не разбирает +11");

            l = new IntLexer("-12");
            Assert.IsTrue(l.Parse(), "Не разбирает -12");
        }

        [Test]
        public void TestIntFailDot()
        {
            Lexer.Lexer l = new IntLexer("1.54216");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 1.54216");
        }

        [Test]
        public void TestIntFailSymbol()
        {
            Lexer.Lexer l = new IntLexer("а1");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает a1");
            l = new IntLexer("1a");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 1а");
        }

        [Test]
        public void TestIntFailEpty()
        {
            Lexer.Lexer l = new IntLexer("");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает пустую строку");
        }

        [Test]
        public void TestIntCollectNumber()
        {
            IntLexer lInt = new IntLexer("12");
            Assert.IsTrue(lInt.Parse());
            Assert.AreEqual(lInt.parseResult, 12, "Не собирает число 12");

            lInt = new IntLexer("-12");
            Assert.IsTrue(lInt.Parse());
            Assert.AreEqual(lInt.parseResult, -12, "Не собирает число -12");

            lInt = new IntLexer("+12");
            Assert.IsTrue(lInt.Parse());
            Assert.AreEqual(lInt.parseResult, 12, "Не собирает число +12");

            lInt = new IntLexer("+0");
            Assert.IsTrue(lInt.Parse());
            Assert.AreEqual(lInt.parseResult, 0, "Не собирает число +0");
        }
    }

    [TestFixture]
    public class TestIdLexer
    {
        [Test]
        public void TestIdParse()
        {
            IdentLexer l = new IdentLexer("abc22");
            Assert.IsTrue(l.Parse(), "Не разбирает аbc22");

            l = new IdentLexer("a");
            Assert.IsTrue(l.Parse(), "Не разбирает а");
        }

        [Test]
        public void TestIdEmpty()
        {
            IdentLexer l = new IdentLexer("");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает пустую строку");
        }

        [Test]
        public void TestIdCaps()
        {
            IdentLexer l = new IdentLexer("AAAAA");
            Assert.IsTrue(l.Parse(), "Не разбирает заглавные буквы");
        }

        [Test]
        public void TestIdNumbers()
        {
            IdentLexer l = new IdentLexer("a12345");
            Assert.IsTrue(l.Parse(), "Не разбирает цифры в конце");
        }

        [Test]
        public void TestIdUnderscore()
        {
            IdentLexer l = new IdentLexer("a12_5");
            Assert.IsTrue(l.Parse(), "Не разбирает _");
        }

        [Test]
        public void TestIdDot()
        {
            IdentLexer l = new IdentLexer("f67.3");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает точку");
        }

        [Test]
        public void TestIdDollar()
        {
            IdentLexer l = new IdentLexer("f67$3");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает $");
        }
    }

    [TestFixture]
    public class TestIntNotZeroLexer
    {
        [Test]
        public void TestIntNotZeroParse()
        {
            IntNoZeroLexer l = new IntNoZeroLexer("1234");
            Assert.IsTrue(l.Parse(), "Не разбирает 1234");
        }

        [Test]
        public void TestIntNotZeroFail()
        {
            IntNoZeroLexer l = new IntNoZeroLexer("01234");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 0");

            l = new IntNoZeroLexer("+01234");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает +0");

            l = new IntNoZeroLexer("-01234");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает -0");

            l = new IntNoZeroLexer("0");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 0");
        }

        [Test]
        public void TestIntNotZeroPass()
        {
            IntNoZeroLexer l = new IntNoZeroLexer("12034");
            Assert.IsTrue(l.Parse(), "Не разбирает 12034");

            l = new IntNoZeroLexer("+12034");
            Assert.IsTrue(l.Parse(), "Не разбирает +12034");

            l = new IntNoZeroLexer("12034");
            Assert.IsTrue(l.Parse(), "Не разбирает 12034");

            l = new IntNoZeroLexer("-12034");
            Assert.IsTrue(l.Parse(), "Не разбирает -12034");
        }

    }

    [TestFixture]
    public class TestLetterDigitLexer
    {

        [Test]
        public void TestLetterDigitParse()
        {
            LetterDigitLexer l = new LetterDigitLexer("a2f5j3");
            Assert.IsTrue(l.Parse(), "Не разбирает a2f5j3");

            l = new LetterDigitLexer("a");
            Assert.IsTrue(l.Parse(), "Не разбирает a");

            l = new LetterDigitLexer("a2");
            Assert.IsTrue(l.Parse(), "Не разбирает a2");

            l = new LetterDigitLexer("a2B");
            Assert.IsTrue(l.Parse(), "Не разбирает a2B");

        }

        [Test]
        public void TestLetterDigitFail()
        {
            LetterDigitLexer l = new LetterDigitLexer("a25j3");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает a25j3");

            l = new LetterDigitLexer("a2jb3");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает a2jb3");

            l = new LetterDigitLexer("2a3b");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 2a3b");

            l = new LetterDigitLexer("a2f 3b");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает a2f 3b");
        }
    }


    [TestFixture]
    public class TestLetterListLexer
    {
        [Test]
        public void TestLetterListParse()
        {
            LetterListLexer l = new LetterListLexer("a,b,c");
            Assert.IsTrue(l.Parse(), "Не пропускает a,b,c");
            Assert.AreEqual(l.ParseResult.Count, 3, "неправильно собран список-результат");
            Assert.Contains('a', l.ParseResult);
            Assert.Contains('b', l.ParseResult);
            Assert.Contains('c', l.ParseResult);

            l = new LetterListLexer("a");
            Assert.IsTrue(l.Parse(), "Не пропускает a");
            Assert.AreEqual(l.ParseResult.Count, 1, "неправильно собран список-результат");
            Assert.Contains('a', l.ParseResult);

            l = new LetterListLexer("a,B");
            Assert.IsTrue(l.Parse(), "Не пропускает a,B");
            Assert.AreEqual(l.ParseResult.Count, 2, "неправильно собран список-результат");
            Assert.Contains('a', l.ParseResult);
            Assert.Contains('B', l.ParseResult);

            l = new LetterListLexer("a;B");
            Assert.IsTrue(l.Parse(), "Не пропускает a;B");

            l = new LetterListLexer("a,B;c");
            Assert.IsTrue(l.Parse(), "Не пропускает a,B;c");

            l = new LetterListLexer("a;b,c;d");
            Assert.IsTrue(l.Parse(), "Не пропускает a;b,c;d");

        }

        [Test]
        public void TestLetterListFail()
        {
            LetterListLexer l = new LetterListLexer("a,b,");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает a,b,");

            l = new LetterListLexer(",b");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает ,b");

            l = new LetterListLexer(";b");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает ;b");

            l = new LetterListLexer("B;");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает B;");

            l = new LetterListLexer("a,,b");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает a,,b");

            l = new LetterListLexer("a;;b");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает a;;b");

            l = new LetterListLexer("a;1");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает цифры");
        }

    }

    [TestFixture]
    public class TestDigitListLexer
    {
        [Test]
        public void TestDigitListParse()
        {
            DigitListLexer l = new DigitListLexer("1 2");
            Assert.IsTrue(l.Parse(), "Не пропускает 1 2");
            Assert.AreEqual(l.ParseResult.Count, 2, "неправильно собран список-результат");
            Assert.Contains(1, l.ParseResult);
            Assert.Contains(2, l.ParseResult);

            l = new DigitListLexer("1");
            Assert.IsTrue(l.Parse(), "Не пропускает 1");
            Assert.AreEqual(l.ParseResult.Count, 1, "неправильно собран список-результат");
            Assert.Contains(1, l.ParseResult);

            l = new DigitListLexer("5    6");
            Assert.IsTrue(l.Parse(), "Не пропускает 5    6");
            Assert.AreEqual(l.ParseResult.Count, 2, "неправильно собран список-результат");
            Assert.Contains(5, l.ParseResult);
            Assert.Contains(6, l.ParseResult);
        }

        [Test]
        public void TestDigitListFail()
        {
            DigitListLexer l = new DigitListLexer("12");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 12");

            l = new DigitListLexer(" 1");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает пробелы в начале");

            l = new DigitListLexer("1  ");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает пробелы в конце");

            l = new DigitListLexer("1, 2");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает ,");

            l = new DigitListLexer("1;2");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает ;");

            l = new DigitListLexer("1a2");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 1a2");
        }

    }

    [TestFixture]
    public class TestLetterDigitGroupLexer
    {
        [Test]
        public void TestLetterDigitGroupParse()
        {
            LetterDigitGroupLexer l = new LetterDigitGroupLexer("aa12ab23");
            Assert.IsTrue(l.Parse(), "Не пропускает aa12ab23");
            Assert.AreEqual(l.ParseResult, "aa12ab23", "неправильно собран aa12ab23");

            l = new LetterDigitGroupLexer("a1b2");
            Assert.IsTrue(l.Parse(), "Не пропускает a1b2");
            Assert.AreEqual(l.ParseResult, "a1b2", "неправильно собран a1b2");

            l = new LetterDigitGroupLexer("aa");
            Assert.IsTrue(l.Parse(), "Не пропускает aa");
            Assert.AreEqual(l.ParseResult, "aa", "неправильно собран aa");

            l = new LetterDigitGroupLexer("A11");
            Assert.IsTrue(l.Parse(), "Не пропускает A11");
            Assert.AreEqual(l.ParseResult, "A11", "неправильно собран A11");

            l = new LetterDigitGroupLexer("ab33cd58e3n5ss32");
            Assert.IsTrue(l.Parse(), "Не пропускает ab33cd58e3n5ss32");
            Assert.AreEqual(l.ParseResult, "ab33cd58e3n5ss32", "неправильно собран ab33cd58e3n5ss32");
        }

        [Test]
        public void TestLetterDigitGroupFail()
        {
            LetterDigitGroupLexer l = new LetterDigitGroupLexer("1a");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 1a");

            l = new LetterDigitGroupLexer("a111b");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает a111b");

            l = new LetterDigitGroupLexer("A_34b");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает A_34b");

            l = new LetterDigitGroupLexer("");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает пустую строку");

            l = new LetterDigitGroupLexer("11");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 11");

            l = new LetterDigitGroupLexer("_A34");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает _A34");
        }
    }

    [TestFixture]
    public class TestDoubleLexer
    {
        [Test]
        public void TestDoubleParse()
        {

            DoubleLexer l = new DoubleLexer("123.4");
            Assert.IsTrue(l.Parse(), "Не понимает 123.4");
            Assert.AreEqual(l.ParseResult, 123.4, "Неправильно прочитал 123.4");

            l = new DoubleLexer("123");
            Assert.IsTrue(l.Parse(), "Не понимает 123");
            Assert.AreEqual(l.ParseResult, 123, "Неправильно прочитал 123");

            l = new DoubleLexer("0.4");
            Assert.IsTrue(l.Parse(), "Не понимает 0.4");
            Assert.AreEqual(l.ParseResult, 0.4, "Неправильно прочитал 0.4");

            l = new DoubleLexer("0.4");
            Assert.IsTrue(l.Parse(), "Не понимает 0.4");
            Assert.AreEqual(l.ParseResult, 0.4, "Неправильно прочитал 0.4");

        }

        [Test]
        public void TestDoubleFail()
        {
            DoubleLexer l = new DoubleLexer(".4");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает .4");

            l = new DoubleLexer("4.");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 4.");

            l = new DoubleLexer(".");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает .");

            l = new DoubleLexer("");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает пустую строку");
        }

    }

    [TestFixture]
    public class TestQuotedStringLexer
    {

        [Test]
        public void TestQuotedStringParse()
        {
            StringLexer l = new StringLexer("''");
            Assert.IsTrue(l.Parse(), "Не пропускает ''");

            l = new StringLexer("'aa#2N3@_3-x//45'");
            Assert.IsTrue(l.Parse(), "Не пропускает 'aa#2N3@_3-x//45'");

            l = new StringLexer("'23 3 a'");
            Assert.IsTrue(l.Parse(), "Не пропускает '23 3 a'");
        }

        [Test]
        public void TestQuotedStringFail()
        {
            StringLexer l = new StringLexer("'");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает '");

            l = new StringLexer("aa'");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает aa'");

            l = new StringLexer("b 'add'");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает b 'add'");

            l = new StringLexer("'add' d");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 'add' d");
        }

    }

    [TestFixture]
    public class TestCommentLexer
    {

        [Test]
        public void TestCommentParse()
        {
            CommentLexer l = new CommentLexer("/**/");
            Assert.IsTrue(l.Parse(), "Не пропускает /**/");

            l = new CommentLexer("/*as3 @4&*_ -dd %~f*/");
            Assert.IsTrue(l.Parse(), "Не пропускает /*as3 @4&*_ -dd %~f*/");

            l = new CommentLexer("/*asda \n */");
            Assert.IsTrue(l.Parse(), "Не пропускает перенос строки");
        }

        [Test]
        public void TestCommentFail()
        {
            CommentLexer l = new CommentLexer("*/");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает */'");

            l = new CommentLexer("/*");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает /*'");

            l = new CommentLexer("/* \n");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает */ \n'");

            l = new CommentLexer("/**/*/");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает /**/*/'");
        }

    }
    
    [TestFixture]
    public class TestIdChainLexer
    {
        [Test]
        public void TestIdChainParse()
        {
            IdentChainLexer l = new IdentChainLexer("Id1");
            Assert.IsTrue(l.Parse(), "Не пропускает Id1");
            
            l = new IdentChainLexer("Id1.Id2");
            Assert.IsTrue(l.Parse(), "Не пропускает Id1.Id2");
            
            l = new IdentChainLexer("uUd.k_22.sa3");
            Assert.IsTrue(l.Parse(), "Не пропускает uUd.k_22.sa3");
        }
        
        [Test]
        public void TestIdChainFail()
        {
            IdentChainLexer l = new IdentChainLexer("3Id1");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 3Id1");
            
            l = new IdentChainLexer(".Id2");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает .Id2");
            
            l = new IdentChainLexer("uUd.");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает uUd.");
            
            l = new IdentChainLexer("uUd.3sa");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает uUd.3sa");
            
            l = new IdentChainLexer("uUd. _sa");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает uUd. _sa");
            
            l = new IdentChainLexer("uUd,sa");
            Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает uUd,sa");
            
        }
    }
}