1use crate::core::{Join, Matcher, SourceLocation, TestResult};
2
3pub fn expect<A>(value: A) -> ActualValue<A> {
7 ActualValue::new(value)
8}
9
10#[derive(Debug)]
12pub struct ActualValue<A> {
13 value: A,
14 location: Option<SourceLocation>,
15}
16
17impl<A> ActualValue<A> {
18 pub fn new(value: A) -> Self {
23 ActualValue {
24 value: value,
25 location: None,
26 }
27 }
28
29 pub fn location(mut self, l: SourceLocation) -> Self {
31 self.location = Some(l);
32 self
33 }
34
35 pub fn to<M, E>(self, matcher: M) -> TestResult
37 where
38 M: Matcher<A, E>,
39 {
40 self.matching(matcher, Join::To)
41 }
42
43 pub fn to_not<M, E>(self, matcher: M) -> TestResult
45 where
46 M: Matcher<A, E>,
47 {
48 self.matching(matcher, Join::ToNot)
49 }
50
51 pub fn not_to<M, E>(self, matcher: M) -> TestResult
53 where
54 M: Matcher<A, E>,
55 {
56 self.matching(matcher, Join::NotTo)
57 }
58
59 fn matching<M, E>(self, matcher: M, join: Join) -> TestResult
61 where
62 M: Matcher<A, E>,
63 {
64 let success = if join.is_assertion() {
65 matcher.matches(&self.value)
66 } else {
67 !matcher.matches(&self.value)
68 };
69 if success {
70 TestResult::new_success()
71 } else {
72 let message = matcher.failure_message(join, &self.value);
73 TestResult::new_failure(message, self.location)
74 }
75 }
76}