Exemplo n.º 1
0
        public static void Case_Insensitive()
        {
            var value = "C";

            switch (value.ToLower())
            {
            case "a":
                HandleA();
                break;

            case "b":
                HandleB();
                break;

            case "c":
                break;

            default:
                HandleOther();
                break;
            }

            FluentSwitch <string>
            .Comparer(StringComparer.OrdinalIgnoreCase.Equals)
            .Case("A").Do(HandleA)
            .Case("B").Do(HandleB)
            .Case("C").Do(HandleC)
            .Default(HandleOther)
            .Switch(value);
        }
Exemplo n.º 2
0
        public void Comparer_Should_Return_An_Object_Instance()
        {
            var obj = FluentSwitch <string>
                      .Comparer((a, b) => { return(a.Equals(b)); });

            Assert.IsNotNull(obj);
        }
Exemplo n.º 3
0
        public static void Simple()
        {
            var value = "C";

            switch (value)
            {
            case "A":
                HandleA();
                break;

            case "B":
                HandleB();
                break;

            case "C":
                HandleC();
                break;

            default:
                HandleOther();
                break;
            }

            FluentSwitch <string>
            .Comparer(String.Equals)
            .Case("A").Do(HandleA)
            .Case("B").Do(HandleB)
            .Case("C").Do(HandleC)
            .Default(HandleOther)
            .Switch(value);
        }
Exemplo n.º 4
0
 public void Comparer_Should_Throw_ArgumentNullException_When_Comparer_Is_Null()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         FluentSwitch <string>
         .Comparer(null);
     });
 }
Exemplo n.º 5
0
 public void Case_Should_Throw_ArgumentNullException_When_Values_Is_Null()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         FluentSwitch <string>
         .Comparer((a, b) => { return(a == b); })
         .Case((IEnumerable <string>)null);
     });
 }
Exemplo n.º 6
0
 public void Do_T_Should_Allow_Null_Action()
 {
     Assert.DoesNotThrow(() =>
     {
         FluentSwitch <string>
         .Comparer((a, b) => { return(a == b); })
         .Case("A")
         .Do((Action <string>)null);
     });
 }
Exemplo n.º 7
0
 public void Null_Do_Action_Should_Be_Handled()
 {
     Assert.DoesNotThrow(() =>
     {
         FluentSwitch <string>
         .Comparer((a, b) => { return(a == b); })
         .Case("A")
         .Do((Action)null)
         .Default(() => { })
         .Switch("A");
     });
 }
Exemplo n.º 8
0
 public void SwitchFirst_Should_Throw_ArgumentNullException_When_Params_Null()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         FluentSwitch <string>
         .Comparer((a, b) => { return(a == b); })
         .Case("A")
         .Do(() => { })
         .Default(() => { })
         .SwitchFirst(null);
     });
 }
Exemplo n.º 9
0
        public void SwitchFirst_Should_Execute_Matched_Case()
        {
            bool executed = false;

            FluentSwitch <string>
            .Comparer((a, b) => { return(a == b); })
            .Case("A")
            .Do(() => { executed = true; })
            .Default(() => { })
            .SwitchFirst("B", "A");

            Assert.IsTrue(executed);
        }
Exemplo n.º 10
0
        public void Switch_Should_Match_Null_Value()
        {
            bool handled = false;

            FluentSwitch <string>
            .Comparer((a, b) => { return(a == b); })
            .Case((string)null)
            .Do(() => { handled = true; })
            .Default(() => { })
            .Switch(null);

            Assert.IsTrue(handled);
        }
Exemplo n.º 11
0
        public void SwitchAll_Should_Throw_ArgumentNullException_When_Values_Null()
        {
            List <string> values = null;

            Assert.Throws <ArgumentNullException>(() =>
            {
                FluentSwitch <string>
                .Comparer((a, b) => { return(a == b); })
                .Case("A")
                .Do(() => { })
                .Default(() => { })
                .SwitchAll(values);
            });
        }
Exemplo n.º 12
0
        public void SwitchFirst_Should_Not_Execute_Default_When_A_Match_Is_Made()
        {
            bool executed = false;

            FluentSwitch <string>
            .Comparer((a, b) => { return(a == b); })
            .Case("A")
            .Do(() => { })
            .Case("B")
            .Do(() => { })
            .Default(() => { executed = true; })
            .SwitchFirst("A", "B");

            Assert.IsFalse(executed);
        }
Exemplo n.º 13
0
        public void SwitchAll_Should_Execute_Default_When_No_Matches()
        {
            bool executed = false;

            FluentSwitch <string>
            .Comparer((a, b) => { return(a == b); })
            .Case("A")
            .Do(() => { })
            .Case("B")
            .Do(() => { })
            .Default(() => { executed = true; })
            .SwitchAll("1", "2");

            Assert.IsTrue(executed);
        }
Exemplo n.º 14
0
        public void SwitchFirst_Should_Not_Execute_Unmatched()
        {
            bool aExecuted = false;
            bool bExecuted = false;

            FluentSwitch <string>
            .Comparer((a, b) => { return(a == b); })
            .Case("A")
            .Do(() => { aExecuted = true; })
            .Case("B")
            .Do(() => { bExecuted = true; })
            .Default(() => { })
            .SwitchFirst("1", "2");

            Assert.IsFalse(aExecuted || bExecuted);
        }
Exemplo n.º 15
0
        public void SwitchFirst_Should_Not_Execute_Subsequent_Matches()
        {
            bool aExecuted = false;
            bool bExecuted = false;

            FluentSwitch <string>
            .Comparer((a, b) => { return(a == b); })
            .Case("A")
            .Do(() => { aExecuted = true; })
            .Case("B")
            .Do(() => { bExecuted = true; })
            .Default(() => { })
            .SwitchFirst("A", "B");

            Assert.IsTrue(aExecuted && !bExecuted);
        }
Exemplo n.º 16
0
        public void SwitchAll_Should_Execute_All_Matched()
        {
            bool aExecuted = false;
            bool bExecuted = false;

            FluentSwitch <string>
            .Comparer((a, b) => { return(a == b); })
            .Case("A")
            .Do(() => { aExecuted = true; })
            .Case("B")
            .Do(() => { bExecuted = true; })
            .Default(() => { })
            .SwitchAll("A", "B");

            Assert.IsTrue(aExecuted && bExecuted);
        }
Exemplo n.º 17
0
        public static void List_First_Match()
        {
            var values = new List <string> {
                "A", "B", "C"
            };

            bool handled = false;

            foreach (var value in values)
            {
                switch (value)
                {
                case "A":
                    HandleA();
                    break;

                case "B":
                    HandleB();
                    break;

                case "C":
                    HandleC();
                    break;

                default:
                    HandleOther();
                    break;
                }

                if (handled)
                {
                    break;
                }
            }

            FluentSwitch <string>
            .Comparer(String.Equals)
            .Case("A").Do(HandleA)
            .Case("B").Do(HandleB)
            .Case("C").Do(HandleC)
            .Default(HandleOther)
            .SwitchFirst(values);
        }