mk: Start testing the cheatsheet

This commit is contained in:
Brian Anderson 2014-01-07 12:16:48 -08:00
parent c2ef4e5372
commit 77ec04487b
3 changed files with 16 additions and 3 deletions

1
configure vendored
View File

@ -806,6 +806,7 @@ do
make_dir $h/test/doc-guide-container
make_dir $h/test/doc-guide-tasks
make_dir $h/test/doc-guide-conditions
make_dir $h/test/doc-complement-cheatsheet
make_dir $h/test/doc-rust
done

View File

@ -48,7 +48,7 @@ let y: int = x.unwrap();
Use [`File::open`](http://static.rust-lang.org/doc/master/std/io/fs/struct.File.html#method.open) to create a [`File`](http://static.rust-lang.org/doc/master/std/io/fs/struct.File.html) struct, which implements the [`Reader`](http://static.rust-lang.org/doc/master/std/io/trait.Reader.html) trait.
~~~
~~~ {.xfail-test}
use std::path::Path;
use std::io::fs::File;
@ -63,6 +63,9 @@ Use the [`lines`](http://static.rust-lang.org/doc/master/std/io/trait.Buffer.htm
~~~
use std::io::buffered::BufferedReader;
# use std::io::mem::MemReader;
# let reader = MemReader::new(~[]);
let mut reader = BufferedReader::new(reader);
for line in reader.lines() {
@ -149,6 +152,9 @@ Phantom types are useful for enforcing state at compile time. For example:
~~~
struct Door<State>(~str);
struct Open;
struct Closed;
fn close(Door(name): Door<Open>) -> Door<Closed> {
Door::<Closed>(name)
}
@ -157,7 +163,12 @@ fn open(Door(name): Door<Closed>) -> Door<Open> {
Door::<Open>(name)
}
let _ = close(Door::<Open>(~"front")); // ok
let _ = close(Door::<Open>(~"front"));
~~~
Attempting to close a closed door is prevented statically:
~~~ {.xfail-test}
let _ = close(Door::<Closed>(~"front")); // error: mismatched types: expected `main::Door<main::Open>` but found `main::Door<main::Closed>`
~~~
@ -185,7 +196,7 @@ Window* createWindow(int width, int height);
You can use a zero-element `enum` ([phantom type](#how-do-i-express-phantom-types)) to represent the opaque object handle. The FFI would look like this:
~~~
~~~ {.xfail-test}
enum Window {}
extern "C" {
fn createWindow(width: c_int, height: c_int) -> *Window;

View File

@ -22,6 +22,7 @@ TEST_CRATES = $(TEST_TARGET_CRATES) $(TEST_HOST_CRATES)
# Markdown files under doc/ that should have their code extracted and run
DOC_TEST_NAMES = tutorial guide-ffi guide-macros guide-lifetimes \
guide-tasks guide-conditions guide-container guide-pointers \
complement-cheatsheet \
rust
######################################################################