Skip to content

Commit efcf063

Browse files
committed
test(enum): add enum testcases
Refs: #178
1 parent 5bcd7f0 commit efcf063

File tree

7 files changed

+431
-5
lines changed

7 files changed

+431
-5
lines changed

.github/workflows/coverage.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ name: coverage
22
on:
33
push:
44
branches-ignore:
5-
- 'release-plz-*'
5+
- "release-plz-*"
66
pull_request:
77
branches-ignore:
8-
- 'release-plz-*'
8+
- "release-plz-*"
99
env:
1010
# increment this manually to force cache eviction
1111
RUST_CACHE_PREFIX: "v0-rust"
@@ -63,6 +63,6 @@ jobs:
6363
cargo tarpaulin --version
6464
- name: Run tests
6565
run: |
66-
cargo tarpaulin --engine llvm --workspace --all-features --tests --exclude tests --exclude-files docsrs_bindings.rs --timeout 120 --out Xml
66+
cargo tarpaulin --engine llvm --workspace --all-features --tests --exclude tests --exclude-files docsrs_bindings.rs --exclude-files "crates/macros/tests/expand/*.expanded.rs" --timeout 120 --out Xml
6767
- name: Upload coverage
6868
uses: coverallsapp/github-action@v2

crates/macros/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,7 @@ mod tests {
12581258
&[
12591259
("php_class", php_class_internal as AttributeFn),
12601260
("php_const", php_const_internal as AttributeFn),
1261+
("php_enum", php_enum_internal as AttributeFn),
12611262
("php_extern", php_extern_internal as AttributeFn),
12621263
("php_function", php_function_internal as AttributeFn),
12631264
("php_impl", php_impl_internal as AttributeFn),
Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
#[macro_use]
2+
extern crate ext_php_rs_derive;
3+
#[allow(dead_code)]
4+
/// Doc comments for MyEnum.
5+
/// This is a basic enum example.
6+
enum MyEnum {
7+
/// Variant1 of MyEnum.
8+
/// This variant represents the first case.
9+
Variant1,
10+
Variant2,
11+
/// Variant3 of MyEnum.
12+
Variant3,
13+
}
14+
impl ::ext_php_rs::class::RegisteredClass for MyEnum {
15+
const CLASS_NAME: &'static str = "MyEnum";
16+
const BUILDER_MODIFIER: ::std::option::Option<
17+
fn(::ext_php_rs::builders::ClassBuilder) -> ::ext_php_rs::builders::ClassBuilder,
18+
> = None;
19+
const EXTENDS: ::std::option::Option<::ext_php_rs::class::ClassEntryInfo> = None;
20+
const IMPLEMENTS: &'static [::ext_php_rs::class::ClassEntryInfo] = &[];
21+
const FLAGS: ::ext_php_rs::flags::ClassFlags = ::ext_php_rs::flags::ClassFlags::Enum;
22+
const DOC_COMMENTS: &'static [&'static str] = &[
23+
" Doc comments for MyEnum.",
24+
" This is a basic enum example.",
25+
];
26+
fn get_metadata() -> &'static ::ext_php_rs::class::ClassMetadata<Self> {
27+
static METADATA: ::ext_php_rs::class::ClassMetadata<MyEnum> = ::ext_php_rs::class::ClassMetadata::new();
28+
&METADATA
29+
}
30+
#[inline]
31+
fn get_properties<'a>() -> ::std::collections::HashMap<
32+
&'static str,
33+
::ext_php_rs::internal::property::PropertyInfo<'a, Self>,
34+
> {
35+
::std::collections::HashMap::new()
36+
}
37+
#[inline]
38+
fn method_builders() -> ::std::vec::Vec<
39+
(
40+
::ext_php_rs::builders::FunctionBuilder<'static>,
41+
::ext_php_rs::flags::MethodFlags,
42+
),
43+
> {
44+
use ::ext_php_rs::internal::class::PhpClassImpl;
45+
::ext_php_rs::internal::class::PhpClassImplCollector::<Self>::default()
46+
.get_methods()
47+
}
48+
#[inline]
49+
fn constructor() -> ::std::option::Option<
50+
::ext_php_rs::class::ConstructorMeta<Self>,
51+
> {
52+
None
53+
}
54+
#[inline]
55+
fn constants() -> &'static [(
56+
&'static str,
57+
&'static dyn ::ext_php_rs::convert::IntoZvalDyn,
58+
&'static [&'static str],
59+
)] {
60+
use ::ext_php_rs::internal::class::PhpClassImpl;
61+
::ext_php_rs::internal::class::PhpClassImplCollector::<Self>::default()
62+
.get_constants()
63+
}
64+
}
65+
impl ::ext_php_rs::enum_::RegisteredEnum for MyEnum {
66+
const CASES: &'static [::ext_php_rs::enum_::EnumCase] = &[
67+
::ext_php_rs::enum_::EnumCase {
68+
name: "Variant1",
69+
discriminant: None,
70+
docs: &[" Variant1 of MyEnum.", " This variant represents the first case."],
71+
},
72+
::ext_php_rs::enum_::EnumCase {
73+
name: "Variant_2",
74+
discriminant: None,
75+
docs: &[],
76+
},
77+
::ext_php_rs::enum_::EnumCase {
78+
name: "VARIANT_3",
79+
discriminant: None,
80+
docs: &[" Variant3 of MyEnum."],
81+
},
82+
];
83+
fn from_name(name: &str) -> ::ext_php_rs::error::Result<Self> {
84+
match name {
85+
"Variant1" => Ok(Self::Variant1),
86+
"Variant_2" => Ok(Self::Variant2),
87+
"VARIANT_3" => Ok(Self::Variant3),
88+
_ => Err(::ext_php_rs::error::Error::InvalidProperty),
89+
}
90+
}
91+
fn to_name(&self) -> &'static str {
92+
match self {
93+
Self::Variant1 => "Variant1",
94+
Self::Variant2 => "Variant_2",
95+
Self::Variant3 => "VARIANT_3",
96+
}
97+
}
98+
}
99+
#[allow(dead_code)]
100+
enum MyEnumWithIntValues {
101+
Variant1,
102+
Variant2,
103+
}
104+
impl ::ext_php_rs::class::RegisteredClass for MyEnumWithIntValues {
105+
const CLASS_NAME: &'static str = "MyIntValuesEnum";
106+
const BUILDER_MODIFIER: ::std::option::Option<
107+
fn(::ext_php_rs::builders::ClassBuilder) -> ::ext_php_rs::builders::ClassBuilder,
108+
> = None;
109+
const EXTENDS: ::std::option::Option<::ext_php_rs::class::ClassEntryInfo> = None;
110+
const IMPLEMENTS: &'static [::ext_php_rs::class::ClassEntryInfo] = &[];
111+
const FLAGS: ::ext_php_rs::flags::ClassFlags = ::ext_php_rs::flags::ClassFlags::Enum;
112+
const DOC_COMMENTS: &'static [&'static str] = &[];
113+
fn get_metadata() -> &'static ::ext_php_rs::class::ClassMetadata<Self> {
114+
static METADATA: ::ext_php_rs::class::ClassMetadata<MyEnumWithIntValues> = ::ext_php_rs::class::ClassMetadata::new();
115+
&METADATA
116+
}
117+
#[inline]
118+
fn get_properties<'a>() -> ::std::collections::HashMap<
119+
&'static str,
120+
::ext_php_rs::internal::property::PropertyInfo<'a, Self>,
121+
> {
122+
::std::collections::HashMap::new()
123+
}
124+
#[inline]
125+
fn method_builders() -> ::std::vec::Vec<
126+
(
127+
::ext_php_rs::builders::FunctionBuilder<'static>,
128+
::ext_php_rs::flags::MethodFlags,
129+
),
130+
> {
131+
use ::ext_php_rs::internal::class::PhpClassImpl;
132+
::ext_php_rs::internal::class::PhpClassImplCollector::<Self>::default()
133+
.get_methods()
134+
}
135+
#[inline]
136+
fn constructor() -> ::std::option::Option<
137+
::ext_php_rs::class::ConstructorMeta<Self>,
138+
> {
139+
None
140+
}
141+
#[inline]
142+
fn constants() -> &'static [(
143+
&'static str,
144+
&'static dyn ::ext_php_rs::convert::IntoZvalDyn,
145+
&'static [&'static str],
146+
)] {
147+
use ::ext_php_rs::internal::class::PhpClassImpl;
148+
::ext_php_rs::internal::class::PhpClassImplCollector::<Self>::default()
149+
.get_constants()
150+
}
151+
}
152+
impl ::ext_php_rs::enum_::RegisteredEnum for MyEnumWithIntValues {
153+
const CASES: &'static [::ext_php_rs::enum_::EnumCase] = &[
154+
::ext_php_rs::enum_::EnumCase {
155+
name: "Variant1",
156+
discriminant: Some(::ext_php_rs::enum_::Discriminant::Int(1i64)),
157+
docs: &[],
158+
},
159+
::ext_php_rs::enum_::EnumCase {
160+
name: "Variant2",
161+
discriminant: Some(::ext_php_rs::enum_::Discriminant::Int(42i64)),
162+
docs: &[],
163+
},
164+
];
165+
fn from_name(name: &str) -> ::ext_php_rs::error::Result<Self> {
166+
match name {
167+
"Variant1" => Ok(Self::Variant1),
168+
"Variant2" => Ok(Self::Variant2),
169+
_ => Err(::ext_php_rs::error::Error::InvalidProperty),
170+
}
171+
}
172+
fn to_name(&self) -> &'static str {
173+
match self {
174+
Self::Variant1 => "Variant1",
175+
Self::Variant2 => "Variant2",
176+
}
177+
}
178+
}
179+
impl TryFrom<i64> for MyEnumWithIntValues {
180+
type Error = ::ext_php_rs::error::Error;
181+
fn try_from(value: i64) -> ::ext_php_rs::error::Result<Self> {
182+
match value {
183+
1i64 => Ok(Self::Variant1),
184+
42i64 => Ok(Self::Variant2),
185+
_ => Err(::ext_php_rs::error::Error::InvalidProperty),
186+
}
187+
}
188+
}
189+
impl Into<i64> for MyEnumWithIntValues {
190+
fn into(self) -> i64 {
191+
match self {
192+
Self::Variant1 => 1i64,
193+
Self::Variant2 => 42i64,
194+
}
195+
}
196+
}
197+
#[allow(dead_code)]
198+
enum MyEnumWithStringValues {
199+
Variant1,
200+
Variant2,
201+
}
202+
impl ::ext_php_rs::class::RegisteredClass for MyEnumWithStringValues {
203+
const CLASS_NAME: &'static str = "MY_ENUM_WITH_STRING_VALUES";
204+
const BUILDER_MODIFIER: ::std::option::Option<
205+
fn(::ext_php_rs::builders::ClassBuilder) -> ::ext_php_rs::builders::ClassBuilder,
206+
> = None;
207+
const EXTENDS: ::std::option::Option<::ext_php_rs::class::ClassEntryInfo> = None;
208+
const IMPLEMENTS: &'static [::ext_php_rs::class::ClassEntryInfo] = &[];
209+
const FLAGS: ::ext_php_rs::flags::ClassFlags = ::ext_php_rs::flags::ClassFlags::Enum;
210+
const DOC_COMMENTS: &'static [&'static str] = &[];
211+
fn get_metadata() -> &'static ::ext_php_rs::class::ClassMetadata<Self> {
212+
static METADATA: ::ext_php_rs::class::ClassMetadata<MyEnumWithStringValues> = ::ext_php_rs::class::ClassMetadata::new();
213+
&METADATA
214+
}
215+
#[inline]
216+
fn get_properties<'a>() -> ::std::collections::HashMap<
217+
&'static str,
218+
::ext_php_rs::internal::property::PropertyInfo<'a, Self>,
219+
> {
220+
::std::collections::HashMap::new()
221+
}
222+
#[inline]
223+
fn method_builders() -> ::std::vec::Vec<
224+
(
225+
::ext_php_rs::builders::FunctionBuilder<'static>,
226+
::ext_php_rs::flags::MethodFlags,
227+
),
228+
> {
229+
use ::ext_php_rs::internal::class::PhpClassImpl;
230+
::ext_php_rs::internal::class::PhpClassImplCollector::<Self>::default()
231+
.get_methods()
232+
}
233+
#[inline]
234+
fn constructor() -> ::std::option::Option<
235+
::ext_php_rs::class::ConstructorMeta<Self>,
236+
> {
237+
None
238+
}
239+
#[inline]
240+
fn constants() -> &'static [(
241+
&'static str,
242+
&'static dyn ::ext_php_rs::convert::IntoZvalDyn,
243+
&'static [&'static str],
244+
)] {
245+
use ::ext_php_rs::internal::class::PhpClassImpl;
246+
::ext_php_rs::internal::class::PhpClassImplCollector::<Self>::default()
247+
.get_constants()
248+
}
249+
}
250+
impl ::ext_php_rs::enum_::RegisteredEnum for MyEnumWithStringValues {
251+
const CASES: &'static [::ext_php_rs::enum_::EnumCase] = &[
252+
::ext_php_rs::enum_::EnumCase {
253+
name: "Variant1",
254+
discriminant: Some(::ext_php_rs::enum_::Discriminant::String("foo")),
255+
docs: &[],
256+
},
257+
::ext_php_rs::enum_::EnumCase {
258+
name: "Variant2",
259+
discriminant: Some(::ext_php_rs::enum_::Discriminant::String("bar")),
260+
docs: &[],
261+
},
262+
];
263+
fn from_name(name: &str) -> ::ext_php_rs::error::Result<Self> {
264+
match name {
265+
"Variant1" => Ok(Self::Variant1),
266+
"Variant2" => Ok(Self::Variant2),
267+
_ => Err(::ext_php_rs::error::Error::InvalidProperty),
268+
}
269+
}
270+
fn to_name(&self) -> &'static str {
271+
match self {
272+
Self::Variant1 => "Variant1",
273+
Self::Variant2 => "Variant2",
274+
}
275+
}
276+
}
277+
impl TryFrom<&str> for MyEnumWithStringValues {
278+
type Error = ::ext_php_rs::error::Error;
279+
fn try_from(value: &str) -> ::ext_php_rs::error::Result<Self> {
280+
match value {
281+
"foo" => Ok(Self::Variant1),
282+
"bar" => Ok(Self::Variant2),
283+
_ => Err(::ext_php_rs::error::Error::InvalidProperty),
284+
}
285+
}
286+
}
287+
impl Into<&'static str> for MyEnumWithStringValues {
288+
fn into(self) -> &'static str {
289+
match self {
290+
Self::Variant1 => "foo",
291+
Self::Variant2 => "bar",
292+
}
293+
}
294+
}

crates/macros/tests/expand/enum.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#[macro_use]
2+
extern crate ext_php_rs_derive;
3+
4+
/// Doc comments for MyEnum.
5+
/// This is a basic enum example.
6+
#[php_enum]
7+
enum MyEnum {
8+
/// Variant1 of MyEnum.
9+
/// This variant represents the first case.
10+
Variant1,
11+
#[php(name = "Variant_2")]
12+
Variant2,
13+
/// Variant3 of MyEnum.
14+
#[php(change_case = "UPPER_CASE")]
15+
Variant3,
16+
}
17+
18+
#[php_enum]
19+
#[php(name = "MyIntValuesEnum")]
20+
enum MyEnumWithIntValues {
21+
#[php(value = 1)]
22+
Variant1,
23+
#[php(value = 42)]
24+
Variant2,
25+
}
26+
27+
#[php_enum]
28+
#[php(change_case = "UPPER_CASE")]
29+
enum MyEnumWithStringValues {
30+
#[php(value = "foo")]
31+
Variant1,
32+
#[php(value = "bar")]
33+
Variant2,
34+
}

0 commit comments

Comments
 (0)