Ejemplo n.º 1
0
        public void IsGenericAssignableFromTest()
        {
            Assert.IsTrue(typeof(IEnumerable <>).IsGenericAssignableFrom(typeof(List <>)));
            Assert.IsTrue(typeof(List <>).IsGenericAssignableFrom(typeof(List <string>)));

            ExceptionAssert.IsException <ArgumentException>(() =>
                                                            (typeof(string)).IsGenericAssignableFrom(typeof(int)));
        }
Ejemplo n.º 2
0
 public void CheckFileExistsTest()
 {
     ExceptionAssert.IsException <ArgumentNullException>(() => ((string)null).CheckFileExists("param"));
     ShimFile.ExistsString = m => false;
     ExceptionAssert.IsException <FileNotFoundException>(() => "filename".CheckFileExists("param"));
     ShimFile.ExistsString = m => true;
     "filename".CheckFileExists("param");
 }
Ejemplo n.º 3
0
 public void CheckGreaterThanTest()
 {
     ExceptionAssert.IsException <ArgumentOutOfRangeException>(() => 5.CheckGreaterThan("param", 6));
     ExceptionAssert.IsException <ArgumentOutOfRangeException>(() => 5.CheckGreaterThan("param", 5));
     ExceptionAssert.IsException <ArgumentOutOfRangeException>(() => 5.CheckGreaterThan("param", 6, true));
     5.CheckGreaterThan("param", 4);
     5.CheckGreaterThan("param", 5, true);
 }
Ejemplo n.º 4
0
 public void CheckDirectoryExistsTest()
 {
     ExceptionAssert.IsException <ArgumentNullException>(() => ((string)null).CheckDirectoryExists("param"));
     ShimDirectory.ExistsString = m => false;
     ExceptionAssert.IsException <DirectoryNotFoundException>(() => "path".CheckDirectoryExists("param"));
     ShimDirectory.ExistsString = m => true;
     "path".CheckDirectoryExists("param");
 }
Ejemplo n.º 5
0
        public void GetRandomLetterAndNumberString()
        {
            Random rnd = new Random();

            ExceptionAssert.IsException <ArgumentOutOfRangeException>(() => rnd.GetRandomNumberString(-5));
            string rndNum = rnd.GetRandomLetterAndNumberString(10);

            Assert.IsTrue(rndNum.Length == 10);
        }
Ejemplo n.º 6
0
        public void NextBytesTest()
        {
            Random rnd = new Random();

            ExceptionAssert.IsException <ArgumentOutOfRangeException>(() => rnd.NextBytes(-5));

            Byte[] bytes = rnd.NextBytes(10);
            Assert.IsTrue(bytes.Length == 10);
            Assert.IsTrue(bytes.Distinct().Count() > 8);
        }
Ejemplo n.º 7
0
 public void CheckBetweenTest()
 {
     ExceptionAssert.IsException <ArgumentOutOfRangeException>(() => 5.CheckBetween("param", 1, 4));
     ExceptionAssert.IsException <ArgumentOutOfRangeException>(() => 5.CheckBetween("param", 6, 9));
     ExceptionAssert.IsException <ArgumentOutOfRangeException>(() => 5.CheckBetween("param", 1, 5));
     ExceptionAssert.IsException <ArgumentOutOfRangeException>(() => 5.CheckBetween("param", 5, 9));
     5.CheckBetween("param", 1, 9);
     5.CheckBetween("param", 1, 5, false, true);
     5.CheckBetween("param", 5, 9, true, false);
     5.CheckBetween("param", 5, 5, true, true);
 }
Ejemplo n.º 8
0
        public void ExpandAndToStringTest_IEnumerable2()
        {
            List <int> source = new List <int> {
                1, 2, 3, 4, 5, 6
            };

            //转换委托不能为空
            ExceptionAssert.IsException <ArgumentNullException>(() => source.ExpandAndToString(null));
            //没有分隔符时,默认为逗号
            Assert.AreEqual(source.ExpandAndToString(item => (item + 1).ToString()), "2,3,4,5,6,7");
            Assert.AreEqual(source.ExpandAndToString(item => (item + 1).ToString(), "|"), "2|3|4|5|6|7");
        }
Ejemplo n.º 9
0
        public void CastToTest()
        {
            Assert.AreEqual(((object)null).CastTo <object>(), null);
            Assert.AreEqual("123".CastTo <int>(), 123);
            Assert.AreEqual(123.CastTo < string > (), "123");
            Assert.AreEqual(true.CastTo <string>(), "True");
            Assert.AreEqual("true".CastTo <bool>(), true);
            Assert.AreEqual("56D768A3-3D74-43B4-BD7B-2871D675CC4B".CastTo <Guid>(), new Guid("56D768A3-3D74-43B4-BD7B-2871D675CC4B"));
            Assert.AreEqual(1.CastTo < UriKind > (), UriKind.Absolute);
            Assert.AreEqual("RelativeOrAbsolute".CastTo <UriKind>(), UriKind.RelativeOrAbsolute);

            Assert.AreEqual("abc".CastTo <int>(123), 123);

            ExceptionAssert.IsException <FormatException>(() => "abc".CastTo <int>());
        }
