forked from graphql-rust/juniper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathderive_enum.rs
More file actions
120 lines (102 loc) · 3 KB
/
derive_enum.rs
File metadata and controls
120 lines (102 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#[cfg(test)]
use fnv::FnvHashMap;
#[cfg(test)]
use juniper::{self, DefaultScalarValue, FromInputValue, GraphQLType, InputValue, ToInputValue};
pub struct CustomContext {}
impl juniper::Context for CustomContext {}
#[derive(juniper::GraphQLEnum, Debug, PartialEq)]
#[graphql(name = "Some", description = "enum descr")]
enum SomeEnum {
Regular,
#[graphql(name = "FULL", description = "field descr", deprecated = "depr")]
Full,
}
/// Enum doc.
#[derive(juniper::GraphQLEnum)]
enum DocEnum {
/// Variant doc.
Foo,
}
/// Doc 1.\
/// Doc 2.
///
/// Doc 4.
#[derive(juniper::GraphQLEnum, Debug, PartialEq)]
enum MultiDocEnum {
/// Variant 1.
/// Variant 2.
Foo,
}
/// This is not used as the description.
#[derive(juniper::GraphQLEnum, Debug, PartialEq)]
#[graphql(description = "enum override")]
enum OverrideDocEnum {
/// This is not used as the description.
#[graphql(description = "variant override")]
Foo,
}
#[derive(juniper::GraphQLEnum)]
#[graphql(context = CustomContext, noasync)]
enum ContextEnum {
A,
}
#[test]
fn test_derived_enum() {
// Ensure that rename works.
assert_eq!(<SomeEnum as GraphQLType>::name(&()), Some("Some"));
// Ensure validity of meta info.
let mut registry: juniper::Registry = juniper::Registry::new(FnvHashMap::default());
let meta = SomeEnum::meta(&(), &mut registry);
assert_eq!(meta.name(), Some("Some"));
assert_eq!(meta.description(), Some(&"enum descr".to_string()));
// Test Regular variant.
assert_eq!(
<_ as ToInputValue>::to_input_value(&SomeEnum::Regular),
InputValue::scalar("REGULAR")
);
assert_eq!(
FromInputValue::from_input_value(&InputValue::scalar("REGULAR")),
Some(SomeEnum::Regular)
);
// Test FULL variant.
assert_eq!(
<_ as ToInputValue>::to_input_value(&SomeEnum::Full),
InputValue::scalar("FULL")
);
assert_eq!(
FromInputValue::from_input_value(&InputValue::scalar("FULL")),
Some(SomeEnum::Full)
);
}
#[test]
fn test_doc_comment() {
let mut registry: juniper::Registry = juniper::Registry::new(FnvHashMap::default());
let meta = DocEnum::meta(&(), &mut registry);
assert_eq!(meta.description(), Some(&"Enum doc.".to_string()));
}
#[test]
fn test_multi_doc_comment() {
let mut registry: juniper::Registry = juniper::Registry::new(FnvHashMap::default());
let meta = MultiDocEnum::meta(&(), &mut registry);
assert_eq!(
meta.description(),
Some(&"Doc 1. Doc 2.\n\nDoc 4.".to_string())
);
}
#[test]
fn test_doc_comment_override() {
let mut registry: juniper::Registry = juniper::Registry::new(FnvHashMap::default());
let meta = OverrideDocEnum::meta(&(), &mut registry);
assert_eq!(meta.description(), Some(&"enum override".to_string()));
}
fn test_context<T>(_t: T)
where
T: GraphQLType<Context = CustomContext>,
{
// empty
}
#[test]
fn test_doc_custom_context() {
test_context(ContextEnum::A);
// test_context(OverrideDocEnum::Foo); does not work
}