Move comment tests to integration 🚚

This commit is contained in:
Laurenz 2021-01-13 15:44:41 +01:00
parent 60154474ba
commit 1d01b93f67
5 changed files with 50 additions and 27 deletions

View File

@ -59,13 +59,16 @@ impl Eval for Spanned<&ExprArgs> {
/// Evaluated arguments to a function. /// Evaluated arguments to a function.
#[derive(Debug)] #[derive(Debug)]
pub struct Args { pub struct Args {
span: Span, /// The span of the whole argument list.
pos: SpanVec<Value>, pub span: Span,
named: Vec<(Spanned<String>, Spanned<Value>)>, /// The positional arguments.
pub pos: SpanVec<Value>,
/// The named arguments.
pub named: Vec<(Spanned<String>, Spanned<Value>)>,
} }
impl Args { impl Args {
/// Find the first convertible positional argument. /// Find and remove the first convertible positional argument.
pub fn find<T>(&mut self, ctx: &mut EvalContext) -> Option<T> pub fn find<T>(&mut self, ctx: &mut EvalContext) -> Option<T>
where where
T: Cast<Spanned<Value>>, T: Cast<Spanned<Value>>,
@ -73,8 +76,8 @@ impl Args {
self.pos.iter_mut().find_map(move |slot| try_cast(ctx, slot)) self.pos.iter_mut().find_map(move |slot| try_cast(ctx, slot))
} }
/// Find the first convertible positional argument, producing an error if /// Find and remove the first convertible positional argument, producing an
/// no match was found. /// error if no match was found.
pub fn require<T>(&mut self, ctx: &mut EvalContext, what: &str) -> Option<T> pub fn require<T>(&mut self, ctx: &mut EvalContext, what: &str) -> Option<T>
where where
T: Cast<Spanned<Value>>, T: Cast<Spanned<Value>>,
@ -86,7 +89,7 @@ impl Args {
found found
} }
/// Filter out all convertible positional arguments. /// Filter out and remove all convertible positional arguments.
pub fn filter<'a, T>( pub fn filter<'a, T>(
&'a mut self, &'a mut self,
ctx: &'a mut EvalContext, ctx: &'a mut EvalContext,
@ -97,8 +100,8 @@ impl Args {
self.pos.iter_mut().filter_map(move |slot| try_cast(ctx, slot)) self.pos.iter_mut().filter_map(move |slot| try_cast(ctx, slot))
} }
/// Convert the value for the given named argument, producing an error if /// Convert and remove the value for the given named argument, producing an
/// the conversion fails. /// error if the conversion fails.
pub fn get<'a, T>(&mut self, ctx: &mut EvalContext, name: &str) -> Option<T> pub fn get<'a, T>(&mut self, ctx: &mut EvalContext, name: &str) -> Option<T>
where where
T: Cast<Spanned<Value>>, T: Cast<Spanned<Value>>,
@ -108,6 +111,13 @@ impl Args {
cast(ctx, value) cast(ctx, value)
} }
/// Drain all remainings arguments into an array and a dictionary.
pub fn drain(&mut self) -> (ValueArray, ValueDict) {
let array = self.pos.drain(..).map(|s| s.v).collect();
let dict = self.named.drain(..).map(|(k, v)| (k.v, v.v)).collect();
(array, dict)
}
/// Produce "unexpected argument" errors for all remaining arguments. /// Produce "unexpected argument" errors for all remaining arguments.
pub fn finish(self, ctx: &mut EvalContext) { pub fn finish(self, ctx: &mut EvalContext) {
let a = self.pos.iter().map(|v| v.as_ref()); let a = self.pos.iter().map(|v| v.as_ref());

View File

@ -217,23 +217,6 @@ macro_rules! Let {
}; };
} }
#[test]
fn test_parse_comments() {
// In markup.
t!("a// you\nb" Text("a"), Space, Text("b"));
t!("* // \n /*\n\n*/*" Strong, Space, Space, Strong);
// In code.
t!("[v /*12pt*/]" Call!("v"));
t!("[v //\n]" Call!("v"));
t!("[v 12, /*\n*/ size: 14]" Call!("v", Args![Int(12), "size" => Int(14)]));
// Error.
t!("a*/b"
nodes: [Text("a"), Text("b")],
errors: [S(1..3, "unexpected end of block comment")]);
}
#[test] #[test]
fn test_parse_simple_nodes() { fn test_parse_simple_nodes() {
// Basics. // Basics.

BIN
tests/ref/comments.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

23
tests/typ/comments.typ Normal file
View File

@ -0,0 +1,23 @@
// Test interaction with words, spacing and expressions.
A// you
B
C/*
/* */
*/D
[dump /*1*/ a: "b" //
, 1]
---
// Test error.
//
// ref: false
// error: 3:7-3:9 unexpected end of block comment
// No start of block comment.
/* */ */
// Unterminated block comment is okay.
/*

View File

@ -15,7 +15,7 @@ use ttf_parser::OutlineBuilder;
use typst::diag::{Diag, Feedback, Level, Pass}; use typst::diag::{Diag, Feedback, Level, Pass};
use typst::env::{Env, ImageResource, ResourceLoader, SharedEnv}; use typst::env::{Env, ImageResource, ResourceLoader, SharedEnv};
use typst::eval::State; use typst::eval::{Args, EvalContext, State, Value, ValueFunc};
use typst::export::pdf; use typst::export::pdf;
use typst::font::FontLoader; use typst::font::FontLoader;
use typst::geom::{Length, Point, Sides, Size}; use typst::geom::{Length, Point, Sides, Size};
@ -189,6 +189,13 @@ fn test_part(i: usize, src: &str, env: &SharedEnv) -> (bool, Vec<Frame>) {
state.page.size = Size::uniform(Length::pt(120.0)); state.page.size = Size::uniform(Length::pt(120.0));
state.page.margins = Sides::uniform(Some(Length::pt(10.0).into())); state.page.margins = Sides::uniform(Some(Length::pt(10.0).into()));
pub fn dump(_: &mut EvalContext, args: &mut Args) -> Value {
let (array, dict) = args.drain();
Value::Array(vec![Value::Array(array), Value::Dict(dict)])
}
Rc::make_mut(&mut state.scope).set("dump", ValueFunc::new("dump", dump));
let Pass { let Pass {
output: mut frames, output: mut frames,
feedback: Feedback { mut diags, .. }, feedback: Feedback { mut diags, .. },