-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.rs
More file actions
162 lines (152 loc) · 5.83 KB
/
Copy pathtesting.rs
File metadata and controls
162 lines (152 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use crate::{MockFunction, StdFunction, StdlibModule, StdlibRegistry};
use std::collections::HashMap;
use std::rc::Rc;
use techscript_runtime::{
error::{RuntimeError, RuntimeErrorKind},
function::Callable,
value::RuntimeValue,
RuntimeContext,
};
impl StdlibRegistry {
pub fn register_testing(&mut self) {
let mut exports: HashMap<String, Rc<dyn Callable>> = HashMap::new();
exports.insert(
"assert".to_string(),
Rc::new(StdFunction {
name: "assert".to_string(),
arity: 2,
callback: |_ctx, args| {
let cond = args[0].try_into_bool()?;
let msg = args[1].try_into_string()?;
if !cond {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(format!(
"Assertion failed: {}",
msg
)),
None,
None,
));
}
Ok(RuntimeValue::Null)
},
}),
);
exports.insert(
"assert_eq".to_string(),
Rc::new(StdFunction {
name: "assert_eq".to_string(),
arity: 3,
callback: |_ctx, args| {
let actual = &args[0];
let expected = &args[1];
let msg = args[2].try_into_string()?;
let is_eq = match (actual, expected) {
(RuntimeValue::Int(a), RuntimeValue::Int(b)) => a == b,
(RuntimeValue::Float(a), RuntimeValue::Float(b)) => a == b,
(RuntimeValue::Str(a), RuntimeValue::Str(b)) => a == b,
(RuntimeValue::Bool(a), RuntimeValue::Bool(b)) => a == b,
(RuntimeValue::Null, RuntimeValue::Null) => true,
_ => false,
};
if !is_eq {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(format!(
"Assertion failed (actual != expected): {}",
msg
)),
None,
None,
));
}
Ok(RuntimeValue::Null)
},
}),
);
exports.insert(
"assert_ne".to_string(),
Rc::new(StdFunction {
name: "assert_ne".to_string(),
arity: 3,
callback: |_ctx, args| {
let actual = &args[0];
let expected = &args[1];
let msg = args[2].try_into_string()?;
let is_eq = match (actual, expected) {
(RuntimeValue::Int(a), RuntimeValue::Int(b)) => a == b,
(RuntimeValue::Float(a), RuntimeValue::Float(b)) => a == b,
(RuntimeValue::Str(a), RuntimeValue::Str(b)) => a == b,
(RuntimeValue::Bool(a), RuntimeValue::Bool(b)) => a == b,
(RuntimeValue::Null, RuntimeValue::Null) => true,
_ => false,
};
if is_eq {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(format!(
"Assertion failed (actual == expected): {}",
msg
)),
None,
None,
));
}
Ok(RuntimeValue::Null)
},
}),
);
exports.insert(
"mock_fn".to_string(),
Rc::new(StdFunction {
name: "mock_fn".to_string(),
arity: 1,
callback: |_ctx, args| {
let val = args[0].clone();
Ok(RuntimeValue::Function(Rc::new(MockFunction {
name: "mock".to_string(),
val,
})))
},
}),
);
exports.insert(
"mock_object".to_string(),
Rc::new(StdFunction {
name: "mock_object".to_string(),
arity: 1,
callback: |_ctx, args| Ok(args[0].clone()),
}),
);
exports.insert(
"benchmark".to_string(),
Rc::new(StdFunction {
name: "benchmark".to_string(),
arity: 2,
callback: |ctx, args| {
if let RuntimeValue::Function(func) = &args[0] {
let iterations = args[1].try_into_int()?;
let start = std::time::Instant::now();
for _ in 0..iterations {
func.call(ctx, vec![]).ok();
}
let elapsed = start.elapsed().as_secs_f64();
println!(
"Benchmark completed: {} iterations in {:.5}s",
iterations, elapsed
);
return Ok(RuntimeValue::Float(elapsed));
}
Ok(RuntimeValue::Null)
},
}),
);
self.register_module(
"std.testing",
StdlibModule {
name: "std.testing".to_string(),
version: "1.0.0".to_string(),
exports,
required_capabilities: Vec::new(),
},
);
}
}