Skip to content

Commit 51673d8

Browse files
committed
refactor(tests): share array completion oracle
1 parent 44c13ae commit 51673d8

4 files changed

Lines changed: 48 additions & 76 deletions

File tree

tests/oracle/object/oracle_object_intrinsic.rs

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::ffi::OsStr;
2-
use std::process::Command;
32

3+
use super::quickjs_array_completion_oracle::observe_array_completion;
44
use quickjs_oxide::{
55
CallableRef, CompleteOrdinaryPropertyDescriptor, Context, ObjectRef, PropertyKey, Runtime,
66
RuntimeError, Value,
@@ -539,7 +539,7 @@ fn object_oracle_vectors_execute_on_pinned_quickjs() {
539539
("prototype suffix", PROTOTYPE_SUFFIX_CASES),
540540
] {
541541
for &(description, source) in cases {
542-
let observation = observe_oracle(&oracle, source, description);
542+
let observation = observe_array_completion(&oracle, source, description);
543543
assert!(
544544
observation.starts_with("return|") || observation.starts_with("throw|"),
545545
"{group} oracle vector did not produce a completion: {description}: {observation:?}",
@@ -663,7 +663,7 @@ fn compare_value_cases(group: &str, cases: &[(&str, &str)]) {
663663
return;
664664
};
665665
for &(description, source) in cases {
666-
let expected = observe_oracle(&oracle, source, description);
666+
let expected = observe_array_completion(&oracle, source, description);
667667
let runtime = Runtime::new();
668668
let mut context = runtime.new_context();
669669
assert_eq!(
@@ -712,42 +712,6 @@ fn observe_rust_eval(
712712
}
713713
}
714714

715-
fn observe_oracle(oracle: &OsStr, source: &str, description: &str) -> String {
716-
let wrapper = r#"
717-
try {
718-
var value = std.evalScript(scriptArgs[0]);
719-
if (Array.isArray(value)) {
720-
var text = '';
721-
for (var index = 0; index < value.length; index++) {
722-
if (index) text += ',';
723-
text += String(value[index]);
724-
}
725-
print('return|array|' + text);
726-
} else {
727-
print('return|' + typeof value + '|' + String(value));
728-
}
729-
} catch (error) {
730-
if (error !== null && typeof error === 'object')
731-
print('throw|object|' + error.name + '|' + error.message);
732-
else
733-
print('throw|' + typeof error + '|' + String(error));
734-
}
735-
"#;
736-
let output = Command::new(oracle)
737-
.args(["--std", "-e", wrapper, source])
738-
.output()
739-
.unwrap_or_else(|error| panic!("could not run QuickJS for {description}: {error}"));
740-
assert!(
741-
output.status.success(),
742-
"QuickJS observer failed for {description}: {}",
743-
String::from_utf8_lossy(&output.stderr),
744-
);
745-
String::from_utf8(output.stdout)
746-
.unwrap_or_else(|error| panic!("QuickJS output was not UTF-8 for {description}: {error}"))
747-
.trim_end()
748-
.to_owned()
749-
}
750-
751715
fn array_value_text(
752716
runtime: &Runtime,
753717
context: &mut Context,

tests/oracle_math_intrinsic.rs

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use std::ffi::OsStr;
22
use std::process::Command;
33

4+
#[path = "support/quickjs_array_completion_oracle.rs"]
5+
mod quickjs_array_completion_oracle;
6+
7+
use quickjs_array_completion_oracle::observe_array_completion;
48
use quickjs_oxide::{
59
CallableRef, CompleteOrdinaryPropertyDescriptor, Context, ObjectRef, PropertyKey, Runtime,
610
RuntimeError, Value, WellKnownSymbol,
@@ -633,7 +637,7 @@ fn compare_value_cases(group: &str, cases: &[(&str, &str)]) {
633637
return;
634638
};
635639
for &(description, source) in cases {
636-
let expected = observe_oracle(&oracle, source, description);
640+
let expected = observe_array_completion(&oracle, source, description);
637641
let runtime = Runtime::new();
638642
let mut context = runtime.new_context();
639643
assert_eq!(
@@ -682,42 +686,6 @@ fn observe_rust_eval(
682686
}
683687
}
684688

685-
fn observe_oracle(oracle: &OsStr, source: &str, description: &str) -> String {
686-
let wrapper = r#"
687-
try {
688-
var value = std.evalScript(scriptArgs[0]);
689-
if (Array.isArray(value)) {
690-
var text = '';
691-
for (var index = 0; index < value.length; index++) {
692-
if (index) text += ',';
693-
text += String(value[index]);
694-
}
695-
print('return|array|' + text);
696-
} else {
697-
print('return|' + typeof value + '|' + String(value));
698-
}
699-
} catch (error) {
700-
if (error !== null && typeof error === 'object')
701-
print('throw|object|' + error.name + '|' + error.message);
702-
else
703-
print('throw|' + typeof error + '|' + String(error));
704-
}
705-
"#;
706-
let output = Command::new(oracle)
707-
.args(["--std", "-e", wrapper, source])
708-
.output()
709-
.unwrap_or_else(|error| panic!("could not run QuickJS for {description}: {error}"));
710-
assert!(
711-
output.status.success(),
712-
"QuickJS observer failed for {description}: {}",
713-
String::from_utf8_lossy(&output.stderr),
714-
);
715-
String::from_utf8(output.stdout)
716-
.unwrap_or_else(|error| panic!("QuickJS output was not UTF-8 for {description}: {error}"))
717-
.trim_end()
718-
.to_owned()
719-
}
720-
721689
fn array_value_text(
722690
runtime: &Runtime,
723691
context: &mut Context,

tests/oracle_object_semantics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Keep the Object oracle implementations in separate modules so their private
22
// helpers remain isolated while Cargo builds one integration target.
33

4+
#[path = "support/quickjs_array_completion_oracle.rs"]
5+
mod quickjs_array_completion_oracle;
46
#[path = "support/quickjs_object_pattern_oracle.rs"]
57
mod quickjs_object_pattern_oracle;
68
#[path = "support/quickjs_object_super_oracle.rs"]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use std::ffi::OsStr;
2+
use std::process::Command;
3+
4+
pub(super) fn observe_array_completion(oracle: &OsStr, source: &str, description: &str) -> String {
5+
let wrapper = r#"
6+
try {
7+
var value = std.evalScript(scriptArgs[0]);
8+
if (Array.isArray(value)) {
9+
var text = '';
10+
for (var index = 0; index < value.length; index++) {
11+
if (index) text += ',';
12+
text += String(value[index]);
13+
}
14+
print('return|array|' + text);
15+
} else {
16+
print('return|' + typeof value + '|' + String(value));
17+
}
18+
} catch (error) {
19+
if (error !== null && typeof error === 'object')
20+
print('throw|object|' + error.name + '|' + error.message);
21+
else
22+
print('throw|' + typeof error + '|' + String(error));
23+
}
24+
"#;
25+
let output = Command::new(oracle)
26+
.args(["--std", "-e", wrapper, source])
27+
.output()
28+
.unwrap_or_else(|error| panic!("could not run QuickJS for {description}: {error}"));
29+
assert!(
30+
output.status.success(),
31+
"QuickJS observer failed for {description}: {}",
32+
String::from_utf8_lossy(&output.stderr),
33+
);
34+
String::from_utf8(output.stdout)
35+
.unwrap_or_else(|error| panic!("QuickJS output was not UTF-8 for {description}: {error}"))
36+
.trim_end()
37+
.to_owned()
38+
}

0 commit comments

Comments
 (0)