Skip to content

Commit

Permalink
libsyntax: Add tests for parse_view_item
Browse files Browse the repository at this point in the history
  • Loading branch information
murarth committed Nov 18, 2014
1 parent 7ef2007 commit 2293a04
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/libsyntax/parse/mod.rs
Expand Up @@ -730,10 +730,11 @@ mod test {
use attr::AttrMetaMethods;
use parse::parser::Parser;
use parse::token::{str_to_ident};
use print::pprust::view_item_to_string;
use ptr::P;
use util::parser_testing::{string_to_tts, string_to_parser};
use util::parser_testing::{string_to_expr, string_to_item};
use util::parser_testing::string_to_stmt;
use util::parser_testing::{string_to_stmt, string_to_view_item};

// produce a codemap::span
fn sp(a: u32, b: u32) -> Span {
Expand Down Expand Up @@ -1083,6 +1084,30 @@ mod test {
span: sp(0,21)})));
}

#[test] fn parse_use() {
let use_s = "use foo::bar::baz;";
let vitem = string_to_view_item(use_s.to_string());
let vitem_s = view_item_to_string(&vitem);
assert_eq!(vitem_s.as_slice(), use_s);

let use_s = "use foo::bar as baz;";
let vitem = string_to_view_item(use_s.to_string());
let vitem_s = view_item_to_string(&vitem);
assert_eq!(vitem_s.as_slice(), use_s);
}

#[test] fn parse_extern_crate() {
let ex_s = "extern crate foo;";
let vitem = string_to_view_item(ex_s.to_string());
let vitem_s = view_item_to_string(&vitem);
assert_eq!(vitem_s.as_slice(), ex_s);

let ex_s = "extern crate \"foo\" as bar;";
let vitem = string_to_view_item(ex_s.to_string());
let vitem_s = view_item_to_string(&vitem);
assert_eq!(vitem_s.as_slice(), ex_s);
}

fn get_spans_of_pat_idents(src: &str) -> Vec<Span> {
let item = string_to_item(src.to_string()).unwrap();

Expand Down
7 changes: 7 additions & 0 deletions src/libsyntax/util/parser_testing.rs
Expand Up @@ -67,6 +67,13 @@ pub fn string_to_stmt(source_str : String) -> P<ast::Stmt> {
})
}

/// Parse a string, return a view item
pub fn string_to_view_item (source_str : String) -> ast::ViewItem {
with_error_checking_parse(source_str, |p| {
p.parse_view_item(Vec::new())
})
}

/// Parse a string, return a pat. Uses "irrefutable"... which doesn't
/// (currently) affect parsing.
pub fn string_to_pat(source_str: String) -> P<ast::Pat> {
Expand Down

5 comments on commit 2293a04

@bors
Copy link
Contributor

@bors bors commented on 2293a04 Nov 18, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at murarth@2293a04

@bors
Copy link
Contributor

@bors bors commented on 2293a04 Nov 18, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging murarth/rust/libsyntax-view-item = 2293a04 into auto

@bors
Copy link
Contributor

@bors bors commented on 2293a04 Nov 18, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

murarth/rust/libsyntax-view-item = 2293a04 merged ok, testing candidate = e09d986

@bors
Copy link
Contributor

@bors bors commented on 2293a04 Nov 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 2293a04 Nov 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = e09d986

Please sign in to comment.