File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -108,3 +108,49 @@ impl<T: Validator> ops::DerefMut for Accessor<T> {
108108 & mut self . data
109109 }
110110}
111+
112+ #[ cfg( test) ]
113+ mod tests {
114+ use anyhow:: anyhow;
115+
116+ use super :: * ;
117+
118+ #[ derive( Clone , Copy ) ]
119+ struct Odd ( u32 ) ;
120+
121+ impl Validator for Odd {
122+ fn validate ( & self ) -> anyhow:: Result < ( ) > {
123+ if self . 0 % 2 == 0 {
124+ Err ( anyhow ! ( "{} is not odd" , self . 0 ) )
125+ } else {
126+ Ok ( ( ) )
127+ }
128+ }
129+ }
130+
131+ #[ test]
132+ fn validation ( ) {
133+ let right = Accessor :: new ( Odd ( 41 ) ) ;
134+ let wrong = Accessor :: new ( Odd ( 42 ) ) ;
135+
136+ assert ! ( right. validate( ) . is_ok( ) ) ;
137+ _ = * right;
138+
139+ assert ! ( wrong. validate( ) . is_err( ) ) ;
140+ }
141+
142+ #[ test]
143+ #[ should_panic( expected = "config accessed before validation" ) ]
144+ fn panic_if_accessed_before_validation ( ) {
145+ let accessor = Accessor :: new ( Odd ( 41 ) ) ;
146+ _ = * accessor;
147+ }
148+
149+ #[ test]
150+ #[ should_panic( expected = "config accessed before validation" ) ]
151+ fn panic_if_accessed_invalid ( ) {
152+ let accessor = Accessor :: new ( Odd ( 42 ) ) ;
153+ assert ! ( accessor. validate( ) . is_err( ) ) ;
154+ _ = * accessor;
155+ }
156+ }
You can’t perform that action at this time.
0 commit comments