Merge pull request #5179 from roc-lang/i5143-tuple-abilities

Implement ability obligation checking and derivation for tuples
This commit is contained in:
Ayaz 2023-03-25 15:51:39 -05:00 committed by GitHub
commit 61dd5cc8c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
36 changed files with 4225 additions and 2296 deletions

View file

@ -825,6 +825,52 @@ fn encode_derived_record_with_many_types() {
)
}
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
fn encode_derived_tuple_two_fields() {
assert_evals_to!(
indoc!(
r#"
app "test"
imports [Encode, Json]
provides [main] to "./platform"
main =
tup = ("foo", 10u8)
result = Str.fromUtf8 (Encode.toBytes tup Json.toUtf8)
when result is
Ok s -> s
_ -> "<bad>"
"#
),
RocStr::from(r#"["foo",10]"#),
RocStr
)
}
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
fn encode_derived_tuple_of_tuples() {
assert_evals_to!(
indoc!(
r#"
app "test"
imports [Encode, Json]
provides [main] to "./platform"
main =
tup = ( ("foo", 10u8), (23u8, "bar", 15u8) )
result = Str.fromUtf8 (Encode.toBytes tup Json.toUtf8)
when result is
Ok s -> s
_ -> "<bad>"
"#
),
RocStr::from(r#"[["foo",10],[23,"bar",15]]"#),
RocStr
)
}
#[test]
#[cfg(all(any(feature = "gen-llvm", feature = "gen-wasm")))]
fn encode_derived_generic_record_with_different_field_types() {
@ -1312,6 +1358,50 @@ fn decode_record_of_record() {
)
}
#[test]
#[cfg(all(
any(feature = "gen-llvm", feature = "gen-wasm"),
not(debug_assertions) // https://github.com/roc-lang/roc/issues/3898
))]
fn decode_tuple_two_elements() {
assert_evals_to!(
indoc!(
r#"
app "test" imports [Json] provides [main] to "./platform"
main =
when Str.toUtf8 "[\"ab\",10]" |> Decode.fromBytes Json.fromUtf8 is
Ok ("ab", 10u8) -> "abcd"
_ -> "something went wrong"
"#
),
RocStr::from("abcd"),
RocStr
)
}
#[test]
#[cfg(all(
any(feature = "gen-llvm", feature = "gen-wasm"),
not(debug_assertions) // https://github.com/roc-lang/roc/issues/3898
))]
fn decode_tuple_of_tuples() {
assert_evals_to!(
indoc!(
r#"
app "test" imports [Json] provides [main] to "./platform"
main =
when Str.toUtf8 "[[\"ab\",10],[\"cd\",25]]" |> Decode.fromBytes Json.fromUtf8 is
Ok ( ("ab", 10u8), ("cd", 25u8) ) -> "abcd"
_ -> "something went wrong"
"#
),
RocStr::from("abcd"),
RocStr
)
}
#[cfg(all(test, any(feature = "gen-llvm", feature = "gen-wasm")))]
mod hash {
#[cfg(feature = "gen-llvm")]
@ -1572,6 +1662,35 @@ mod hash {
)
}
#[test]
fn tuple_of_u8_and_str() {
assert_evals_to!(
&build_test(r#"(15u8, "bc")"#),
RocList::from_slice(&[15, 98, 99]),
RocList<u8>
)
}
#[test]
fn tuple_of_tuples() {
assert_evals_to!(
&build_test(r#"( (15u8, "bc"), (23u8, "ef") )"#),
RocList::from_slice(&[15, 98, 99, 23, 101, 102]),
RocList<u8>
)
}
#[test]
fn tuple_of_list_of_tuples() {
assert_evals_to!(
&build_test(
r#"( [ ( 15u8, 32u8 ), ( 23u8, 41u8 ) ], [ (45u8, 63u8), (58u8, 73u8) ] )"#
),
RocList::from_slice(&[15, 32, 23, 41, 45, 63, 58, 73]),
RocList<u8>
)
}
#[test]
fn hash_singleton_union() {
assert_evals_to!(
@ -1840,6 +1959,22 @@ mod eq {
use indoc::indoc;
use roc_std::RocStr;
#[test]
fn eq_tuple() {
assert_evals_to!(
indoc!(
r#"
app "test" provides [main] to "./platform"
main =
("a", "b") == ("a", "b")
"#
),
true,
bool
)
}
#[test]
fn custom_eq_impl() {
assert_evals_to!(