Skip to content

Commit

Permalink
Convert record tests to golden
Browse files Browse the repository at this point in the history
Summary: Need to add a few more.

Reviewed By: JakobDegen

Differential Revision: D64214360

fbshipit-source-id: 376156fe1e4076d8f0e7b2709465224ca5123856
  • Loading branch information
stepancheg authored and facebook-github-bot committed Oct 11, 2024
1 parent 0765cd7 commit b3ce39f
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 13 deletions.
9 changes: 9 additions & 0 deletions starlark/src/assert/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,15 @@ pub fn fail(program: &str, msg: &str) -> crate::Error {
Assert::new().fail(program, msg)
}

#[cfg(test)]
pub(crate) fn fail_golden(path: &str, program: &str) -> crate::Error {
let program = program.trim();
let e = fails(program, &[]);
let output = format!("Program:\n\n{program}\n\nError:\n\n{e:?}\n");
starlark_syntax::golden_test_template::golden_test_template(path, &output);
e
}

#[cfg(test)]
pub(crate) fn fail_skip_typecheck(program: &str, msg: &str) -> crate::Error {
let mut a = Assert::new();
Expand Down
12 changes: 6 additions & 6 deletions starlark/src/values/types/record/record_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,8 @@ f_pass(RecPass(a = 1, b = 2))

#[test]
fn test_record_type_as_type_compile_time() {
assert::fail(
assert::fail_golden(
"src/values/types/record/record_type/record_type_as_type_compile_time.golden",
r"
RecFailCt1 = record(a = field(int), b = field(int))
RecFailCt2 = record(a = field(int), b = field(int))
Expand All @@ -383,13 +384,13 @@ def f_fail_ct(x: RecFailCt1):
def test():
f_fail_ct(RecFailCt2(a = 1, b = 2))
",
"Expected type `RecFailCt1` but got `RecFailCt2`",
);
}

#[test]
fn test_record_type_as_type_runtime() {
assert::fail(
assert::fail_golden(
"src/values/types/record/record_type/record_type_as_type_runtime.golden",
r"
RecFailRt1 = record(a = field(int), b = field(int))
RecFailRt2 = record(a = field(int), b = field(int))
Expand All @@ -399,15 +400,14 @@ def f_fail_rt(x: RecFailRt1):
noop(f_fail_rt)(RecFailRt2(a = 1, b = 2))
",
"Value `record[RecFailRt2](a=1, b=2)` of type `record` does not match the type annotation",
);
}

#[test]
fn test_anon_record() {
assert::fail(
assert::fail_golden(
"src/values/types/record/record_type/anon_record.golden",
"record(a = field(int))(a = 1)",
"not assigned to a global variable",
);
}
}
21 changes: 21 additions & 0 deletions starlark/src/values/types/record/record_type/anon_record.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# @generated
# To regenerate, run:
# ```
# STARLARK_RUST_REGENERATE_GOLDEN_TESTS=1 cargo test -p starlark --lib
# ```

Program:

record(a = field(int))(a = 1)

Error:

Traceback (most recent call last):
* assert.bzl:1, in <module>
record(a = field(int))(a = 1)
error: Record instance cannot be created if record type is not assigned to a global variable
--> assert.bzl:1:1
|
1 | record(a = field(int))(a = 1)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# @generated
# To regenerate, run:
# ```
# STARLARK_RUST_REGENERATE_GOLDEN_TESTS=1 cargo test -p starlark --lib
# ```

Program:

RecFailCt1 = record(a = field(int), b = field(int))
RecFailCt2 = record(a = field(int), b = field(int))

def f_fail_ct(x: RecFailCt1):
return x.a

def test():
f_fail_ct(RecFailCt2(a = 1, b = 2))

Error:

error: Expected type `RecFailCt1` but got `RecFailCt2`
--> assert.bzl:8:15
|
8 | f_fail_ct(RecFailCt2(a = 1, b = 2))
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# @generated
# To regenerate, run:
# ```
# STARLARK_RUST_REGENERATE_GOLDEN_TESTS=1 cargo test -p starlark --lib
# ```

Program:

RecFailRt1 = record(a = field(int), b = field(int))
RecFailRt2 = record(a = field(int), b = field(int))

def f_fail_rt(x: RecFailRt1):
return x.a

noop(f_fail_rt)(RecFailRt2(a = 1, b = 2))

Error:

Traceback (most recent call last):
* assert.bzl:7, in <module>
noop(f_fail_rt)(RecFailRt2(a = 1, b = 2))
error: Value `record[RecFailRt2](a=1, b=2)` of type `record` does not match the type annotation `RecFailRt1` for argument `x`
--> assert.bzl:7:1
|
7 | noop(f_fail_rt)(RecFailRt2(a = 1, b = 2))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
14 changes: 7 additions & 7 deletions starlark/src/values/types/record/ty_record_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ foo(MyRec(x = 1))

#[test]
fn test_fail_compile_time() {
assert::fail(
assert::fail_golden(
"src/values/types/record/ty_record_type/fail_compile_time.golden",
r#"
MyRec = record(x = int)
WrongRec = record(x = int)
Expand All @@ -62,22 +63,21 @@ def foo(x: MyRec): pass
def bar():
foo(WrongRec(x = 1))
"#,
r#"Expected type `MyRec` but got `WrongRec`"#,
);
}

#[test]
fn test_fail_runtime_time() {
assert::fail_skip_typecheck(
assert::fail_golden(
"src/values/types/record/ty_record_type/fail_runtime_time.golden",
r#"
MyRec = record(x = int)
WrongRec = record(x = int)
def foo(x: MyRec): pass
foo(WrongRec(x = 1))
noop(foo)(WrongRec(x = 1))
"#,
r#"Value `record[WrongRec](x=1)` of type `record` does not match the type annotation `MyRec`"#,
);
}

Expand Down Expand Up @@ -111,14 +111,14 @@ assert_eq(f(MyRec(x = 1, y = 2)), 3)

#[test]
fn test_typecheck_field_fail() {
assert::fail(
assert::fail_golden(
"src/values/types/record/ty_record_type/typecheck_field_fail.golden",
r#"
MyRec = record(x = int, y = int)
def f(rec: MyRec) -> int:
return rec.z
"#,
r#"The attribute `z` is not available on the type `MyRec`"#,
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# @generated
# To regenerate, run:
# ```
# STARLARK_RUST_REGENERATE_GOLDEN_TESTS=1 cargo test -p starlark --lib
# ```

Program:

MyRec = record(x = int)
WrongRec = record(x = int)

def foo(x: MyRec): pass

def bar():
foo(WrongRec(x = 1))

Error:

error: Expected type `MyRec` but got `WrongRec`
--> assert.bzl:7:9
|
7 | foo(WrongRec(x = 1))
| ^^^^^^^^^^^^^^^
|
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# @generated
# To regenerate, run:
# ```
# STARLARK_RUST_REGENERATE_GOLDEN_TESTS=1 cargo test -p starlark --lib
# ```

Program:

MyRec = record(x = int)
WrongRec = record(x = int)

def foo(x: MyRec): pass

noop(foo)(WrongRec(x = 1))

Error:

Traceback (most recent call last):
* assert.bzl:6, in <module>
noop(foo)(WrongRec(x = 1))
error: Value `record[WrongRec](x=1)` of type `record` does not match the type annotation `MyRec` for argument `x`
--> assert.bzl:6:1
|
6 | noop(foo)(WrongRec(x = 1))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# @generated
# To regenerate, run:
# ```
# STARLARK_RUST_REGENERATE_GOLDEN_TESTS=1 cargo test -p starlark --lib
# ```

Program:

MyRec = record(x = int, y = int)

def f(rec: MyRec) -> int:
return rec.z

Error:

error: The attribute `z` is not available on the type `MyRec`
--> assert.bzl:4:16
|
4 | return rec.z
| ^
|

0 comments on commit b3ce39f

Please sign in to comment.