Skip to content

Commit

Permalink
fill in new derive tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Mar 27, 2024
1 parent e39136d commit 8885a47
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 0 deletions.
13 changes: 13 additions & 0 deletions derive/test/compile_fail/enum_dynamic_unindexed_variants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use sval_derive::*;

#[derive(Value)]
#[sval(dynamic, unindexed_variants)]
pub enum Enum {
A,
B,
C,
}

fn main() {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: proc-macro derive panicked
--> compile_fail/enum_dynamic_unindexed_variants.rs:3:10
|
3 | #[derive(Value)]
| ^^^^^
|
= help: message: dynamic enums don't have variants
13 changes: 13 additions & 0 deletions derive/test/compile_fail/enum_dynamic_unlabeled_variants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use sval_derive::*;

#[derive(Value)]
#[sval(dynamic, unlabeled_variants)]
pub enum Enum {
A,
B,
C,
}

fn main() {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: proc-macro derive panicked
--> compile_fail/enum_dynamic_unlabeled_variants.rs:3:10
|
3 | #[derive(Value)]
| ^^^^^
|
= help: message: dynamic enums don't have variants
195 changes: 195 additions & 0 deletions derive/test/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,201 @@ mod derive_enum {
});
}

#[test]
fn unlabeled() {
#[derive(Value)]
#[sval(unlabeled_variants)]
enum Enum {
Tag,
Tagged(i32),
Record { a: i32 },
Tuple(i32, i32),
}

assert_tokens(&Enum::Tag, {
use sval_test::Token::*;

&[
EnumBegin(None, Some(sval::Label::new("Enum")), None),
Tag(None, None, Some(sval::Index::new(0))),
EnumEnd(None, Some(sval::Label::new("Enum")), None),
]
});

assert_tokens(&Enum::Tagged(42), {
use sval_test::Token::*;

&[
EnumBegin(None, Some(sval::Label::new("Enum")), None),
TaggedBegin(None, None, Some(sval::Index::new(1))),
I32(42),
TaggedEnd(None, None, Some(sval::Index::new(1))),
EnumEnd(None, Some(sval::Label::new("Enum")), None),
]
});

assert_tokens(&Enum::Record { a: 42 }, {
use sval_test::Token::*;

&[
EnumBegin(None, Some(sval::Label::new("Enum")), None),
RecordTupleBegin(None, None, Some(sval::Index::new(2)), Some(1)),
RecordTupleValueBegin(None, sval::Label::new("a"), sval::Index::new(0)),
I32(42),
RecordTupleValueEnd(None, sval::Label::new("a"), sval::Index::new(0)),
RecordTupleEnd(None, None, Some(sval::Index::new(2))),
EnumEnd(None, Some(sval::Label::new("Enum")), None),
]
});

assert_tokens(&Enum::Tuple(42, 43), {
use sval_test::Token::*;

&[
EnumBegin(None, Some(sval::Label::new("Enum")), None),
TupleBegin(None, None, Some(sval::Index::new(3)), Some(2)),
TupleValueBegin(None, sval::Index::new(0)),
I32(42),
TupleValueEnd(None, sval::Index::new(0)),
TupleValueBegin(None, sval::Index::new(1)),
I32(43),
TupleValueEnd(None, sval::Index::new(1)),
TupleEnd(None, None, Some(sval::Index::new(3))),
EnumEnd(None, Some(sval::Label::new("Enum")), None),
]
});
}

