Skip to content

Commit 975f264

Browse files
committed
refactoring test functions
1 parent f3b458b commit 975f264

File tree

3 files changed

+105
-70
lines changed

3 files changed

+105
-70
lines changed

src/libsyntax/parse/mod.rs

Lines changed: 7 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -357,65 +357,18 @@ mod test {
357357
use abi;
358358
use ast_util::mk_ident;
359359
use parse::parser::Parser;
360-
use parse::token::{ident_interner, mk_fresh_ident_interner};
361-
use diagnostic::{mk_span_handler, mk_handler};
362-
363-
// add known names to interner for testing
364-
fn mk_testing_interner() -> @ident_interner {
365-
let i = mk_fresh_ident_interner();
366-
// baby hack; in order to put the identifiers
367-
// 'a' and 'b' at known locations, we're going
368-
// to fill up the interner to length 100. If
369-
// the # of preloaded items on the interner
370-
// ever gets larger than 100, we'll have to
371-
// adjust this number (say, to 200) and
372-
// change the numbers in the identifier
373-
// test cases below.
374-
375-
assert!(i.len() < 100);
376-
for int::range(0,100-((i.len()).to_int())) |_dc| {
377-
i.gensym("dontcare");
378-
}
379-
i.intern("a");
380-
i.intern("b");
381-
i.intern("c");
382-
i.intern("d");
383-
i.intern("return");
384-
assert_eq!(i.get(ast::ident{repr:101,ctxt:0}), @~"b");
385-
i
386-
}
387-
388-
// make a parse_sess that's closed over a
389-
// testing interner (where a -> 100, b -> 101)
390-
fn mk_testing_parse_sess() -> @mut ParseSess {
391-
let interner = mk_testing_interner();
392-
let cm = @CodeMap::new();
393-
@mut ParseSess {
394-
cm: cm,
395-
next_id: 1,
396-
span_diagnostic: mk_span_handler(mk_handler(None), cm),
397-
interner: interner,
398-
}
399-
}
400-
401-
// map a string to tts, using a made-up filename: return both the token_trees
402-
// and the ParseSess
403-
fn string_to_tts_t (source_str : @~str) -> (~[ast::token_tree],@mut ParseSess) {
404-
let ps = mk_testing_parse_sess();
405-
(filemap_to_tts(ps,string_to_filemap(ps,source_str,~"bogofile")),ps)
406-
}
360+
use parse::token::{ident_interner, mk_ident_interner, mk_fresh_ident_interner};
361+
use diagnostic::{span_handler, mk_span_handler, mk_handler, Emitter};
362+
use util::parser_testing::{string_to_tts_and_sess,string_to_parser};
363+
use util::parser_testing::{string_to_crate, string_to_expr, string_to_item};
364+
use util::parser_testing::{string_to_stmt};
407365

408366
// map a string to tts, return the tt without its parsesess
409367
fn string_to_tts_only(source_str : @~str) -> ~[ast::token_tree] {
410-
let (tts,_ps) = string_to_tts_t(source_str);
368+
let (tts,_ps) = string_to_tts_and_sess(source_str);
411369
tts
412370
}
413371

414-
// map string to parser (via tts)
415-
fn string_to_parser(source_str: @~str) -> Parser {
416-
let ps = mk_testing_parse_sess();
417-
new_parser_from_source_str(ps,~[],~"bogofile",source_str)
418-
}
419372

420373
#[cfg(test)] fn to_json_str<E : Encodable<std::json::Encoder>>(val: @E) -> ~str {
421374
do io::with_str_writer |writer| {
@@ -424,22 +377,6 @@ mod test {
424377
}
425378
}
426379

427-
fn string_to_crate (source_str : @~str) -> @ast::crate {
428-
string_to_parser(source_str).parse_crate_mod()
429-
}
430-
431-
fn string_to_expr (source_str : @~str) -> @ast::expr {
432-
string_to_parser(source_str).parse_expr()
433-
}
434-
435-
fn string_to_item (source_str : @~str) -> Option<@ast::item> {
436-
string_to_parser(source_str).parse_item(~[])
437-
}
438-
439-
fn string_to_stmt (source_str : @~str) -> @ast::stmt {
440-
string_to_parser(source_str).parse_stmt(~[])
441-
}
442-
443380
// produce a codemap::span
444381
fn sp (a: uint, b: uint) -> span {
445382
span{lo:BytePos(a),hi:BytePos(b),expn_info:None}
@@ -482,7 +419,7 @@ mod test {
482419
}*/
483420
484421
#[test] fn string_to_tts_1 () {
485-
let (tts,_ps) = string_to_tts_t(@~"fn a (b : int) { b; }");
422+
let (tts,_ps) = string_to_tts_and_sess(@~"fn a (b : int) { b; }");
486423
assert_eq!(to_json_str(@tts),
487424
~"[\
488425
[\"tt_tok\",null,[\"IDENT\",\"fn\",false]],\

src/libsyntax/syntax.rc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ extern mod std;
3030
#[macro_escape]
3131
pub mod util {
3232
pub mod interner;
33+
#[cfg(test)]
34+
pub mod parser_testing;
3335
}
3436

3537
pub mod syntax {

src/libsyntax/util/parser_testing.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright 2013 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+
use std::serialize::Encodable;
12+
use std;
13+
use core::io;
14+
use core::option::{Option,None};
15+
use core::int;
16+
use core::num::NumCast;
17+
use codemap::{dummy_sp, CodeMap, BytePos, spanned};
18+
use opt_vec;
19+
use ast;
20+
use abi;
21+
use ast_util::mk_ident;
22+
use parse::parser::Parser;
23+
use parse::token::{ident_interner, mk_ident_interner, mk_fresh_ident_interner};
24+
use diagnostic::{span_handler, mk_span_handler, mk_handler, Emitter};
25+
26+
use syntax::parse::{ParseSess,new_parse_sess,string_to_filemap,filemap_to_tts};
27+
use syntax::parse::{new_parser_from_source_str};
28+
29+
// add known names to interner for testing
30+
fn mk_testing_interner() -> @ident_interner {
31+
let i = mk_fresh_ident_interner();
32+
// baby hack; in order to put the identifiers
33+
// 'a' and 'b' at known locations, we're going
34+
// to fill up the interner to length 100. If
35+
// the # of preloaded items on the interner
36+
// ever gets larger than 100, we'll have to
37+
// adjust this number (say, to 200) and
38+
// change the numbers in the identifier
39+
// test cases below.
40+
41+
assert!(i.len() < 100);
42+
for int::range(0,100-((i.len()).to_int())) |_dc| {
43+
i.gensym(~"dontcare");
44+
}
45+
i.intern("a");
46+
i.intern("b");
47+
i.intern("c");
48+
i.intern("d");
49+
i.intern("return");
50+
assert!(i.get(ast::ident{repr:101,ctxt:0}) == @~"b");
51+
i
52+
}
53+
54+
// make a parse_sess that's closed over a
55+
// testing interner (where a -> 100, b -> 101)
56+
fn mk_testing_parse_sess() -> @mut ParseSess {
57+
let interner = mk_testing_interner();
58+
let cm = @CodeMap::new();
59+
@mut ParseSess {
60+
cm: cm,
61+
next_id: 1,
62+
span_diagnostic: mk_span_handler(mk_handler(None), cm),
63+
interner: interner,
64+
}
65+
}
66+
67+
// map a string to tts, using a made-up filename: return both the token_trees
68+
// and the ParseSess
69+
pub fn string_to_tts_and_sess (source_str : @~str) -> (~[ast::token_tree],@mut ParseSess) {
70+
let ps = mk_testing_parse_sess();
71+
(filemap_to_tts(ps,string_to_filemap(ps,source_str,~"bogofile")),ps)
72+
}
73+
74+
// map string to parser (via tts)
75+
pub fn string_to_parser(source_str: @~str) -> Parser {
76+
let ps = mk_testing_parse_sess();
77+
new_parser_from_source_str(ps,~[],~"bogofile",source_str)
78+
}
79+
80+
pub fn string_to_crate (source_str : @~str) -> @ast::crate {
81+
string_to_parser(source_str).parse_crate_mod()
82+
}
83+
84+
// parse a string, return an expr
85+
pub fn string_to_expr (source_str : @~str) -> @ast::expr {
86+
string_to_parser(source_str).parse_expr()
87+
}
88+
89+
pub fn string_to_item (source_str : @~str) -> Option<@ast::item> {
90+
string_to_parser(source_str).parse_item(~[])
91+
}
92+
93+
pub fn string_to_stmt (source_str : @~str) -> @ast::stmt {
94+
string_to_parser(source_str).parse_stmt(~[])
95+
}
96+

0 commit comments

Comments
 (0)