Пример #1
0
        public static ValidateLogic JsonLogic()
        {
            ValidateLogic VL             = new ValidateLogic("Main");
            Area          skipChars      = new Area("SkipArea");
            Area          objectArea     = new Area("ObjectArea");
            Area          arrayArea      = new Area("ArrayArea");
            Area          valueArea      = new Area("ValueArea");
            Area          propertiesArea = new Area("PropertiesArea");
            Area          stringArea     = new Area("StringArea");

            VL.Areas.Add(skipChars);
            VL.Areas.Add(objectArea);
            VL.Areas.Add(arrayArea);
            VL.Areas.Add(valueArea);
            VL.Areas.Add(propertiesArea);
            VL.Areas.Add(stringArea);

            //StringArea
            UnitSet us12 = new UnitSet(CharUnits.QuotationMark);

            stringArea.StartNode = us12;
            List <char> excludeChars = new List <char>
            {
                '\\', '\"'
            };

            for (int i = 0; i < 32; i++)
            {
                excludeChars.Add((char)i);
            }
            AnyStringPart asp1 = new AnyStringPart(null, stringArea, "asp1", new List <char> {
                '\\', '\"'
            }, excludeChars, 0, 0);

            us12.NextNode = asp1;
            Status st4 = new Status("st4");

            asp1.NextNode = st4;
            st4.Choices.Add(new Choice(new UnitSet(CharUnits.BackSlash)));
            st4.Choices.Add(new Choice(new UnitSet(CharUnits.QuotationMark)));
            st4.Choices[1].Node.NextNode = EndNode.Instance;
            Status st5 = new Status("st5");

            st4.Choices[0].Node.NextNode = st5;
            st5.Choices.Add(new Choice(new UnitSet(new CharUnit('b'))));
            st5.Choices.Add(new Choice(new UnitSet(new CharUnit('f'))));
            st5.Choices.Add(new Choice(new UnitSet(new CharUnit('n'))));
            st5.Choices.Add(new Choice(new UnitSet(new CharUnit('r'))));
            st5.Choices.Add(new Choice(new UnitSet(new CharUnit('t'))));
            st5.Choices.Add(new Choice(new UnitSet(CharUnits.Slash)));
            st5.Choices.Add(new Choice(new UnitSet(CharUnits.BackSlash)));
            st5.Choices.Add(new Choice(new UnitSet(CharUnits.QuotationMark)));
            UnitSet unicodeSet = new UnitSet(new CharUnit('u'));

            unicodeSet.Units.Add(CharUnits.HexdecimalDigit);
            unicodeSet.Units.Add(CharUnits.HexdecimalDigit);
            unicodeSet.Units.Add(CharUnits.HexdecimalDigit);
            unicodeSet.Units.Add(CharUnits.HexdecimalDigit);
            st5.Choices.Add(new Choice(unicodeSet));
            for (int i = 0; i < st5.Choices.Count; i++)
            {
                st5.Choices[i].Node.NextNode = asp1;
            }

            //Skip Area
            UnitSet us  = new UnitSet(CharUnits.WhiteSpace, skipChars);
            UnitSet us2 = new UnitSet(CharUnits.CarriageReturn, skipChars);
            UnitSet us3 = new UnitSet(CharUnits.LineFeed, skipChars);
            UnitSet us4 = new UnitSet(CharUnits.HorizontalTab, skipChars);
            Status  st1 = new Status();

            st1.Choices.Add(new Choice(us));
            st1.Choices.Add(new Choice(us2));
            st1.Choices.Add(new Choice(us3));
            st1.Choices.Add(new Choice(us4));
            st1.Choices.Add(Choice.EndChoice);
            skipChars.StartNode = st1;
            us.NextNode         = us2.NextNode = us3.NextNode = us4.NextNode = skipChars.StartNode;

            //Object Area
            UnitSet   leftCurlBracket  = new UnitSet(CharUnits.LeftCurlyBracket);
            UnitSet   rightCurlBracket = new UnitSet(CharUnits.RightCurlyBracket);
            AreaStart skSt             = new AreaStart(skipChars, objectArea);

            objectArea.StartNode = leftCurlBracket;
            AreaStart paSt = new AreaStart(propertiesArea, objectArea);

            leftCurlBracket.NextNode = skSt;
            skSt.NextNode            = paSt;
            skSt                      = new AreaStart(skipChars, objectArea);
            paSt.NextNode             = skSt;
            skSt.NextNode             = rightCurlBracket;
            rightCurlBracket.NextNode = EndNode.Instance;

            //ArrayArea
            UnitSet leftSquareBracket  = new UnitSet(CharUnits.LeftSquareBracket);
            UnitSet rightSquareBracket = new UnitSet(CharUnits.RightSquareBracket);

            arrayArea.StartNode = leftSquareBracket;
            skSt = new AreaStart(skipChars, arrayArea);
            leftSquareBracket.NextNode = skSt;
            AreaStart vaSt = new AreaStart(valueArea, arrayArea);

            skSt.NextNode = vaSt;
            skSt          = new AreaStart(skipChars, arrayArea);
            vaSt.NextNode = skSt;
            Status st2 = new Status("array_st2", arrayArea);

            skSt.NextNode = st2;
            UnitSet us5 = new UnitSet(CharUnits.Comma, arrayArea);

            st2.Choices.Add(new Choice(us5));
            st2.Choices.Add(new Choice(rightSquareBracket));
            skSt          = new AreaStart(skipChars, arrayArea);
            us5.NextNode  = skSt;
            skSt.NextNode = vaSt;
            rightSquareBracket.NextNode = EndNode.Instance;

            //Value Area
            CharsToBooleanPart cbp = new CharsToBooleanPart();

            cbp.Parent = valueArea;
            CharsToDoublePart cdp = new CharsToDoublePart();

            cdp.Parent = valueArea;
            CharsToIntegerPart cip = new CharsToIntegerPart();

            cip.Parent = valueArea;
            AreaStart stSt = new AreaStart(stringArea, valueArea);
            Status    st3  = new Status();

            st3.Choices.Add(new Choice("null".ToUnitSet()));
            st3.Choices.Add(new Choice(cbp));
            st3.Choices.Add(new Choice(cip));
            st3.Choices.Add(new Choice(cdp));
            AreaStart oaSt = new AreaStart(objectArea, valueArea);

            st3.Choices.Add(new Choice(oaSt));
            AreaStart arSt = new AreaStart(arrayArea, valueArea);

            st3.Choices.Add(new Choice(arSt));
            st3.Choices.Add(new Choice(stSt));
            for (int i = 0; i < st3.Choices.Count; i++)
            {
                st3.Choices[i].Node.NextNode = EndNode.Instance;
            }
            valueArea.StartNode = st3;

            //Properties Area
            stSt = new AreaStart(stringArea, propertiesArea);
            propertiesArea.StartNode = stSt;
            skSt          = new AreaStart(skipChars, propertiesArea);
            stSt.NextNode = skSt;
            UnitSet us8 = new UnitSet(CharUnits.Colon, propertiesArea);

            skSt.NextNode = us8;
            skSt          = new AreaStart(skipChars, propertiesArea);
            us8.NextNode  = skSt;
            vaSt          = new AreaStart(valueArea, propertiesArea);
            skSt.NextNode = vaSt;
            Status st6 = new Status("pa_st6");

            vaSt.NextNode = st6;
            st6.Choices.Add(Choice.EndChoice);
            skSt = new AreaStart(skipChars, propertiesArea);
            st6.Choices.Add(new Choice(skSt, null, 4));
            UnitSet us9 = new UnitSet(CharUnits.Comma, propertiesArea);

            skSt.NextNode = us9;
            skSt          = new AreaStart(skipChars, propertiesArea);
            us9.NextNode  = skSt;
            skSt.NextNode = propertiesArea.StartNode;

            //Start Main
            skSt         = new AreaStart(skipChars, VL, null, "Main_SKIP1");
            VL.StartNode = skSt;
            Status JsonStartStatus = new Status("Main_ST_Object_Or_Array", VL);

            skSt.NextNode = JsonStartStatus;
            AreaStart ap1 = new AreaStart(objectArea, VL, null, "Main_AS_ObjectArea");
            AreaStart ap2 = new AreaStart(arrayArea, VL, null, "Main_AR_ArrayArea");

            skSt          = new AreaStart(skipChars, VL, null, "Main_SKIP2");
            ap1.NextNode  = ap2.NextNode = skSt;
            skSt.NextNode = EndNode.Instance;
            JsonStartStatus.Choices.Add(new Choice(ap1));
            JsonStartStatus.Choices.Add(new Choice(ap2));
            return(VL);
        }