#[test]
fn unindexed() {
#[derive(Value)]
#[sval(unindexed_variants)]
enum Enum {
Tag,
Tagged(i32),
Record { a: i32 },
Tuple(i32, i32),
}

assert_tokens(&Enum::Tag, {
use sval_test::Token::*;

&[
EnumBegin(None, Some(sval::Label::new("Enum")), None),
Tag(None, Some(sval::Label::new("Tag")), None),
EnumEnd(None, Some(sval::Label::new("Enum")), None),
]
});

assert_tokens(&Enum::Tagged(42), {
use sval_test::Token::*;

&[
EnumBegin(None, Some(sval::Label::new("Enum")), None),
TaggedBegin(None, Some(sval::Label::new("Tagged")), None),
I32(42),
TaggedEnd(None, Some(sval::Label::new("Tagged")), None),
EnumEnd(None, Some(sval::Label::new("Enum")), None),
]
});

assert_tokens(&Enum::Record { a: 42 }, {
use sval_test::Token::*;

&[
EnumBegin(None, Some(sval::Label::new("Enum")), None),
RecordTupleBegin(None, Some(sval::Label::new("Record")), None, Some(1)),
RecordTupleValueBegin(None, sval::Label::new("a"), sval::Index::new(0)),
I32(42),
RecordTupleValueEnd(None, sval::Label::new("a"), sval::Index::new(0)),
RecordTupleEnd(None, Some(sval::Label::new("Record")), None),
EnumEnd(None, Some(sval::Label::new("Enum")), None),
]
});

assert_tokens(&Enum::Tuple(42, 43), {
use sval_test::Token::*;

&[
EnumBegin(None, Some(sval::Label::new("Enum")), None),
TupleBegin(None, Some(sval::Label::new("Tuple")), None, Some(2)),
TupleValueBegin(None, sval::Index::new(0)),
I32(42),
TupleValueEnd(None, sval::Index::new(0)),
TupleValueBegin(None, sval::Index::new(1)),
I32(43),
TupleValueEnd(None, sval::Index::new(1)),
TupleEnd(None, Some(sval::Label::new("Tuple")), None),
EnumEnd(None, Some(sval::Label::new("Enum")), None),
]
});
}

#[test]
fn unlabeled_unindexed() {
#[derive(Value)]
#[sval(unlabeled_variants, unindexed_variants)]
enum Enum {
Tag,
Tagged(i32),
Record { a: i32 },
Tuple(i32, i32),
}

assert_tokens(&Enum::Tag, {
use sval_test::Token::*;

&[
EnumBegin(None, Some(sval::Label::new("Enum")), None),
Tag(None, None, None),
EnumEnd(None, Some(sval::Label::new("Enum")), None),
]
});

assert_tokens(&Enum::Tagged(42), {
use sval_test::Token::*;

&[
EnumBegin(None, Some(sval::Label::new("Enum")), None),
TaggedBegin(None, None, None),
I32(42),
TaggedEnd(None, None, None),
EnumEnd(None, Some(sval::Label::new("Enum")), None),
]
});

assert_tokens(&Enum::Record { a: 42 }, {
use sval_test::Token::*;

&[
EnumBegin(None, Some(sval::Label::new("Enum")), None),
RecordTupleBegin(None, None, None, Some(1)),
RecordTupleValueBegin(None, sval::Label::new("a"), sval::Index::new(0)),
I32(42),
RecordTupleValueEnd(None, sval::Label::new("a"), sval::Index::new(0)),
RecordTupleEnd(None, None, None),
EnumEnd(None, Some(sval::Label::new("Enum")), None),
]
});

assert_tokens(&Enum::Tuple(42, 43), {
use sval_test::Token::*;

&[
EnumBegin(None, Some(sval::Label::new("Enum")), None),
TupleBegin(None, None, None, Some(2)),
TupleValueBegin(None, sval::Index::new(0)),
I32(42),
TupleValueEnd(None, sval::Index::new(0)),
TupleValueBegin(None, sval::Index::new(1)),
I32(43),
TupleValueEnd(None, sval::Index::new(1)),
TupleEnd(None, None, None),
EnumEnd(None, Some(sval::Label::new("Enum")), None),
]
});
}

#[test]
fn tagged() {
const CONTAINER: sval::Tag = sval::Tag::new("container");
Expand Down
3 changes: 3 additions & 0 deletions derive_macros/src/derive/derive_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ impl EnumAttrs {
assert!(tag.is_none(), "dynamic enums can't have tags");
assert!(label.is_none(), "dynamic enums can't have labels");
assert!(index.is_none(), "dynamic enums can't have indexes");

assert!(!unlabeled_variants, "dynamic enums don't have variants");
assert!(!unindexed_variants, "dynamic enums don't have variants");
}

EnumAttrs {
Expand Down

0 comments on commit 8885a47

Please sign in to comment.