Add skeptic tests

This commit is contained in:
Alexandre Bury 2016-09-28 15:22:21 -07:00
parent 7e9609826c
commit 51e955808c
7 changed files with 43 additions and 29 deletions

View File

@ -9,6 +9,7 @@ name = "cursive"
readme = "Readme.md"
repository = "https://github.com/gyscos/Cursive"
version = "0.2.6"
build="build.rs"
[dependencies]
odds = "0.2"
@ -22,6 +23,10 @@ version = "5.82.0"
[dev-dependencies]
rand = "0.3"
skeptic = "0.5"
[build-dependencies]
skeptic = "0.5"
[lib]
name = "cursive"

View File

@ -26,7 +26,7 @@ cursive = { git = "https://github.com/gyscos/Cursive" }
(You will also need ncurses installed - if it isn't already, check in your package manager. Make sure you install the `ncursesw` version if available, for UTF-8 support.)
```rust
```rust,no_run
extern crate cursive;
use cursive::prelude::*;

8
build.rs Normal file
View File

@ -0,0 +1,8 @@
extern crate skeptic;
fn main() {
skeptic::generate_doc_tests(&["Readme.md",
"doc/tutorial_1.md",
"doc/tutorial_2.md",
"doc/tutorial_3.md" ]);
}

View File

@ -5,7 +5,7 @@
In this first tutorial, we'll learn the basics of cursive,
and write a very basic first application:
```rust
```rust,no_run
extern crate cursive;
use cursive::Cursive;
@ -50,7 +50,7 @@ cursive = "*"
Finally, update `src/main.rs` to import it:
```rust
```rust,no_run
extern crate cursive;
fn main() {
@ -74,7 +74,7 @@ so let's skip it for now.
In its simplest form, a cursive application is therefore:
```rust
```rust,no_run
extern crate cursive;
use cursive::Cursive;
@ -111,7 +111,7 @@ This method takes 2 arguments: a trigger, and a callback.
In the end, we have:
```rust
```rust,no_run
# src/main.rs
extern crate cursive;
@ -154,7 +154,7 @@ exactly that.
Once we've added this line, our first application is complete:
```rust
```rust,no_run
extern crate cursive;
use cursive::Cursive;

View File

@ -5,7 +5,7 @@
This time, we'll work on a slightly bigger example, where the user will need to
actually make some choices. Here is what the code will look like:
```rust
```rust,no_run
extern crate cursive;
use cursive::Cursive;
@ -45,8 +45,8 @@ fn show_answer(s: &mut Cursive, msg: &str) {
This time you're not a beginner anymore, so we'll skip the introduction!
Let's start from a basic cursive application:
```rust
extern crate cursive
```rust,no_run
extern crate cursive;
use cursive::Cursive;
@ -66,7 +66,7 @@ and/or buttons.
[`Dialog::new`] directly takes a view, so we'll directly give it the
`TextView`:
```rust
```rust,no_run
extern crate cursive;
use cursive::Cursive;
@ -87,7 +87,7 @@ Since creating a `Dialog` around a `TextView` is a pretty common task,
becomes a little shorter (and we don't need to import
`cursive::views::TextView` anymore):
```rust
```rust,ignore
siv.add_layer(Dialog::text("..."));
```
@ -95,7 +95,7 @@ Next, let's add a title. To do that, `Dialog` has a chainable [`Dialog::title`]
method. It takes the dialog by value, and return it back, making function
chains easy:
```rust
```rust,ignore
siv.add_layer(Dialog::text("...").title("..."));
```
@ -114,7 +114,7 @@ Just like with the title, `Dialog` has a [`Dialog::button`] method to add a
button in a chain. This method takes a label and a callback, the same kind
we saw in the previous tutorial:
```rust
```rust,ignore
siv.add_layer(Dialog::text("...")
.title("...")
.button("Quit", |s| s.quit()));
@ -124,7 +124,7 @@ Only this time, we don't want to exit the application right away. Instead of
packing everything into the closure, let's use a separate function for the
callback. Here is the current state:
```rust
```rust,no_run
extern crate cursive;
use cursive::Cursive;
@ -152,7 +152,7 @@ and show a new one instead. We'll use [`Cursive::pop_layer`] to do that.
Then, we add a new `Dialog`, this time with a few more buttons:
```rust
```rust,ignore
fn show_next(s: &mut Cursive) {
s.pop_layer();
s.add_layer(Dialog::text("Did you do the thing?")
@ -169,7 +169,7 @@ The `<Uh?>` button will add a new popup, without removing the current one:
it'll act as a dismissable infobox. `Dialog::info()` is a shortcut to build
such a popup:
```rust
```rust,ignore
fn show_next(s: &mut Cursive) {
s.pop_layer();
s.add_layer(Dialog::text("Did you do the thing?")
@ -183,7 +183,7 @@ fn show_next(s: &mut Cursive) {
Finally, let's have the "Yes" and "No" buttons use the same callback method to
print a message, but with a different text parameter:
```rust
```rust,ignore
fn show_next(s: &mut Cursive) {
s.pop_layer();
s.add_layer(Dialog::text("Did you do the thing?")

View File

@ -8,7 +8,7 @@ them to update them.
Here is the code we'll end up with:
```rust
```rust,no_run
extern crate cursive;
use cursive::prelude::*;
@ -81,7 +81,7 @@ The main element in our application will be a list of names. For this, we'll
use a [`SelectView`]. This type is generic on the item stored. We just want
to store the names, so let's build a `SelectView<String>`:
```rust
```rust,ignore
let select = SelectView::<String>::new();
```
@ -92,7 +92,7 @@ professional-looking, so we'll give it a fixed size.
To do that, a [`BoxView`] can wrap any view and give it a fixed size.
We could do:
```rust
```rust,ignore
let select = BoxView::with_fixed_size((10, 5), SelectView::<String>::new());
```
@ -100,7 +100,7 @@ But there is another shorter way: the [`Boxable`] trait is conveniently
implemented for any `View`, and allow to wrap in a `BoxView` with a chainable
call:
```rust
```rust,ignore
let select = SelectView::<String>::new()
.fixed_size((10, 5));
```
@ -110,7 +110,7 @@ We'll also want to add a callback when the user chooses a name. The
the selected item. Since we're using `String`, our callback will have to be
`Fn(&mut Cursive, &String)`:
```rust
```rust,ignore
let select = SelectView::<String>::new()
.on_submit(on_submit)
.fixed_size((10, 5));
@ -149,7 +149,7 @@ We'll use two of them:
Let's start with the column of buttons:
```rust
```rust,ignore
let buttons = LinearLayout::vertical()
.child(Button::new("Add new", add_name))
.child(Button::new("Delete", delete_name))
@ -166,7 +166,7 @@ as a cheap spacer.
We can now create the second linear layout inside a Dialog:
```rust
```rust,ignore
siv.add_layer(Dialog::new(LinearLayout::horizontal()
.child(select)
.child(DummyView)
@ -188,7 +188,7 @@ this list with names!
When the user presses the `<Add new>` button, we want to show a popup where he
can enter a new name:
```rust
```rust,ignore
fn add_name(s: &mut Cursive) {
s.add_layer(Dialog::new(EditView::new()
.fixed_width(10))
@ -222,7 +222,7 @@ reference to the view.
Here's what it looks like in action:
```rust
```rust,ignore
fn add_name(s: &mut Cursive) {
s.add_layer(Dialog::new(EditView::new()
.with_id("name")
@ -242,7 +242,7 @@ calling `find_id`.
Now we just need to do something with this name: add it to the list!
Remember the `SelectView` we created? Let's give it an ID too:
```rust
```rust,ignore
let select = SelectView::<String>::new()
.on_submit(on_submit)
.with_id("select")
@ -253,7 +253,7 @@ the `BoxView`. But we still need to call `on_submit` before that.)
That way, we can update it with a new item:
```rust
```rust,ignore
fn add_name(s: &mut Cursive) {
fn ok(s: &mut Cursive, name: &str) {
s.find_id::<SelectView<String>>("select").unwrap().add_item_str(name);
@ -277,7 +277,7 @@ fn add_name(s: &mut Cursive) {
Now that we know how to access the `SelectView`, removing an item is not very
complicated:
```rust
```rust,ignore
fn delete_name(s: &mut Cursive) {
match s.find_id::<SelectView<String>>("select").unwrap().selected_id() {
None => s.add_layer(Dialog::info("No name to remove")),

1
tests/skeptic.rs Normal file
View File

@ -0,0 +1 @@
include!(concat!(env!("OUT_DIR"), "/skeptic-tests.rs"));