4 个版本 (1 个稳定版)
1.0.0 | 2021 年 3 月 31 日 |
---|---|
0.1.0 | 2020 年 9 月 18 日 |
0.1.0-beta.1 | 2020 年 9 月 17 日 |
0.1.0-beta.0 | 2020 年 9 月 14 日 |
#1796 in Rust 模式
每月下载 53 次
62KB
452 行
依赖关系
[dependencies]
beaver = "1"
serde = { version = "1.0", features = ["derive"] }
如果你想在你的 struct 字段中使用 chrono,你的 Cargo.toml
可能看起来像这样。
[dependencies]
beaver = "1"
serde = { version = "1.0", features = ["derive"] }
# you need `serde` feature.
chrono = { version = "0.4", features = ["serde"] }
用法
快速入门
use serde::{Deserialize, Serialize};
// `Post` needs both of `Serialize` and `Deserialize`.
#[derive(Serialize, Deserialize, Debug)]
struct Post {
id: u16,
title: String,
approved: bool,
}
beaver::define! {
PostFactory (Post) {
id -> |n| n,
title -> |n| format!("post-{}", n),
approved -> |_| false,
}
}
fn main() {
let post_factory = PostFactory::new();
let post1 = post_factory.build(|_| {});
let post2 = post_factory.build(|_| {});
println!("{:?}", post1);
println!("{:?}", post2);
}
定义一个工厂
beaver::define! {
// [factory name] (struct)
PostFactory (Post) {
// `n` is a sequence number.
id -> |n| n,
title -> |n| format!("{}", n),
approved -> |_| false,
}
}
这个 define!
宏定义了一个 struct,PostFactory
作为工厂。如果你想在模块外使用工厂,你需要使工厂和 struct 都为公共。更多信息请参阅这个 示例。
构建 struct
// initialize a factory.
let post_factory = PostFactory::new();
// build a `Post`.
post_factory.build(|_| {});
// build a vector of some `Posts`.
post_factory.build_list(3, |_| {});
// override attributes of a factory.
post_factory.build(|post| {
post.id = 1024;
post.title = "foo bar".to_string()
});
示例
公共工厂
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
// `Post` needs to be public.
#[derive(Serialize, Deserialize, Debug)]
pub struct Post {
id: u16,
title: String,
approved: bool,
created_at: NaiveDateTime,
}
mod factory {
use crate::Post;
use chrono::NaiveDate;
beaver::define! {
// `PostFactory` needs to be public.
pub PostFactory (Post) {
id -> |n| n,
title -> |n| format!("post-{}", n),
approved -> |_| false,
created_at -> |_| NaiveDate::from_ymd(2020, 1, 1).and_hms(0, 0, 0),
}
}
}
fn main() {
use factory::PostFactory;
let post_factory = PostFactory::new();
let post1 = post_factory.build(|_| {});
let post2 = post_factory.build(|_| {});
println!("{:?}\n{:?}", post1, post2);
}
输出
Post { id: 1, title: "post-1", approved: false, created_at: 2020-01-01T00:00:00 }
Post { id: 2, title: "post-2", approved: false, created_at: 2020-01-01T00:00:00 }
子工厂向量
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct Post {
id: u16,
title: String,
approved: bool,
tags: Vec<Tag>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Tag {
id: u16,
name: String,
}
mod factory {
use crate::Post;
use crate::Tag;
beaver::define! {
pub PostFactory (Post) {
id -> |n| n,
title -> |n| format!("post-{}", n),
approved -> |_| true,
// use `build_list`
tags -> |n| TagFactory::build_list(3, n),
}
}
beaver::define! {
TagFactory (Tag) {
id -> |n| beaver::sequence(100, n),
name -> |n| format!("tag-{}", n),
}
}
}
fn main() {
use factory::PostFactory;
let post_factory = PostFactory::new();
let post1 = post_factory.build(|_| {});
let post2 = post_factory.build(|_| {});
println!("{:?}\n{:?}", post1, post2);
let posts = post_factory.build_list(3, |_| {});
for post in posts {
println!("{:?}", post);
}
}
输出
Post { id: 1, title: "post-1", approved: true, tags: [Tag { id: 1, name: "tag-1" }, Tag { id: 2, name: "tag-2" }, Tag { id: 3, name: "tag-3" }] }
Post { id: 2, title: "post-2", approved: true, tags: [Tag { id: 4, name: "tag-4" }, Tag { id: 5, name: "tag-5" }, Tag { id: 6, name: "tag-6" }] }
Post { id: 3, title: "post-3", approved: true, tags: [Tag { id: 7, name: "tag-7" }, Tag { id: 8, name: "tag-8" }, Tag { id: 9, name: "tag-9" }] }
Post { id: 4, title: "post-4", approved: true, tags: [Tag { id: 10, name: "tag-10" }, Tag { id: 11, name: "tag-11" }, Tag { id: 12, name: "tag-12" }] }
Post { id: 5, title: "post-5", approved: true, tags: [Tag { id: 13, name: "tag-13" }, Tag { id: 14, name: "tag-14" }, Tag { id: 15, name: "tag-15" }] }
其他
贡献
欢迎贡献、问题和拉取请求!
许可证
在 MIT 许可证下授权 (LICENSE)。
依赖关系
~0.7–1.4MB
~33K SLoC