11use crate :: {
2- ast:: { FromInputValue , InputValue } ,
2+ ast:: { Document , FromInputValue , InputValue } ,
33 executor:: Registry ,
44 parser:: parse_document_source,
55 schema:: {
@@ -812,20 +812,13 @@ where
812812 }
813813}
814814
815- pub fn validate < ' a , Q , M , Sub , V , F , S > (
816- r : Q ,
817- m : M ,
818- s : Sub ,
819- q : & ' a str ,
820- factory : F ,
821- ) -> Vec < RuleError >
815+ pub fn validate < ' a , Q , M , Sub , F , S > ( r : Q , m : M , s : Sub , q : & ' a str , visit_fn : F ) -> Vec < RuleError >
822816where
823817 S : ScalarValue + ' a ,
824818 Q : GraphQLType < S , TypeInfo = ( ) > ,
825819 M : GraphQLType < S , TypeInfo = ( ) > ,
826820 Sub : GraphQLType < S , TypeInfo = ( ) > ,
827- V : Visitor < ' a , S > + ' a ,
828- F : Fn ( ) -> V ,
821+ F : FnOnce ( & mut ValidatorContext < ' a , S > , & ' a Document < S > ) ,
829822{
830823 let mut root = RootNode :: new_with_scalar_value ( r, m, s) ;
831824
@@ -864,10 +857,7 @@ where
864857 parse_document_source ( q, & root. schema ) . expect ( & format ! ( "Parse error on input {:#?}" , q) ) ;
865858 let mut ctx = ValidatorContext :: new ( unsafe { :: std:: mem:: transmute ( & root. schema ) } , & doc) ;
866859
867- let mut mv = MultiVisitorNil . with ( factory ( ) ) ;
868- visit ( & mut mv, & mut ctx, unsafe {
869- :: std:: mem:: transmute ( doc. as_slice ( ) )
870- } ) ;
860+ visit_fn ( & mut ctx, unsafe { :: std:: mem:: transmute ( doc. as_slice ( ) ) } ) ;
871861
872862 ctx. into_errors ( )
873863}
@@ -881,6 +871,14 @@ where
881871 expect_passes_rule_with_schema ( QueryRoot , MutationRoot , SubscriptionRoot , factory, q) ;
882872}
883873
874+ pub fn expect_passes_fn < ' a , F , S > ( visit_fn : F , q : & ' a str )
875+ where
876+ S : ScalarValue + ' a ,
877+ F : FnOnce ( & mut ValidatorContext < ' a , S > , & ' a Document < S > ) ,
878+ {
879+ expect_passes_fn_with_schema ( QueryRoot , MutationRoot , SubscriptionRoot , visit_fn, q) ;
880+ }
881+
884882pub fn expect_passes_rule_with_schema < ' a , Q , M , Sub , V , F , S > (
885883 r : Q ,
886884 m : M ,
@@ -893,16 +891,40 @@ pub fn expect_passes_rule_with_schema<'a, Q, M, Sub, V, F, S>(
893891 M : GraphQLType < S , TypeInfo = ( ) > ,
894892 Sub : GraphQLType < S , TypeInfo = ( ) > ,
895893 V : Visitor < ' a , S > + ' a ,
896- F : Fn ( ) -> V ,
894+ F : FnOnce ( ) -> V ,
897895{
898- let errs = validate ( r, m, s, q, factory) ;
896+ let errs = validate ( r, m, s, q, move |ctx, doc| {
897+ let mut mv = MultiVisitorNil . with ( factory ( ) ) ;
898+ visit ( & mut mv, ctx, unsafe { :: std:: mem:: transmute ( doc) } ) ;
899+ } ) ;
899900
900901 if !errs. is_empty ( ) {
901902 print_errors ( & errs) ;
902903 panic ! ( "Expected rule to pass, but errors found" ) ;
903904 }
904905}
905906
907+ pub fn expect_passes_fn_with_schema < ' a , Q , M , Sub , F , S > (
908+ r : Q ,
909+ m : M ,
910+ s : Sub ,
911+ visit_fn : F ,
912+ q : & ' a str ,
913+ ) where
914+ S : ScalarValue + ' a ,
915+ Q : GraphQLType < S , TypeInfo = ( ) > ,
916+ M : GraphQLType < S , TypeInfo = ( ) > ,
917+ Sub : GraphQLType < S , TypeInfo = ( ) > ,
918+ F : FnOnce ( & mut ValidatorContext < ' a , S > , & ' a Document < S > ) ,
919+ {
920+ let errs = validate ( r, m, s, q, visit_fn) ;
921+
922+ if !errs. is_empty ( ) {
923+ print_errors ( & errs) ;
924+ panic ! ( "Expected `visit_fn` to pass, but errors found" ) ;
925+ }
926+ }
927+
906928pub fn expect_fails_rule < ' a , V , F , S > ( factory : F , q : & ' a str , expected_errors : & [ RuleError ] )
907929where
908930 S : ScalarValue + ' a ,
@@ -912,6 +934,14 @@ where
912934 expect_fails_rule_with_schema ( QueryRoot , MutationRoot , factory, q, expected_errors) ;
913935}
914936
937+ pub fn expect_fails_fn < ' a , F , S > ( visit_fn : F , q : & ' a str , expected_errors : & [ RuleError ] )
938+ where
939+ S : ScalarValue + ' a ,
940+ F : FnOnce ( & mut ValidatorContext < ' a , S > , & ' a Document < S > ) ,
941+ {
942+ expect_fails_fn_with_schema ( QueryRoot , MutationRoot , visit_fn, q, expected_errors) ;
943+ }
944+
915945pub fn expect_fails_rule_with_schema < ' a , Q , M , V , F , S > (
916946 r : Q ,
917947 m : M ,
@@ -923,9 +953,18 @@ pub fn expect_fails_rule_with_schema<'a, Q, M, V, F, S>(
923953 Q : GraphQLType < S , TypeInfo = ( ) > ,
924954 M : GraphQLType < S , TypeInfo = ( ) > ,
925955 V : Visitor < ' a , S > + ' a ,
926- F : Fn ( ) -> V ,
927- {
928- let errs = validate ( r, m, crate :: EmptySubscription :: < S > :: new ( ) , q, factory) ;
956+ F : FnOnce ( ) -> V ,
957+ {
958+ let errs = validate (
959+ r,
960+ m,
961+ crate :: EmptySubscription :: < S > :: new ( ) ,
962+ q,
963+ move |ctx, doc| {
964+ let mut mv = MultiVisitorNil . with ( factory ( ) ) ;
965+ visit ( & mut mv, ctx, unsafe { :: std:: mem:: transmute ( doc) } ) ;
966+ } ,
967+ ) ;
929968
930969 if errs. is_empty ( ) {
931970 panic ! ( "Expected rule to fail, but no errors were found" ) ;
@@ -940,6 +979,33 @@ pub fn expect_fails_rule_with_schema<'a, Q, M, V, F, S>(
940979 }
941980}
942981
982+ pub fn expect_fails_fn_with_schema < ' a , Q , M , F , S > (
983+ r : Q ,
984+ m : M ,
985+ visit_fn : F ,
986+ q : & ' a str ,
987+ expected_errors : & [ RuleError ] ,
988+ ) where
989+ S : ScalarValue + ' a ,
990+ Q : GraphQLType < S , TypeInfo = ( ) > ,
991+ M : GraphQLType < S , TypeInfo = ( ) > ,
992+ F : FnOnce ( & mut ValidatorContext < ' a , S > , & ' a Document < S > ) ,
993+ {
994+ let errs = validate ( r, m, crate :: EmptySubscription :: < S > :: new ( ) , q, visit_fn) ;
995+
996+ if errs. is_empty ( ) {
997+ panic ! ( "Expected `visit_fn`` to fail, but no errors were found" ) ;
998+ } else if errs != expected_errors {
999+ println ! ( "==> Expected errors:" ) ;
1000+ print_errors ( expected_errors) ;
1001+
1002+ println ! ( "\n ==> Actual errors:" ) ;
1003+ print_errors ( & errs) ;
1004+
1005+ panic ! ( "Unexpected set of errors found" ) ;
1006+ }
1007+ }
1008+
9431009fn print_errors ( errs : & [ RuleError ] ) {
9441010 for err in errs {
9451011 for p in err. locations ( ) {
0 commit comments