Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/typstsharp.tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public async Task BasicSource()
{
var compiler = TypstCompiler.FromSource("Hello World 2");
var result = compiler.Compile().Buffers[0];

Check warning on line 15 in src/typstsharp.tests/Tests.cs

View workflow job for this annotation

GitHub Actions / pack

'CompileOutcome.Buffers' is obsolete: 'Buffers is deprecated and will be removed in a future version. For PDF export, prefer 'compiler.CompilePdf()' or 'outcome.AsPdf()'. For SVG/PNG, prefer 'compiler.CompileSvg()' or 'compiler.CompilePng()'.'

Check warning on line 15 in src/typstsharp.tests/Tests.cs

View workflow job for this annotation

GitHub Actions / test-package (Windows, windows-latest)

'CompileOutcome.Buffers' is obsolete: 'Buffers is deprecated and will be removed in a future version. For PDF export, prefer 'compiler.CompilePdf()' or 'outcome.AsPdf()'. For SVG/PNG, prefer 'compiler.CompileSvg()' or 'compiler.CompilePng()'.'

Check warning on line 15 in src/typstsharp.tests/Tests.cs

View workflow job for this annotation

GitHub Actions / test-package (Ubuntu, ubuntu-latest)

'CompileOutcome.Buffers' is obsolete: 'Buffers is deprecated and will be removed in a future version. For PDF export, prefer 'compiler.CompilePdf()' or 'outcome.AsPdf()'. For SVG/PNG, prefer 'compiler.CompileSvg()' or 'compiler.CompilePng()'.'
var plainText = GetPlainText(result);
await Assert.That(plainText).Contains("World 2");
}
Expand Down Expand Up @@ -326,7 +326,7 @@
{
// Reported as #9
var compiler = TypstCompiler.FromSource("= Hello world’s");
var result = compiler.Compile().Buffers[0];

Check warning on line 329 in src/typstsharp.tests/Tests.cs

View workflow job for this annotation

GitHub Actions / pack

'CompileOutcome.Buffers' is obsolete: 'Buffers is deprecated and will be removed in a future version. For PDF export, prefer 'compiler.CompilePdf()' or 'outcome.AsPdf()'. For SVG/PNG, prefer 'compiler.CompileSvg()' or 'compiler.CompilePng()'.'

Check warning on line 329 in src/typstsharp.tests/Tests.cs

View workflow job for this annotation

GitHub Actions / test-package (Windows, windows-latest)

'CompileOutcome.Buffers' is obsolete: 'Buffers is deprecated and will be removed in a future version. For PDF export, prefer 'compiler.CompilePdf()' or 'outcome.AsPdf()'. For SVG/PNG, prefer 'compiler.CompileSvg()' or 'compiler.CompilePng()'.'

Check warning on line 329 in src/typstsharp.tests/Tests.cs

View workflow job for this annotation

GitHub Actions / test-package (Ubuntu, ubuntu-latest)

'CompileOutcome.Buffers' is obsolete: 'Buffers is deprecated and will be removed in a future version. For PDF export, prefer 'compiler.CompilePdf()' or 'outcome.AsPdf()'. For SVG/PNG, prefer 'compiler.CompileSvg()' or 'compiler.CompilePng()'.'
var plainText = GetPlainText(result);
await Assert.That(plainText).Contains("Hello world’s");
}
Expand Down Expand Up @@ -519,7 +519,7 @@
packagePath: packages.Path,
includeSystemPackages: false);

var plainText = GetPlainText(compiler.Compile().Buffers[0]);

Check warning on line 522 in src/typstsharp.tests/Tests.cs

View workflow job for this annotation

GitHub Actions / pack

'CompileOutcome.Buffers' is obsolete: 'Buffers is deprecated and will be removed in a future version. For PDF export, prefer 'compiler.CompilePdf()' or 'outcome.AsPdf()'. For SVG/PNG, prefer 'compiler.CompileSvg()' or 'compiler.CompilePng()'.'

Check warning on line 522 in src/typstsharp.tests/Tests.cs

View workflow job for this annotation

GitHub Actions / test-package (Windows, windows-latest)

'CompileOutcome.Buffers' is obsolete: 'Buffers is deprecated and will be removed in a future version. For PDF export, prefer 'compiler.CompilePdf()' or 'outcome.AsPdf()'. For SVG/PNG, prefer 'compiler.CompileSvg()' or 'compiler.CompilePng()'.'

Check warning on line 522 in src/typstsharp.tests/Tests.cs

View workflow job for this annotation

GitHub Actions / test-package (Ubuntu, ubuntu-latest)

'CompileOutcome.Buffers' is obsolete: 'Buffers is deprecated and will be removed in a future version. For PDF export, prefer 'compiler.CompilePdf()' or 'outcome.AsPdf()'. For SVG/PNG, prefer 'compiler.CompileSvg()' or 'compiler.CompilePng()'.'

await Assert.That(plainText).Contains("Hello from a bundled package");
}
Expand Down Expand Up @@ -1150,6 +1150,26 @@
await Assert.That(document.Warnings is string[]).IsFalse();
}

/// <summary>
/// The warning-free case had no coverage. This pins the public contract rather than the
/// allocation, so it holds whichever way the empty list is produced: empty, and not a mutable
/// array handed out to callers.
/// </summary>
[Test]
public async Task WarningsFromACleanCompileAreEmptyAndStillImmutable()
{
using var compiler = TypstCompiler.FromSource("= No warnings here");
var document = compiler.CompileToDocument();

await Assert.That(document.Warnings.Count).IsEqualTo(0);
await Assert.That(document.Warnings is string[]).IsFalse();

document.Dispose();

// Warnings are copied out of native memory eagerly, so they outlive the document.
await Assert.That(document.Warnings.Count).IsEqualTo(0);
}

private const string TwoPageSource = """
First page
#pagebreak()
Expand Down
3 changes: 2 additions & 1 deletion src/typstsharp/TypstDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ internal unsafe TypstDocument(CsBindgen.CompileResult native)
}

// Warnings are small and are copied eagerly so that they stay usable after disposal.
var warnings = new string[warningCount];
// A clean compile is the common case, and Array.Empty spares it the only allocation here.
var warnings = warningCount == 0 ? Array.Empty<string>() : new string[warningCount];
for (int i = 0; i < warnings.Length; i++)
{
var warning = native.warnings[i];
Expand Down
Loading