fn test_record()

in starlark/src/stdlib/record.rs [75:135]


    fn test_record() {
        assert::pass(
            r#"
rec_type = record(host=str.type, port=int.type)
rec1 = rec_type(host = "test", port=80)
rec2 = rec_type(host = "test", port=90)
assert_eq(rec1, rec1)
assert_eq(rec1 == rec2, False)
assert_eq(rec1.host, "test")
assert_eq(rec1.port, 80)
assert_eq(dir(rec1), ["host", "port"])
"#,
        );
        assert::fails(
            r#"
rec_type = record(host=str.type, port=int.type)
rec_type(host=1, port=80)
"#,
            &["`1`", "`string`", "`host`"],
        );
        assert::fails(
            r#"
rec_type = record(host=str.type, port=int.type)
rec_type(port=80)
"#,
            &["Missing parameter", "`host`"],
        );
        assert::fails(
            r#"
rec_type = record(host=str.type, port=int.type)
rec_type(host="localhost", port=80, mask=255)
"#,
            &["extra named", "mask"],
        );
        assert::pass(
            r#"
rec_type = record(host=str.type, port=int.type)
def foo(x: rec_type.type) -> "rec_type":
    return x
foo(rec_type(host="localhost", port=80))"#,
        );
        assert::pass(
            r#"
v = [record(host=str.type, port=int.type)]
def foo(x: v[0].type) -> "record":
    return x
foo(v[0](host="localhost", port=80))"#,
        );
        assert::pass(
            r#"
rec_type = record(host=str.type, port=field(int.type, 80), mask=int.type)
assert_eq(rec_type(host="localhost", mask=255), rec_type(host="localhost", port=80, mask=255))"#,
        );
        // Make sure the default value is heap allocated (used to fail with a GC issue)
        assert::pass(
            r#"
heap_string = "test{}".format(42)
rec_type = record(test_gc=field(str.type, heap_string))
assert_eq(rec_type().test_gc, "test42")"#,
        );
    }