]> git.lizzy.rs Git - rust.git/blob - src/test/run-make/unicode-input/span_length.rs
Rollup merge of #21964 - semarie:openbsd-env, r=alexcrichton
[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::old_io::{File, Command};
12 use std::iter::repeat;
13 use std::rand::{thread_rng, Rng};
14 use std::{char, os};
15
16 // creates a file with `fn main() { <random ident> }` and checks the
17 // compiler emits a span of the appropriate length (for the
18 // "unresolved name" message); currently just using the number of code
19 // points, but should be the number of graphemes (FIXME #7043)
20
21 fn random_char() -> char {
22     let mut rng = thread_rng();
23     // a subset of the XID_start Unicode table (ensuring that the
24     // compiler doesn't fail with an "unrecognised token" error)
25     let (lo, hi): (u32, u32) = match rng.gen_range(1u32, 4u32 + 1) {
26         1 => (0x41, 0x5a),
27         2 => (0xf8, 0x1ba),
28         3 => (0x1401, 0x166c),
29         _ => (0x10400, 0x1044f)
30     };
31
32     char::from_u32(rng.gen_range(lo, hi + 1)).unwrap()
33 }
34
35 fn main() {
36     let args = os::args();
37     let rustc = &args[1];
38     let tmpdir = Path::new(&args[2]);
39     let main_file = tmpdir.join("span_main.rs");
40
41     for _ in 0u..100 {
42         let n = thread_rng().gen_range(3u, 20);
43
44         {
45             let _ = write!(&mut File::create(&main_file).unwrap(),
46                            "#![feature(non_ascii_idents)] fn main() {{ {} }}",
47                            // random string of length n
48                            (0..n).map(|_| random_char()).collect::<String>());
49         }
50
51         // rustc is passed to us with --out-dir and -L etc., so we
52         // can't exec it directly
53         let result = Command::new("sh")
54                              .arg("-c")
55                              .arg(&format!("{} {}",
56                                            rustc,
57                                            main_file.as_str()
58                                                     .unwrap()))
59                              .output().unwrap();
60
61         let err = String::from_utf8_lossy(&result.error);
62
63         // the span should end the line (e.g no extra ~'s)
64         let expected_span = format!("^{}\n", repeat("~").take(n - 1)
65                                                         .collect::<String>());
66         assert!(err.contains(&expected_span));
67     }
68
69     // Test multi-column characters and tabs
70     {
71         let _ = write!(&mut File::create(&main_file).unwrap(),
72                        r#"extern "路濫狼á́́" fn foo() {{}} extern "路濫狼á́" fn bar() {{}}"#);
73     }
74
75     // Extra characters. Every line is preceded by `filename:lineno <actual code>`
76     let offset = main_file.as_str().unwrap().len() + 3;
77
78     let result = Command::new("sh")
79                          .arg("-c")
80                          .arg(format!("{} {}",
81                                       rustc,
82                                       main_file.as_str()
83                                                .unwrap()).as_slice())
84                          .output().unwrap();
85
86     let err = String::from_utf8_lossy(result.error.as_slice());
87
88     // Test both the length of the snake and the leading spaces up to it
89
90     // First snake is 8 ~s long, with 7 preceding spaces (excluding file name/line offset)
91     let expected_span = format!("\n{}^{}\n",
92                                 repeat(" ").take(offset + 7).collect::<String>(),
93                                 repeat("~").take(8).collect::<String>());
94     assert!(err.contains(expected_span.as_slice()));
95     // Second snake is 8 ~s long, with 36 preceding spaces
96     let expected_span = format!("\n{}^{}\n",
97                                 repeat(" ").take(offset + 36).collect::<String>(),
98                                 repeat("~").take(8).collect::<String>());
99     assert!(err.contains(expected_span.as_slice()));
100 }