async-codegen 0.12.1

Minimalist async-IO code generation framework.
Documentation
/*
 * Copyright © 2025 Anand Beh
 *
 * 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.
 */
use crate::common::{ArrSeq, NoOp, NoOpSeq, SingularSeq, Str};
use crate::context::EmptyContext;
use crate::java::{
    Array, AssertStmt, ClassDef, ConstructorDef, DeclareParam, EnhancedFor, Extends, FunctionCall,
    FunctionDef, Implements, MemberAccess, Modifier, Parameterized, Stmt,
};
use crate::util::InMemoryOutput;

#[test]
fn assert() {
    let assert_stmt = AssertStmt(Str("true == true"));
    let string = InMemoryOutput::print_output(EmptyContext, &assert_stmt);
    assert_eq!("assert true == true;\n", string);
}

#[test]
fn class_with_constructor() {
    let class_def = ClassDef {
        mods: ArrSeq(&[Modifier::Public, Modifier::Abstract]),
        name: Implements(
            Extends(
                Str("MySet"),
                Parameterized(Str("java.util.AbstractSet"), SingularSeq(Str("String"))),
            ),
            SingularSeq(Parameterized(
                Str("java.util.Set"),
                SingularSeq(Str("String")),
            )),
        ),
        body: ConstructorDef {
            mods: SingularSeq(Modifier::Public),
            type_args: SingularSeq(Extends(Str("S"), Str("CharSequence"))),
            name: Str("MySet"),
            args: SingularSeq(DeclareParam(
                Parameterized(Str("java.util.Set"), SingularSeq(Str("S"))),
                Str("source"),
            )),
            body: EnhancedFor {
                var_name: DeclareParam(Str("CharSequence"), Str("element")),
                iter: Str("source"),
                body: Stmt(FunctionCall {
                    name: Str("add"),
                    args: SingularSeq(MemberAccess(
                        Str("element"),
                        FunctionCall {
                            name: Str("toString"),
                            args: NoOpSeq,
                        },
                    )),
                }),
            },
            throws: NoOpSeq,
        },
    };
    assert_eq!(
        r#"public abstract class MySet extends java.util.AbstractSet<String> implements java.util.Set<String> {
public <S extends CharSequence> MySet(java.util.Set<S> source) {
for (CharSequence element : source) {
add(element.toString());
}
}

}
"#,
        InMemoryOutput::print_output(EmptyContext, &class_def)
    );
}

#[test]
fn void_main_function() {
    let function_def = FunctionDef {
        mods: ArrSeq(&[Modifier::Public, Modifier::Static]),
        type_args: NoOpSeq,
        return_type: Str("void"),
        name: Str("main"),
        args: SingularSeq(DeclareParam(Array(Str("String")), Str("args"))),
        body: NoOp,
        throws: SingularSeq(Str("java.lang.Exception")),
    };
    let string = InMemoryOutput::print_output(EmptyContext, &function_def);
    assert_eq!(
        "public static void main(String[] args) throws java.lang.Exception {\n}\n\n",
        string
    );
}