|
| 1 | +use std::task::Poll; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + csr::{CsrAttributes, RenderAttributes}, |
| 5 | + ssr::SsrAttributes, |
| 6 | + values::{ |
| 7 | + r#const::{ConstAttributes, HasConstAttributes}, |
| 8 | + EitherAttributes, Empty, |
| 9 | + }, |
| 10 | +}; |
| 11 | + |
| 12 | +use crate::attrs::one; |
| 13 | + |
| 14 | +fn csr_ssr_const<T: ?Sized + HasConstAttributes>( |
| 15 | + v: ConstAttributes<T>, |
| 16 | +) -> (Vec<(String, String)>, String) { |
| 17 | + use async_str_iter::AsyncStrIterator; |
| 18 | + |
| 19 | + let ssr = v.into_ssr_attributes(); |
| 20 | + let mut ssr = std::pin::pin!(ssr); |
| 21 | + let cx = &mut std::task::Context::from_waker(std::task::Waker::noop()); |
| 22 | + let ssr_str = match ssr.as_mut().poll_next_str(cx) { |
| 23 | + Poll::Ready(v) => v.unwrap_or(""), |
| 24 | + Poll::Pending => panic!(), |
| 25 | + }; |
| 26 | + |
| 27 | + let ssr_str = ssr_str.to_string(); |
| 28 | + |
| 29 | + match ssr.poll_next_str(cx) { |
| 30 | + Poll::Ready(None) => {} |
| 31 | + _ => panic!(), |
| 32 | + } |
| 33 | + |
| 34 | + let csr = { |
| 35 | + struct RenderInit(Vec<(String, String)>); |
| 36 | + |
| 37 | + impl RenderAttributes for RenderInit { |
| 38 | + fn set_attribute(&mut self, name: &str, value: &str) { |
| 39 | + self.0.push((name.into(), value.into())); |
| 40 | + } |
| 41 | + |
| 42 | + fn remove_attribute(&mut self, _: &str) { |
| 43 | + panic!() |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + let mut renderer = RenderInit(Vec::new()); |
| 48 | + let mut state = v.render_init(&mut renderer); |
| 49 | + |
| 50 | + struct RenderUpdate; |
| 51 | + |
| 52 | + impl RenderAttributes for RenderUpdate { |
| 53 | + fn set_attribute(&mut self, _: &str, _: &str) { |
| 54 | + panic!() |
| 55 | + } |
| 56 | + |
| 57 | + fn remove_attribute(&mut self, _: &str) { |
| 58 | + panic!() |
| 59 | + } |
| 60 | + } |
| 61 | + v.render_update(&mut RenderUpdate, &mut state); |
| 62 | + |
| 63 | + renderer.0 |
| 64 | + }; |
| 65 | + |
| 66 | + (csr, ssr_str) |
| 67 | +} |
| 68 | + |
| 69 | +#[test] |
| 70 | +fn match_clause() { |
| 71 | + let f = || one!(match (panic!()) {}); |
| 72 | + |
| 73 | + let _ = f as fn() -> crate::values::Never; |
| 74 | + |
| 75 | + let Empty = one!(match (()) { |
| 76 | + _ => (), |
| 77 | + }); |
| 78 | + |
| 79 | + { |
| 80 | + let (csr, ssr) = csr_ssr_const(one!(match (()) { |
| 81 | + _ => "", |
| 82 | + })); |
| 83 | + assert_eq!(csr, []); |
| 84 | + assert_eq!(ssr, ""); |
| 85 | + } |
| 86 | + |
| 87 | + match one!(match (true) { |
| 88 | + a if a => "", |
| 89 | + _ => attrs!(), |
| 90 | + }) { |
| 91 | + EitherAttributes::A(ConstAttributes { .. }) => {} |
| 92 | + EitherAttributes::B(Empty) => unreachable!(), |
| 93 | + } |
| 94 | + |
| 95 | + match one!(match (1) { |
| 96 | + // empty (think unit tuple `()` as an empty list) |
| 97 | + a if a > 0 => (), |
| 98 | + b if b < 0 => { |
| 99 | + // empty wrapped in a block |
| 100 | + () |
| 101 | + } |
| 102 | + // empty wrapped in parenthesis |
| 103 | + _ => (()), |
| 104 | + }) { |
| 105 | + EitherAttributes::A(Empty) => {} |
| 106 | + EitherAttributes::B(other) => match other { |
| 107 | + EitherAttributes::A(Empty) => panic!(), |
| 108 | + EitherAttributes::B(Empty) => panic!(), |
| 109 | + }, |
| 110 | + } |
| 111 | +} |
0 commit comments