use jstime_core as jstime;
mod common;
#[cfg(test)]
mod conformance_base64 {
use super::*;
#[test]
fn btoa_exists_on_global() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("globalThis.hasOwnProperty('btoa');", "test");
assert_eq!(result.unwrap(), "true");
}
#[test]
fn atob_exists_on_global() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("globalThis.hasOwnProperty('atob');", "test");
assert_eq!(result.unwrap(), "true");
}
#[test]
fn btoa_is_function() {
let result = common::get_type_of("btoa");
assert_eq!(result.unwrap(), "function");
}
#[test]
fn atob_is_function() {
let result = common::get_type_of("atob");
assert_eq!(result.unwrap(), "function");
}
#[test]
fn btoa_empty_string() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("btoa('');", "test");
assert_eq!(result.unwrap(), "");
}
#[test]
fn atob_empty_string() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("atob('');", "test");
assert_eq!(result.unwrap(), "");
}
#[test]
fn btoa_ascii_text() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("btoa('hello');", "test");
assert_eq!(result.unwrap(), "aGVsbG8=");
}
#[test]
fn atob_valid_base64() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("atob('aGVsbG8=');", "test");
assert_eq!(result.unwrap(), "hello");
}
#[test]
fn btoa_atob_round_trip() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("atob(btoa('Hello, World!'));", "test");
assert_eq!(result.unwrap(), "Hello, World!");
}
#[test]
fn btoa_special_characters() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("btoa('\\n\\t\\r');", "test");
assert_eq!(result.unwrap(), "CgkN");
}
#[test]
fn btoa_null_byte() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("btoa('\\x00');", "test");
assert_eq!(result.unwrap(), "AA==");
}
#[test]
fn btoa_latin1_max_character() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("btoa('\\xFF');", "test");
assert_eq!(result.unwrap(), "/w==");
}
#[test]
fn btoa_throws_on_unicode() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script(
"try { btoa('\\u0100'); 'no error'; } catch(e) { 'error'; }",
"test",
);
assert_eq!(result.unwrap(), "error");
}
#[test]
fn btoa_throws_on_emoji() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script(
"try { btoa('😀'); 'no error'; } catch(e) { 'error'; }",
"test",
);
assert_eq!(result.unwrap(), "error");
}
#[test]
fn btoa_throws_on_chinese() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script(
"try { btoa('世界'); 'no error'; } catch(e) { 'error'; }",
"test",
);
assert_eq!(result.unwrap(), "error");
}
#[test]
fn btoa_requires_argument() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("try { btoa(); 'no error'; } catch(e) { 'error'; }", "test");
assert_eq!(result.unwrap(), "error");
}
#[test]
fn atob_requires_argument() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("try { atob(); 'no error'; } catch(e) { 'error'; }", "test");
assert_eq!(result.unwrap(), "error");
}
#[test]
fn atob_throws_on_invalid_character() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script(
"try { atob('!!!'); 'no error'; } catch(e) { 'error'; }",
"test",
);
assert_eq!(result.unwrap(), "error");
}
#[test]
fn atob_throws_on_invalid_length() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script(
"try { atob('abc'); 'no error'; } catch(e) { 'error'; }",
"test",
);
assert_eq!(result.unwrap(), "error");
}
#[test]
fn atob_with_single_padding() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("atob('YQ==');", "test");
assert_eq!(result.unwrap(), "a");
}
#[test]
fn atob_with_double_padding() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("atob('YWI=');", "test");
assert_eq!(result.unwrap(), "ab");
}
#[test]
fn atob_without_padding() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("atob('YWJj');", "test");
assert_eq!(result.unwrap(), "abc");
}
#[test]
fn atob_ignores_whitespace() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("atob('Y W Jj');", "test");
assert_eq!(result.unwrap(), "abc");
}
#[test]
fn btoa_converts_to_string() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("btoa(123);", "test");
assert_eq!(result.unwrap(), "MTIz");
}
#[test]
fn atob_converts_to_string() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let _result = jstime.run_script("atob(123);", "test");
let result = jstime.run_script(
"try { atob(123); 'no error'; } catch(e) { 'error'; }",
"test",
);
assert_eq!(result.unwrap(), "error");
}
#[test]
fn btoa_ascii_printable() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("btoa('ABC123xyz');", "test");
assert_eq!(result.unwrap(), "QUJDMTIzeHl6");
}
#[test]
fn atob_decodes_correctly() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("atob('QUJDMTIzeHl6');", "test");
assert_eq!(result.unwrap(), "ABC123xyz");
}
#[test]
fn btoa_all_latin1_characters() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script("btoa('\\x00\\x01\\x7F\\x80\\xFF');", "test");
assert_eq!(result.unwrap(), "AAF/gP8=");
}
#[test]
fn atob_returns_latin1_characters() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let result = jstime.run_script(
"const result = atob('AAF/gP8='); \
result.charCodeAt(0) === 0 && \
result.charCodeAt(1) === 1 && \
result.charCodeAt(2) === 127 && \
result.charCodeAt(3) === 128 && \
result.charCodeAt(4) === 255;",
"test",
);
assert_eq!(result.unwrap(), "true");
}
}