Skip to content

Commit 26a9881

Browse files
committed
Temp
1 parent 9e09218 commit 26a9881

24 files changed

+814
-276
lines changed

crates/apollo-smith/src/next/ast/definition.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use apollo_compiler::ast::{Definition, InputObjectTypeDefinition, Name, Type};
2-
use apollo_compiler::schema::ExtendedType;
31
use arbitrary::Unstructured;
42

3+
use apollo_compiler::ast::{Definition, InputObjectTypeDefinition, Name, Type};
4+
55
pub(crate) trait DefinitionExt {
66
fn ty(&self, u: &mut Unstructured) -> arbitrary::Result<Type>;
77
}

crates/apollo-smith/src/next/ast/directive_definition.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use super::document::DocumentExt;
2-
use crate::next::unstructured::Unstructured;
1+
use std::ops::Deref;
2+
33
use apollo_compiler::ast::{
4-
Argument, Directive, DirectiveDefinition, DirectiveList, DirectiveLocation, Document,
4+
Argument, Directive, DirectiveDefinition, DirectiveList, DirectiveLocation,
55
};
66
use apollo_compiler::{Node, Schema};
7-
use std::ops::Deref;
7+
8+
use crate::next::unstructured::Unstructured;
89

910
pub(crate) struct LocationFilter<I>(I, DirectiveLocation);
1011

@@ -57,7 +58,7 @@ impl<I: ?Sized> DirectiveDefinitionIterExt for I {
5758
if arg.is_required() || u.arbitrary()? {
5859
arguments.push(Node::new(Argument {
5960
name: arg.name.clone(),
60-
value: Node::new(u.arbitrary_value(arg.ty.deref(), schema)?),
61+
value: Node::new(u.arbitrary_value(schema, arg.ty.deref())?),
6162
}))
6263
}
6364
}

crates/apollo-smith/src/next/ast/document.rs

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use paste::paste;
22

3+
use crate::next::ast::definition::DefinitionKind;
34
use apollo_compiler::ast::{
45
Definition, DirectiveDefinition, Document, EnumTypeDefinition, EnumTypeExtension,
56
FragmentDefinition, InputObjectTypeDefinition, InputObjectTypeExtension,
@@ -18,7 +19,7 @@ macro_rules! access {
1819
fn [<random_ $ty:snake>](
1920
&self,
2021
u: &mut Unstructured,
21-
) -> arbitrary::Result<&Node<$ty>> {
22+
) -> arbitrary::Result<Option<&Node<$ty>>> {
2223
let mut existing = self
2324
.target()
2425
.definitions
@@ -31,20 +32,18 @@ macro_rules! access {
3132
}
3233
})
3334
.collect::<Vec<_>>();
34-
let idx = u.choose_index(existing.len()).map_err(|e|{
35-
if let arbitrary::Error::EmptyChoose = e {
36-
panic!("no existing definitions of type {}", stringify!($ty))
37-
} else {
38-
e
39-
}
40-
})?;
41-
Ok(existing.remove(idx))
35+
match u.choose_index(existing.len()) {
36+
Ok(idx)=> Ok(Some(existing.remove(idx))),
37+
Err(arbitrary::Error::EmptyChoose)=> Ok(None),
38+
Err(e)=> Err(e)
39+
}
40+
4241
}
4342

4443
fn [<random_ $ty:snake _mut>](
4544
&mut self,
4645
u: &mut Unstructured,
47-
) -> arbitrary::Result<&mut Node<$ty>> {
46+
) -> arbitrary::Result<Option<&mut Node<$ty>>> {
4847
let mut existing = self
4948
.target_mut()
5049
.definitions
@@ -57,14 +56,12 @@ macro_rules! access {
5756
}
5857
})
5958
.collect::<Vec<_>>();
60-
let idx = u.choose_index(existing.len()).map_err(|e|{
61-
if let arbitrary::Error::EmptyChoose = e {
62-
panic!("no existing definitions of type {}", stringify!($ty))
63-
} else {
64-
e
65-
}
66-
})?;
67-
Ok(existing.remove(idx))
59+
60+
match u.choose_index(existing.len()) {
61+
Ok(idx)=> Ok(Some(existing.remove(idx))),
62+
Err(arbitrary::Error::EmptyChoose)=> Ok(None),
63+
Err(e)=> Err(e)
64+
}
6865
}
6966

