if

test_if.zig

  1. // If expressions have three uses, corresponding to the three types:
  2. // * bool
  3. // * ?T
  4. // * anyerror!T
  5. const expect = @import("std").testing.expect;
  6. test "if expression" {
  7. // If expressions are used instead of a ternary expression.
  8. const a: u32 = 5;
  9. const b: u32 = 4;
  10. const result = if (a != b) 47 else 3089;
  11. try expect(result == 47);
  12. }
  13. test "if boolean" {
  14. // If expressions test boolean conditions.
  15. const a: u32 = 5;
  16. const b: u32 = 4;
  17. if (a != b) {
  18. try expect(true);
  19. } else if (a == 9) {
  20. unreachable;
  21. } else {
  22. unreachable;
  23. }
  24. }
  25. test "if error union" {
  26. // If expressions test for errors.
  27. // Note the |err| capture on the else.
  28. const a: anyerror!u32 = 0;
  29. if (a) |value| {
  30. try expect(value == 0);
  31. } else |err| {
  32. _ = err;
  33. unreachable;
  34. }
  35. const b: anyerror!u32 = error.BadValue;
  36. if (b) |value| {
  37. _ = value;
  38. unreachable;
  39. } else |err| {
  40. try expect(err == error.BadValue);
  41. }
  42. // The else and |err| capture is strictly required.
  43. if (a) |value| {
  44. try expect(value == 0);
  45. } else |_| {}
  46. // To check only the error value, use an empty block expression.
  47. if (b) |_| {} else |err| {
  48. try expect(err == error.BadValue);
  49. }
  50. // Access the value by reference using a pointer capture.
  51. var c: anyerror!u32 = 3;
  52. if (c) |*value| {
  53. value.* = 9;
  54. } else |_| {
  55. unreachable;
  56. }
  57. if (c) |value| {
  58. try expect(value == 9);
  59. } else |_| {
  60. unreachable;
  61. }
  62. }

Shell

  1. $ zig test test_if.zig
  2. 1/3 test_if.test.if expression... OK
  3. 2/3 test_if.test.if boolean... OK
  4. 3/3 test_if.test.if error union... OK
  5. All 3 tests passed.

if with Optionals

test_if_optionals.zig

  1. const expect = @import("std").testing.expect;
  2. test "if optional" {
  3. // If expressions test for null.
  4. const a: ?u32 = 0;
  5. if (a) |value| {
  6. try expect(value == 0);
  7. } else {
  8. unreachable;
  9. }
  10. const b: ?u32 = null;
  11. if (b) |_| {
  12. unreachable;
  13. } else {
  14. try expect(true);
  15. }
  16. // The else is not required.
  17. if (a) |value| {
  18. try expect(value == 0);
  19. }
  20. // To test against null only, use the binary equality operator.
  21. if (b == null) {
  22. try expect(true);
  23. }
  24. // Access the value by reference using a pointer capture.
  25. var c: ?u32 = 3;
  26. if (c) |*value| {
  27. value.* = 2;
  28. }
  29. if (c) |value| {
  30. try expect(value == 2);
  31. } else {
  32. unreachable;
  33. }
  34. }
  35. test "if error union with optional" {
  36. // If expressions test for errors before unwrapping optionals.
  37. // The |optional_value| capture's type is ?u32.
  38. const a: anyerror!?u32 = 0;
  39. if (a) |optional_value| {
  40. try expect(optional_value.? == 0);
  41. } else |err| {
  42. _ = err;
  43. unreachable;
  44. }
  45. const b: anyerror!?u32 = null;
  46. if (b) |optional_value| {
  47. try expect(optional_value == null);
  48. } else |_| {
  49. unreachable;
  50. }
  51. const c: anyerror!?u32 = error.BadValue;
  52. if (c) |optional_value| {
  53. _ = optional_value;
  54. unreachable;
  55. } else |err| {
  56. try expect(err == error.BadValue);
  57. }
  58. // Access the value by reference by using a pointer capture each time.
  59. var d: anyerror!?u32 = 3;
  60. if (d) |*optional_value| {
  61. if (optional_value.*) |*value| {
  62. value.* = 9;
  63. }
  64. } else |_| {
  65. unreachable;
  66. }
  67. if (d) |optional_value| {
  68. try expect(optional_value.? == 9);
  69. } else |_| {
  70. unreachable;
  71. }
  72. }

Shell

  1. $ zig test test_if_optionals.zig
  2. 1/2 test_if_optionals.test.if optional... OK
  3. 2/2 test_if_optionals.test.if error union with optional... OK
  4. All 2 tests passed.

See also: