Skip to content

Commit 69c34b1

Browse files
committed
Add Impl SetVal for Result tests.
1 parent d783c21 commit 69c34b1

File tree

5 files changed

+60
-25
lines changed

5 files changed

+60
-25
lines changed

phper/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub trait Throwable: error::Error {
5151
fn code(&self) -> u64;
5252
}
5353

54-
pub(crate) const EXCEPTION_CLASS_NAME: &'static str = "PHPerException";
54+
pub(crate) const EXCEPTION_CLASS_NAME: &'static str = "Phper\\OtherException";
5555

5656
impl Throwable for Error {
5757
fn class_entity(&self) -> *const ClassEntity {

phper/src/values.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -313,35 +313,33 @@ impl<T: SetVal> SetVal for Vec<T> {
313313
/// order of array is not guarantee.
314314
impl<K: AsRef<str>, V: SetVal> SetVal for HashMap<K, V> {
315315
fn set_val(self, val: &mut Val) {
316-
unsafe {
317-
phper_array_init(val.as_mut_ptr());
318-
for (k, v) in self {
319-
let k = k.as_ref();
320-
zend_hash_str_update(
321-
(*val.as_mut_ptr()).value.arr,
322-
k.as_ptr().cast(),
323-
k.len(),
324-
Val::new(v).as_mut_ptr(),
325-
);
326-
}
327-
}
316+
map_set_val(self, val);
328317
}
329318
}
330319

331320
/// Setting the val to an array, which preserves item order.
332321
impl<K: AsRef<str>, V: SetVal> SetVal for IndexMap<K, V> {
333322
fn set_val(self, val: &mut Val) {
334-
unsafe {
335-
phper_array_init(val.as_mut_ptr());
336-
for (k, v) in self {
337-
let k = k.as_ref();
338-
zend_hash_str_update(
339-
(*val.as_mut_ptr()).value.arr,
340-
k.as_ptr().cast(),
341-
k.len(),
342-
Val::new(v).as_mut_ptr(),
343-
);
344-
}
323+
map_set_val(self, val);
324+
}
325+
}
326+
327+
fn map_set_val<K, V, I>(iterator: I, val: &mut Val)
328+
where
329+
I: IntoIterator<Item = (K, V)>,
330+
K: AsRef<str>,
331+
V: SetVal,
332+
{
333+
unsafe {
334+
phper_array_init(val.as_mut_ptr());
335+
for (k, v) in iterator.into_iter() {
336+
let k = k.as_ref();
337+
zend_hash_str_update(
338+
(*val.as_mut_ptr()).value.arr,
339+
k.as_ptr().cast(),
340+
k.len(),
341+
Val::new(v).as_mut_ptr(),
342+
);
345343
}
346344
}
347345
}

tests/integration/src/values.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ fn integrate_returns(module: &mut Module) {
9292
integration_values_return_option_i64_none,
9393
vec![],
9494
);
95+
module.add_function(
96+
"integration_values_return_result_string_ok",
97+
integration_values_return_result_string_ok,
98+
vec![],
99+
);
100+
module.add_function(
101+
"integration_values_return_result_string_err",
102+
integration_values_return_result_string_err,
103+
vec![],
104+
);
95105
}
96106

97107
fn integration_values_return_null(_: &mut [Val]) {}
@@ -174,3 +184,11 @@ fn integration_values_return_option_i64_some(_: &mut [Val]) -> Option<i64> {
174184
fn integration_values_return_option_i64_none(_: &mut [Val]) -> Option<i64> {
175185
None
176186
}
187+
188+
fn integration_values_return_result_string_ok(_: &mut [Val]) -> phper::Result<String> {
189+
Ok("foo".to_string())
190+
}
191+
192+
fn integration_values_return_result_string_err(_: &mut [Val]) -> phper::Result<()> {
193+
Err(phper::Error::other("a zhe"))
194+
}

tests/integration/tests/php/_common.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,24 @@
66

77
function assert_eq($left, $right) {
88
if ($left !== $right) {
9-
throw new Exception(sprintf("left != right,\n left: %s,\n right: %s", var_export($left, true), var_export($right, true)));
9+
throw new AssertionError(sprintf("left != right,\n left: %s,\n right: %s", var_export($left, true), var_export($right, true)));
10+
}
11+
}
12+
13+
function assert_throw($callable, $expect_exception_class, $expect_exception_code, $expect_exception_message) {
14+
try {
15+
$callable();
16+
throw new AssertionError("`{$expect_exception_message}` not throws");
17+
} catch (Exception $e) {
18+
if (get_class($e) != $expect_exception_class) {
19+
throw new AssertionError(sprintf("expect exception class `%s`, found `%s`", $expect_exception_class, get_class($e)));
20+
}
21+
if ($e->getCode() != $expect_exception_code) {
22+
throw new AssertionError(sprintf("expect exception code `%s`, found `%s`", $expect_exception_code, $e->getCode()));
23+
}
24+
if ($e->getMessage() != $expect_exception_message) {
25+
throw new AssertionError(sprintf("expect exception message `%s`, found `%s`", $expect_exception_message, $e->getMessage()));
26+
}
1027
}
1128
}
1229

tests/integration/tests/php/values.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@
1919
assert_eq(integration_values_return_array(), ["a" => 1, "b" => "foo"]);
2020
assert_eq(integration_values_return_option_i64_some(), 64);
2121
assert_eq(integration_values_return_option_i64_none(), null);
22+
assert_eq(integration_values_return_result_string_ok(), "foo");
23+
assert_throw("integration_values_return_result_string_err", "Phper\\OtherException", 500, "a zhe");

0 commit comments

Comments
 (0)