]>
Commit | Line | Data |
---|---|---|
c34b1796 | 1 | // Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
223e47cc LB |
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 | ||
1a4d82fc JJ |
11 | // ignore-pretty |
12 | // ignore-test | |
223e47cc | 13 | |
1a4d82fc JJ |
14 | #![feature(quote)] |
15 | ||
16 | extern crate syntax; | |
223e47cc | 17 | |
970d7e83 | 18 | use std::io::*; |
223e47cc LB |
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 { | |
1a4d82fc | 29 | fn cfg() -> ast::CrateConfig; |
223e47cc LB |
30 | fn parse_sess() -> parse::parse_sess; |
31 | fn call_site() -> span; | |
970d7e83 | 32 | fn ident_of(st: &str) -> ast::ident; |
223e47cc LB |
33 | } |
34 | ||
35 | type fake_session = parse::parse_sess; | |
36 | ||
37 | impl fake_ext_ctxt for fake_session { | |
1a4d82fc | 38 | fn cfg() -> ast::CrateConfig { Vec::new() } |
223e47cc LB |
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), | |
1a4d82fc | 44 | expn_id: codemap::NO_EXPANSION |
223e47cc LB |
45 | } |
46 | } | |
970d7e83 LB |
47 | fn ident_of(st: &str) -> ast::ident { |
48 | self.interner.intern(st) | |
223e47cc LB |
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() { | |
1a4d82fc | 57 | let cx = mk_ctxt(); |
223e47cc | 58 | |
1a4d82fc JJ |
59 | let abc = quote_expr!(cx, 23); |
60 | check_pp(ext_cx, abc, pprust::print_expr, "23".to_string()); | |
223e47cc LB |
61 | |
62 | ||
c34b1796 AL |
63 | let ty = quote_ty!(cx, isize); |
64 | check_pp(ext_cx, ty, pprust::print_type, "isize".to_string()); | |
223e47cc | 65 | |
c34b1796 AL |
66 | let item = quote_item!(cx, static x : isize = 10;).get(); |
67 | check_pp(ext_cx, item, pprust::print_item, "static x: isize = 10;".to_string()); | |
223e47cc | 68 | |
1a4d82fc JJ |
69 | let stmt = quote_stmt!(cx, let x = 20;); |
70 | check_pp(ext_cx, *stmt, pprust::print_stmt, "let x = 20;".to_string()); | |
223e47cc | 71 | |
1a4d82fc JJ |
72 | let pat = quote_pat!(cx, Some(_)); |
73 | check_pp(ext_cx, pat, pprust::print_pat, "Some(_)".to_string()); | |
223e47cc | 74 | |
1a4d82fc JJ |
75 | let arm = quote_arm!(cx, (ref x, ref y) => (x, y)); |
76 | check_pp(ext_cx, arm, pprust::print_stmt, "(ref x, ref y) = (x, y)".to_string()); | |
c34b1796 AL |
77 | |
78 | let attr = quote_attr!(cx, #![cfg(foo = "bar")]); | |
79 | check_pp(ext_cx, attr, pprust::print_attribute, "#![cfg(foo = "bar")]".to_string()); | |
223e47cc LB |
80 | } |
81 | ||
82 | fn check_pp<T>(cx: fake_ext_ctxt, | |
1a4d82fc JJ |
83 | expr: T, f: |pprust::ps, T|, expect: String) { |
84 | let s = io::with_str_writer(|wr| { | |
223e47cc LB |
85 | let pp = pprust::rust_printer(wr, cx.parse_sess().interner); |
86 | f(pp, expr); | |
87 | pp::eof(pp.s); | |
1a4d82fc | 88 | }); |
223e47cc | 89 | stdout().write_line(s); |
1a4d82fc JJ |
90 | if expect != "".to_string() { |
91 | println!("expect: '%s', got: '%s'", expect, s); | |
970d7e83 | 92 | assert_eq!(s, expect); |
223e47cc LB |
93 | } |
94 | } |