예제 #1
0
        public void Ok_Failure()
        {
            Result <int, string> wrappedResult = new Failure <int, string>("Error");
            var result = new ResultGuard <int, string, string>(wrappedResult).Default(i => i + i).Do();

            Assert.AreEqual(result, "ErrorError");
        }
예제 #2
0
        public void Ok_Success()
        {
            Result <int, string> wrappedResult = new Success <int, string>(10);
            var guard  = new ResultGuard <int, string, int>(wrappedResult);
            var result = guard.Default(i => i * 2).Do();

            Assert.AreEqual(result, 20);
        }
예제 #3
0
        public void MethodsThatReturnThis()
        {
            Result <int, string> wrappedResult = new Success <int, string>(10);
            var guard = new ResultGuard <int, string, string>(wrappedResult);

            Assert.AreSame(guard, ((IGuardEntryPoint <int, string, string>)guard).Success());
            Assert.AreSame(guard, ((IGuardFailureClosing <int, string, string>)guard).Success());
            Assert.AreSame(guard, ((IGuardEntryPoint <int, string, string>)guard).Failure());
            Assert.AreSame(guard, ((IGuardSuccessClosing <int, string, string>)guard).Failure());
        }
예제 #4
0
        public void Ok_Failure_MultipleMatches()
        {
            Result <int, string> wrappedResult = new Failure <int, string>("Error");
            var guard = new ResultGuard <int, string, string>(wrappedResult);

            guard.Where(i => i == "Error", i => "first match");
            guard.Where(i => i == "Error", i => "second match");
            var result = guard.Do();

            Assert.AreEqual(result, "first match");
        }
예제 #5
0
        public void Ok_Success_MultipleMatches()
        {
            Result <int, string> wrappedResult = new Success <int, string>(10);
            var guard = new ResultGuard <int, string, string>(wrappedResult);

            guard.Where(i => i == 10, i => "first match");
            guard.Where(i => i == 10, i => "second match");
            var result = guard.Do();

            Assert.AreEqual(result, "first match");
        }
예제 #6
0
        public void Ok_Failure()
        {
            Result <int, string> wrappedResult = new Failure <int, string>("Error");
            var guard = new ResultGuard <int, string, string>(wrappedResult);

            guard.Where(i => i == "something", i => string.Empty);
            guard.Where(i => i == "Error", i => i + i);
            var result = guard.Do();

            Assert.AreEqual(result, "ErrorError");
        }
예제 #7
0
        public void Ok_Failure_FallsThroughToDefault()
        {
            Result <int, string> wrappedResult = new Failure <int, string>("Error");
            var guard = new ResultGuard <int, string, string>(wrappedResult);

            guard.Where(i => i == string.Empty, i => "no match");
            guard.Where(i => i == string.Empty, i => "no match");
            guard.Default((string i) => "match");
            var result = guard.Do();

            Assert.AreEqual(result, "match");
        }
예제 #8
0
        public void Ok_Success_FallsThroughToDefault()
        {
            Result <int, string> wrappedResult = new Success <int, string>(10);
            var guard = new ResultGuard <int, string, string>(wrappedResult);

            guard.Where(i => i == 11, i => "no match");
            guard.Where(i => i == 11, i => "no match");
            guard.Default((int i) => "match");
            var result = guard.Do();

            Assert.AreEqual(result, "match");
        }
예제 #9
0
        public void Ok()
        {
            // Slightly odd test - here to make sure the fluent interface isn't broken by any changes. No way to automatically test this 'properly'
            Result <int, string> wrappedResult           = new Success <int, string>(10);
            IGuardEntryPoint <int, string, string> guard = new ResultGuard <int, string, string>(wrappedResult);

            var result = guard
                         //// Set up some success paths ( we could also have set up failure paths first)
                         .Success()
                         .Where(i => i == 5, i => i.ToString() + " is 5!")
                         .Where(i => i % 2 == 0, i => i.ToString() + " is even!")
                         .Where(i => i < 0, i => i.ToString() + " is a negative number!")
                         .Default(i => i + " is just some number") //// To start defining failure paths we NEED to provide a default value
                         .Failure()                                //// Since we "closed" successes when we defined a default we can't go back later to add more paths
                         .Where(i => i.Length > 100, i => i + " is a long message")
                         .Where(i => i.StartsWith("Error"), i => "An error occurred: " + i)
                         .Default(i => i + " is just some message") //// For safety we have to provide both a success and failure default so no paths are left uncovered
                                                                    //// .Success()                            //// Should not work - we've already defined our success paths!
                         .Do();                                     //// Both defaults have been defined, only Do() is left

            Assert.AreEqual(result, "10 is even!");
        }