stof 0.3.24

Stof is a unified data interface and interchange format for creating, sharing, and manipulating data. Stof removes the fragile and cumbersome parts of combining and using data in applications.
Documentation
//
// 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.
//

field: 42

object: {
    ref super.field
}

#[test]
fn field_data() {
    let dref = Data.from('self.field');
    assertEq(dref, Data.from('self.object.field'));
}

#[test]
fn exists() {
    let dref = Data.from('self.field');
    assert(dref.exists());

    let dne = Data.fromId('does_not_exist');
    assertNot(dne.exists());
}

#[test]
fn objects() {
    let dref = Data.from('self.field');
    assertEq(dref.objects().len(), 2);
}

#[test]
fn id() {
    let dref = Data.from('self.field');
    assert(dref.id().len() > 0);
}

attach: {
    field: 'hello';
    a: {}

    #[test]
    fn to_object() {
        let dref = Data.from('self.field');
        assertEq(dref.objects().len(), 1);
        
        dref.attach(self.a); // does the same thing as 'Object.reference'
        assertEq(dref.objects().len(), 2);
        assertEq(self.a.field, 'hello');
    }

    another: 42;
    b: {}

    #[test]
    fn reference_test() {
        let dref = Data.from('self.another');
        assertEq(dref.objects().len(), 1);

        assert(self.b.reference('super.another'));
        assertEq(self.b.another, 42);
        assertEq(dref.objects().len(), 2);
    }
}

drop: {
    field: 'hello';
    a: {
        ref super.field;
    }
    b: {
        ref super.field;
    }

    #[test]
    fn from_object() {
        let dref: data = Data.from('self.field');
        assertEq(dref.objects().len(), 3);
        dref.drop(self.a); // drop the data from a

        assertEq(dref.objects().len(), 2);
        assertNull(self.a.field);

        dref.drop(); // drop from everywhere!
        assertNull(self.field);
        assertNot(dref.exists());
    }
}

// Make sure the data type works
fn pass_data(dref: data): bool {
    return dref.exists();
}

#[test]
fn data_type() {
    let dref = data('self.field'); // STD Lib Data Constructor
    assert(self.pass_data(dref));

    let new_data = Data.fromId(dref.id());
    assert(self.pass_data(new_data));
}