Fix Doc Tests (#674)

* fix doc tests

* fix tests for non-default format types

* make fetch example representative of using JSON

* add doc tests to ci

* clean up fetch docs

* update blurb above a fetch example

* minor grammar fix
This commit is contained in:
Henry Zimmerman 2019-09-30 19:06:04 -04:00 committed by Justin Starry
parent 933f666503
commit 70a95f87bf
10 changed files with 249 additions and 59 deletions

View File

@ -32,6 +32,9 @@ cargo test --test macro_test
echo "Testing derive props macro..."
cargo test --test derive_props_test
echo "Testing docs"
cargo test --doc
echo "Testing macro docs..."
(cd crates/macro && cargo test)

View File

@ -3,17 +3,35 @@
//! helps you to track selected value in an original type. Example:
//!
//! ```
//!# use yew::{Html, Component, components::Select, ComponentLink, Renderable, html};
//!# struct Model;
//!# impl Component for Model {
//!# type Message = ();type Properties = ();
//!# fn create(props: Self::Properties,link: ComponentLink<Self>) -> Self {unimplemented!()}
//!# fn update(&mut self,msg: Self::Message) -> bool {unimplemented!()}
//!# }
//!# impl Renderable<Model> for Model {fn view(&self) -> Html<Model> {unimplemented!()}}
//! #[derive(PartialEq, Clone)]
//! enum Scene {
//! First,
//! Second,
//! }
//! impl ToString for Scene {
//! fn to_string(&self) -> String {
//! match self {
//! Scene::First => "First".to_string(),
//! Scene::Second => "Second".to_string()
//! }
//! }
//! }
//!
//! fn view() -> Html<Model> {
//! let scenes = vec![Scene::First, Scene::Second];
//! html! {
//! <Select<Scenes> options=scenes />
//! <Select<Scene> options=scenes onchange=|_| () />
//! }
//! }
//! ```
use crate::callback::Callback;
use crate::html::{ChangeData, Component, ComponentLink, Html, Renderable, ShouldRender};

View File

@ -5,12 +5,16 @@ use serde_cbor;
/// A representation of a CBOR data. Use it as wrapper to
/// set a format you want to use for conversion:
///
/// ```rust
/// ```
/// // Converts (lazy) data to a Cbor
///# use yew::format::Cbor;
///# fn dont_execute() {
///# let data: String = unimplemented!();
/// let dump = Cbor(&data);
///
/// // Converts CBOR string to a data (lazy).
/// let Cbor(data) = dump;
///# }
/// ```
pub struct Cbor<T>(pub T);

View File

@ -3,8 +3,10 @@
/// A representation of a JSON data. Use it as wrapper to
/// set a format you want to use for conversion:
///
/// ```rust
/// ```
/// // Converts (lazy) data to a Json
/// use yew::format::Json;
/// let data: String = r#"{lorem: "ipsum"}"#.to_string();
/// let dump = Json(&data);
///
/// // Converts JSON string to a data (lazy).

View File

@ -5,12 +5,17 @@ use rmp_serde;
/// A representation of a MessagePack data. Use it as wrapper to
/// set a format you want to use for conversion:
///
/// ```rust
/// ```
/// // Converts (lazy) data to a MsgPack
///
///# use yew::format::MsgPack;
///# fn dont_execute() {
///# let data: String = unimplemented!();
/// let dump = MsgPack(&data);
///
/// // Converts MessagePack string to a data (lazy).
/// let MsgPack(data) = dump;
///# }
/// ```
pub struct MsgPack<T>(pub T);

View File

@ -5,12 +5,16 @@ use toml;
/// A representation of a TOML data. Use it as wrapper to
/// set a format you want to use for conversion:
///
/// ```rust
/// ```
/// // Converts (lazy) data to a Toml
///# use yew::format::Toml;
///# fn dont_execute() {
///# let data: String = unimplemented!();
/// let dump = Toml(&data);
///
/// // Converts TOML string to a data (lazy).
/// let Toml(data) = dump;
/// }
/// ```
pub struct Toml<T>(pub T);

View File

@ -5,12 +5,17 @@ use serde_yaml;
/// A representation of a YAML data. Use it as wrapper to
/// set a format you want to use for conversion:
///
/// ```rust
/// ```
/// // Converts (lazy) data to a Yaml
///# use yew::format::Yaml;
///
///# fn dont_execute() {
///# let data: String = unimplemented!();
/// let dump = Yaml(&data);
///
/// // Converts YAML string to a data (lazy).
/// let Yaml(data) = dump;
///# }
/// ```
pub struct Yaml<T>(pub T);

View File

@ -56,12 +56,28 @@ pub type Html<MSG> = VNode<MSG>;
///
/// In this example, the Wrapper component is used to wrap other elements.
/// ```
///
///# use yew::{Children, Html, Renderable, Properties, Component, ComponentLink, html};
/// #[derive(Properties)]
///# struct WrapperProps {
///# children: Children<Wrapper>,
///# }
///# struct Wrapper;
///# impl Component for Wrapper{
///# type Message = ();type Properties = WrapperProps;
///# fn create(props: Self::Properties,link: ComponentLink<Self>) -> Self {unimplemented!()}
///# fn update(&mut self,msg: Self::Message) -> bool {unimplemented!()}
///# }
///# impl Renderable<Wrapper> for Wrapper {
///# fn view(&self) -> Html<Wrapper> { // This is a recursively defined render impl - which would never work. This is done for space convenience.
/// html!{
/// <Wrapper>
/// <h4> {"Hi"} </h4>
/// <div> {"Hello"} </div>
/// </Wrapper>
/// }
///# }
///# }
/// ```
///
/// **`wrapper.rs`**
@ -69,15 +85,25 @@ pub type Html<MSG> = VNode<MSG>;
/// The Wrapper component must define a `children` property in order to wrap other elements. The
/// children property can be used to render the wrapped elements.
/// ```
///# use yew::{Children, Html, Renderable, Properties, Component, ComponentLink, html};
///# struct Wrapper {props: WrapperProps};
///# impl Component for Wrapper{
///# type Message = ();type Properties = WrapperProps;
///# fn create(props: Self::Properties,link: ComponentLink<Self>) -> Self {unimplemented!()}
///# fn update(&mut self,msg: Self::Message) -> bool {unimplemented!()}
///# }
/// #[derive(Properties)]
/// struct WrapperProps {
/// children: Children<Wrapper>,
/// }
///
/// html!{
/// <div id="container">
/// { self.props.children.view() }
/// </div>
/// impl Renderable<Wrapper> for Wrapper {
/// fn view(&self) -> Html<Wrapper> {
/// html!{
/// <div id="container">
/// { self.props.children.view() }
/// </div>
/// }
/// }
/// }
/// ```
pub type Children<T> = ChildrenRenderer<Html<T>>;
@ -89,6 +115,34 @@ pub type Children<T> = ChildrenRenderer<Html<T>>;
///
/// In this example, the `List` component can wrap `ListItem` components.
/// ```
/// use yew::{html, Component, Renderable, Html, ComponentLink, ChildrenWithProps, Properties};
///
///# #[derive(Properties)]
///# struct ListProps {
///# children: ChildrenWithProps<ListItem, List>,
///# }
///
///# struct List;
///# impl Component for List {
///# type Message = ();type Properties = ListProps;
///# fn create(props: Self::Properties,link: ComponentLink<Self>) -> Self {unimplemented!()}
///# fn update(&mut self,msg: Self::Message) -> bool {unimplemented!()}
///# }
///# impl Renderable<List> for List {fn view(&self) -> Html<List> {unimplemented!()}}
///
///
///# #[derive(Properties)]
///# struct ListItemProps {
///# value: String
///# }
///# struct ListItem;
///# impl Component for ListItem {
///# type Message = ();type Properties = ListItemProps;
///# fn create(props: Self::Properties,link: ComponentLink<Self>) -> Self {unimplemented!()}
///# fn update(&mut self,msg: Self::Message) -> bool {unimplemented!()}
///# }
///# impl Renderable<ListItem> for ListItem {fn view(&self) -> Html<ListItem> {unimplemented!()}}
///# fn view() -> Html<List> {
/// html!{
/// <List>
/// <ListItem value="a" />
@ -96,6 +150,7 @@ pub type Children<T> = ChildrenRenderer<Html<T>>;
/// <ListItem value="c" />
/// </List>
/// }
///# }
/// ```
///
/// **`list.rs`**
@ -103,17 +158,42 @@ pub type Children<T> = ChildrenRenderer<Html<T>>;
/// The `List` component must define a `children` property in order to wrap the list items. The
/// `children` property can be used to filter, mutate, and render the items.
/// ```
/// use yew::ChildrenWithProps;
///# use yew::{html, Component, Renderable, Html, ComponentLink, Properties};
///# struct List {props: ListProps};
///# impl Component for List {
///# type Message = ();type Properties = ListProps;
///# fn create(props: Self::Properties,link: ComponentLink<Self>) -> Self {unimplemented!()}
///# fn update(&mut self,msg: Self::Message) -> bool {unimplemented!()}
///# }
///
///# #[derive(Properties)]
///# struct ListItemProps {
///# value: String
///# }
///# struct ListItem;
///# impl Component for ListItem {
///# type Message = ();type Properties = ListItemProps;
///# fn create(props: Self::Properties,link: ComponentLink<Self>) -> Self {unimplemented!()}
///# fn update(&mut self,msg: Self::Message) -> bool {unimplemented!()}
///# }
///# impl Renderable<ListItem> for ListItem {fn view(&self) -> Html<ListItem> {unimplemented!()}}
///
/// #[derive(Properties)]
/// struct ListProps {
/// children: ChildrenWithProps<ListItem, List>,
/// }
///
/// html!{{
/// for self.props.children.iter().map(|mut item| {
/// item.props.value = format!("item-{}", item.props.value);
/// item
/// })
/// }}
/// impl Renderable<List> for List {
/// fn view(&self) -> Html<List> {
/// html!{{
/// for self.props.children.iter().map(|mut item| {
/// item.props.value = format!("item-{}", item.props.value);
/// item
/// })
/// }}
/// }
/// }
/// ```
pub type ChildrenWithProps<C, P> = ChildrenRenderer<VChild<C, P>>;

View File

@ -8,8 +8,6 @@
//! Minimal example:
//!
//! ```rust
//! #[macro_use]
//! extern crate yew;
//! use yew::prelude::*;
//!
//! struct Model {
@ -47,12 +45,13 @@
//! }
//! }
//! }
//!
//!# fn dont_execute() {
//! fn main() {
//! yew::initialize();
//! App::<Model>::new().mount_to_body();
//! yew::run_loop();
//! }
//!# }
//! ```
//!

View File

@ -115,48 +115,99 @@ impl FetchService {
/// You may use a Request builder to build your request declaratively as on the
/// following examples:
///
/// ```rust
/// let post_request = Request::post("https://my.api/v1/resource")
/// .header("Content-Type", "application/json")
/// .body(Json(&json!({"foo": "bar"})))
/// .expect("Failed to build request.");
/// ```
///# use yew::format::{Nothing, Json};
///# use yew::services::fetch::Request;
///# use serde_json::json;
/// let post_request = Request::post("https://my.api/v1/resource")
/// .header("Content-Type", "application/json")
/// .body(Json(&json!({"foo": "bar"})))
/// .expect("Failed to build request.");
///
/// let get_request = Request::get("https://my.api/v1/resource")
/// .body(Nothing)
/// .expect("Failed to build request.");
/// let get_request = Request::get("https://my.api/v1/resource")
/// .body(Nothing)
/// .expect("Failed to build request.");
/// ```
///
/// The callback function can build a loop message by passing or analizing the
/// response body and metadata.
///
/// ```rust
/// context.web.fetch(
/// post_request,
/// |response| {
/// if response.status().is_success() {
/// Msg::Noop
/// } else {
/// Msg::Error
/// }
/// ```
///# use yew::{Component, ComponentLink, Html, Renderable};
///# use yew::services::FetchService;
///# use yew::services::fetch::{Response, Request};
///# struct Comp;
///# impl Component for Comp {
///# type Message = Msg;type Properties = ();
///# fn create(props: Self::Properties,link: ComponentLink<Self>) -> Self {unimplemented!()}
///# fn update(&mut self,msg: Self::Message) -> bool {unimplemented!()}
///# }
///# impl Renderable<Comp> for Comp {fn view(&self) -> Html<Comp> {unimplemented!()}}
///# enum Msg {
///# Noop,
///# Error
///# }
///# fn dont_execute() {
///# let mut link: ComponentLink<Comp> = unimplemented!();
///# let mut fetch_service: FetchService = FetchService::new();
///# let post_request: Request<Result<String, failure::Error>> = unimplemented!();
/// let task = fetch_service.fetch(
/// post_request,
/// link.send_back(|response: Response<Result<String, failure::Error>>| {
/// if response.status().is_success() {
/// Msg::Noop
/// } else {
/// Msg::Error
/// }
/// )
/// })
/// );
///# }
/// ```
///
/// One can also simply consume and pass the response or body object into
/// the message.
/// For a full example, you can specify that the response must be in the JSON format,
/// and be a specific serialized data type. If the mesage isn't Json, or isn't the specified
/// data type, then you will get a message indicating failure.
///
/// ```rust
/// context.web.fetch(
/// get_request,
/// |response| {
/// let (meta, Json(body)) = response.into_parts();
/// if meta.status.is_success() {
/// Msg::FetchResourceComplete(body)
/// } else {
/// Msg::FetchResourceFailed
/// }
/// ```
///# use yew::format::{Json, Nothing, Format};
///# use yew::services::FetchService;
///# use http::Request;
///# use yew::services::fetch::Response;
///# use yew::{Component, ComponentLink, Renderable, Html};
///# use serde_derive::Deserialize;
///# struct Comp;
///# impl Component for Comp {
///# type Message = Msg;type Properties = ();
///# fn create(props: Self::Properties,link: ComponentLink<Self>) -> Self {unimplemented!()}
///# fn update(&mut self,msg: Self::Message) -> bool {unimplemented!()}
///# }
///# impl Renderable<Comp> for Comp {fn view(&self) -> Html<Comp> {unimplemented!()}}
///# enum Msg {
///# FetchResourceComplete(Data),
///# FetchResourceFailed
///# }
/// #[derive(Deserialize)]
/// struct Data {
/// value: String
/// }
///# fn dont_execute() {
///# let mut link: ComponentLink<Comp> = unimplemented!();
/// let get_request = Request::get("/thing").body(Nothing).unwrap();
/// let callback = link.send_back(|response: Response<Json<Result<Data, failure::Error>>>| {
/// if let (meta, Json(Ok(body))) = response.into_parts() {
/// if meta.status.is_success() {
/// return Msg::FetchResourceComplete(body);
/// }
/// }
/// )
/// Msg::FetchResourceFailed
/// }
/// );
///
/// let task = FetchService::new().fetch(
/// get_request,
/// callback
/// );
///# }
/// ```
///
pub fn fetch<IN, OUT: 'static>(
@ -173,14 +224,33 @@ impl FetchService {
/// `fetch` with provided `FetchOptions` object.
/// Use it if you need to send cookies with a request:
/// ```rust
/// let request = fetch::Request::get("/path/")
/// .body(Nothing).unwrap();
/// let options = FetchOptions {
/// credentials: Some(Credentials::SameOrigin),
/// ..FetchOptions::default()
/// };
/// let task = fetch_service.fetch_with_options(request, options, callback);
/// ```
///# use yew::format::Nothing;
///# use yew::services::fetch::{self, FetchOptions, Credentials};
///# use yew::{Renderable, Html, Component, ComponentLink};
///# use yew::services::FetchService;
///# use http::Response;
///# struct Comp;
///# impl Component for Comp {
///# type Message = Msg;type Properties = ();
///# fn create(props: Self::Properties,link: ComponentLink<Self>) -> Self {unimplemented!()}
///# fn update(&mut self,msg: Self::Message) -> bool {unimplemented!()}
///# }
///# impl Renderable<Comp> for Comp {fn view(&self) -> Html<Comp> {unimplemented!()}}
///# pub enum Msg {}
///# fn dont_execute() {
///# let mut link: ComponentLink<Comp> = unimplemented!();
///# let callback = link.send_back(|response: Response<Result<String, failure::Error>>| unimplemented!());
/// let request = fetch::Request::get("/path/")
/// .body(Nothing)
/// .unwrap();
/// let options = FetchOptions {
/// credentials: Some(Credentials::SameOrigin),
/// ..FetchOptions::default()
/// };
///# let mut fetch_service = FetchService::new();
/// let task = fetch_service.fetch_with_options(request, options, callback);
///# }
/// ```
pub fn fetch_with_options<IN, OUT: 'static>(
&mut self,