//
// 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]
fn arrow_function() {
let func = ():str => 'hello, arrows';
let res = func.call();
assertEq(res, 'hello, arrows');
}
#[test]
fn arrow_with_parameter() {
let arrow = (name: str): str => {
return `Hello, ${name}`;
};
assertEq(arrow.call('CJ'), 'Hello, CJ');
assertEq(arrow.call('Amelia'), 'Hello, Amelia');
}
passing: {
fn take_arrow(arrow: fn): str {
return arrow.call();
}
#[test('ARROWED')]
fn pass_arrow(): str {
return self.take_arrow((): str => {
return 'ARROWED';
});
}
}
field: {
arrow: (): float => 42; // Creates a field that is an arrow function...
#[test(42)]
fn call_arrow_field(): float {
return self.arrow.call();
}
}
#[test]
fn vars() {
let symbol = 42;
let func = (): int => {
// This is not a closure, but takes advantage of the std libraries access to the current symbol table...
return symbol;
};
{
let symbol = 11;
assertEq(func.call(), 11);
}
}
#[test([('leftover', 'should stay')])]
fn array_retain(): vec {
let array = [('hello', 'dude'), ('hello', 'world'), ('leftover', 'should stay')];
let key = 'hello';
array.retain((val: (str, str)): bool => val[0] != key); // tests symbol table access from array lib
return array;
}
test_outer: {
fn use_outer(): int {
return outer;
}
#[test]
fn no_outer() {
assertNull(self.use_outer());
}
#[test]
fn no_declared_outer() {
let outer = 42;
assertNull(self.use_outer());
}
#[test]
fn yes_outer() {
let outer = 42;
let func = self.use_outer;
assertEq(func.call(), 42);
}
}