diff --git a/Firely.Fhir.Packages.Tests/ManifestSerializationTest.cs b/Firely.Fhir.Packages.Tests/ManifestSerializationTest.cs index 5b91852..f14d219 100644 --- a/Firely.Fhir.Packages.Tests/ManifestSerializationTest.cs +++ b/Firely.Fhir.Packages.Tests/ManifestSerializationTest.cs @@ -75,7 +75,6 @@ public void TestReadingAComplexAuthorProperty() { var json = File.ReadAllText($"TestData/unknown-properties-package.json"); var manifest = PackageParser.ParseManifest(json); - //manifest.AuthorInformation() manifest.Should().NotBeNull(); } @@ -86,7 +85,7 @@ public void TestReadingAComplexAuthorProperty() [DataTestMethod] public void TestAuthorStringParsing(string? fullAuthor, string? name, string? email, string? url) { - PackageManifest manif = new(name: "authot.test", version: "1.0.1") + PackageManifest manif = new(name: "author.test", version: "1.0.1") { Author = fullAuthor }; @@ -103,7 +102,7 @@ public void TestAuthorStringParsing(string? fullAuthor, string? name, string? em [DataTestMethod] public void TestAuthorStringSerializing(string? fullAuthor, string? name, string? email, string? url) { - PackageManifest manif = new(name: "authot.test", version: "1.0.1") + PackageManifest manif = new(name: "author.test", version: "1.0.1") { AuthorInformation = new() { diff --git a/Firely.Fhir.Packages.Tests/TestHelper.cs b/Firely.Fhir.Packages.Tests/TestHelper.cs index 1fc5dc3..ade45a8 100644 --- a/Firely.Fhir.Packages.Tests/TestHelper.cs +++ b/Firely.Fhir.Packages.Tests/TestHelper.cs @@ -97,14 +97,7 @@ static IEnumerable createDependencies(string[] dependencies) { foreach (var dep in dependencies) { - var splitDependency = dep.Split('@'); - if (splitDependency.Length == 1) - yield return new PackageDependency(dep, "latest"); - else - { - var versionDep = splitDependency[1]; - yield return new PackageDependency(splitDependency[0], versionDep); - } + yield return (PackageDependency)dep; } } } diff --git a/Firely.Fhir.Packages.Tests/VersionsTest.cs b/Firely.Fhir.Packages.Tests/VersionsTest.cs index c455e29..22ec238 100644 --- a/Firely.Fhir.Packages.Tests/VersionsTest.cs +++ b/Firely.Fhir.Packages.Tests/VersionsTest.cs @@ -1,3 +1,4 @@ +using FluentAssertions; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Firely.Fhir.Packages.Tests @@ -20,54 +21,60 @@ public void ParseReferences() Assert.AreEqual("hl7.fhir.r4.core", reference.Name); Assert.AreEqual("4.0.1", reference.Version); } - } - [TestClass] - public class VersionsTest - { [TestMethod] - public void Resolve_PackageDependencyExistsInVersionList_FixedVersionSearchFindsVersion() + public void ParseDependencie() { - var target = new Versions(new string[] { "1.0.0", "2.0.0" }); + var dependencie = (PackageDependency)"hl7.fhir.r4.core@4.0.1"; - PackageReference result = target.Resolve(new PackageDependency("SomeName", "1.0.0")); - - Assert.IsTrue(result.Found); - Assert.AreEqual("1.0.0", result.Version); - } - - [TestMethod] - public void Resolve_PackageDependencyExistsInVersionList_RangedVersionSearchFindsVersion() - { - var target = new Versions(new string[] { "1.0.0", "2.0.0" }); + Assert.AreEqual("hl7.fhir.r4.core", dependencie.Name); + Assert.AreEqual("4.0.1", dependencie.Range); - PackageReference result = target.Resolve(new PackageDependency("SomeName", "1.x")); + dependencie = "hl7.fhir.r4.core@4.0.1"; - Assert.IsTrue(result.Found); - Assert.AreEqual("1.0.0", result.Version); + Assert.AreEqual("hl7.fhir.r4.core", dependencie.Name); + Assert.AreEqual("4.0.1", dependencie.Range); } [TestMethod] - public void Resolve_PackageDependencyDoesNotExistInVersionList_FixedVersionSearchReturnsNotFound() + public void ParseDependencieWithoutVersion() { - var target = new Versions(new string[] { "1.0.0", "2.0.0" }); + var dependencie = (PackageDependency)"hl7.fhir.r4.core"; + + Assert.AreEqual("hl7.fhir.r4.core", dependencie.Name); + Assert.AreEqual("latest", dependencie.Range); - PackageReference result = target.Resolve(new PackageDependency("SomeName", "3.0.0")); + dependencie = "hl7.fhir.r4.core"; - Assert.IsFalse(result.Found); - Assert.IsTrue(result.NotFound); + Assert.AreEqual("hl7.fhir.r4.core", dependencie.Name); + Assert.AreEqual("latest", dependencie.Range); } + } - [TestMethod] - public void Resolve_PackageDependencyDoesNotExistInVersionList_RangedVersionSearchReturnsNotFound() + [TestClass] + public class VersionsTest + { + [DataRow("1.0.0", "1.0.0")] + [DataRow("1.x", "1.0.0")] + [DataRow("latest", "2.0.0")] + [DataRow(null, "2.0.0")] + [DataRow("3.0.0", null)] + [DataRow("3.x", null)] + [DataTestMethod] + public void ResolveVersionTest(string? version, string? versionReturned) { var target = new Versions(new string[] { "1.0.0", "2.0.0" }); - PackageReference result = target.Resolve(new PackageDependency("SomeName", "3.x")); + PackageReference result = target.Resolve(new PackageDependency("SomeName", version)); - Assert.IsFalse(result.Found); - Assert.IsTrue(result.NotFound); - } + bool found = versionReturned is not null; + result.Found.Should().Be(found); + result.NotFound.Should().NotBe(found); + if (found) + { + result.Version.Should().Be(versionReturned); + } + } } } diff --git a/Firely.Fhir.Packages/Models/PackageDependency.cs b/Firely.Fhir.Packages/Models/PackageDependency.cs index 63f8b18..914f552 100644 --- a/Firely.Fhir.Packages/Models/PackageDependency.cs +++ b/Firely.Fhir.Packages/Models/PackageDependency.cs @@ -11,17 +11,17 @@ namespace Firely.Fhir.Packages public struct PackageDependency { public string Name; - public string? Range; // 3.x, 3.1 - 3.3, 1.1 | 1.2 + public string Range; // 3.x, 3.1 - 3.3, 1.1 | 1.2 /// /// Initializes a new package dependency /// /// name of the package - /// the version range for a specific package + /// the version range for a specific package, when no version range is specified, the dependency will be on "latest" public PackageDependency(string name, string? range = null) { this.Name = name; - this.Range = range; + this.Range = range ?? "latest"; } /// @@ -39,7 +39,14 @@ public static implicit operator PackageDependency(KeyValuePair /// a defining a package name of a new package dependency public static implicit operator PackageDependency(string reference) { - return new PackageDependency(reference, null); // latest + var splitDependency = reference.Split('@'); + if (splitDependency.Length == 1) + return new PackageDependency(reference, null); + else + { + var versionDep = splitDependency[1]; + return new PackageDependency(splitDependency[0], versionDep); + } } /// @@ -48,8 +55,7 @@ public static implicit operator PackageDependency(string reference) /// A including the package name and the version public override string ToString() { - string? range = string.IsNullOrEmpty(Range) ? "(latest)" : Range; - return $"{Name} {range}"; + return $"{Name} ({Range})"; } } } diff --git a/Firely.Fhir.Packages/Source/FhirPackageSource.cs b/Firely.Fhir.Packages/Source/FhirPackageSource.cs index 22619c7..7b6cb9b 100644 --- a/Firely.Fhir.Packages/Source/FhirPackageSource.cs +++ b/Firely.Fhir.Packages/Source/FhirPackageSource.cs @@ -90,14 +90,7 @@ static IEnumerable createDependencies(string[] dependencies) { foreach (var dep in dependencies) { - var splitDependency = dep.Split('@'); - if (splitDependency.Length == 1) - yield return new PackageDependency(dep, "latest"); - else - { - var versionDep = splitDependency[1]; - yield return new PackageDependency(splitDependency[0], versionDep); - } + yield return (PackageDependency)dep; } } }