Skip to content

Add classes implements method and errors tests. #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests.
  • Loading branch information
jmjoy committed Dec 13, 2022
commit d1cbef443576c382e3e2af6c04307651f1042e7a
63 changes: 43 additions & 20 deletions tests/integration/src/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
// See the Mulan PSL v2 for more details.

use phper::{
alloc::RefClone,
classes::{array_access_class, iterator_class, ClassEntity, Visibility},
functions::Argument,
modules::Module,
values::ZVal,
};
use std::collections::HashMap;

pub fn integrate(module: &mut Module) {
integrate_a(module);
Expand Down Expand Up @@ -52,12 +54,14 @@ fn integrate_a(module: &mut Module) {

struct Foo {
position: usize,
array: Vec<ZVal>,
array: HashMap<i64, ZVal>,
}

fn integrate_foo(module: &mut Module) {
let mut class =
ClassEntity::new_with_state_constructor("IntegrationTest\\Foo", || Foo { position: 0, array: Vec::new() });
let mut class = ClassEntity::new_with_state_constructor("IntegrationTest\\Foo", || Foo {
position: 0,
array: Default::default(),
});

class.implements(iterator_class);
class.implements(array_access_class);
Expand All @@ -79,28 +83,47 @@ fn integrate_foo(module: &mut Module) {
let state = this.as_mut_state();
state.position = 0;
});
class.add_method("valid", Visibility::Public, |_this, _arguments| {
true
class.add_method("valid", Visibility::Public, |this, _arguments| {
let state = this.as_state();
state.position < 3
});

// Implement ArrayAccess interface.
class.add_method("offsetExists", Visibility::Public, |this, arguments| {
let offset = arr_offset(&arguments[0]);
let state = this.as_state();
state.array.get(offset).is_some()
});
class
.add_method("offsetExists", Visibility::Public, |this, arguments| {
let offset = arguments[0].expect_long()?;
let state = this.as_state();
Ok::<_, phper::Error>(state.array.get(&offset).is_some())
})
.argument(Argument::by_val("offset"));

// public offsetExists(mixed $offset): bool
// public offsetGet(mixed $offset): mixed
// public offsetSet(mixed $offset, mixed $value): void
// public offsetUnset(mixed $offset): void
class
.add_method("offsetGet", Visibility::Public, |this, arguments| {
let offset = arguments[0].expect_long()?;
let state = this.as_mut_state();
let val = state.array.get_mut(&offset).map(|val| val.ref_clone());
Ok::<_, phper::Error>(val)
})
.argument(Argument::by_val("offset"));

class
.add_method("offsetSet", Visibility::Public, |this, arguments| {
let offset = arguments[0].expect_long()?;
let value = arguments[1].clone();
let state = this.as_mut_state();
state.array.insert(offset, value);
Ok::<_, phper::Error>(())
})
.arguments([Argument::by_val("offset"), Argument::by_val("value")]);

module.add_class(class);
}
class
.add_method("offsetUnset", Visibility::Public, |this, arguments| {
let offset = arguments[0].expect_long()?;
let state = this.as_mut_state();
state.array.remove(&offset);
Ok::<_, phper::Error>(())
})
.argument(Argument::by_val("offset"));

fn arr_offset(argument: &ZVal) -> usize {
let mut offset = argument.clone();
offset.convert_to_long();
offset.as_long().unwrap() as usize
module.add_class(class);
}
14 changes: 14 additions & 0 deletions tests/integration/tests/php/classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,17 @@


$foo = new \IntegrationTest\Foo();

// Test implementation of Iterator interface.
$tmp_arr = [];
foreach ($foo as $key => $value) {
$tmp_arr[] = [$key, $value];
}
assert_eq($tmp_arr, [[0, 'Current: 0'], [1, 'Current: 1'], [2, 'Current: 2']]);

// Test implementation of ArrayAccess interface.
assert_eq($foo[10], null);
$foo[10] = "10";
assert_eq($foo[10], "10");
unset($foo[10]);
assert_eq($foo[10], null);