Skip to content

Commit f91160b

Browse files
committed
json add enum encoder test case
1 parent 0e9495b commit f91160b

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/libstd/json.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ pub impl Encoder: serialize::Encoder {
133133
f();
134134
self.wr.write_char(']');
135135
}
136-
fn emit_enum_variant_arg(&self, idx: uint, f: fn()) {
136+
137+
fn emit_enum_variant_arg(&self, _idx: uint, f: fn()) {
137138
self.wr.write_char(',');
138139
f();
139140
}
@@ -1182,6 +1183,8 @@ mod tests {
11821183
11831184
use core::result;
11841185
use core::hashmap::linear::LinearMap;
1186+
use core::cmp;
1187+
11851188
11861189
fn mk_object(items: &[(~str, Json)]) -> Json {
11871190
let mut d = ~LinearMap::new();
@@ -1249,6 +1252,43 @@ mod tests {
12491252
assert a == b;
12501253
}
12511254

1255+
// two fns copied from libsyntax/util/testing.rs.
1256+
// Should they be in their own crate?
1257+
pub pure fn check_equal_ptr<T : cmp::Eq> (given : &T, expected: &T) {
1258+
if !((given == expected) && (expected == given )) {
1259+
die!(fmt!("given %?, expected %?",given,expected));
1260+
}
1261+
}
1262+
1263+
pub pure fn check_equal<T : cmp::Eq> (given : T, expected: T) {
1264+
if !((given == expected) && (expected == given )) {
1265+
die!(fmt!("given %?, expected %?",given,expected));
1266+
}
1267+
}
1268+
1269+
// testing both auto_encode's calling patterns
1270+
// and json... not sure where to put these tests.
1271+
#[test]
1272+
fn test_write_enum () {
1273+
let bw = @io::BytesWriter {bytes: dvec::DVec(), pos: 0};
1274+
let bww : @io::Writer = (bw as @io::Writer);
1275+
let encoder = (@Encoder(bww) as @serialize::Encoder);
1276+
do encoder.emit_enum(~"animal") {
1277+
do encoder.emit_enum_variant (~"frog",37,1242) {
1278+
// name of frog:
1279+
do encoder.emit_enum_variant_arg (0) {
1280+
encoder.emit_owned_str(~"Henry")
1281+
}
1282+
// mass of frog in grams:
1283+
do encoder.emit_enum_variant_arg (1) {
1284+
encoder.emit_int(349);
1285+
}
1286+
}
1287+
}
1288+
check_equal(str::from_bytes(bw.bytes.data),
1289+
~"[\"frog\",\"Henry\",349]");
1290+
}
1291+
12521292
#[test]
12531293
fn test_trailing_characters() {
12541294
assert from_str(~"nulla") ==

0 commit comments

Comments
 (0)