Skip to content

Commit b4d38c1

Browse files
committed
---
yaml --- r: 148447 b: refs/heads/try2 c: d4640f9 h: refs/heads/master i: 148445: 623ac32 148443: f71208a 148439: bdae902 148431: 895d45e 148415: 257ad4d v: v3
1 parent d1ebc32 commit b4d38c1

40 files changed

+746
-307
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 6b18ef535808f41c9292da0bd4ed366dac649656
8+
refs/heads/try2: d4640f9d661ac293bce5f5bc9f8e9150d363e178
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/doc/guide-testing.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
3333
# Unit testing in Rust
3434

3535
Rust has built in support for simple unit testing. Functions can be
36-
marked as unit tests using the 'test' attribute.
36+
marked as unit tests using the `test` attribute.
3737

3838
~~~
3939
#[test]
@@ -44,13 +44,13 @@ fn return_none_if_empty() {
4444

4545
A test function's signature must have no arguments and no return
4646
value. To run the tests in a crate, it must be compiled with the
47-
'--test' flag: `rustc myprogram.rs --test -o myprogram-tests`. Running
47+
`--test` flag: `rustc myprogram.rs --test -o myprogram-tests`. Running
4848
the resulting executable will run all the tests in the crate. A test
4949
is considered successful if its function returns; if the task running
5050
the test fails, through a call to `fail!`, a failed `check` or
5151
`assert`, or some other (`assert_eq`, ...) means, then the test fails.
5252

53-
When compiling a crate with the '--test' flag '--cfg test' is also
53+
When compiling a crate with the `--test` flag `--cfg test` is also
5454
implied, so that tests can be conditionally compiled.
5555

5656
~~~
@@ -64,17 +64,17 @@ mod tests {
6464
~~~
6565

6666
Additionally `#[test]` items behave as if they also have the
67-
`#[cfg(test)]` attribute, and will not be compiled when the --test flag
67+
`#[cfg(test)]` attribute, and will not be compiled when the `--test` flag
6868
is not used.
6969

70-
Tests that should not be run can be annotated with the 'ignore'
70+
Tests that should not be run can be annotated with the `ignore`
7171
attribute. The existence of these tests will be noted in the test
7272
runner output, but the test will not be run. Tests can also be ignored
7373
by configuration so, for example, to ignore a test on windows you can
7474
write `#[ignore(cfg(target_os = "win32"))]`.
7575

7676
Tests that are intended to fail can be annotated with the
77-
'should_fail' attribute. The test will be run, and if it causes its
77+
`should_fail` attribute. The test will be run, and if it causes its
7878
task to fail then the test will be counted as successful; otherwise it
7979
will be counted as a failure. For example:
8080

@@ -87,11 +87,11 @@ fn test_out_of_bounds_failure() {
8787
}
8888
~~~
8989

90-
A test runner built with the '--test' flag supports a limited set of
90+
A test runner built with the `--test` flag supports a limited set of
9191
arguments to control which tests are run: the first free argument
9292
passed to a test runner specifies a filter used to narrow down the set
93-
of tests being run; the '--ignored' flag tells the test runner to run
94-
only tests with the 'ignore' attribute.
93+
of tests being run; the `--ignored` flag tells the test runner to run
94+
only tests with the `ignore` attribute.
9595

9696
## Parallelism
9797

branches/try2/src/compiletest/runtest.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,15 @@ actual:\n\
237237

238238
fn make_typecheck_args(config: &config, props: &TestProps, testfile: &Path) -> ProcArgs {
239239
let aux_dir = aux_output_dir_name(config, testfile);
240+
let target = if props.force_host {
241+
config.host.as_slice()
242+
} else {
243+
config.target.as_slice()
244+
};
240245
// FIXME (#9639): This needs to handle non-utf8 paths
241246
let mut args = ~[~"-",
242247
~"--no-trans", ~"--lib",
248+
~"--target=" + target,
243249
~"-L", config.build_base.as_str().unwrap().to_owned(),
244250
~"-L",
245251
aux_dir.as_str().unwrap().to_owned()];

branches/try2/src/libextra/base64.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,9 @@ impl<'a> FromBase64 for &'a str {
237237
}
238238

239239
for (idx, byte) in it {
240-
if (byte as char) != '=' {
241-
return Err(InvalidBase64Character(self.char_at(idx), idx));
240+
match byte as char {
241+
'='|'\r'|'\n' => continue,
242+
_ => return Err(InvalidBase64Character(self.char_at(idx), idx)),
242243
}
243244
}
244245

@@ -310,6 +311,8 @@ mod test {
310311
fn test_from_base64_newlines() {
311312
assert_eq!("Zm9v\r\nYmFy".from_base64().unwrap(),
312313
"foobar".as_bytes().to_owned());
314+
assert_eq!("Zm9vYg==\r\n".from_base64().unwrap(),
315+
"foob".as_bytes().to_owned());
313316
}
314317

315318
#[test]

branches/try2/src/libextra/json.rs

Lines changed: 223 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,211 @@
1414
#[forbid(non_camel_case_types)];
1515
#[allow(missing_doc)];
1616

17-
//! json parsing and serialization
17+
/*!
18+
JSON parsing and serialization
19+
20+
# What is JSON?
21+
22+
JSON (JavaScript Object Notation) is a way to write data in Javascript.
23+
Like XML it allows one to encode structured data in a text format that can be read by humans easily.
24+
Its native compatibility with JavaScript and its simple syntax make it used widely.
25+
26+
Json data are encoded in a form of "key":"value".
27+
Data types that can be encoded are JavaScript types :
28+
boolean (`true` or `false`), number (`f64`), string, array, object, null.
29+
An object is a series of string keys mapping to values, in `"key": value` format.
30+
Arrays are enclosed in square brackets ([ ... ]) and objects in curly brackets ({ ... }).
31+
A simple JSON document encoding a person, his/her age, address and phone numbers could look like:
32+
33+
```
34+
{
35+
"FirstName": "John",
36+
"LastName": "Doe",
37+
"Age": 43,
38+
"Address": {
39+
"Street": "Downing Street 10",
40+
"City": "London",
41+
"Country": "Great Britain"
42+
},
43+
"PhoneNumbers": [
44+
"+44 1234567",
45+
"+44 2345678"
46+
]
47+
}
48+
```
49+
50+
# Rust Type-based Encoding and Decoding
51+
52+
Rust provides a mechanism for low boilerplate encoding & decoding
53+
of values to and from JSON via the serialization API.
54+
To be able to encode a piece of data, it must implement the `extra::serialize::Encodable` trait.
55+
To be able to decode a piece of data, it must implement the `extra::serialize::Decodable` trait.
56+
The Rust compiler provides an annotation to automatically generate
57+
the code for these traits: `#[deriving(Decodable, Encodable)]`
58+
59+
To encode using Encodable :
60+
61+
```rust
62+
use extra::json;
63+
use std::io;
64+
use extra::serialize::Encodable;
65+
66+
#[deriving(Encodable)]
67+
pub struct TestStruct {
68+
data_str: ~str,
69+
}
70+
71+
fn main() {
72+
let to_encode_object = TestStruct{data_str:~"example of string to encode"};
73+
let mut m = io::MemWriter::new();
74+
{
75+
let mut encoder = json::Encoder::new(&mut m as &mut std::io::Writer);
76+
to_encode_object.encode(&mut encoder);
77+
}
78+
}
79+
```
80+
81+
Two wrapper functions are provided to encode a Encodable object
82+
into a string (~str) or buffer (~[u8]): `str_encode(&m)` and `buffer_encode(&m)`.
83+
84+
```rust
85+
use extra::json;
86+
let to_encode_object = ~"example of string to encode";
87+
let encoded_str: ~str = json::Encoder::str_encode(&to_encode_object);
88+
```
89+
90+
JSON API provide an enum `json::Json` and a trait `ToJson` to encode object.
91+
The trait `ToJson` encode object into a container `json::Json` and the API provide writer
92+
to encode them into a stream or a string ...
93+
94+
When using `ToJson` the `Encodable` trait implementation is not mandatory.
95+
96+
A basic `ToJson` example using a TreeMap of attribute name / attribute value:
97+
98+
99+
```rust
100+
use extra::json;
101+
use extra::json::ToJson;
102+
use extra::treemap::TreeMap;
103+
104+
pub struct MyStruct {
105+
attr1: u8,
106+
attr2: ~str,
107+
}
108+
109+
impl ToJson for MyStruct {
110+
fn to_json( &self ) -> json::Json {
111+
let mut d = ~TreeMap::new();
112+
d.insert(~"attr1", self.attr1.to_json());
113+
d.insert(~"attr2", self.attr2.to_json());
114+
json::Object(d)
115+
}
116+
}
117+
118+
fn main() {
119+
let test2: MyStruct = MyStruct {attr1: 1, attr2:~"test"};
120+
let tjson: json::Json = test2.to_json();
121+
let json_str: ~str = tjson.to_str();
122+
}
123+
```
124+
125+
To decode a json string using `Decodable` trait :
126+
127+
```rust
128+
use extra::serialize::Decodable;
129+
130+
#[deriving(Decodable)]
131+
pub struct MyStruct {
132+
attr1: u8,
133+
attr2: ~str,
134+
}
135+
136+
fn main() {
137+
let json_str_to_decode: ~str =
138+
~"{\"attr1\":1,\"attr2\":\"toto\"}";
139+
let json_object = extra::json::from_str(json_str_to_decode);
140+
let mut decoder = extra::json::Decoder::new(json_object.unwrap());
141+
let decoded_object: MyStruct = Decodable::decode(&mut decoder); // create the final object
142+
}
143+
```
144+
145+
# Examples of use
146+
147+
## Using Autoserialization
148+
149+
Create a struct called TestStruct1 and serialize and deserialize it to and from JSON
150+
using the serialization API, using the derived serialization code.
151+
152+
```rust
153+
use extra::json;
154+
use extra::serialize::{Encodable, Decodable};
155+
156+
#[deriving(Decodable, Encodable)] //generate Decodable, Encodable impl.
157+
pub struct TestStruct1 {
158+
data_int: u8,
159+
data_str: ~str,
160+
data_vector: ~[u8],
161+
}
162+
163+
// To serialize use the `json::str_encode` to encode an object in a string.
164+
// It calls the generated `Encodable` impl.
165+
fn main() {
166+
let to_encode_object = TestStruct1
167+
{data_int: 1, data_str:~"toto", data_vector:~[2,3,4,5]};
168+
let encoded_str: ~str = json::Encoder::str_encode(&to_encode_object);
169+
170+
// To unserialize use the `extra::json::from_str` and `extra::json::Decoder`
171+
172+
let json_object = extra::json::from_str(encoded_str);
173+
let mut decoder = json::Decoder::new(json_object.unwrap());
174+
let decoded1: TestStruct1 = Decodable::decode(&mut decoder); // create the final object
175+
}
176+
```
177+
178+
## Using `ToJson`
179+
180+
This example use the ToJson impl to unserialize the json string.
181+
Example of `ToJson` trait implementation for TestStruct1.
182+
183+
```rust
184+
use extra::json;
185+
use extra::json::ToJson;
186+
use extra::serialize::{Encodable, Decodable};
187+
use extra::treemap::TreeMap;
188+
189+
#[deriving(Decodable, Encodable)] // generate Decodable, Encodable impl.
190+
pub struct TestStruct1 {
191+
data_int: u8,
192+
data_str: ~str,
193+
data_vector: ~[u8],
194+
}
195+
196+
impl ToJson for TestStruct1 {
197+
fn to_json( &self ) -> json::Json {
198+
let mut d = ~TreeMap::new();
199+
d.insert(~"data_int", self.data_int.to_json());
200+
d.insert(~"data_str", self.data_str.to_json());
201+
d.insert(~"data_vector", self.data_vector.to_json());
202+
json::Object(d)
203+
}
204+
}
205+
206+
fn main() {
207+
// Seralization using our impl of to_json
208+
209+
let test2: TestStruct1 = TestStruct1 {data_int: 1, data_str:~"toto", data_vector:~[2,3,4,5]};
210+
let tjson: json::Json = test2.to_json();
211+
let json_str: ~str = tjson.to_str();
212+
213+
// Unserialize like before.
214+
215+
let mut decoder = json::Decoder::new(json::from_str(json_str).unwrap());
216+
// create the final object
217+
let decoded2: TestStruct1 = Decodable::decode(&mut decoder);
218+
}
219+
```
220+
221+
*/
18222

19223
use std::char;
20224
use std::cast::transmute;
@@ -93,6 +297,23 @@ impl<'a> Encoder<'a> {
93297
pub fn new<'a>(wr: &'a mut io::Writer) -> Encoder<'a> {
94298
Encoder { wr: wr }
95299
}
300+
301+
/// Encode the specified struct into a json [u8]
302+
pub fn buffer_encode<T:Encodable<Encoder<'a>>>(to_encode_object: &T) -> ~[u8] {
303+
//Serialize the object in a string using a writer
304+
let mut m = MemWriter::new();
305+
{
306+
let mut encoder = Encoder::new(&mut m as &mut io::Writer);
307+
to_encode_object.encode(&mut encoder);
308+
}
309+
m.unwrap()
310+
}
311+
312+
/// Encode the specified struct into a json str
313+
pub fn str_encode<T:Encodable<Encoder<'a>>>(to_encode_object: &T) -> ~str {
314+
let buff:~[u8] = Encoder::buffer_encode(to_encode_object);
315+
str::from_utf8_owned(buff)
316+
}
96317
}
97318

98319
impl<'a> serialize::Encoder for Encoder<'a> {
@@ -688,7 +909,7 @@ impl<T : Iterator<char>> Parser<T> {
688909
}
689910
}
690911

691-
let exp: f64 = num::pow_with_uint(10u, exp);
912+
let exp: f64 = num::pow(10u as f64, exp);
692913
if neg_exp {
693914
res /= exp;
694915
} else {

0 commit comments

Comments
 (0)