81 lines
3.3 KiB
C#
81 lines
3.3 KiB
C#
using LehrerApp.TemplateDesigner;
|
|
using LehrerApp.Templating;
|
|
using Xunit;
|
|
|
|
namespace LehrerApp.TemplateDesigner.Tests;
|
|
|
|
public sealed class StarterTemplateLibraryTests : IDisposable
|
|
{
|
|
private readonly string _directory = Path.Combine(Path.GetTempPath(), $"starter-library-{Guid.NewGuid():N}");
|
|
public StarterTemplateLibraryTests() => Directory.CreateDirectory(_directory);
|
|
|
|
[Fact]
|
|
public void SpeichernUndAktualisieren_VerwendetStabileIdUndErsetztInhalt()
|
|
{
|
|
var library = new StarterTemplateLibrary(_directory);
|
|
var manifest = Manifest("briefkopf", "Briefkopf");
|
|
library.Save(manifest, "PAGE 210 297 mm\nTEXT 10 10 \"Version 1\"", EmptyAssets());
|
|
|
|
library.Save(manifest, "PAGE 210 297 mm\nTEXT 10 10 \"Version 2\"", EmptyAssets());
|
|
|
|
var item = Assert.Single(library.GetAll());
|
|
var (_, layout) = library.Load(item);
|
|
Assert.Equal("briefkopf", item.Id);
|
|
Assert.Contains("Version 2", layout);
|
|
Assert.DoesNotContain("Version 1", layout);
|
|
}
|
|
|
|
[Fact]
|
|
public void Duplizieren_ErzeugtUnabhaengigeIdUndLaesstOriginalUnveraendert()
|
|
{
|
|
var library = new StarterTemplateLibrary(_directory);
|
|
var original = library.Save(Manifest("briefkopf", "Briefkopf"),
|
|
"PAGE 210 297 mm", EmptyAssets());
|
|
|
|
var firstCopy = library.Duplicate(original);
|
|
var secondCopy = library.Duplicate(original);
|
|
|
|
Assert.Equal("briefkopf-kopie", firstCopy.Id);
|
|
Assert.Equal("briefkopf-kopie-2", secondCopy.Id);
|
|
Assert.Equal(3, library.GetAll().Count);
|
|
Assert.Equal("Briefkopf", library.Load(original).Template.Manifest.Name);
|
|
}
|
|
|
|
[Fact]
|
|
public void ImportUndExport_UebertragenPortablesPaket()
|
|
{
|
|
var sourceLibrary = new StarterTemplateLibrary(Path.Combine(_directory, "source"));
|
|
var targetLibrary = new StarterTemplateLibrary(Path.Combine(_directory, "target"));
|
|
var item = sourceLibrary.Save(Manifest("kopf", "Schulbriefkopf"), "PAGE 210 297 mm", EmptyAssets());
|
|
var exported = Path.Combine(_directory, "export.lavorlage");
|
|
|
|
sourceLibrary.Export(item, exported);
|
|
var imported = targetLibrary.Import(exported);
|
|
|
|
Assert.Equal("kopf", imported.Id);
|
|
Assert.Equal("Schulbriefkopf", Assert.Single(targetLibrary.GetAll()).Name);
|
|
}
|
|
|
|
[Fact]
|
|
public void AlsNeuesProjekt_BehaeltLayoutUndVergibtNeueMetadaten()
|
|
{
|
|
var library = new StarterTemplateLibrary(_directory);
|
|
var item = library.Save(Manifest("kopf", "Schulbriefkopf"),
|
|
"PAGE 210 297 mm\nTEXT 10 10 \"Schule\"", EmptyAssets());
|
|
var (template, layout) = library.Load(item);
|
|
var viewModel = new DesignerViewModel();
|
|
|
|
viewModel.LoadAsNewProject(template, layout);
|
|
|
|
Assert.Equal("kopf-neu", viewModel.TemplateId);
|
|
Assert.Equal("Schulbriefkopf - Neu", viewModel.TemplateName);
|
|
Assert.Contains("Schule", viewModel.LayoutSource);
|
|
Assert.Equal("kopf", library.Load(item).Template.Manifest.Id);
|
|
}
|
|
|
|
private static TemplateManifest Manifest(string id, string name) => new()
|
|
{ Id = id, Name = name, Description = "Wiederverwendbarer Ausgangspunkt" };
|
|
private static IReadOnlyDictionary<string, byte[]> EmptyAssets() => new Dictionary<string, byte[]>();
|
|
public void Dispose() { if (Directory.Exists(_directory)) Directory.Delete(_directory, true); }
|
|
}
|