Skip to content

Commit 1fcce5c

Browse files
committed
Improve test.
1 parent e3e67ee commit 1fcce5c

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,32 @@ use std::io;
1111
pub enum InterpreterError {
1212
Io(io::Error),
1313
Unmarshal(marshal::decode::UnmarshalError),
14+
Processor(processor::ProcessorError),
1415
}
1516

1617
impl fmt::Display for InterpreterError {
1718
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1819
match *self {
19-
InterpreterError::Io(ref e) => write!(f, "I/O error:").and_then(|_| e.fmt(f)),
20-
InterpreterError::Unmarshal(ref e) => write!(f, "Unmarshal error:").and_then(|_| e.fmt(f)),
20+
InterpreterError::Io(ref e) => write!(f, "I/O error: ").and_then(|_| e.fmt(f)),
21+
InterpreterError::Unmarshal(ref e) => write!(f, "Unmarshal error: ").and_then(|_| e.fmt(f)),
22+
InterpreterError::Processor(ref e) => write!(f, "Processor error: ").and_then(|_| e.fmt(f)),
2123
}
2224
}
2325
}
2426

25-
pub fn run_module<R: io::Read, EP: sandbox::EnvProxy>(reader: &mut R, envproxy: &mut EP) -> Result<(), InterpreterError> {
27+
pub fn run_module<R: io::Read, EP: sandbox::EnvProxy>(reader: &mut R, envproxy: &mut EP) -> Result<objects::ObjectRef, InterpreterError> {
2628
let mut buf = [0; 12];
2729
try!(reader.read_exact(&mut buf).map_err(InterpreterError::Io));
2830
// TODO: do something with the content of the buffer
2931
let mut store = objects::ObjectStore::new();
3032
let module = try!(marshal::read_object(reader, &mut store).map_err(InterpreterError::Unmarshal));
31-
processor::run_code_object(envproxy, &mut store, module);
32-
Ok(())
33+
processor::run_code_object(envproxy, &mut store, module).map_err(InterpreterError::Processor)
3334
}
3435

3536
#[test]
3637
fn test_hello_world() {
3738
let mut reader: &[u8] = b"\xee\x0c\r\n\x15j\nW\x15\x00\x00\x00\xe3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00@\x00\x00\x00s\x0e\x00\x00\x00e\x00\x00d\x00\x00\x83\x01\x00\x01d\x01\x00S)\x02z\x0bHello worldN)\x01\xda\x05print\xa9\x00r\x02\x00\x00\x00r\x02\x00\x00\x00\xfa\x0b/tmp/foo.py\xda\x08<module>\x01\x00\x00\x00s\x00\x00\x00\x00";
3839
let mut envproxy = sandbox::MockEnvProxy::new();
39-
run_module(&mut reader, &mut envproxy).unwrap();
40+
println!("{:?}", run_module(&mut reader, &mut envproxy).unwrap());
4041
assert_eq!(*envproxy.stdout_content.lock().unwrap(), b"Hello world\n");
4142
}

src/processor/mod.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,30 @@ use super::objects::{Object, Code, ObjectStore, ObjectRef, ObjectContent};
44
use super::sandbox::EnvProxy;
55
use super::stack::{Stack, VectorStack};
66
use self::instructions::Instruction;
7+
use std::fmt;
78

9+
#[derive(Debug)]
810
pub enum ProcessorError {
911
CircularReference,
1012
InvalidReference,
11-
NotACodeObject,
13+
NotACodeObject(String),
1214
CodeObjectIsNotBytes,
1315
InvalidProgramCounter,
1416
StackTooSmall,
1517
InvalidConstIndex,
1618
InvalidNameIndex,
1719
}
1820

21+
impl fmt::Display for ProcessorError {
22+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23+
fmt::Debug::fmt(self, f)
24+
}
25+
}
26+
1927
fn call_function<EP: EnvProxy>(envproxy: &mut EP, store: &mut ObjectStore, func_ref: &ObjectRef, args: Vec<ObjectRef>, kwags: Vec<ObjectRef>) -> Result<ObjectRef, ProcessorError> {
2028
let code = match store.deref(func_ref).content {
2129
ObjectContent::Code(ref code) => code.clone(),
22-
_ => return Err(ProcessorError::NotACodeObject),
30+
ref o => return Err(ProcessorError::NotACodeObject(format!("{:?}", o))),
2331
};
2432
run_code(envproxy, store, code)
2533
}
@@ -54,7 +62,7 @@ fn run_code<EP: EnvProxy>(envproxy: &mut EP, store: &mut ObjectStore, code: Code
5462
pub fn run_code_object<EP: EnvProxy>(envproxy: &mut EP, store: &mut ObjectStore, module: ObjectRef) -> Result<ObjectRef, ProcessorError> {
5563
let code = match store.deref(&module).content {
5664
ObjectContent::Code(ref code) => code.clone(),
57-
_ => return Err(ProcessorError::NotACodeObject),
65+
ref o => return Err(ProcessorError::NotACodeObject(format!("{:?}", o))),
5866
};
5967
run_code(envproxy, store, code)
6068
}

0 commit comments

Comments
 (0)