]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass-fulldeps/qquote.rs
3b3e4066e0ccd48cda9565faec154d0a977b6a88
[rust.git] / src / test / run-pass-fulldeps / qquote.rs
1 // Copyright 2012-2014 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-pretty
12 // ignore-test
13
14 #![feature(quote)]
15
16 extern crate syntax;
17
18 use std::io::*;
19
20 use syntax::diagnostic;
21 use syntax::ast;
22 use syntax::codemap;
23 use syntax::codemap::span;
24 use syntax::parse;
25 use syntax::print::*;
26
27
28 trait fake_ext_ctxt {
29     fn cfg() -> ast::CrateConfig;
30     fn parse_sess() -> parse::parse_sess;
31     fn call_site() -> span;
32     fn ident_of(st: &str) -> ast::ident;
33 }
34
35 type fake_session = parse::parse_sess;
36
37 impl fake_ext_ctxt for fake_session {
38     fn cfg() -> ast::CrateConfig { Vec::new() }
39     fn parse_sess() -> parse::parse_sess { self }
40     fn call_site() -> span {
41         codemap::span {
42             lo: codemap::BytePos(0),
43             hi: codemap::BytePos(0),
44             expn_info: None
45         }
46     }
47     fn ident_of(st: &str) -> ast::ident {
48         self.interner.intern(st)
49     }
50 }
51
52 fn mk_ctxt() -> fake_ext_ctxt {
53     parse::new_parse_sess(None) as fake_ext_ctxt
54 }
55
56 fn main() {
57     let cx = mk_ctxt();
58
59     let abc = quote_expr!(cx, 23);
60     check_pp(ext_cx, abc,  pprust::print_expr, "23".to_owned());
61
62
63     let ty = quote_ty!(cx, int);
64     check_pp(ext_cx, ty, pprust::print_type, "int".to_owned());
65
66     let item = quote_item!(cx, static x : int = 10;).get();
67     check_pp(ext_cx, item, pprust::print_item, "static x: int = 10;".to_owned());
68
69     let stmt = quote_stmt!(cx, let x = 20;);
70     check_pp(ext_cx, *stmt, pprust::print_stmt, "let x = 20;".to_owned());
71
72     let pat = quote_pat!(cx, Some(_));
73     check_pp(ext_cx, pat, pprust::print_pat, "Some(_)".to_owned());
74
75 }
76
77 fn check_pp<T>(cx: fake_ext_ctxt,
78                expr: T, f: |pprust::ps, T|, expect: ~str) {
79     let s = io::with_str_writer(|wr| {
80         let pp = pprust::rust_printer(wr, cx.parse_sess().interner);
81         f(pp, expr);
82         pp::eof(pp.s);
83     });
84     stdout().write_line(s);
85     if expect != "".to_owned() {
86         println!("expect: '%s', got: '%s'", expect, s);
87         assert_eq!(s, expect);
88     }
89 }