]> git.lizzy.rs Git - rust.git/blob - src/test/run-make/unicode-input/span_length.rs
f957fbae65ca065b3ae97afbca608a56f39e8044
[rust.git] / src / test / run-make / unicode-input / span_length.rs
1 // Copyright 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 use std::{char, os, str};
12 use std::io::{File, Command};
13 use std::rand::{task_rng, Rng};
14
15 // creates a file with `fn main() { <random ident> }` and checks the
16 // compiler emits a span of the appropriate length (for the
17 // "unresolved name" message); currently just using the number of code
18 // points, but should be the number of graphemes (FIXME #7043)
19
20 fn random_char() -> char {
21     let mut rng = task_rng();
22     // a subset of the XID_start unicode table (ensuring that the
23     // compiler doesn't fail with an "unrecognised token" error)
24     let (lo, hi): (u32, u32) = match rng.gen_range(1, 4 + 1) {
25         1 => (0x41, 0x5a),
26         2 => (0xf8, 0x1ba),
27         3 => (0x1401, 0x166c),
28         _ => (0x10400, 0x1044f)
29     };
30
31     char::from_u32(rng.gen_range(lo, hi + 1)).unwrap()
32 }
33
34 fn main() {
35     let args = os::args();
36     let rustc = args.get(1).as_slice();
37     let tmpdir = Path::new(args.get(2).as_slice());
38     let main_file = tmpdir.join("span_main.rs");
39
40     for _ in range(0, 100) {
41         let n = task_rng().gen_range(3u, 20);
42
43         {
44             let _ = write!(&mut File::create(&main_file).unwrap(),
45                            "#![feature(non_ascii_idents)] fn main() {{ {} }}",
46                            // random string of length n
47                            range(0, n).map(|_| random_char()).collect::<String>());
48         }
49
50         // rustc is passed to us with --out-dir and -L etc., so we
51         // can't exec it directly
52         let result = Command::new("sh")
53                              .arg("-c")
54                              .arg(format!("{} {}",
55                                           rustc,
56                                           main_file.as_str()
57                                                    .unwrap()).as_slice())
58                              .output().unwrap();
59
60         let err = str::from_utf8_lossy(result.error.as_slice());
61
62         // the span should end the line (e.g no extra ~'s)
63         let expected_span = format!("^{}\n", "~".repeat(n - 1));
64         assert!(err.as_slice().contains(expected_span.as_slice()));
65     }
66 }