Skip to content

Commit

Permalink
Add doc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NightEule5 committed Jun 10, 2024
1 parent fa62894 commit 5919070
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/generate/gen_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,86 @@ impl<'a, P: Parent> GenEnum<'a, P> {
}

/// Inherit the generic parameters of the parent type.
///
/// ```
/// # use virtue::prelude::Generator;
/// # use virtue::parse::{Generic, Lifetime};
/// # use proc_macro2::{Ident, Span};
/// # let mut generator = Generator::with_name("Bar").with_lifetime("a");
/// // given a derive on enum Bar<'a>
/// generator
/// .generate_enum("Foo")
/// .inherit_generics()
/// .add_value("Bar")
/// .make_tuple()
/// .add_field("bar", "&'a str");
/// # generator.assert_eq("enum Foo < 'a > { Bar (&'a str ,) , }");
/// # Ok::<_, virtue::Error>(())
/// ```
///
/// Generates:
/// ```ignore
/// // given a derive on enum Bar<'a>
/// enum Foo<'a> {
/// Bar(&'a str)
/// }
/// ```
pub fn inherit_generics(&mut self) -> &mut Self {
self.generics = self.parent.generics().cloned();
self
}

/// Append generic parameters to the type.
///
/// ```
/// # use virtue::prelude::Generator;
/// # use virtue::parse::{Generic, Lifetime};
/// # use proc_macro2::{Ident, Span};
/// # let mut generator = Generator::with_name("Bar").with_lifetime("a");
/// generator
/// .generate_enum("Foo")
/// .append_generics([Lifetime { ident: Ident::new("a", Span::call_site()), constraint: vec![] }.into()])
/// .add_value("Bar")
/// .make_tuple()
/// .add_field("bar", "&'a str");
/// # generator.assert_eq("enum Foo < 'a > { Bar (&'a str ,) , }");
/// # Ok::<_, virtue::Error>(())
/// ```
///
/// Generates:
/// ```ignore
/// enum Foo<'a> {
/// Bar(&'a str)
/// }
/// ```
pub fn append_generics(&mut self, generics: impl IntoIterator<Item = Generic>) -> &mut Self {
self.generics.get_or_insert_with(|| Generics(Vec::new())).extend(generics);
self
}

/// Add a generic parameter to the type.
///
/// ```
/// # use virtue::prelude::Generator;
/// # use virtue::parse::{Generic, Lifetime};
/// # use proc_macro2::{Ident, Span};
/// # let mut generator = Generator::with_name("Bar").with_lifetime("a");
/// generator
/// .generate_enum("Foo")
/// .add_generic(Lifetime { ident: Ident::new("a", Span::call_site()), constraint: vec![] }.into())
/// .add_value("Bar")
/// .make_tuple()
/// .add_field("bar", "&'a str");
/// # generator.assert_eq("enum Foo < 'a > { Bar (&'a str ,) , }");
/// # Ok::<_, virtue::Error>(())
/// ```
///
/// Generates:
/// ```ignore
/// enum Foo<'a> {
/// Bar(&'a str)
/// }
/// ```
pub fn add_generic(&mut self, generic: Generic) -> &mut Self {
self.generics.get_or_insert_with(|| Generics(Vec::new())).push(generic);
self
Expand Down
60 changes: 60 additions & 0 deletions src/generate/gen_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,78 @@ impl<'a, P: Parent> GenStruct<'a, P> {
}

/// Inherit the generic parameters of the parent type.
///
/// ```
/// # use virtue::prelude::Generator;
/// # let mut generator = Generator::with_name("Bar").with_lifetime("a");
/// // given a derive on struct Bar<'a>
/// generator
/// .generate_struct("Foo")
/// .inherit_generics()
/// .add_field("bar", "&'a str");
/// # generator.assert_eq("struct Foo < 'a > { bar : &'a str , }");
/// # Ok::<_, virtue::Error>(())
/// ```
///
/// Generates:
/// ```ignore
/// // given a derive on struct Bar<'a>
/// struct Foo<'a> {
/// bar: &'a str
/// }
/// ```
pub fn inherit_generics(&mut self) -> &mut Self {
self.generics = self.parent.generics().cloned();
self
}

/// Append generic parameters to the type.
///
/// ```
/// # use virtue::prelude::Generator;
/// # use virtue::parse::{Generic, Lifetime};
/// # use proc_macro2::{Ident, Span};
/// # let mut generator = Generator::with_name("Bar").with_lifetime("a");
/// generator
/// .generate_struct("Foo")
/// .append_generics([Lifetime { ident: Ident::new("a", Span::call_site()), constraint: vec![] }.into()])
/// .add_field("bar", "&'a str");
/// # generator.assert_eq("struct Foo < 'a > { bar : &'a str , }");
/// # Ok::<_, virtue::Error>(())
/// ```
///
/// Generates:
/// ```ignore
/// struct Foo<'a> {
/// bar: &'a str
/// }
/// ```
pub fn append_generics(&mut self, generics: impl IntoIterator<Item = Generic>) -> &mut Self {
self.generics.get_or_insert_with(|| Generics(Vec::new())).extend(generics);
self
}

/// Add a generic parameter to the type.
///
/// ```
/// # use virtue::prelude::Generator;
/// # use virtue::parse::{Generic, Lifetime};
/// # use proc_macro2::{Ident, Span};
/// # let mut generator = Generator::with_name("Bar").with_lifetime("a");
/// generator
/// .generate_struct("Foo")
/// .add_generic(Lifetime { ident: Ident::new("a", Span::call_site()), constraint: vec![] }.into())
/// .add_field("bar", "&'a str");
/// # generator.assert_eq("struct Foo < 'a > { bar : &'a str , }");
/// # Ok::<_, virtue::Error>(())
/// ```
///
/// Generates:
/// ```ignore
/// struct Foo<'a> {
/// bar: &'a str
/// }
/// ```
pub fn add_generic(&mut self, generic: Generic) -> &mut Self {
self.generics.get_or_insert_with(|| Generics(Vec::new())).push(generic);
self
Expand Down

0 comments on commit 5919070

Please sign in to comment.