//
// 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 boxed_unknown() {
let box: Box<unknown> = 32;
assertEq(typeof box, 'Box<int>');
}
#[test]
fn boxed_unknown_as() {
let box = 32 as Box<unknown>;
assertEq(typeof box, 'Box<int>');
}
add_one: {
fn add_one(val: Box<int>) {
val += 1;
}
#[test]
fn add_one_reference() {
let val: Box<int> = 0;
self.add_one(val);
assertEq(val, 1);
assertEq(typeof val, 'Box<int>');
}
#[test]
fn add_one_failed() {
let val = 0;
// val is cast to a Box<int> by the function receiving it
// This is by design as we don't want side effects on val...
// Debate me if you feel otherwise - open to changes
self.add_one(val);
assertEq(val, 0);
assertEq(typeof val, 'int');
}
#[test]
fn unboxed() {
let val = box(5);
self.add_one(val);
assertEq(val, 6);
let other = unbox(val);
self.add_one(other);
assertEq(other, 6);
}
}
field_types: {
field: 44 as Box<int>
#[test]
fn boxed_fields() {
assertEq(typeof self.field, 'Box<int>');
self.field = self.field as Box<int>;
assertEq(typeof self.field, 'Box<int>');
assertEq(self.field, 44);
let tmp = self.field;
tmp = 55;
assertEq(self.field, 55);
let cpy = unbox(tmp);
cpy = 11;
assertEq(cpy, 11);
assertEq(tmp, 55);
assertEq(self.field, 55);
tmp = 66;
assertEq(self.field, 66);
self.field = unbox(self.field);
tmp = 99;
assertEq(self.field, 66);
}
type CustomType {
field: Box<int> = 42 as Box<int>
}
CustomType object: {}
#[test]
fn boxed_object_fields() {
assertEq(typeof self.object.field, 'Box<int>');
assertEq(self.object.field, 42);
let tmp = self.object.field;
tmp = 66;
assertEq(self.object.field, 66);
}
}