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.
//

simple_task: {
    #[run]
    fn do_something() {
        self.field = 42;
    }

    #[run(1)]
    fn increment_field() {
        self.field += 1;
    }

    #[test]
    fn exec_test() {
        assertNull(self.field);
        self.exec();
        assertEq(self.field, 43);
    }
}

ordering: {
    Box<vec> context: []

    #[test]
    fn exec_test() {
        self.exec();
        assertEq(self.context, [
            'first',
            'second',
            'third',
            'fourth',
            'fifth',
            'sixth',
        ]);
    }

    #[run(0)]
    fn first() {
        pln(typeof self.context);
        self.context.push('first');
    }

    #[run(10)]
    fn second() {
        self.context.push('second');
    }

    #[run(12)]
    sub_task: {
        #[run(0)]
        fn third() {
            super.context.push('third');
        }

        #[run(5)]
        fn fourth() {
            super.context.push('fourth');
        }

        #[run(60)]
        fn fifth() {
            super.context.push('fifth');
        }
    }

    #[run(20)]
    fn sixth() {
        self.context.push('sixth');
    }
}

default_order: {
    Box<vec> context: []

    #[run]
    sub_task: {
        #[run]
        fn task() {
            super.context.push(0);
        }
    }

    #[run]
    fn task() {
        self.context.push(1);
    }

    #[test]
    fn exec_test() {
        self.exec();
        assertEq(self.context, [0, 1]);
    }
}

nested: {
    #[run]
    a: {
        #[run]
        b: {
            #[run]
            c: {
                #[run]
                d: {
                    #[run]
                    fn test() {
                        self.c.b.a.nested.tested = true;
                    }
                }
            }
        }
    }

    #[test]
    fn test() {
        self.exec();
        assert(self.tested);
    }
}

arrays: {
    #[run]
    array: [
        {
            #[run]
            fn task() { self.ran = true; }
        },
        {
            #[run]
            fn task() { self.didit = true; }
        }
    ]

    #[test]
    fn test() {
        self.exec();

        let first = self.array[0];
        assert(first.ran);

        let second = self.array[1];
        assert(second.didit);
    }
}

sets: {
    first: {
        #[run]
        fn task() { self.ran = true; }
    }

    second: {
        #[run]
        fn task() { self.ran = true; }
    }

    #[run]
    set: set(self.first, self.second)

    #[test]
    fn test() {
        self.exec();
        assert(self.first.ran);
        assert(self.second.ran);
    }
}

maps: {
    first: {
        #[run]
        fn task() { self.ran = true; }
    }

    second: {
        #[run]
        fn task() { self.ran = true; }
    }

    #[run]
    collection: map(('first', self.first), ('second', self.second))

    #[test]
    fn test() {
        self.exec();
        assert(self.first.ran);
        assert(self.second.ran);
    }
}

types: {
    type Base {
        #[run]
        fn do_something() {
            self.shouldnt = true; // overridden, so shouldn't happen
        }

        #[run]
        fn do_base_things() {
            self.base = true;
        }
    }

    type MyTask extends Base {
        #[run]
        fn do_something() {
            self.did_something = true;
        }
    }

    MyTask task: {}

    #[test]
    fn test() {
        self.task.exec();
        assert(self.task.base);
        assert(self.task.did_something);
        assertNull(self.task.shouldnt);
    }
}