]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass-fulldeps/qquote.rs
New upstream version 1.19.0+dfsg1
[rustc.git] / src / test / run-pass-fulldeps / qquote.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // ignore-cross-compile
12
13 #![feature(quote, rustc_private)]
14
15 extern crate syntax;
16 extern crate syntax_pos;
17
18 use syntax::codemap::FilePathMapping;
19 use syntax::print::pprust::*;
20 use syntax::symbol::Symbol;
21 use syntax_pos::DUMMY_SP;
22
23 fn main() {
24 let ps = syntax::parse::ParseSess::new(FilePathMapping::empty());
25 let mut resolver = syntax::ext::base::DummyResolver;
26 let mut cx = syntax::ext::base::ExtCtxt::new(
27 &ps,
28 syntax::ext::expand::ExpansionConfig::default("qquote".to_string()),
29 &mut resolver);
30 let cx = &mut cx;
31
32 macro_rules! check {
33 ($f: ident, $($e: expr),+; $expect: expr) => ({
34 $(assert_eq!($f(&$e), $expect);)+
35 });
36 }
37
38 let abc = quote_expr!(cx, 23);
39 check!(expr_to_string, abc, *quote_expr!(cx, $abc); "23");
40
41 let ty = quote_ty!(cx, isize);
42 check!(ty_to_string, ty, *quote_ty!(cx, $ty); "isize");
43
44 let item = quote_item!(cx, static x: $ty = 10;).unwrap();
45 check!(item_to_string, item, quote_item!(cx, $item).unwrap(); "static x: isize = 10;");
46
47 let twenty: u16 = 20;
48 let stmt = quote_stmt!(cx, let x = $twenty;).unwrap();
49 check!(stmt_to_string, stmt, quote_stmt!(cx, $stmt).unwrap(); "let x = 20u16;");
50
51 let pat = quote_pat!(cx, Some(_));
52 check!(pat_to_string, pat, *quote_pat!(cx, $pat); "Some(_)");
53
54 let expr = quote_expr!(cx, (x, y));
55 let arm = quote_arm!(cx, (ref x, ref y) => $expr,);
56 check!(arm_to_string, arm, quote_arm!(cx, $arm); " (ref x, ref y) => (x, y),");
57
58 let attr = quote_attr!(cx, #![cfg(foo = "bar")]);
59 check!(attribute_to_string, attr, quote_attr!(cx, $attr); r#"#![cfg(foo = "bar")]"#);
60
61 // quote_arg!
62
63 let arg = quote_arg!(cx, foo: i32);
64 check!(arg_to_string, arg, quote_arg!(cx, $arg); "foo: i32");
65
66 let function = quote_item!(cx, fn f($arg) { }).unwrap();
67 check!(item_to_string, function; "fn f(foo: i32) { }");
68
69 let args = vec![arg, quote_arg!(cx, bar: u32)];
70 let args = &args[..];
71 let function = quote_item!(cx, fn f($args) { }).unwrap();
72 check!(item_to_string, function; "fn f(foo: i32, bar: u32) { }");
73
74 // quote_block!
75
76 let block = quote_block!(cx, { $stmt let y = 40u32; });
77 check!(block_to_string, block, *quote_block!(cx, $block); "{ let x = 20u16; let y = 40u32; }");
78
79 let function = quote_item!(cx, fn f() $block).unwrap();
80 check!(item_to_string, function; "fn f() { let x = 20u16; let y = 40u32; }");
81
82 // quote_path!
83
84 let path = quote_path!(cx, ::syntax::ptr::P<MetaItem>);
85 check!(path_to_string, path, quote_path!(cx, $path); "::syntax::ptr::P<MetaItem>");
86
87 let ty = quote_ty!(cx, $path);
88 check!(ty_to_string, ty; "::syntax::ptr::P<MetaItem>");
89
90 // quote_meta_item!
91
92 let meta = quote_meta_item!(cx, cfg(foo = "bar"));
93 check!(meta_item_to_string, meta, quote_meta_item!(cx, $meta); r#"cfg(foo = "bar")"#);
94
95 let attr = quote_attr!(cx, #![$meta]);
96 check!(attribute_to_string, attr; r#"#![cfg(foo = "bar")]"#);
97 }