@@ -1223,3 +1223,114 @@ func TestErrorHandlingOptimization(t *testing.T) {
12231223 t .Errorf ("Expected cause to be original error, but got: %v" , promiseErr2 .Cause )
12241224 }
12251225}
1226+
1227+ // TestTry tests the Try function
1228+ func TestTry (t * testing.T ) {
1229+ t .Run ("Try with successful function" , func (t * testing.T ) {
1230+ promise := Try (func () string {
1231+ return "success"
1232+ })
1233+
1234+ result , err := promise .Await ()
1235+ if err != nil {
1236+ t .Errorf ("Expected no error, got %v" , err )
1237+ }
1238+ if result != "success" {
1239+ t .Errorf ("Expected 'success', got %s" , result )
1240+ }
1241+ })
1242+
1243+ t .Run ("Try with panic" , func (t * testing.T ) {
1244+ promise := Try (func () string {
1245+ panic ("test panic" )
1246+ })
1247+
1248+ _ , err := promise .Await ()
1249+ if err == nil {
1250+ t .Error ("Expected error for panic, got nil" )
1251+ }
1252+
1253+ var promiseErr * PromiseError
1254+ if ! errors .As (err , & promiseErr ) {
1255+ t .Error ("Expected PromiseError type" )
1256+ }
1257+ if promiseErr .Type != PanicError {
1258+ t .Errorf ("Expected PanicError type, got %v" , promiseErr .Type )
1259+ }
1260+ })
1261+
1262+ t .Run ("Try with error panic" , func (t * testing.T ) {
1263+ testErr := errors .New ("test error" )
1264+ promise := Try (func () string {
1265+ panic (testErr )
1266+ })
1267+
1268+ _ , err := promise .Await ()
1269+ if err == nil {
1270+ t .Error ("Expected error for panic, got nil" )
1271+ }
1272+
1273+ var promiseErr * PromiseError
1274+ if ! errors .As (err , & promiseErr ) {
1275+ t .Error ("Expected PromiseError type" )
1276+ }
1277+ if promiseErr .Type != PanicError {
1278+ t .Errorf ("Expected PanicError type, got %v" , promiseErr .Type )
1279+ }
1280+ if promiseErr .Cause != testErr {
1281+ t .Errorf ("Expected cause to be testErr, got %v" , promiseErr .Cause )
1282+ }
1283+ })
1284+ }
1285+
1286+ // TestTryWithError tests the TryWithError function
1287+ func TestTryWithError (t * testing.T ) {
1288+ t .Run ("TryWithError with successful function" , func (t * testing.T ) {
1289+ promise := TryWithError (func () (string , error ) {
1290+ return "success" , nil
1291+ })
1292+
1293+ result , err := promise .Await ()
1294+ if err != nil {
1295+ t .Errorf ("Expected no error, got %v" , err )
1296+ }
1297+ if result != "success" {
1298+ t .Errorf ("Expected 'success', got %s" , result )
1299+ }
1300+ })
1301+
1302+ t .Run ("TryWithError with error return" , func (t * testing.T ) {
1303+ testErr := errors .New ("test error" )
1304+ promise := TryWithError (func () (string , error ) {
1305+ return "" , testErr
1306+ })
1307+
1308+ _ , err := promise .Await ()
1309+ if err == nil {
1310+ t .Error ("Expected error, got nil" )
1311+ }
1312+ // The error might be wrapped, so check if it contains the original error
1313+ if ! errors .Is (err , testErr ) && ! strings .Contains (err .Error (), testErr .Error ()) {
1314+ t .Errorf ("Expected error to contain testErr, got %v" , err )
1315+ }
1316+ })
1317+
1318+ t .Run ("TryWithError with panic" , func (t * testing.T ) {
1319+ promise := TryWithError (func () (string , error ) {
1320+ panic ("test panic" )
1321+ })
1322+
1323+ _ , err := promise .Await ()
1324+ if err == nil {
1325+ t .Error ("Expected error for panic, got nil" )
1326+ }
1327+
1328+ var promiseErr * PromiseError
1329+ if ! errors .As (err , & promiseErr ) {
1330+ t .Error ("Expected PromiseError type" )
1331+ }
1332+ if promiseErr .Type != PanicError {
1333+ t .Errorf ("Expected PanicError type, got %v" , promiseErr .Type )
1334+ }
1335+ })
1336+ }
0 commit comments