Skip to content

Commit

Permalink
test: add more test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Aug 29, 2024
1 parent f3723fb commit e3b0952
Showing 1 changed file with 83 additions and 40 deletions.
123 changes: 83 additions & 40 deletions crates/oxc_regular_expression/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,8 @@ fn character_to_string(
'\n' => String::from(r"\n"),
'\r' => String::from(r"\r"),
'\t' => String::from(r"\t"),
'\u{b}' => String::from(r"\v"),
'\u{c}' => String::from(r"\f"),
'\u{8}' => String::from(r"\b"),
'\u{2D}' => String::from(r"\-"),
_ => format!(r"\{ch}"),
Expand All @@ -423,59 +425,100 @@ fn character_to_string(
#[cfg(test)]
mod test {
use oxc_allocator::Allocator;

static CASES: &[(&str, /* expected display */ Option<&str>)] = &[
("/ab/", None),
("/abc/i", None),
("/a*?/i", None),
("/emo👈🏻ji/u", None),
("/ab|c/i", None),
("/a|b+|c/i", None),
("/(?=a)|(?<=b)|(?!c)|(?<!d)/i", None),
(r"/(cg)(?<n>cg)(?:g)/", None),
(r"/^(?=ab)\b(?!cd)(?<=ef)\B(?<!gh)$/", None),
(r"/^(?<!ab)$/", None),
(r"/[abc]/", None),
(r"/[a&&b]/v", None),
(r"/[a--b]/v", None),
(r"/[^a--b--c]/v", None),
(r"/[a[b[c[d[e[f[g[h[i[j[k[l]]]]]]]]]]]]/v", None),
(r"/[\q{abc|d|e|}]/v", None),
(r"/\p{Basic_Emoji}/v", None),
(r"/[|\]]/", None),
(r"/c\]/", None),
("/a{0}|b{1,2}|c{3,}/i", None),
(r"/Em🥹j/", None),
(r"/\n\cM\0\x41\./", None),
(r"/\n\cM\0\x41\u1234\./u", None),
(r"/[\bb]/", None),
type Case<'a> = (
&'a str,
/* expected display */ Option<&'a str>,
/* test with unicode mode as well */ bool,
);

static CASES: &[Case] = &[
("/ab/", None, true),
("/abc/i", None, true),
("/a*?/i", None, true),
("/emo👈🏻ji/u", None, true),
("/ab|c/i", None, true),
("/a|b+|c/i", None, true),
("/(?=a)|(?<=b)|(?!c)|(?<!d)/i", None, true),
(r"/(cg)(?<n>cg)(?:g)/", None, true),
(r"/^(?=ab)\b(?!cd)(?<=ef)\B(?<!gh)$/", None, true),
(r"/^(?<!ab)$/", None, true),
(r"/[abc]/", None, true),
(r"/[a&&b]/v", None, true),
(r"/[a--b]/v", None, true),
(r"/[^a--b--c]/v", None, true),
(r"/[a[b[c[d[e[f[g[h[i[j[k[l]]]]]]]]]]]]/v", None, true),
(r"/[\q{abc|d|e|}]/v", None, true),
(r"/\p{Basic_Emoji}/v", None, true),
(r"/[|\]]/", None, true),
(r"/c\]/", None, true),
("/a{0}|b{1,2}|c{3,}/i", None, true),
(r"/Em🥹j/", None, true),
(r"/\n\cM\0\x41\./", None, true),
(r"/\n\cM\0\x41\u1234\./u", None, true),
(r"/[\bb]/", None, true),
(r"/\d+/gu", None, true),
(r"/\D/gu", None, true),
(r"/\w/gu", None, true),
(r"/\w+/gu", None, true),
(r"/\s/g", None, true),
(r"/\s+/g", None, true),
(r"/\t\n\v\f\r/", None, true),
// we lose the flags ordering
("/abcd/igv", Some("/abcd/giv"), true),
// we lose the flags ordering
("/abcd/igv", Some("/abcd/giv")),
(r"/\d/ug", Some(r"/\d/gu")),
(r"/\d/ug", Some(r"/\d/gu"), true),
// NOTE: we capitalize all hex values.
(r"/\n\cM\0\x41\u{1f600}\./u", Some(r"/\n\cM\0\x41\u{1F600}\./u")),
// TODO: fails with unicode mode
// (r"/c]/", None),
(r"/\n\cM\0\x41\u{1f600}\./u", Some(r"/\n\cM\0\x41\u{1F600}\./u"), true),
// TODO: tests below do **NOT** parse with unicode mode on
(r"/c]/", None, false),
// Octal tests from: <https://github.com/tc39/test262/blob/d62fa93c8f9ce5e687c0bbaa5d2b59670ab2ff60/test/annexB/language/literals/regexp/legacy-octal-escape.js>
(r"/\1/", None, false),
(r"/\2/", None, false),
(r"/\3/", None, false),
(r"/\4/", None, false),
(r"/\5/", None, false),
(r"/\6/", None, false),
(r"/\7/", None, false),
// NOTE: we remove leading zeroes
(r"/\00/", Some(r"/\0/"), false),
// NOTE: we remove leading zeroes
(r"/\07/", Some(r"/\7/"), false),
(r"/\40/", None, false),
(r"/\47/", None, false),
(r"/\70/", None, false),
(r"/\77/", None, false),
// NOTE: we remove leading zeroes
(r"/\000/", Some(r"/\0/"), false),
// NOTE: we remove leading zeroes
(r"/\007/", Some(r"/\7/"), false),
// NOTE: we remove leading zeroes
(r"/\070/", Some(r"/\70/"), false),
(r"/\300/", None, false),
(r"/\307/", None, false),
(r"/\370/", None, false),
(r"/\377/", None, false),
(r"/(.)\1/", None, false),
];

fn test_display(allocator: &Allocator, source: &str, expect: Option<&str>) {
fn test_display(allocator: &Allocator, (source, expect, unicode): Case) {
use crate::{Parser, ParserOptions};
let expect = expect.unwrap_or(source);
dbg!(source);
let parsed_utf16 =
Parser::new(allocator, source, ParserOptions::default()).parse().unwrap();
let parsed_unicode =
Parser::new(allocator, source, ParserOptions::default().with_unicode_mode())
.parse()
.unwrap();
assert_eq!(expect, parsed_utf16.to_string());
assert_eq!(expect, parsed_unicode.to_string());
if unicode {
let parsed_unicode =
Parser::new(allocator, source, ParserOptions::default().with_unicode_mode())
.parse()
.unwrap();
assert_eq!(expect, parsed_unicode.to_string());
}
}

#[test]
fn test() {
let allocator = &Allocator::default();
CASES
.iter()
.for_each(|(source, expect)| test_display(allocator, source, expect.as_deref()));
CASES.iter().for_each(|case| test_display(allocator, *case));
}
}

0 comments on commit e3b0952

Please sign in to comment.