]> git.lizzy.rs Git - rust.git/commitdiff
libsyntax: Add tests for `parse_view_item`
authorMurarth <murarth@gmail.com>
Mon, 17 Nov 2014 21:37:59 +0000 (14:37 -0700)
committerMurarth <murarth@gmail.com>
Tue, 18 Nov 2014 03:26:21 +0000 (20:26 -0700)
src/libsyntax/parse/mod.rs
src/libsyntax/util/parser_testing.rs

index 2810db4eaddd8ee493793b05dab0aaacb45bb343..3ce49b9d7a3ec00e234e492d53e3df4f103ca1b2 100644 (file)
@@ -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 {
@@ -1083,6 +1084,30 @@ fn parser_done(p: Parser){
                             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();
 
index d0faa3c682064029ecec220d2858ffd5f35b0b4b..c1ea8f60b82010419243bca299fc45605ab6dd62 100644 (file)
@@ -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> {