| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#![allow(dead_code)] |
| 37 |
use quasi_declare::declare; |
| 38 |
use quasi_router::{Bar, Chart}; |
| 39 |
|
| 40 |
|
| 41 |
pub(crate) struct Takings { |
| 42 |
pub bars: Vec<Bucket>, |
| 43 |
pub most: usize, |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
pub(crate) struct Bucket { |
| 48 |
pub day: String, |
| 49 |
pub cents: usize, |
| 50 |
pub money: String, |
| 51 |
pub sales: String, |
| 52 |
} |
| 53 |
|
| 54 |
impl Takings { |
| 55 |
|
| 56 |
pub(crate) fn new(days: usize) -> Self { |
| 57 |
let bars: Vec<Bucket> = (0..days) |
| 58 |
.map(|n| { |
| 59 |
let cents = (n + 1) * 137; |
| 60 |
Bucket { |
| 61 |
day: format!("Mar {}", n + 1), |
| 62 |
cents, |
| 63 |
money: format!("${}.{:02}", cents / 100, cents % 100), |
| 64 |
sales: if n == 0 { |
| 65 |
"1 sale".to_string() |
| 66 |
} else { |
| 67 |
format!("{} sales", n + 1) |
| 68 |
}, |
| 69 |
} |
| 70 |
}) |
| 71 |
.collect(); |
| 72 |
let most = bars.iter().map(|bar| bar.cents).max().unwrap_or(0); |
| 73 |
Self { bars, most } |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
pub(crate) fn flat(days: usize) -> Self { |
| 81 |
let mut takings = Self::new(days); |
| 82 |
for bar in &mut takings.bars { |
| 83 |
bar.cents = 0; |
| 84 |
} |
| 85 |
takings.most = 0; |
| 86 |
takings |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
declare! { |
| 91 |
|
| 92 |
#[staged] |
| 93 |
pub(crate) shape takings(read: &Takings) -> Node; |
| 94 |
|
| 95 |
chart Chart::new(read.most) { |
| 96 |
for bucket in read.bars.iter() { |
| 97 |
bar Bar::at(bucket.day.clone()) |
| 98 |
.of(bucket.cents) |
| 99 |
.reading(bucket.money.clone()) |
| 100 |
.note(bucket.sales.clone()); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
#[cfg(test)] |
| 106 |
mod tests { |
| 107 |
use quasi_http::Serves as _; |
| 108 |
use quasi_router::stage::{Op, Plan, Residual}; |
| 109 |
use quasi_webview::Webview; |
| 110 |
|
| 111 |
use super::*; |
| 112 |
|
| 113 |
fn residual() -> Residual { |
| 114 |
quasi_webview::stage::derive(&Webview::new(), takings_staged) |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
#[test] |
| 123 |
fn the_axis_is_outside_the_loop_and_the_bars_are_inside_it() { |
| 124 |
let residual = residual(); |
| 125 |
|
| 126 |
let outside = residual |
| 127 |
.ops() |
| 128 |
.iter() |
| 129 |
.filter(|op| matches!(op, Op::Hole { .. })) |
| 130 |
.count(); |
| 131 |
assert_eq!( |
| 132 |
outside, |
| 133 |
1, |
| 134 |
"the axis is the one hole beside the loop: {:#?}", |
| 135 |
residual.ops() |
| 136 |
); |
| 137 |
|
| 138 |
let loops: Vec<&[Op]> = residual |
| 139 |
.ops() |
| 140 |
.iter() |
| 141 |
.filter_map(|op| match op { |
| 142 |
Op::Loop(body) => Some(&**body), |
| 143 |
_ => None, |
| 144 |
}) |
| 145 |
.collect(); |
| 146 |
let [body] = loops.as_slice() else { |
| 147 |
panic!("one loop, for the bars: {:#?}", residual.ops()); |
| 148 |
}; |
| 149 |
|
| 150 |
|
| 151 |
assert_eq!( |
| 152 |
body.iter() |
| 153 |
.filter(|op| matches!(op, Op::Hole { .. })) |
| 154 |
.count(), |
| 155 |
4, |
| 156 |
"a bar's four holes: {body:#?}" |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
#[test] |
| 164 |
fn the_residual_holds_no_computed_width() { |
| 165 |
fn literals(ops: &[Op], out: &mut String) { |
| 166 |
for op in ops { |
| 167 |
match op { |
| 168 |
Op::Lit(text) => out.push_str(text), |
| 169 |
Op::Branch(body) | Op::Loop(body) => literals(body, out), |
| 170 |
Op::Arms(arms) => { |
| 171 |
for arm in arms.iter() { |
| 172 |
literals(arm, out); |
| 173 |
} |
| 174 |
} |
| 175 |
Op::Hole { .. } => {} |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
let mut baked = String::new(); |
| 180 |
literals(residual().ops(), &mut baked); |
| 181 |
assert!(!baked.contains('%'), "a width was baked in: {baked}"); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
const HOLE: &str = "\u{0}"; |
| 196 |
|
| 197 |
|
| 198 |
fn without_stand_ins(render: &str) -> String { |
| 199 |
let mut out = String::with_capacity(render.len()); |
| 200 |
let mut rest = render; |
| 201 |
loop { |
| 202 |
let sentinel = quasi_router::stage::find_sentinel(rest); |
| 203 |
let number = quasi_router::stage::find_number(rest); |
| 204 |
let (at, width) = match (sentinel, number) { |
| 205 |
(Some(s), Some(n)) if s <= n => (s, quasi_router::stage::SENTINEL_LEN), |
| 206 |
(Some(_), Some(n)) => (n, quasi_router::stage::NUMBER_LEN), |
| 207 |
(Some(s), None) => (s, quasi_router::stage::SENTINEL_LEN), |
| 208 |
(None, Some(n)) => (n, quasi_router::stage::NUMBER_LEN), |
| 209 |
(None, None) => { |
| 210 |
out.push_str(rest); |
| 211 |
return out; |
| 212 |
} |
| 213 |
}; |
| 214 |
out.push_str(&rest[..at]); |
| 215 |
out.push_str(HOLE); |
| 216 |
rest = &rest[at + width..]; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
#[test] |
| 221 |
fn replaying_the_residual_reproduces_the_staged_render() { |
| 222 |
let webview = Webview::new(); |
| 223 |
let residual = residual(); |
| 224 |
|
| 225 |
fn replay(ops: &[Op], bars: usize, out: &mut String) { |
| 226 |
for op in ops { |
| 227 |
match op { |
| 228 |
Op::Lit(text) => out.push_str(text), |
| 229 |
Op::Hole { .. } => out.push_str(HOLE), |
| 230 |
Op::Branch(body) => replay(body, bars, out), |
| 231 |
Op::Arms(arms) => replay(&arms[0], bars, out), |
| 232 |
Op::Loop(body) => { |
| 233 |
for _ in 0..bars { |
| 234 |
replay(body, bars, out); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
for bars in [1, 2, 7] { |
| 242 |
let mut replayed = String::new(); |
| 243 |
replay(residual.ops(), bars, &mut replayed); |
| 244 |
assert_eq!( |
| 245 |
without_stand_ins(&webview.fragment(&takings_staged(&Plan::full(bars)))), |
| 246 |
replayed, |
| 247 |
"the residual and the renderer disagree at {bars} bars" |
| 248 |
); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
#[test] |
| 260 |
fn a_magnitude_stands_in_as_a_number_and_a_place_as_text() { |
| 261 |
let render = Webview::new().fragment(&takings_staged(&Plan::full(1))); |
| 262 |
for (property, wants_number) in [("--most: ", true), ("--value: ", true)] { |
| 263 |
let at = render.find(property).expect(property) + property.len(); |
| 264 |
assert_eq!( |
| 265 |
quasi_router::stage::read_number(&render[at..]).is_some(), |
| 266 |
wants_number, |
| 267 |
"{property} in {render}" |
| 268 |
); |
| 269 |
} |
| 270 |
let label = |
| 271 |
render.find("chart-bar-label\">").expect("the label") + "chart-bar-label\">".len(); |
| 272 |
assert!( |
| 273 |
quasi_router::stage::read_sentinel(&render[label..]).is_some(), |
| 274 |
"a place is text: {render}" |
| 275 |
); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
#[test] |
| 281 |
fn a_filled_chart_is_what_the_renderer_would_have_produced() { |
| 282 |
let webview = Webview::new(); |
| 283 |
let residual = residual(); |
| 284 |
|
| 285 |
for days in [0, 1, 2, 7, 31] { |
| 286 |
let read = Takings::new(days); |
| 287 |
assert_eq!( |
| 288 |
webview.fragment(&takings(&read)), |
| 289 |
takings_serve(&residual, &read), |
| 290 |
"{days} days" |
| 291 |
); |
| 292 |
|
| 293 |
let flat = Takings::flat(days); |
| 294 |
assert_eq!( |
| 295 |
webview.fragment(&takings(&flat)), |
| 296 |
takings_serve(&residual, &flat), |
| 297 |
"{days} days, all of them nothing" |
| 298 |
); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
#[test] |
| 307 |
fn a_large_magnitude_is_not_mistaken_for_a_stand_in() { |
| 308 |
let webview = Webview::new(); |
| 309 |
let residual = residual(); |
| 310 |
|
| 311 |
let mut read = Takings::new(3); |
| 312 |
read.bars[2].cents = 9_090_905_000_100_002; |
| 313 |
read.most = read.bars[2].cents; |
| 314 |
assert_eq!( |
| 315 |
webview.fragment(&takings(&read)), |
| 316 |
takings_serve(&residual, &read) |
| 317 |
); |
| 318 |
} |
| 319 |
} |
| 320 |
|