1
1
use crate :: {
2
- ast:: { FromInputValue , InputValue } ,
2
+ ast:: { Document , FromInputValue , InputValue } ,
3
3
executor:: Registry ,
4
4
parser:: parse_document_source,
5
5
schema:: {
@@ -812,20 +812,13 @@ where
812
812
}
813
813
}
814
814
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 >
822
816
where
823
817
S : ScalarValue + ' a ,
824
818
Q : GraphQLType < S , TypeInfo = ( ) > ,
825
819
M : GraphQLType < S , TypeInfo = ( ) > ,
826
820
Sub : GraphQLType < S , TypeInfo = ( ) > ,
827
- V : Visitor < ' a , S > + ' a ,
828
- F : Fn ( ) -> V ,
821
+ F : FnOnce ( & mut ValidatorContext < ' a , S > , & ' a Document < S > ) ,
829
822
{
830
823
let mut root = RootNode :: new_with_scalar_value ( r, m, s) ;
831
824
@@ -864,10 +857,7 @@ where
864
857
parse_document_source ( q, & root. schema ) . expect ( & format ! ( "Parse error on input {:#?}" , q) ) ;
865
858
let mut ctx = ValidatorContext :: new ( unsafe { :: std:: mem:: transmute ( & root. schema ) } , & doc) ;
866
859
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 ( ) ) } ) ;
871
861
872
862
ctx. into_errors ( )
873
863
}
@@ -881,6 +871,14 @@ where
881
871
expect_passes_rule_with_schema ( QueryRoot , MutationRoot , SubscriptionRoot , factory, q) ;
882
872
}
883
873
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
+
884
882
pub fn expect_passes_rule_with_schema < ' a , Q , M , Sub , V , F , S > (
885
883
r : Q ,
886
884
m : M ,
@@ -893,16 +891,40 @@ pub fn expect_passes_rule_with_schema<'a, Q, M, Sub, V, F, S>(
893
891
M : GraphQLType < S , TypeInfo = ( ) > ,
894
892
Sub : GraphQLType < S , TypeInfo = ( ) > ,
895
893
V : Visitor < ' a , S > + ' a ,
896
- F : Fn ( ) -> V ,
894
+ F : FnOnce ( ) -> V ,
897
895
{
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
+ } ) ;
899
900
900
901
if !errs. is_empty ( ) {
901
902
print_errors ( & errs) ;
902
903
panic ! ( "Expected rule to pass, but errors found" ) ;
903
904
}
904
905
}
905
906
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
+
906
928
pub fn expect_fails_rule < ' a , V , F , S > ( factory : F , q : & ' a str , expected_errors : & [ RuleError ] )
907
929
where
908
930
S : ScalarValue + ' a ,
@@ -912,6 +934,14 @@ where
912
934
expect_fails_rule_with_schema ( QueryRoot , MutationRoot , factory, q, expected_errors) ;
913
935
}
914
936
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
+
915
945
pub fn expect_fails_rule_with_schema < ' a , Q , M , V , F , S > (
916
946
r : Q ,
917
947
m : M ,
@@ -923,9 +953,18 @@ pub fn expect_fails_rule_with_schema<'a, Q, M, V, F, S>(
923
953
Q : GraphQLType < S , TypeInfo = ( ) > ,
924
954
M : GraphQLType < S , TypeInfo = ( ) > ,
925
955
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
+ ) ;
929
968
930
969
if errs. is_empty ( ) {
931
970
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>(
940
979
}
941
980
}
942
981
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
+
943
1009
fn print_errors ( errs : & [ RuleError ] ) {
944
1010
for err in errs {
945
1011
for p in err. locations ( ) {
0 commit comments