IfTest.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using System.Linq;
  8. using Xunit;
  9. namespace Tests
  10. {
  11. public class IfTest : Tests
  12. {
  13. [Fact]
  14. public void If_Arguments()
  15. {
  16. AssertThrows<ArgumentNullException>(() => EnumerableEx.If<int>(null, new[] { 1 }));
  17. AssertThrows<ArgumentNullException>(() => EnumerableEx.If<int>(() => true, null));
  18. AssertThrows<ArgumentNullException>(() => EnumerableEx.If<int>(null, new[] { 1 }, new[] { 1 }));
  19. AssertThrows<ArgumentNullException>(() => EnumerableEx.If<int>(() => true, null, new[] { 1 }));
  20. AssertThrows<ArgumentNullException>(() => EnumerableEx.If<int>(() => true, new[] { 1 }, null));
  21. }
  22. [Fact]
  23. public void If1()
  24. {
  25. var x = 5;
  26. var res = EnumerableEx.If(() => x > 0, new[] { +1 }, new[] { -1 });
  27. Assert.Equal(+1, res.Single());
  28. x = -x;
  29. Assert.Equal(-1, res.Single());
  30. }
  31. [Fact]
  32. public void If2()
  33. {
  34. var x = 5;
  35. var res = EnumerableEx.If(() => x > 0, new[] { +1 });
  36. Assert.Equal(+1, res.Single());
  37. x = -x;
  38. Assert.True(res.IsEmpty());
  39. }
  40. }
  41. }