use openjd_expr::{ExprValue, ParsedExpression, SymbolTable};
fn eval(expr: &str) -> ExprValue {
ParsedExpression::new(expr)
.and_then(|p| p.evaluate(&SymbolTable::new()))
.unwrap()
}
#[allow(dead_code)]
fn eval_err(expr: &str) -> String {
ParsedExpression::new(expr)
.and_then(|p| p.evaluate(&SymbolTable::new()))
.unwrap_err()
.to_string()
}
#[allow(dead_code)]
fn eval_fails(expr: &str) -> bool {
ParsedExpression::new(expr)
.and_then(|p| p.evaluate(&SymbolTable::new()))
.is_err()
}
#[test]
fn len_cafe() {
assert_eq!(eval("len('café')"), ExprValue::Int(4));
}
#[test]
fn len_nihongo() {
assert_eq!(eval("len('日本語')"), ExprValue::Int(3));
}
#[test]
fn len_emoji() {
assert_eq!(eval("len('hello🌍')"), ExprValue::Int(6));
}
#[test]
fn len_zwj_family() {
assert_eq!(eval("len('👨\u{200D}👩\u{200D}👧')"), ExprValue::Int(5));
}
#[test]
fn find_cafe_accent() {
assert_eq!(eval("find('café', 'é')"), ExprValue::Int(3));
}
#[test]
fn find_nihongo_last() {
assert_eq!(eval("find('日本語', '語')"), ExprValue::Int(2));
}
#[test]
fn find_after_emoji() {
assert_eq!(eval("find('hello🌍world', 'world')"), ExprValue::Int(6));
}
#[test]
fn rfind_with_multibyte() {
assert_eq!(eval("rfind('abcéabc', 'abc')"), ExprValue::Int(4));
}
#[test]
fn rfind_cjk() {
assert_eq!(eval("rfind('日本語日', '日')"), ExprValue::Int(3));
}
#[test]
fn index_cafe_accent() {
assert_eq!(eval("index('café', 'é')"), ExprValue::Int(3));
}
#[test]
fn index_nihongo() {
assert_eq!(eval("index('日本語', '語')"), ExprValue::Int(2));
}
#[test]
fn rindex_multibyte() {
assert_eq!(eval("rindex('caféfé', 'fé')"), ExprValue::Int(4));
}
#[test]
fn rindex_cjk() {
assert_eq!(eval("rindex('日本語日', '日')"), ExprValue::Int(3));
}
#[test]
fn center_cafe() {
assert_eq!(eval("center('café', 10)").to_display_string(), " café ");
}
#[test]
fn center_cjk() {
assert_eq!(
eval("center('日本語', 9)").to_display_string(),
" 日本語 "
);
}
#[test]
fn center_emoji() {
assert_eq!(eval("center('🌍', 5)").to_display_string(), " 🌍 ");
}
#[test]
fn center_already_wide() {
assert_eq!(eval("center('café', 2)").to_display_string(), "café");
}
#[test]
fn ljust_cafe() {
assert_eq!(eval("ljust('café', 10)").to_display_string(), "café ");
}
#[test]
fn ljust_cjk() {
assert_eq!(
eval("ljust('日本語', 9)").to_display_string(),
"日本語 "
);
}
#[test]
fn ljust_emoji() {
assert_eq!(eval("ljust('🌍', 5)").to_display_string(), "🌍 ");
}
#[test]
fn rjust_cafe() {
assert_eq!(eval("rjust('café', 10)").to_display_string(), " café");
}
#[test]
fn rjust_cjk() {
assert_eq!(
eval("rjust('日本語', 9)").to_display_string(),
" 日本語"
);
}
#[test]
fn rjust_emoji() {
assert_eq!(eval("rjust('🌍', 5)").to_display_string(), " 🌍");
}
#[test]
fn zfill_cafe() {
assert_eq!(eval("zfill('café', 8)").to_display_string(), "0000café");
}
#[test]
fn zfill_neg_cafe() {
assert_eq!(eval("zfill('-café', 10)").to_display_string(), "-00000café");
}
#[test]
fn repr_json_newline() {
assert_eq!(
eval(r#"repr_json("hello\nworld")"#).to_display_string(),
r#""hello\nworld""#
);
}
#[test]
fn repr_json_tab() {
assert_eq!(
eval(r#"repr_json("hello\tworld")"#).to_display_string(),
r#""hello\tworld""#
);
}
#[test]
fn repr_json_carriage_return() {
assert_eq!(
eval(r#"repr_json("hello\rworld")"#).to_display_string(),
r#""hello\rworld""#
);
}
#[test]
fn repr_json_ensure_ascii_cafe() {
assert_eq!(
eval("repr_json('café')").to_display_string(),
r#""caf\u00e9""#
);
}
#[test]
fn repr_json_ensure_ascii_cjk() {
assert_eq!(
eval("repr_json('日本')").to_display_string(),
r#""\u65e5\u672c""#
);
}
#[test]
fn repr_json_ensure_ascii_emoji() {
assert_eq!(
eval("repr_json('🌍')").to_display_string(),
r#""\ud83c\udf0d""#
);
}
#[test]
fn find_after_zwj_emoji() {
assert_eq!(
eval("find('a👨\u{200D}👩\u{200D}👧b', 'b')"),
ExprValue::Int(6)
);
}
#[test]
fn len_string_with_zwj() {
assert_eq!(eval("len('a👨\u{200D}👩\u{200D}👧b')"), ExprValue::Int(7));
}