Ejemplo n.º 1
0
        public void TestNumeric()
        {
            var tnumeric = new TNumeric("NUM");

            tnumeric.Set("0,255");
            Assert.AreEqual(0.255f, tnumeric.value);

            tnumeric.Set("");
            Assert.AreEqual(0f, tnumeric.value);

            var tsum = new TNumeric("SUM")
            {
                function = TNumeric.getFuncByString("SUM")
            };

            tsum.Set("0,5");
            tsum.Set("1,5");
            Assert.AreEqual(2f, tsum.value);

            tsum = new TNumeric("WAT")
            {
                function = TNumeric.getFuncByString("Invalid?")
            };
            tsum.Set("0,5");
            tsum.Set("1,5");
            Assert.AreNotEqual(2f, tsum.value);
        }
Ejemplo n.º 2
0
        public void TestRegExReplace()
        {
            var tstr = new TVariable("")
            {
                regex_pattern = new Regex(@"(\S+)а,\s*(\S+)"),
                regex_replace = "район $2 города $1ы"
            };

            tstr.Set("Москва, ЦАО");
            Assert.AreEqual("район ЦАО города Москвы", tstr.value);


            var tnumeric = new TNumeric("")
            {
                regex_pattern = new Regex(@"(\d)(\d)(\d)"),
                regex_replace = "$2$3$1"
            };

            ((TVariable)tnumeric).Set("123");
            Assert.AreEqual(231f, tnumeric.value);


            var tdate = new TDate("")
            {
                regex_pattern = new Regex(@".* (\d{4}) года за (\d) месяц"),
                regex_replace = "1.$2.$1",
                format        = "d.M.yyyy",
                lastday       = true
            };

            tdate.Set("отчёт 2020 года за 2 месяц");
            Assert.AreEqual(new DateTime(2020, 2, 29), tdate.value);
        }
Ejemplo n.º 3
0
        public static Dictionary <string, TVariable> getVariables()
        {
            Dictionary <string, TVariable> data = new Dictionary <string, TVariable>();

            var tvariable = new TVariable("FIO");

            tvariable.Set("Ivanov Ivan Ivanovich");
            data.Add(tvariable.name, tvariable);

            var tnumeric = new TNumeric("SUMMA");

            tnumeric.Set(12.3456f);
            data.Add(tnumeric.name, tnumeric);

            var tdate = new TDate("DATE");

            tdate.Set("22.11.2001");
            data.Add(tdate.name, tdate);

            return(data);
        }
Ejemplo n.º 4
0
        public void TestRegEx()
        {
            var tstr = new TVariable("")
            {
                regex_pattern = new Regex("(\\S+) округа города (\\S+)"),
                regex_group   = 2
            };

            tstr.Set("Центрального округа города Москвы");
            Assert.AreEqual("Москвы", tstr.value);

            tstr.Set("Не подходящий условию текст");
            Assert.AreEqual("", tstr.value);

            tstr.regex_group = 4;
            tstr.Set("Центрального округа города Москвы");
            Assert.AreEqual("", tstr.value);


            var tnumeric = new TNumeric("")
            {
                regex_pattern = new Regex("Итого (\\S+)р.")
            };

            ((TVariable)tnumeric).Set("Итого 800,45р. начислено");
            Assert.AreEqual(800.45f, tnumeric.value);


            var tdate = new TDate("")
            {
                regex_pattern = new Regex("основан (.*) год"),
                format        = "dd MMMM yyyy"
            };

            ((TVariable)tdate).Set("Санкт-Петербург был основан 27 мая 1703 года");
            Assert.AreEqual(new DateTime(1703, 05, 27), tdate.value);
        }
Ejemplo n.º 5
0
        protected TVariable getVar(XElement xml, bool dynamic)
        {
            var name  = xml.Attribute("name")?.Value ?? throw new NullReferenceException("Variable attribute 'name' can't be null!");
            var ctype = xml.Attribute("type")?.Value ?? "string";

            TVariable variable;

            switch (ctype)
            {
            case "numeric":
                variable = new TNumeric(name);
                break;

            case "date":
                variable = new TDate(name);
                break;

            default:
                variable = new TVariable(name);
                break;
            }

            variable.x = Int32.Parse(xml.Attribute("X")?.Value ?? throw new NullReferenceException("Variable attribute 'X' can't be null!"));
            if (!dynamic)
            {
                variable.y = Int32.Parse(xml.Attribute("Y")?.Value ?? throw new NullReferenceException("Variable attribute 'Y' can't be null!"));
            }
            variable.dynamic = dynamic;

            if (variable is TNumeric tnumeric)
            {
                var function = xml.Attribute("function");

                if (function != null)
                {
                    tnumeric.function = TNumeric.getFuncByString(function.Value);
                }
            }

            if (variable is TDate tdate)
            {
                var lastday  = xml.Attribute("lastday");
                var language = xml.Attribute("language");
                var format   = xml.Attribute("format");

                if (lastday != null)
                {
                    tdate.lastday = Boolean.Parse(lastday.Value);
                }
                if (language != null)
                {
                    tdate.language = language.Value;
                }
                if (format != null)
                {
                    tdate.format = format.Value;
                }
            }

            var regex_pattern = xml.Attribute("regex_pattern");

            if (regex_pattern != null)
            {
                variable.regex_pattern = new Regex(regex_pattern.Value, RegexOptions.Compiled);

                var regex_group = xml.Attribute("regex_group");
                variable.regex_group = int.Parse(regex_group?.Value ?? "1");
            }
            return(variable);
        }