Ejemplo n.º 10
0
        public void EncryptTest()
        {
            string    key    = "12345678";
            string    actual = "TMR29YtnGPI=";
            DesHelper des    = new DesHelper(Encoding.UTF8.GetBytes(key));

            Assert.AreEqual(des.Encrypt("admin"), actual);
            Assert.AreEqual(DesHelper.Encrypt("admin", key), actual);

            //弱密钥
            key = "123456781234567812345678";
            des = new DesHelper(Encoding.UTF8.GetBytes(key));
            ExceptionAssert.IsException <CryptographicException>(() => des.Encrypt("admin"));

            key    = "!@#$%^&*QWERTYUI12345678";
            actual = "Qp4r67VJ8Z0=";
            des    = new DesHelper(Encoding.UTF8.GetBytes(key));
            Assert.AreEqual(des.Encrypt("admin"), actual);
            Assert.AreEqual(DesHelper.Encrypt("admin", key), actual);
        }
Ejemplo n.º 11
0
 public void CheckNotNullOrEmptyTest_Collection()
 {
     ExceptionAssert.IsException <ArgumentNullException>(() => ((object[])null).CheckNotNullOrEmpty("param"));
     ExceptionAssert.IsException <ArgumentException>(() => new object[] { }.CheckNotNullOrEmpty("param"));
     new object[] { "abc" }.CheckNotNullOrEmpty("param");
 }
Ejemplo n.º 12
0
 public void CheckNotNullOrEmptyTest_String()
 {
     ExceptionAssert.IsException <ArgumentNullException>(() => ((string)null).CheckNotNullOrEmpty("param"));
     ExceptionAssert.IsException <ArgumentException>(() => string.Empty.CheckNotNullOrEmpty("param"));
     "abc".CheckNotNullOrEmpty("param");
 }
Ejemplo n.º 13
0
 public void CheckNotEmptyTest()
 {
     ExceptionAssert.IsException <ArgumentException>(() => Guid.Empty.CheckNotEmpty("param"));
     Guid.NewGuid().CheckNotEmpty("guid");
 }
Ejemplo n.º 14
0
 public void CheckNotNullTest()
 {
     ExceptionAssert.IsException <ArgumentNullException>(() => ((object)null).CheckNotNull("param"));
     new object().CheckNotNull("value");
 }
Ejemplo n.º 15
0
 public void RequiredTest()
 {
     ExceptionAssert.IsException <Exception>(() => "abc".Required(str => str.Length > 3, "message"));
     "abc".Required(str => str.Length == 3, "message");
 }
Ejemplo n.º 16
0
        public void GetExpressionTest()
        {
            IQueryable <TestEntity> source = Entities.AsQueryable();

            //空条件
            Expression <Func <TestEntity, bool> > predicate = FilterHelper.GetExpression <TestEntity>();

            Assert.IsTrue(source.Where(predicate).SequenceEqual(source));

            //单条件,属性不存在
            FilterRule rule  = new FilterRule("Name1", "5", FilterOperate.EndsWith);
            FilterRule rule1 = rule;

            ExceptionAssert.IsException <OSharpException>(() => FilterHelper.GetExpression <TestEntity>(rule1));

            //单条件
            rule      = new FilterRule("Name", "5", FilterOperate.EndsWith);
            predicate = FilterHelper.GetExpression <TestEntity>(rule);
            Assert.IsTrue(source.Where(predicate).SequenceEqual(source.Where(m => m.Name.EndsWith("5"))));

            //二级条件
            rule      = new FilterRule("Name.Length", 5, FilterOperate.Greater);
            predicate = FilterHelper.GetExpression <TestEntity>(rule);
            Assert.IsTrue(source.Where(predicate).SequenceEqual(source.Where(m => m.Name.Length > 5)));

            //多条件,异常
            ExceptionAssert.IsException <OSharpException>(() => new FilterGroup
            {
                Rules = new List <FilterRule> {
                    rule, new FilterRule("IsDeleted", true)
                },
                Operate = FilterOperate.Equal
            });

            //多条件
            FilterGroup group = new FilterGroup
            {
                Rules = new List <FilterRule> {
                    rule, new FilterRule("IsDeleted", true)
                },
                Operate = FilterOperate.And
            };

            predicate = FilterHelper.GetExpression <TestEntity>(group);
            Assert.IsTrue(source.Where(predicate).SequenceEqual(source.Where(m => m.Name.Length > 5 && m.IsDeleted)));

            //条件组嵌套
            DateTime dt = DateTime.Now;

            group = new FilterGroup
            {
                Rules = new List <FilterRule>
                {
                    new FilterRule("AddDate", dt, FilterOperate.Greater)
                },
                Groups = new List <FilterGroup> {
                    group
                },
                Operate = FilterOperate.Or
            };
            predicate = FilterHelper.GetExpression <TestEntity>(group);
            Assert.IsTrue(source.Where(predicate).SequenceEqual(source.Where(m => m.AddDate > dt || m.Name.Length > 5 && m.IsDeleted)));
        }