Skip to content

Commit

Permalink
fix: canonical test
Browse files Browse the repository at this point in the history
  • Loading branch information
dubadub committed Jan 16, 2025
1 parent 22147d6 commit 10a963e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 52 deletions.
53 changes: 3 additions & 50 deletions src/parser/quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ fn float(tokens: &[Token], bp: &BlockParser) -> Result<f64, SourceDiag> {
#[cfg(test)]
mod tests {
use super::*;
use crate::{parser::TokenStream, text::Text};
use crate::{parser::TokenStream};
use test_case::test_case;

macro_rules! t {
Expand Down Expand Up @@ -425,47 +425,6 @@ mod tests {
assert!(ctx.is_empty());
}

#[test]
fn value() {
let (q, s, ctx) = t!("100|200|300%ml");
assert_eq!(
q.value,
QuantityValue::Many(vec![
Located::new(num!(100.0), 0..3),
Located::new(num!(200.0), 4..7),
Located::new(num!(300.0), 8..11),
])
);
assert_eq!(s, Some((11..12).into()));
assert_eq!(q.unit.unwrap(), Text::from_str("ml", 12));
assert!(ctx.is_empty());

let (q, s, ctx) = t!("100|2-3|str*%ml");
assert_eq!(
q.value,
QuantityValue::Many(vec![
Located::new(num!(100.0), 0..3),
Located::new(range!(2.0, 3.0), 4..7),
Located::new(Value::Text("str".into()), 8..11),
])
);
assert_eq!(s, Some((12..13).into()));
assert_eq!(q.unit.unwrap(), Text::from_str("ml", 13));
assert_eq!(ctx.errors().count(), 1);
assert_eq!(ctx.warnings().count(), 0);

let (q, _, ctx) = t!("100|");
assert_eq!(
q.value,
QuantityValue::Many(vec![
Located::new(num!(100.0), 0..3),
Located::new(Value::Text("".into()), 4..4)
])
);
assert_eq!(ctx.errors().count(), 1);
assert_eq!(ctx.warnings().count(), 0);
}

#[test]
fn range_value() {
let (q, _, _) = t!("2-3");
Expand Down Expand Up @@ -526,10 +485,7 @@ mod tests {
#[test_case("2 1/2" => (2, 1, 2); "mixed value")]
fn fractional_val(s: &str) -> (u32, u32, u32) {
let (q, _, _) = t!(s);
let QuantityValue { value, .. } = q.value else {
panic!("not single value")
};
let value = value.into_inner();
let value = q.value.value.into_inner();
let Value::Number(num) = value else {
panic!("not number")
};
Expand All @@ -555,10 +511,7 @@ mod tests {
#[test_case("01.0" => panics "not number")]
fn simple_numbers(s: &str) -> f64 {
let (q, _, r) = t!(s);
let QuantityValue { value, .. } = q.value else {
panic!("not single value")
};
let value = value.into_inner();
let value = q.value.value.into_inner();
let Value::Number(num) = value else {
panic!("not number")
};
Expand Down
4 changes: 2 additions & 2 deletions tests/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,12 @@ impl TestStepItem {
impl TestValue {
fn from_cooklang_value(value: ScalableValue) -> Self {
match value {
ScalableValue::Fixed(value) => match value {
ScalableValue::Fixed(_) => panic!("unexpected fixed value"),
ScalableValue::Linear(value) => match value {
Value::Number(num) => TestValue::Number(num.value()),
Value::Range { .. } => panic!("unexpected range value"),
Value::Text(value) => TestValue::Text(value),
},
ScalableValue::Linear(_) => panic!("unexpected linear value"),
ScalableValue::ByServings(_) => panic!("unexpected value by servings"),
}
}
Expand Down

0 comments on commit 10a963e

Please sign in to comment.