diff options
author | HampusM <hampus@hampusmat.com> | 2023-08-16 22:35:47 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-08-16 22:36:09 +0200 |
commit | 800e164a83388aa5ca7675f8031b0f0d7c6b6051 (patch) | |
tree | 744cbab2db5ff7ca4edb3713ceabf24d79620df0 /tests | |
parent | 595806824d41c696b660f40c01914a24e9618f1d (diff) |
test: make the prevent-circular example an integration test
Diffstat (limited to 'tests')
-rw-r--r-- | tests/prevent_circular.rs | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/tests/prevent_circular.rs b/tests/prevent_circular.rs new file mode 100644 index 0000000..4b95dea --- /dev/null +++ b/tests/prevent_circular.rs @@ -0,0 +1,94 @@ +#![deny(clippy::all, clippy::pedantic)] +#![allow(clippy::disallowed_names)] + +use syrette::di_container::blocking::prelude::*; +use syrette::errors::di_container::DIContainerError; +use syrette::errors::injectable::InjectableError; +use syrette::injectable; +use syrette::ptr::TransientPtr; + +#[derive(Debug)] +struct Foo +{ + _bar: TransientPtr<Bar>, +} + +#[injectable] +impl Foo +{ + fn new(bar: TransientPtr<Bar>) -> Self + { + Self { _bar: bar } + } +} + +#[derive(Debug)] +struct Bar +{ + _foo: TransientPtr<Foo>, +} + +#[injectable] +impl Bar +{ + fn new(foo: TransientPtr<Foo>) -> Self + { + Self { _foo: foo } + } +} + +macro_rules! assert_match { + ($target: expr, $pattern: pat => $expr: expr) => {{ + let target = $target; + + // Not all pattern variables will be used here + #[allow(unused_variables)] + { + assert!(matches!(&target, $pattern)); + } + + match target { + $pattern => $expr, + _ => { + unreachable!(); + } + } + }}; +} + +#[test] +fn prevent_circular_works() +{ + let mut di_container = DIContainer::new(); + + di_container.bind::<Foo>().to::<Foo>().expect("Expected Ok"); + di_container.bind::<Bar>().to::<Bar>().expect("Expected Ok"); + + let err = di_container.get::<Foo>().expect_err("Expected Err"); + + let container_err_a = assert_match!( + err, + DIContainerError::BindingResolveFailed { + reason: InjectableError::ResolveFailed { reason, affected: _ }, + interface: _ + } => *reason + ); + + let container_err_b = assert_match!( + container_err_a, + DIContainerError::BindingResolveFailed { + reason: InjectableError::ResolveFailed { reason, affected: _ }, + interface: _ + } => *reason + ); + + assert!(matches!( + container_err_b, + DIContainerError::BindingResolveFailed { + reason: InjectableError::DetectedCircular { + dependency_history: _ + }, + interface: _ + } + )); +} |