7067
fn [<sample_ $ty:snake s>](
@@ -110,6 +107,42 @@ pub(crate) trait DocumentExt {
110107
access!(EnumTypeExtension);
111108
access!(InputObjectTypeExtension);
112109

110+
fn random_definition(
111+
&self,
112+
u: &mut Unstructured,
113+
definitions: Vec<DefinitionKind>,
114+
) -> arbitrary::Result<Option<&Definition>> {
115+
let mut existing = self
116+
.target()
117+
.definitions
118+
.iter()
119+
.filter(|d| definitions.iter().any(|t| t.matches(*d)))
120+
.collect::<Vec<_>>();
121+
match u.choose_index(existing.len()) {
122+
Ok(idx) => Ok(Some(existing.remove(idx))),
123+
Err(arbitrary::Error::EmptyChoose) => Ok(None),
124+
Err(e) => Err(e),
125+
}
126+
}
127+
128+
fn random_definition_mut(
129+
&mut self,
130+
u: &mut Unstructured,
131+
definitions: Vec<DefinitionKind>,
132+
) -> arbitrary::Result<Option<&mut Definition>> {
133+
let mut existing = self
134+
.target_mut()
135+
.definitions
136+
.iter_mut()
137+
.filter(|d| definitions.iter().any(|t| t.matches(*d)))
138+
.collect::<Vec<_>>();
139+
match u.choose_index(existing.len()) {
140+
Ok(idx) => Ok(Some(existing.remove(idx))),
141+
Err(arbitrary::Error::EmptyChoose) => Ok(None),
142+
Err(e) => Err(e),
143+
}
144+
}
145+
113146
fn target(&self) -> &Document;
114147
fn target_mut(&mut self) -> &mut Document;
115148
}
Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,95 @@
1+
use apollo_compiler::ast::{FieldDefinition, InterfaceTypeDefinition, ObjectTypeDefinition};
2+
use apollo_compiler::Node;
3+
14
pub(crate) mod definition;
25
pub(crate) mod directive_definition;
36
pub(crate) mod document;
4-
pub(crate) mod object_type_definition;
7+
8+
/// macro for accessing fields on ast elements
9+
macro_rules! field_access {
10+
($ty:ty) => {
11+
paste::paste! {
12+
pub(crate) trait [<$ty Ext>] {
13+
fn random_field(
14+
&self,
15+
u: &mut crate::next::Unstructured,
16+
) -> arbitrary::Result<&apollo_compiler::Node<apollo_compiler::ast::FieldDefinition>> {
17+
Ok(u.choose(&self.target().fields).map_err(|e| {
18+
if let arbitrary::Error::EmptyChoose = e {
19+
panic!("no existing fields")
20+
} else {
21+
e
22+
}
23+
})?)
24+
}
25+
26+
fn random_field_mut(
27+
&mut self,
28+
u: &mut crate::next::Unstructured,
29+
) -> arbitrary::Result<&mut apollo_compiler::Node<apollo_compiler::ast::FieldDefinition>> {
30+
let idx = u.choose_index(self.target().fields.len()).map_err(|e| {
31+
if let arbitrary::Error::EmptyChoose = e {
32+
panic!("no existing fields")
33+
} else {
34+
e
35+
}
36+
})?;
37+
Ok(&mut self.target_mut().fields[idx])
38+
}
39+
40+
fn sample_fields(
41+
&self,
42+
u: &mut crate::next::Unstructured,
43+
) -> arbitrary::Result<Vec<&apollo_compiler::Node<apollo_compiler::ast::FieldDefinition>>> {
44+
let existing = self
45+
.target()
46+
.fields
47+
.iter()
48+
.filter(|_| u.arbitrary().unwrap_or(false))
49+
.collect::<Vec<_>>();
50+
51+
Ok(existing)
52+
}
53+
fn target(&self) -> &$ty;
54+
fn target_mut(&mut self) -> &mut $ty;
55+
}
56+
57+
impl [<$ty Ext>] for $ty {
58+
fn target(&self) -> &$ty {
59+
self
60+
}
61+
fn target_mut(&mut self) -> &mut $ty {
62+
self
63+
}
64+
}
65+
}
66+
};
67+
}
68+
69+
field_access!(ObjectTypeDefinition);
70+
field_access!(InterfaceTypeDefinition);
71+
72+
pub(crate) trait HasFields {
73+
fn fields(&self) -> &Vec<Node<FieldDefinition>>;
74+
fn fields_mut(&mut self) -> &mut Vec<Node<FieldDefinition>>;
75+
}
76+
77+
impl HasFields for ObjectTypeDefinition {
78+
fn fields(&self) -> &Vec<Node<FieldDefinition>> {
79+
&self.fields
80+
}
81+
82+
fn fields_mut(&mut self) -> &mut Vec<Node<FieldDefinition>> {
83+
&mut self.fields
84+
}
85+
}
86+
87+
impl HasFields for InterfaceTypeDefinition {
88+
fn fields(&self) -> &Vec<Node<FieldDefinition>> {
89+
&self.fields
90+
}
91+
92+
fn fields_mut(&mut self) -> &mut Vec<Node<FieldDefinition>> {
93+
&mut self.fields
94+
}
95+
}

crates/apollo-smith/src/next/ast/object_type_definition.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)