1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#[macro_use]
extern crate servo_config;
use servo_config::opts::{parse_pref_from_command_line, parse_url_or_filename};
use servo_config::{prefs, prefs::PrefValue};
use std::path::Path;
#[cfg(not(target_os = "windows"))]
const FAKE_CWD: &'static str = "/fake/cwd";
#[cfg(target_os = "windows")]
const FAKE_CWD: &'static str = "C:/fake/cwd";
#[test]
fn test_argument_parsing() {
let fake_cwd = Path::new(FAKE_CWD);
assert!(parse_url_or_filename(fake_cwd, "http://example.net:invalid").is_err());
let url = parse_url_or_filename(fake_cwd, "http://example.net").unwrap();
assert_eq!(url.scheme(), "http");
let url = parse_url_or_filename(fake_cwd, "file:///foo/bar.html").unwrap();
assert_eq!(url.scheme(), "file");
assert_eq!(
url.path_segments().unwrap().collect::<Vec<_>>(),
["foo", "bar.html"]
);
}
#[test]
#[cfg(not(target_os = "windows"))]
fn test_file_path_parsing() {
let fake_cwd = Path::new(FAKE_CWD);
let url = parse_url_or_filename(fake_cwd, "bar.html").unwrap();
assert_eq!(url.scheme(), "file");
assert_eq!(
url.path_segments().unwrap().collect::<Vec<_>>(),
["fake", "cwd", "bar.html"]
);
}
#[test]
#[cfg(target_os = "windows")]
fn test_file_path_parsing() {
let fake_cwd = Path::new(FAKE_CWD);
let url = parse_url_or_filename(fake_cwd, "bar.html").unwrap();
assert_eq!(url.scheme(), "file");
assert_eq!(
url.path_segments().unwrap().collect::<Vec<_>>(),
["C:", "fake", "cwd", "bar.html"]
);
}
#[test]
#[cfg(not(target_os = "windows"))]
// Windows file paths can't contain ?
fn test_argument_parsing_special() {
let fake_cwd = Path::new(FAKE_CWD);
// '?' and '#' have a special meaning in URLs...
let url = parse_url_or_filename(fake_cwd, "file:///foo/bar?baz#buzz.html").unwrap();
assert_eq!(&*url.to_file_path().unwrap(), Path::new("/foo/bar"));
assert_eq!(url.scheme(), "file");
assert_eq!(
url.path_segments().unwrap().collect::<Vec<_>>(),
["foo", "bar"]
);
assert_eq!(url.query(), Some("baz"));
assert_eq!(url.fragment(), Some("buzz.html"));
// but not in file names.
let url = parse_url_or_filename(fake_cwd, "./bar?baz#buzz.html").unwrap();
assert_eq!(
&*url.to_file_path().unwrap(),
Path::new("/fake/cwd/bar?baz#buzz.html")
);
assert_eq!(url.scheme(), "file");
assert_eq!(
url.path_segments().unwrap().collect::<Vec<_>>(),
["fake", "cwd", "bar%3Fbaz%23buzz.html"]
);
assert_eq!(url.query(), None);
assert_eq!(url.fragment(), None);
}
#[test]
fn test_invalid_prefs_from_command_line_panics() {
let err_msg = std::panic::catch_unwind(|| {
parse_pref_from_command_line("doesntexist=true");
})
.err()
.and_then(|a| a.downcast_ref::<String>().cloned())
.expect("Should panic");
assert!(
err_msg.starts_with("Error setting preference"),
"Message should describe the problem"
);
assert!(
err_msg.contains("doesntexist"),
"Message should mention the name of the preference"
);
}
#[test]
fn test_parse_pref_from_command_line() {
// Test with boolean values.
parse_pref_from_command_line("dom.bluetooth.enabled=true");
assert_eq!(
prefs::pref_map().get("dom.bluetooth.enabled"),
PrefValue::Bool(true)
);
assert_eq!(pref!(dom.bluetooth.enabled), true);
parse_pref_from_command_line("dom.bluetooth.enabled=false");
assert_eq!(
prefs::pref_map().get("dom.bluetooth.enabled"),
PrefValue::Bool(false)
);
assert_eq!(pref!(dom.bluetooth.enabled), false);
// Test with numbers
parse_pref_from_command_line("layout.threads=42");
assert_eq!(pref!(layout.threads), 42);
// Test string.
parse_pref_from_command_line("shell.homepage=str");
assert_eq!(pref!(shell.homepage), "str");
// Test with no value (defaults to true).
prefs::pref_map()
.set("dom.bluetooth.enabled", false)
.unwrap();
parse_pref_from_command_line("dom.bluetooth.enabled");
assert_eq!(pref!(dom.bluetooth.enabled), true);
}
|