Пример #2
0
        public void AnyStringTest()
        {
            List <ObjectConst> result;
            AnyStringPart      asp = new AnyStringPart();

            Assert.IsTrue(asp.Validate("1`6dsa8/05".ToObjectList()) == 10);
            Assert.IsTrue(asp.Validate("A-5.5".ToObjectList()) == 5);
            Assert.IsTrue(asp.Validate(new List <ObjectConst> {
                new StringConst("A-rdddv J")
            }) == 1);
            Assert.IsTrue(asp.Validate("136\"d56\"3".ToObjectList()) == 3);
            Assert.IsTrue(asp.Validate("-1\n75\n5535dd".ToObjectList()) == 12);
            Assert.IsTrue(asp.Validate(new List <ObjectConst> {
                new LongConst(7)
            }) == 0);
            Assert.IsTrue(asp.Validate(asp.Random()) != -1);
            Assert.IsTrue(asp.Validate(asp.Random()) != -1);
            TestContext.WriteLine(asp.Random().ForEachToString());
            asp = new AnyStringPart(null, null, null, "EndFile");
            Assert.IsTrue(asp.Validate(asp.Random()) != -1);
            Assert.IsTrue(asp.Validate(asp.Random()) != -1);
            TestContext.WriteLine(asp.Random().ForEachToString());
            asp.MaxLength = 20;
            Assert.IsTrue(asp.Validate(asp.Random()) != -1);
            Assert.IsTrue(asp.Validate(asp.Random()) != -1);
            TestContext.WriteLine(asp.Random().ForEachToString());
            asp = new AnyStringPart(null, null, null, null, new List <string> {
                "AA", "BB", "CCC"
            }, 10, 200);
            Assert.IsTrue(asp.Validate("rrrd145ddaabb".ToObjectList()) == 13);
            Assert.IsTrue(asp.Validate("dd64416d".ToObjectList()) == -1);
            Assert.IsTrue(asp.Validate("dd6t4416dBDDBB".ToObjectList()) == 14);
            Assert.IsTrue(asp.Validate("dd64416d465dsa534586c89das".ToObjectList()) == 26);
            result = asp.Random();
            Assert.IsTrue(asp.Validate(result) != -1);
            TestContext.WriteLine(result.Count.ToString());
            result = asp.Random();
            Assert.IsTrue(asp.Validate(result) != -1);
            TestContext.WriteLine(result.Count.ToString());

            asp = new AnyStringPart();
            asp.EscapeChars.Clear();
            asp.MaxLength = 10;
            asp.MinLength = 5;
            Assert.IsTrue(asp.Validate("rrrdddaaabvbb".ToObjectList()) == -1);
            Assert.IsTrue(asp.Validate("ddd".ToObjectList()) == -1);

            result = asp.Random();
            Assert.IsTrue(result.Count >= 5 && result.Count <= 10);
            Assert.IsTrue(asp.Validate(result) != -1);
            TestContext.WriteLine(result.Count.ToString());
            result = asp.Random();
            Assert.IsTrue(result.Count >= 5 && result.Count <= 10);
            Assert.IsTrue(asp.Validate(result) != -1);
            TestContext.WriteLine(result.Count.ToString());
            result = asp.Random();
            Assert.IsTrue(result.Count >= 5 && result.Count <= 10);
            Assert.IsTrue(asp.Validate(result) != -1);
            TestContext.WriteLine(result.Count.ToString());
            result = asp.Random();
            Assert.IsTrue(result.Count >= 5 && result.Count <= 10);
            Assert.IsTrue(asp.Validate(result) != -1);
            TestContext.WriteLine(result.Count.ToString());
            TestContext.WriteLine(asp.Random().ForEachToString());
            asp.MaxLength = 200;
            result        = asp.Random();
            Assert.IsTrue(result.Count >= 5 && result.Count <= 200);
            Assert.IsTrue(asp.Validate(result) != -1);
            TestContext.WriteLine(result.Count.ToString());
            result = asp.Random();
            Assert.IsTrue(result.Count >= 5 && result.Count <= 200);
            Assert.IsTrue(asp.Validate(result) != -1);
            TestContext.WriteLine(result.Count.ToString());
            result = asp.Random();
            Assert.IsTrue(result.Count >= 5 && result.Count <= 200);
            Assert.IsTrue(asp.Validate(result) != -1);
            TestContext.WriteLine(result.Count.ToString());
            asp.RandomEndChance = 0.01d;
            result = asp.Random();
            Assert.IsTrue(result.Count >= 5 && result.Count <= 200);
            Assert.IsTrue(asp.Validate(result) != -1);
            TestContext.WriteLine(result.Count.ToString());
            result = asp.Random();
            Assert.IsTrue(result.Count >= 5 && result.Count <= 200);
            Assert.IsTrue(asp.Validate(result) != -1);
            TestContext.WriteLine(result.Count.ToString());
            result = asp.Random();
            Assert.IsTrue(result.Count >= 5 && result.Count <= 200);
            Assert.IsTrue(asp.Validate(result) != -1);
            TestContext.WriteLine(result.Count.ToString());

            Assert.ThrowsException <ArgumentOutOfRangeException>(() => asp.RandomEndChance = -0.2);
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => asp.RandomEndChance = 2.3);
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => asp.MinLength       = -9);
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => asp.MaxLength       = -7);
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => asp.MinLength       = 255);
        }