diff options
| author | Laurenz Stampfl <47084093+LaurenzV@users.noreply.github.com> | 2023-05-23 10:41:20 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-23 10:41:20 +0200 |
| commit | 752817ae74607ca23ec0aad51824ddca66faa7a8 (patch) | |
| tree | d9f51f3d029c1ca8754be9ae9ccc9dc91ae6b000 /tests | |
| parent | f4fd6855e7fb833a2125d2e81aa3b9f548b18170 (diff) | |
Add support for date & time handling (#435)
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/src/benches.rs | 6 | ||||
| -rw-r--r-- | tests/src/tests.rs | 6 | ||||
| -rw-r--r-- | tests/typ/compute/construct.typ | 86 |
3 files changed, 96 insertions, 2 deletions
diff --git a/tests/src/benches.rs b/tests/src/benches.rs index 9e9b98d0..aeddcaf9 100644 --- a/tests/src/benches.rs +++ b/tests/src/benches.rs @@ -3,7 +3,7 @@ use std::path::Path; use comemo::{Prehashed, Track, Tracked}; use iai::{black_box, main, Iai}; use typst::diag::{FileError, FileResult}; -use typst::eval::Library; +use typst::eval::{Datetime, Library}; use typst::font::{Font, FontBook}; use typst::geom::Color; use typst::syntax::{Source, SourceId}; @@ -147,4 +147,8 @@ impl World for BenchWorld { fn file(&self, path: &Path) -> FileResult<Buffer> { Err(FileError::NotFound(path.into())) } + + fn today(&self, _: Option<i64>) -> Option<Datetime> { + Some(Datetime::from_ymd(1970, 1, 1).unwrap()) + } } diff --git a/tests/src/tests.rs b/tests/src/tests.rs index f295869f..c2d0cc68 100644 --- a/tests/src/tests.rs +++ b/tests/src/tests.rs @@ -22,7 +22,7 @@ use walkdir::WalkDir; use typst::diag::{bail, FileError, FileResult}; use typst::doc::{Document, Frame, FrameItem, Meta}; -use typst::eval::{func, Library, Value}; +use typst::eval::{func, Datetime, Library, Value}; use typst::font::{Font, FontBook}; use typst::geom::{Abs, Color, RgbaColor, Sides, Smart}; use typst::syntax::{Source, SourceId, Span, SyntaxNode}; @@ -296,6 +296,10 @@ impl World for TestWorld { .get_or_init(|| read(path).map(Buffer::from)) .clone() } + + fn today(&self, _: Option<i64>) -> Option<Datetime> { + Some(Datetime::from_ymd(1970, 1, 1).unwrap()) + } } impl TestWorld { diff --git a/tests/typ/compute/construct.typ b/tests/typ/compute/construct.typ index ee0a24b0..9c05a6d0 100644 --- a/tests/typ/compute/construct.typ +++ b/tests/typ/compute/construct.typ @@ -73,3 +73,89 @@ --- #assert(range(2, 5) == (2, 3, 4)) + +--- +// Test displaying of dates. +#test(datetime(year: 2023, month: 4, day: 29).display(), "2023-04-29") +#test(datetime(year: 2023, month: 4, day: 29).display("[year]"), "2023") +#test( + datetime(year: 2023, month: 4, day: 29) + .display("[year repr:last_two]"), + "23", +) +#test( + datetime(year: 2023, month: 4, day: 29) + .display("[year] [month repr:long] [day] [week_number] [weekday]"), + "2023 April 29 17 Saturday", +) + +// Test displaying of times +#test(datetime(hour: 14, minute: 26, second: 50).display(), "14:26:50") +#test(datetime(hour: 14, minute: 26, second: 50).display("[hour]"), "14") +#test( + datetime(hour: 14, minute: 26, second: 50) + .display("[hour repr:12 padding:none]"), + "2", +) +#test( + datetime(hour: 14, minute: 26, second: 50) + .display("[hour], [minute], [second]"), "14, 26, 50", +) + +// Test displaying of datetimes +#test( + datetime(year: 2023, month: 4, day: 29, hour: 14, minute: 26, second: 50).display(), + "2023-04-29 14:26:50", +) + +// Test getting the year/month/day etc. of a datetime +#let d = datetime(year: 2023, month: 4, day: 29, hour: 14, minute: 26, second: 50) +#test(d.year(), 2023) +#test(d.month(), 4) +#test(d.weekday(), 6) +#test(d.day(), 29) +#test(d.hour(), 14) +#test(d.minute(), 26) +#test(d.second(), 50) + +#let e = datetime(year: 2023, month: 4, day: 29) +#test(e.hour(), none) +#test(e.minute(), none) +#test(e.second(), none) + +// Test today +#test(datetime.today().display(), "1970-01-01") +#test(datetime.today(offset: auto).display(), "1970-01-01") +#test(datetime.today(offset: 2).display(), "1970-01-01") + +--- +// Error: 10-12 at least one of date or time must be fully specified +#datetime() + +--- +// Error: 10-42 time is invalid +#datetime(hour: 25, minute: 0, second: 0) + +--- +// Error: 10-41 date is invalid +#datetime(year: 2000, month: 2, day: 30) + +--- +// Error: 26-35 missing closing bracket for bracket at index 0 +#datetime.today().display("[year") + +--- +// Error: 26-39 invalid component name 'nothing' at index 1 +#datetime.today().display("[nothing]") + +--- +// Error: 26-51 invalid modifier 'wrong' at index 6 +#datetime.today().display("[year wrong:last_two]") + +--- +// Error: 26-34 expected component name at index 2 +#datetime.today().display(" []") + +--- +// Error: 26-36 failed to format datetime in the requested format +#datetime.today().display("[hour]") |
