Skip to content

Commit

Permalink
Adds test for simple case of string replacement
Browse files Browse the repository at this point in the history
+ rustfmt
  • Loading branch information
thiagoarrais committed Oct 25, 2019
1 parent 21b15ac commit f5a513c
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/lib/builtins/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,11 +742,12 @@ pub fn replace(this: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultVal
let replacement_str: &str = &ctx.value_to_rust_string(&args[1]);
// The Rust pattern is interpreted as a Regex and replaced in the original string with
// the replacement by using Regex.replace_all
let result_str = Regex::new(pattern_str).unwrap().replace_all(this_str, replacement_str);
let result_str = Regex::new(pattern_str)
.unwrap()
.replace_all(this_str, replacement_str);
Ok(to_value(result_str.into_owned()))
}


/// Create a new `String` object
pub fn create_constructor(global: &Value) -> Value {
// Create constructor function object
Expand Down Expand Up @@ -1061,4 +1062,17 @@ mod tests {
);
assert_eq!(forward(&mut engine, "result4[0]"), "B");
}

#[test]
fn replace() {
let realm = Realm::create();
let mut engine = Executor::new(realm);
let init = r#"
var str = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
var result = str.replace('dog', 'monkey');
"#;

forward(&mut engine, init);
assert_eq!(forward(&mut engine, "result"), "The quick brown fox jumps over the lazy monkey. If the monkey reacted, was it really lazy?");
}
}

0 comments on commit f5a513c

Please sign in to comment.