Skip to content

Commit

Permalink
Add cursor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jevancc committed Nov 4, 2020
1 parent 2fb24d8 commit 8bfe190
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions boa/src/syntax/lexer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,71 @@ fn take_while_ascii_pred_entire_str() {
assert_eq!(str::from_utf8(buf.as_slice()).unwrap(), "abcdefghijk");
}

#[test]
fn take_while_ascii_pred_non_ascii_stop() {
let mut cur = Cursor::new("abcde😀fghijk".as_bytes());

let mut buf: Vec<u8> = Vec::new();

cur.take_while_ascii_pred(&mut buf, &|_| true).unwrap();

assert_eq!(str::from_utf8(buf.as_slice()).unwrap(), "abcde");
}

#[test]
fn take_while_char_pred_simple() {
let mut cur = Cursor::new(&b"abcdefghijk"[..]);

let mut buf: Vec<u8> = Vec::new();

cur.take_while_char_pred(&mut buf, &|c| {
c == 'a' as u32 || c == 'b' as u32 || c == 'c' as u32
})
.unwrap();

assert_eq!(str::from_utf8(buf.as_slice()).unwrap(), "abc");
}

#[test]
fn take_while_char_pred_immediate_stop() {
let mut cur = Cursor::new(&b"abcdefghijk"[..]);

let mut buf: Vec<u8> = Vec::new();

cur.take_while_char_pred(&mut buf, &|_| false).unwrap();

assert_eq!(str::from_utf8(buf.as_slice()).unwrap(), "");
}

#[test]
fn take_while_char_pred_entire_str() {
let mut cur = Cursor::new(&b"abcdefghijk"[..]);

let mut buf: Vec<u8> = Vec::new();

cur.take_while_char_pred(&mut buf, &|_| true).unwrap();

assert_eq!(str::from_utf8(buf.as_slice()).unwrap(), "abcdefghijk");
}

#[test]
fn take_while_char_pred_utf8_char() {
let mut cur = Cursor::new("abc😀defghijk".as_bytes());

let mut buf: Vec<u8> = Vec::new();

cur.take_while_char_pred(&mut buf, &|c| {
if let Ok(c) = char::try_from(c) {
c == 'a' || c == 'b' || c == 'c' || c == '😀'
} else {
false
}
})
.unwrap();

assert_eq!(str::from_utf8(buf.as_slice()).unwrap(), "abc😀");
}

#[test]
fn illegal_following_numeric_literal() {
// Checks as per https://tc39.es/ecma262/#sec-literals-numeric-literals that a NumericLiteral cannot
Expand Down

0 comments on commit 8bfe190

Please sign in to comment.