mirror of
https://github.com/servo/servo.git
synced 2025-06-25 01:24:37 +01:00
Allow cli prefs to have numerical value
Fix for #14842. Extract the code inside a function to unit-test it.
This commit is contained in:
parent
4216c16879
commit
6a22174eb7
2 changed files with 40 additions and 9 deletions
|
@ -889,14 +889,7 @@ pub fn from_cmdline_args(args: &[String]) -> ArgumentParsingResult {
|
|||
prefs::add_user_prefs();
|
||||
|
||||
for pref in opt_match.opt_strs("pref").iter() {
|
||||
let split: Vec<&str> = pref.splitn(2, '=').collect();
|
||||
let pref_name = split[0];
|
||||
let value = split.get(1);
|
||||
match value {
|
||||
Some(&"false") => PREFS.set(pref_name, PrefValue::Boolean(false)),
|
||||
Some(&"true") | None => PREFS.set(pref_name, PrefValue::Boolean(true)),
|
||||
_ => PREFS.set(pref_name, PrefValue::String(value.unwrap().to_string()))
|
||||
};
|
||||
parse_pref_from_command_line(pref);
|
||||
}
|
||||
|
||||
if let Some(layout_threads) = layout_threads {
|
||||
|
@ -946,6 +939,20 @@ pub fn set_defaults(opts: Opts) {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn parse_pref_from_command_line(pref: &str) {
|
||||
let split: Vec<&str> = pref.splitn(2, '=').collect();
|
||||
let pref_name = split[0];
|
||||
let value = split.get(1);
|
||||
match value {
|
||||
Some(&"false") => PREFS.set(pref_name, PrefValue::Boolean(false)),
|
||||
Some(&"true") | None => PREFS.set(pref_name, PrefValue::Boolean(true)),
|
||||
Some(value) => match value.parse::<f64>() {
|
||||
Ok(v) => PREFS.set(pref_name, PrefValue::Number(v)),
|
||||
Err(_) => PREFS.set(pref_name, PrefValue::String(value.to_string()))
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get() -> &'static Opts {
|
||||
&OPTIONS
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
* 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/. */
|
||||
|
||||
use servo_config::opts::parse_url_or_filename;
|
||||
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"))]
|
||||
|
@ -67,3 +68,26 @@ fn test_argument_parsing_special() {
|
|||
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));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue