Merge servo_config and servo_config_tests

This commit is contained in:
Anthony Ramine 2018-01-19 16:40:18 +01:00
parent ea924e398f
commit 14d0cf595f
7 changed files with 15 additions and 32 deletions

View file

@ -8,6 +8,8 @@ publish = false
[lib]
name = "servo_config"
path = "lib.rs"
test = false
doctest = false
[dependencies]
euclid = "0.16"

View file

@ -0,0 +1,95 @@
/* 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 http://mozilla.org/MPL/2.0/. */
extern crate servo_config;
use servo_config::opts::{parse_url_or_filename, parse_pref_from_command_line};
use servo_config::prefs::{PrefValue, PREFS};
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_parse_pref_from_command_line() {
// Test with boolean values.
parse_pref_from_command_line("testtrue=true");
assert_eq!(*PREFS.get("testtrue"), PrefValue::Boolean(true));
parse_pref_from_command_line("testfalse=false");
assert_eq!(*PREFS.get("testfalse"), PrefValue::Boolean(false));
// Test with numbers.
parse_pref_from_command_line("testint=42");
assert_eq!(*PREFS.get("testint"), PrefValue::Number(42 as f64));
parse_pref_from_command_line("testfloat=4.2");
assert_eq!(*PREFS.get("testfloat"), PrefValue::Number(4.2));
// Test default (string).
parse_pref_from_command_line("teststr=str");
assert_eq!(*PREFS.get("teststr"), PrefValue::String("str".to_owned()));
// Test with no value.
parse_pref_from_command_line("testempty");
assert_eq!(*PREFS.get("testempty"), PrefValue::Boolean(true));
}

View file

@ -0,0 +1,77 @@
/* 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 http://mozilla.org/MPL/2.0/. */
extern crate servo_config;
use servo_config::basedir;
use servo_config::prefs::{PREFS, PrefValue, read_prefs_from_file};
use std::fs::{self, File};
use std::io::{Read, Write};
#[test]
fn test_create_pref() {
let json_str = "{\
\"layout.writing-mode.enabled\": true,\
\"network.mime.sniff\": false,\
\"shell.homepage\": \"https://servo.org\"\
}";
let prefs = read_prefs_from_file(json_str.as_bytes());
assert!(prefs.is_ok());
let prefs = prefs.unwrap();
assert_eq!(prefs.len(), 3);
}
#[test]
fn test_get_set_reset_extend() {
let json_str = "{\
\"layout.writing-mode.enabled\": true,\
\"extra.stuff\": false,\
\"shell.homepage\": \"https://google.com\"\
}";
assert_eq!(*PREFS.get("test"), PrefValue::Missing);
PREFS.set("test", PrefValue::String("hi".to_owned()));
assert_eq!(*PREFS.get("test"), PrefValue::String("hi".to_owned()));
assert_eq!(*PREFS.get("shell.homepage"), PrefValue::String("https://servo.org".to_owned()));
PREFS.set("shell.homepage", PrefValue::Boolean(true));
assert_eq!(*PREFS.get("shell.homepage"), PrefValue::Boolean(true));
PREFS.reset("shell.homepage");
assert_eq!(*PREFS.get("shell.homepage"), PrefValue::String("https://servo.org".to_owned()));
let extension = read_prefs_from_file(json_str.as_bytes()).unwrap();
PREFS.extend(extension);
assert_eq!(*PREFS.get("shell.homepage"), PrefValue::String("https://google.com".to_owned()));
assert_eq!(*PREFS.get("layout.writing-mode.enabled"), PrefValue::Boolean(true));
assert_eq!(*PREFS.get("extra.stuff"), PrefValue::Boolean(false));
}
#[test]
fn test_default_config_dir_create_read_write() {
let json_str = "{\
\"layout.writing-mode.enabled\": true,\
\"extra.stuff\": false,\
\"shell.homepage\": \"https://google.com\"\
}";
let mut expected_json = String::new();
let config_path = basedir::default_config_dir().unwrap();
if !config_path.exists() {
fs::create_dir_all(&config_path).unwrap();
}
let json_path = config_path.join("test_config.json");
let mut fd = File::create(&json_path).unwrap();
assert_eq!(json_path.exists(), true);
fd.write_all(json_str.as_bytes()).unwrap();
let mut fd = File::open(&json_path).unwrap();
fd.read_to_string(&mut expected_json).unwrap();
assert_eq!(json_str, expected_json);
fs::remove_file(&json_path).unwrap();
}