Skip to content

Commit

Permalink
Add doc comment tests
Browse files Browse the repository at this point in the history
Add testcases for the struct and enum generators.
  • Loading branch information
SlayerOfTheBad committed Jan 4, 2024
1 parent 6d1a381 commit 5458fa0
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
36 changes: 35 additions & 1 deletion src/generate/gen_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,41 @@ use crate::parse::Visibility;
use crate::prelude::{Delimiter, Ident, Span};
use crate::Result;

/// Builder to generate a `struct <Name> { <field>: <ty>, ... }`
/// Builder to generate an `enum <Name> { <value> { ... }, ... }`
///
/// ```
/// # use virtue::prelude::Generator;
/// # let mut generator = Generator::with_name("Fooz");
/// {
/// let mut enumgen = generator.generate_enum("Foo");
/// enumgen
/// .add_value("ZST")
/// .make_zst();
/// enumgen
/// .add_value("Named")
/// .add_field("bar", "u16")
/// .add_field("baz", "String");
/// enumgen
/// .add_value("Unnamed")
/// .add_field("", "u16")
/// .add_field("baz", "String")
/// .make_fields_unnamed();
/// }
/// # generator.assert_eq("enum Foo { ZST , Named { bar : u16 , baz : String , } , Unnamed (u16 , String ,) , }");
/// # Ok::<_, virtue::Error>(())
/// ```
///
/// Generates:
/// ```
/// enum Foo {
/// ZST,
/// Named {
/// bar: u16,
/// baz: String,
/// },
/// Unnamed(u16, String),
/// };
/// ```
pub struct GenEnum<'a, P: Parent> {
parent: &'a mut P,
name: Ident,
Expand Down
56 changes: 55 additions & 1 deletion src/generate/gen_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use super::{Impl, ImplFor, Parent, StreamBuilder, StringOrIdent};
use crate::parse::Visibility;
use crate::prelude::{Delimiter, Ident, Span};

/// Builder to generate a `struct <Name> { <field>: <ty>, ... }`
/// Builder to generate a struct.
/// Defaults to a struct with named fields `struct <Name> { <field>: <ty>, ... }`
pub struct GenStruct<'a, P: Parent> {
parent: &'a mut P,
name: Ident,
Expand All @@ -27,6 +28,23 @@ impl<'a, P: Parent> GenStruct<'a, P> {
/// Make the struct a zero-sized type (no fields)
///
/// Any fields will be ignored
///
/// ```
/// # use virtue::prelude::Generator;
/// # let mut generator = Generator::with_name("Fooz");
/// generator
/// .generate_struct("Foo")
/// .make_zst()
/// .add_field("bar", "u16")
/// .add_field("baz", "String");
/// # generator.assert_eq("struct Foo ;");
/// # Ok::<_, virtue::Error>(())
/// ```
///
/// Generates:
/// ```
/// struct Foo;
/// ```
pub fn make_zst(&mut self) -> &mut Self {
self.struct_type = StructType::Zst;
self
Expand All @@ -35,6 +53,23 @@ impl<'a, P: Parent> GenStruct<'a, P> {
/// Make the struct fields unnamed
///
/// The names of any field will be ignored
///
/// ```
/// # use virtue::prelude::Generator;
/// # let mut generator = Generator::with_name("Fooz");
/// generator
/// .generate_struct("Foo")
/// .make_fields_unnamed()
/// .add_field("bar", "u16")
/// .add_field("baz", "String");
/// # generator.assert_eq("struct Foo (u16 , String ,) ;");
/// # Ok::<_, virtue::Error>(())
/// ```
///
/// Generates:
/// ```
/// struct Foo(u16, String);
/// ```
pub fn make_fields_unnamed(&mut self) -> &mut Self {
self.struct_type = StructType::Unnamed;
self
Expand All @@ -49,6 +84,25 @@ impl<'a, P: Parent> GenStruct<'a, P> {
/// Add a *private* field to the struct. For adding a public field, see `add_pub_field`
///
/// Names are ignored when the Struct's fields are unnamed
///
/// ```
/// # use virtue::prelude::Generator;
/// # let mut generator = Generator::with_name("Fooz");
/// generator
/// .generate_struct("Foo")
/// .add_field("bar", "u16")
/// .add_field("baz", "String");
/// # generator.assert_eq("struct Foo { bar : u16 , baz : String , }");
/// # Ok::<_, virtue::Error>(())
/// ```
///
/// Generates:
/// ```
/// struct Foo {
/// bar: u16,
/// baz: String,
/// };
/// ```
pub fn add_field(&mut self, name: impl Into<String>, ty: impl Into<String>) -> &mut Self {
self.fields.push(StructField {
name: name.into(),
Expand Down
2 changes: 1 addition & 1 deletion src/generate/generate_mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{GenStruct, GenEnum, Impl, Parent, StreamBuilder};
use super::{GenEnum, GenStruct, Impl, Parent, StreamBuilder};
use crate::{
parse::Visibility,
prelude::{Delimiter, Ident, Span},
Expand Down
2 changes: 1 addition & 1 deletion src/generate/generator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{GenerateMod, Impl, ImplFor, StreamBuilder, StringOrIdent, GenStruct, GenEnum};
use super::{GenEnum, GenStruct, GenerateMod, Impl, ImplFor, StreamBuilder, StringOrIdent};
use crate::parse::{GenericConstraints, Generics};
use crate::prelude::{Ident, TokenStream};

Expand Down

0 comments on commit 5458fa0

Please sign in to comment.