示例#1
0
        public void RegexFormatterTest()
        {
            try
            {
                var f = new RegexFormatter {
                    False = "F", ValueWhenNull = "Y", Pattern = "  ", True = "Y"
                };
                f.Formate("");
                throw new Exception("TEST FAILED.");
            }
            catch (SpiderException se)
            {
                Assert.Equal("Pattern should not be null or empty.", se.Message);
            }
            try
            {
                var f = new RegexFormatter {
                    False = "F", ValueWhenNull = "Y", Pattern = "", True = "Y"
                };
                f.Formate("");
                throw new Exception("TEST FAILED.");
            }
            catch (SpiderException se)
            {
                Assert.Equal("Pattern should not be null or empty.", se.Message);
            }
            try
            {
                var f = new RegexFormatter {
                    False = "F", ValueWhenNull = "Y", Pattern = null, True = "Y"
                };
                f.Formate("");
                throw new Exception("TEST FAILED.");
            }
            catch (SpiderException se)
            {
                Assert.Equal("Pattern should not be null or empty.", se.Message);
            }

            RegexFormatter formatter1 = new RegexFormatter {
                False = "F", ValueWhenNull = "Y", Pattern = "很抱歉", True = "Y"
            };
            string str1 = "";

            Assert.Equal("Y", formatter1.Formate(str1));

            RegexFormatter formatter2 = new RegexFormatter {
                False = "F", ValueWhenNull = "F", Pattern = "很抱歉", True = "Y"
            };

            Assert.Equal("F", formatter2.Formate(str1));

            string str2 = "ABCD";

            Assert.Equal("F", formatter2.Formate(str2));
            Assert.Equal("F", formatter1.Formate(str2));

            string str3 = "ABCD很抱歉";

            Assert.Equal("Y", formatter2.Formate(str3));
            Assert.Equal("Y", formatter1.Formate(str3));

            string str4 = "很抱歉ABCD";

            Assert.Equal("Y", formatter2.Formate(str4));
            Assert.Equal("Y", formatter1.Formate(str4));

            string str5 = "ABCD很抱歉ABCD";

            Assert.Equal("Y", formatter2.Formate(str5));
            Assert.Equal("Y", formatter1.Formate(str5));

            string str6 = "       很抱歉ABCD";

            Assert.Equal("Y", formatter2.Formate(str6));
            Assert.Equal("Y", formatter1.Formate(str6));

            RegexFormatter formatter3 = new RegexFormatter {
                Pattern = "很抱歉", True = "Y"
            };

            Assert.Equal(str2, formatter3.Formate(str2));
            Assert.Equal("Y", formatter3.Formate(str3));

            RegexFormatter formatter4 = new RegexFormatter {
                Pattern = "很抱歉", False = "N"
            };

            Assert.Equal("N", formatter4.Formate(str2));
            Assert.Equal("很抱歉", formatter4.Formate(str3));

            string         str7       = "收货100人啊啊";
            RegexFormatter formatter5 = new RegexFormatter {
                Pattern = @"收货[\d]+人"
            };

            Assert.Equal("ABCD", formatter5.Formate(str2));
            Assert.Equal("ABCD很抱歉", formatter5.Formate(str3));
            Assert.Equal("收货100人", formatter5.Formate(str7));
        }
示例#2
0
        public void RegexFormatter()
        {
            try
            {
                var f = new RegexFormatter {
                    False = "F", Default = "Y", Pattern = "  ", True = "Y"
                };
                f.Format("");
                throw new Exception("TEST FAILED.");
            }
            catch (ArgumentException se)
            {
                Assert.Equal("Pattern should not be null or empty", se.Message);
            }

            try
            {
                var f = new RegexFormatter {
                    False = "F", Default = "Y", Pattern = "", True = "Y"
                };
                f.Format("");
                throw new Exception("TEST FAILED.");
            }
            catch (ArgumentException se)
            {
                Assert.Equal("Pattern should not be null or empty", se.Message);
            }

            try
            {
                var f = new RegexFormatter {
                    False = "F", Default = "Y", Pattern = null, True = "Y"
                };
                f.Format("");
                throw new Exception("TEST FAILED.");
            }
            catch (ArgumentException se)
            {
                Assert.Equal("Pattern should not be null or empty", se.Message);
            }

            var formatter1 = new RegexFormatter {
                False = "F", Default = "Y", Pattern = "很抱歉", True = "Y"
            };
            var str1 = "";

            Assert.Equal("F", formatter1.Format(str1));

            var formatter2 = new RegexFormatter {
                False = "F", Default = "F", Pattern = "很抱歉", True = "Y"
            };

            Assert.Equal("F", formatter2.Format(str1));

            var str2 = "ABCD";

            Assert.Equal("F", formatter2.Format(str2));
            Assert.Equal("F", formatter1.Format(str2));

            var str3 = "ABCD很抱歉";

            Assert.Equal("Y", formatter2.Format(str3));
            Assert.Equal("Y", formatter1.Format(str3));

            var str4 = "很抱歉ABCD";

            Assert.Equal("Y", formatter2.Format(str4));
            Assert.Equal("Y", formatter1.Format(str4));

            var str5 = "ABCD很抱歉ABCD";

            Assert.Equal("Y", formatter2.Format(str5));
            Assert.Equal("Y", formatter1.Format(str5));

            var str6 = "       很抱歉ABCD";

            Assert.Equal("Y", formatter2.Format(str6));
            Assert.Equal("Y", formatter1.Format(str6));

            var formatter3 = new RegexFormatter {
                Pattern = "很抱歉", True = "Y"
            };

            Assert.Equal("", formatter3.Format(str2));
            Assert.Equal("Y", formatter3.Format(str3));

            var formatter4 = new RegexFormatter {
                Pattern = "很抱歉", False = "N"
            };

            Assert.Equal("N", formatter4.Format(str2));
            Assert.Equal("很抱歉", formatter4.Format(str3));

            var str7       = "收货100人啊啊";
            var formatter5 = new RegexFormatter {
                Pattern = @"收货[\d]+人"
            };

            Assert.Equal("", formatter5.Format(str2));
            Assert.Equal("", formatter5.Format(str3));
            Assert.Equal("收货100人", formatter5.Format(str7));
        }