Skip to content

Commit

Permalink
Fix Doc Tests (#674)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hgzimmerman authored and jstarry committed Oct 13, 2019
1 parent 2f9557e commit 74ea945
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 59 deletions.
3 changes: 3 additions & 0 deletions ci/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
20 changes: 19 additions & 1 deletion src/components/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
6 changes: 5 additions & 1 deletion src/format/cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion src/format/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
7 changes: 6 additions & 1 deletion src/format/msgpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 5 additions & 1 deletion src/format/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
7 changes: 6 additions & 1 deletion src/format/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
102 changes: 91 additions & 11 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,54 @@ 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`**
///
/// 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>>;
Expand All @@ -89,31 +115,85 @@ 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" />
/// <ListItem value="b" />
/// <ListItem value="c" />
/// </List>
/// }
///# }
/// ```
///
/// **`list.rs`**
///
/// 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>>;

Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
//! Minimal example:
//!
//! ```rust
//! #[macro_use]
//! extern crate yew;
//! use yew::prelude::*;
//!
//! struct Model {
Expand Down Expand Up @@ -47,12 +45,13 @@
//! }
//! }
//! }
//!
//!# fn dont_execute() {
//! fn main() {
//! yew::initialize();
//! App::<Model>::new().mount_to_body();
//! yew::run_loop();
//! }
//!# }
//! ```
//!
Expand Down
Loading

0 comments on commit 74ea945

Please sign in to comment.