|
| 1 | +use super::*; |
| 2 | + |
| 3 | +#[test] |
| 4 | +fn empty() { |
| 5 | + assert_eq!(HtmlWithLimit::new(0).finish(), ""); |
| 6 | + assert_eq!(HtmlWithLimit::new(60).finish(), ""); |
| 7 | +} |
| 8 | + |
| 9 | +#[test] |
| 10 | +fn basic() { |
| 11 | + let mut buf = HtmlWithLimit::new(60); |
| 12 | + buf.push("Hello "); |
| 13 | + buf.open_tag("em"); |
| 14 | + buf.push("world"); |
| 15 | + buf.close_tag(); |
| 16 | + buf.push("!"); |
| 17 | + assert_eq!(buf.finish(), "Hello <em>world</em>!"); |
| 18 | +} |
| 19 | + |
| 20 | +#[test] |
| 21 | +fn no_tags() { |
| 22 | + let mut buf = HtmlWithLimit::new(60); |
| 23 | + buf.push("Hello"); |
| 24 | + buf.push(" world!"); |
| 25 | + assert_eq!(buf.finish(), "Hello world!"); |
| 26 | +} |
| 27 | + |
| 28 | +#[test] |
| 29 | +fn limit_0() { |
| 30 | + let mut buf = HtmlWithLimit::new(0); |
| 31 | + buf.push("Hello "); |
| 32 | + buf.open_tag("em"); |
| 33 | + buf.push("world"); |
| 34 | + buf.close_tag(); |
| 35 | + buf.push("!"); |
| 36 | + assert_eq!(buf.finish(), ""); |
| 37 | +} |
| 38 | + |
| 39 | +#[test] |
| 40 | +fn exactly_limit() { |
| 41 | + let mut buf = HtmlWithLimit::new(12); |
| 42 | + buf.push("Hello "); |
| 43 | + buf.open_tag("em"); |
| 44 | + buf.push("world"); |
| 45 | + buf.close_tag(); |
| 46 | + buf.push("!"); |
| 47 | + assert_eq!(buf.finish(), "Hello <em>world</em>!"); |
| 48 | +} |
| 49 | + |
| 50 | +#[test] |
| 51 | +fn multiple_nested_tags() { |
| 52 | + let mut buf = HtmlWithLimit::new(60); |
| 53 | + buf.open_tag("p"); |
| 54 | + buf.push("This is a "); |
| 55 | + buf.open_tag("em"); |
| 56 | + buf.push("paragraph"); |
| 57 | + buf.open_tag("strong"); |
| 58 | + buf.push("!"); |
| 59 | + buf.close_tag(); |
| 60 | + buf.close_tag(); |
| 61 | + buf.close_tag(); |
| 62 | + assert_eq!(buf.finish(), "<p>This is a <em>paragraph<strong>!</strong></em></p>"); |
| 63 | +} |
| 64 | + |
| 65 | +#[test] |
| 66 | +fn forgot_to_close_tags() { |
| 67 | + let mut buf = HtmlWithLimit::new(60); |
| 68 | + buf.open_tag("p"); |
| 69 | + buf.push("This is a "); |
| 70 | + buf.open_tag("em"); |
| 71 | + buf.push("paragraph"); |
| 72 | + buf.open_tag("strong"); |
| 73 | + buf.push("!"); |
| 74 | + assert_eq!(buf.finish(), "<p>This is a <em>paragraph<strong>!</strong></em></p>"); |
| 75 | +} |
| 76 | + |
| 77 | +#[test] |
| 78 | +fn past_the_limit() { |
| 79 | + let mut buf = HtmlWithLimit::new(20); |
| 80 | + buf.open_tag("p"); |
| 81 | + (0..10).try_for_each(|n| { |
| 82 | + buf.open_tag("strong"); |
| 83 | + buf.push("word#")?; |
| 84 | + buf.push(&n.to_string())?; |
| 85 | + buf.close_tag(); |
| 86 | + ControlFlow::CONTINUE |
| 87 | + }); |
| 88 | + buf.close_tag(); |
| 89 | + assert_eq!( |
| 90 | + buf.finish(), |
| 91 | + "<p>\ |
| 92 | + <strong>word#0</strong>\ |
| 93 | + <strong>word#1</strong>\ |
| 94 | + <strong>word#2</strong>\ |
| 95 | + </p>" |
| 96 | + ); |
| 97 | +} |
| 98 | + |
| 99 | +#[test] |
| 100 | +fn quickly_past_the_limit() { |
| 101 | + let mut buf = HtmlWithLimit::new(6); |
| 102 | + buf.open_tag("p"); |
| 103 | + buf.push("Hello"); |
| 104 | + buf.push(" World"); |
| 105 | + // intentionally not closing <p> before finishing |
| 106 | + assert_eq!(buf.finish(), "<p>Hello</p>"); |
| 107 | +} |
| 108 | + |
| 109 | +#[test] |
| 110 | +#[should_panic = "called `Option::unwrap()` on a `None` value"] |
| 111 | +fn close_too_many() { |
| 112 | + let mut buf = HtmlWithLimit::new(60); |
| 113 | + buf.open_tag("p"); |
| 114 | + buf.push("Hello"); |
| 115 | + buf.close_tag(); |
| 116 | + buf.close_tag(); |
| 117 | +} |
0 commit comments