//
// Copyright 2024 Formata, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#[test]
#[errors] // make sure the test works
fn try_catch_error(): int {
let x: int = null;
x = self.func_that_doesnt_exist(); // errors
x = 100;
return x;
}
#[test(100)]
fn try_catch(): int {
let x: int = null;
try {
x = self.func_that_doesnt_exist(); // errors
} catch {
x = 100;
}
return x;
}
#[test(100)]
fn try_catch_no_catch(): int {
let x = 10;
try {
x = 100;
} catch {
x = 200;
}
return x;
}
#[test]
fn try_catch_message() {
let seen = false;
try {
throw('this is a message');
} catch (error: str) {
seen = true;
assertEq(error, 'this is a message');
}
assert(seen);
}
#[test]
fn try_catch_error_type_message_only() {
let seen = false;
try {
throw('CustomType', 'this is a message');
} catch (error: str) {
seen = true;
assertEq(error, 'this is a message');
}
assert(seen);
}
#[test]
fn try_catch_std_error_type() {
let seen = false;
try {
throw('this is a message');
} catch (error: (str, str)) {
seen = true;
assertEq(error, ('Std', 'this is a message'));
}
assert(seen);
}
#[test]
fn try_catch_custom_error_type() {
let seen = false;
try {
throw('CustomError', 'this is a message');
} catch (error: (str, str)) {
seen = true;
assertEq(error, ('CustomError', 'this is a message'));
}
assert(seen);
}
#[test]
fn try_catch_map() {
let seen = false;
try {
throw('CustomType', 'this is a message');
} catch (error: map) {
seen = true;
assertEq(error.get('type'), 'CustomType');
assertEq(error.get('message'), 'this is a message');
}
assert(seen);
}