From 03f694fff68568a581d0450291f5b0f9e71208fe Mon Sep 17 00:00:00 2001 From: theduke Date: Sat, 24 Jun 2017 20:20:54 +0200 Subject: [PATCH] Add nesting layer for codegen tests and add test for input_object derive --- juniper_tests/Cargo.toml | 1 + juniper_tests/src/codegen/enums.rs | 46 +++++++++++++++++++++ juniper_tests/src/codegen/input_objects.rs | 21 ++++++++++ juniper_tests/src/codegen/mod.rs | 48 +--------------------- juniper_tests/src/lib.rs | 1 + 5 files changed, 71 insertions(+), 46 deletions(-) create mode 100644 juniper_tests/src/codegen/enums.rs create mode 100644 juniper_tests/src/codegen/input_objects.rs diff --git a/juniper_tests/Cargo.toml b/juniper_tests/Cargo.toml index 23853710..ad456fa9 100644 --- a/juniper_tests/Cargo.toml +++ b/juniper_tests/Cargo.toml @@ -5,6 +5,7 @@ version = "0.1.0" [dependencies] juniper = { path = "../juniper" } juniper_codegen = { path = "../juniper_codegen" } +serde_json = { version = "^1.0.2" } [[test]] name = "integration_tests" diff --git a/juniper_tests/src/codegen/enums.rs b/juniper_tests/src/codegen/enums.rs new file mode 100644 index 00000000..4c476b6c --- /dev/null +++ b/juniper_tests/src/codegen/enums.rs @@ -0,0 +1,46 @@ +use std::collections::HashMap; + +use juniper::{self, InputValue, ToInputValue, GraphQLType, FromInputValue}; + +#[derive(GraphQLEnum, Debug, PartialEq)] +#[graphql(name="Some", description="enum descr")] +enum SomeEnum { + Regular, + + #[graphql( + name="FULL", + description="field descr", + deprecated="depr" + )] + Full, +} + +#[test] +fn test_derived_enum() { + // Ensure that rename works. + assert_eq!(SomeEnum::name(), Some("Some")); + + // Ensure validity of meta info. + let mut registry = juniper::Registry::new(HashMap::new()); + let meta = SomeEnum::meta(&mut registry); + + // Test Regular variant. + assert_eq!( + SomeEnum::Regular.to(), + InputValue::String("Regular".into()) + ); + assert_eq!( + FromInputValue::from(&InputValue::String("Regular".into())), + Some(SomeEnum::Regular) + ); + + // Test FULL variant. + assert_eq!( + SomeEnum::Full.to(), + InputValue::String("FULL".into()) + ); + assert_eq!( + FromInputValue::from(&InputValue::String("FULL".into())), + Some(SomeEnum::Full) + ); +} diff --git a/juniper_tests/src/codegen/input_objects.rs b/juniper_tests/src/codegen/input_objects.rs new file mode 100644 index 00000000..6f70a7b8 --- /dev/null +++ b/juniper_tests/src/codegen/input_objects.rs @@ -0,0 +1,21 @@ +use juniper::{self, InputValue, ToInputValue, GraphQLType, FromInputValue}; + +#[derive(GraphQLInputObject, Debug, PartialEq)] +#[graphql(name="MyInput")] +struct Input { + regular_field: String, + #[graphql(name="haha", default="33")] + c: i32, +} + +#[test] +fn test_derived_input_object() { + assert_eq!(Input::name(), Some("MyInput")); + + let obj = Input { + regular_field: "a".to_string(), + c: 33, + }; + let restored: Input = FromInputValue::from(&obj.to()).unwrap(); + assert_eq!(obj, restored); +} diff --git a/juniper_tests/src/codegen/mod.rs b/juniper_tests/src/codegen/mod.rs index 4c476b6c..33366cf9 100644 --- a/juniper_tests/src/codegen/mod.rs +++ b/juniper_tests/src/codegen/mod.rs @@ -1,46 +1,2 @@ -use std::collections::HashMap; - -use juniper::{self, InputValue, ToInputValue, GraphQLType, FromInputValue}; - -#[derive(GraphQLEnum, Debug, PartialEq)] -#[graphql(name="Some", description="enum descr")] -enum SomeEnum { - Regular, - - #[graphql( - name="FULL", - description="field descr", - deprecated="depr" - )] - Full, -} - -#[test] -fn test_derived_enum() { - // Ensure that rename works. - assert_eq!(SomeEnum::name(), Some("Some")); - - // Ensure validity of meta info. - let mut registry = juniper::Registry::new(HashMap::new()); - let meta = SomeEnum::meta(&mut registry); - - // Test Regular variant. - assert_eq!( - SomeEnum::Regular.to(), - InputValue::String("Regular".into()) - ); - assert_eq!( - FromInputValue::from(&InputValue::String("Regular".into())), - Some(SomeEnum::Regular) - ); - - // Test FULL variant. - assert_eq!( - SomeEnum::Full.to(), - InputValue::String("FULL".into()) - ); - assert_eq!( - FromInputValue::from(&InputValue::String("FULL".into())), - Some(SomeEnum::Full) - ); -} +mod enums; +mod input_objects; diff --git a/juniper_tests/src/lib.rs b/juniper_tests/src/lib.rs index 3396c71a..cce45dc6 100644 --- a/juniper_tests/src/lib.rs +++ b/juniper_tests/src/lib.rs @@ -1,4 +1,5 @@ #[macro_use] extern crate juniper; #[macro_use] extern crate juniper_codegen; +#[macro_use] extern crate serde_json; mod codegen;