github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/core/structs/try_test.go (about) 1 package structs 2 3 import ( 4 "testing" 5 6 "github.com/lmorg/murex/test" 7 ) 8 9 // TestTry tests the `try` builtin 10 func TestTry(t *testing.T) { 11 tests := []test.BooleanTest{ 12 { 13 Block: "try { true -> true }", 14 Result: true, 15 }, 16 { 17 Block: "try { false -> true }", 18 Result: true, 19 }, 20 { 21 Block: "try { true -> false }", 22 Result: false, 23 }, 24 { 25 Block: "try { true; true }", 26 Result: true, 27 }, 28 { 29 Block: "try { false; true }", 30 Result: false, 31 }, 32 { 33 Block: "try { true; false }", 34 Result: false, 35 }, 36 } 37 38 test.RunBooleanTests(tests, t) 39 } 40 41 // TestTryPipe tests the `trypipe` builtin 42 func TestTryPipe(t *testing.T) { 43 tests := []test.BooleanTest{ 44 { 45 Block: "trypipe { true -> true }", 46 Result: true, 47 }, 48 { 49 Block: "trypipe { false -> true }", 50 Result: false, 51 }, 52 { 53 Block: "trypipe { true -> false }", 54 Result: false, 55 }, 56 { 57 Block: "trypipe { true; true }", 58 Result: true, 59 }, 60 { 61 Block: "trypipe { false; true }", 62 Result: false, 63 }, 64 { 65 Block: "trypipe { true; false }", 66 Result: false, 67 }, 68 } 69 70 test.RunBooleanTests(tests, t) 71 } 72 73 // TestCatch tests the `catch` builtin 74 func TestCatch(t *testing.T) { 75 tests := []test.BooleanTest{ 76 // --- catch --- 77 { 78 Block: "try <null> { true }; catch { true }", 79 Result: false, 80 }, 81 { 82 Block: "try <null> { false }; catch { true }", 83 Result: false, 84 }, 85 { 86 Block: "try <null> { true }; catch { false }", 87 Result: false, 88 }, 89 { 90 Block: "try <null> { false }; catch { false }", 91 Result: false, 92 }, 93 // --- !catch --- 94 { 95 Block: "try <null> { true }; !catch { true }", 96 Result: true, 97 }, 98 { 99 Block: "try <null> { false }; !catch { true }", 100 Result: false, 101 }, 102 { 103 Block: "try <null> { true }; !catch { false }", 104 Result: false, 105 }, 106 { 107 Block: "try <null> { false }; !catch { false }", 108 Result: false, 109 }, 110 } 111 112 test.RunBooleanTests(tests, t) 113 } 114 115 func TestUnsafe(t *testing.T) { 116 tests := []test.MurexTest{ 117 { 118 Block: `try { 119 unsafe { err "foo" } 120 out "bar" 121 }`, 122 Stdout: "bar\n", 123 Stderr: "foo\n", 124 }, 125 } 126 127 test.RunMurexTests(tests, t) 128 }