Skip to content

Commit

Permalink
feat: more complete
Browse files Browse the repository at this point in the history
  • Loading branch information
futrime committed Jan 5, 2025
1 parent 9523030 commit b429aa6
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 40 deletions.
151 changes: 134 additions & 17 deletions Lip.Tests/PackageManifestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void FromBytes_FullInput_Parsed()
"name": "Test Package",
"description": "A test package",
"author": "Test Author",
"tags": ["test", "example"],
"tags": ["test", "example:tag"],
"avatar_url": "https://example.com/avatar.png"
},
"variants": [
Expand All @@ -57,7 +57,7 @@ public void FromBytes_FullInput_Parsed()
"urls": ["https://example.com/asset.zip"],
"place": [
{
"type": "directory",
"type": "dir",
"src": "src",
"dest": "dest"
}
Expand Down Expand Up @@ -94,7 +94,7 @@ public void FromBytes_FullInput_Parsed()
Assert.Equal("Test Package", manifest.Info.Name);
Assert.Equal("A test package", manifest.Info.Description);
Assert.Equal("Test Author", manifest.Info.Author);
Assert.Equal(new[] { "test", "example" }, manifest.Info.Tags);
Assert.Equal(new[] { "test", "example:tag" }, manifest.Info.Tags);
Assert.Equal("https://example.com/avatar.png", manifest.Info.AvatarUrl);

Assert.NotNull(manifest.Variants);
Expand All @@ -111,13 +111,13 @@ public void FromBytes_FullInput_Parsed()
Assert.NotNull(variant.Assets);
Assert.Single(variant.Assets);
PackageManifest.AssetType asset = variant.Assets[0];
Assert.Equal("zip", asset.Type);
Assert.Equal(PackageManifest.AssetType.TypeEnum.Zip, asset.Type);
Assert.Equal(new[] { "https://example.com/asset.zip" }, asset.Urls);

Assert.NotNull(asset.Place);
Assert.Single(asset.Place);
PackageManifest.PlaceType place = asset.Place[0];
Assert.Equal("directory", place.Type);
Assert.Equal(PackageManifest.PlaceType.TypeEnum.Dir, place.Type);
Assert.Equal("src", place.Src);
Assert.Equal("dest", place.Dest);

Expand Down Expand Up @@ -158,15 +158,49 @@ public void FromBytes_MissingRequiredField_ThrowsArgumentException()
Assert.Throws<JsonException>(() => PackageManifest.FromBytes(bytes));
}

[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(4)]
public void FromBytes_InvalidFormatVersion_ThrowsArgumentException(int version)
[Fact]
public void FromBytes_InvalidFieldType_ThrowsJsonException()
{
byte[] bytes = """
{
"format_version": "3",
"format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d",
"tooth": "test-tooth",
"version": 1
}
"""u8.ToArray();

Assert.Throws<JsonException>(() => PackageManifest.FromBytes(bytes));
}

[Fact]
public void FromBytes_AdditionalField_Parsed()
{
byte[] bytes = """
{
"format_version": 3,
"format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d",
"tooth": "test-tooth",
"version": "1.0.0",
"additional_field": "additional-value"
}
"""u8.ToArray();

var manifest = PackageManifest.FromBytes(bytes);

Assert.NotNull(manifest);
Assert.Equal(3, manifest.FormatVersion);
Assert.Equal("289f771f-2c9a-4d73-9f3f-8492495a924d", manifest.FormatUuid);
Assert.Equal("test-tooth", manifest.Tooth);
Assert.Equal("1.0.0", manifest.Version);
}

[Fact]
public void FromBytes_InvalidFormatVersion_ThrowsArgumentException()
{
byte[] bytes = Encoding.UTF8.GetBytes($$"""
{
"format_version": {{version}},
"format_version": 0,
"format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d",
"tooth": "test-tooth",
"version": "1.0.0"
Expand All @@ -192,22 +226,79 @@ public void FromBytes_InvalidFormatUuid_ThrowsArgumentException()
}

[Fact]
public void FromBytes_InvalidFieldType_ThrowsJsonException()
public void FromBytes_InvalidVersion_ThrowsArgumentException()
{
byte[] bytes = """
{
"format_version": "3",
"format_version": 3,
"format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d",
"tooth": "test-tooth",
"version": 1
"version": "invalid-version"
}
"""u8.ToArray();

Assert.Throws<JsonException>(() => PackageManifest.FromBytes(bytes));
Assert.Throws<ArgumentException>(() => PackageManifest.FromBytes(bytes));
}

[Fact]
public void FromBytes_InvalidTag_ThrowsJsonException()
{
byte[] bytes = """
{
"format_version": 3,
"format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d",
"tooth": "test-tooth",
"version": "1.0.0",
"info": {
"tags": ["invalid.tag"],
}
}
"""u8.ToArray();

Assert.Throws<ArgumentException>(() => PackageManifest.FromBytes(bytes));
}

[Fact]
public void FromBytes_ValidAdditionalScript_Parsed()
{
byte[] bytes = """
{
"format_version": 3,
"format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d",
"tooth": "test-tooth",
"version": "1.0.0",
"variants": [
{
"platform": "windows",
"scripts": {
"pre_install": ["echo pre-install"],
"custom_script": ["echo custom"]
}
}
]
}
"""u8.ToArray();

var manifest = PackageManifest.FromBytes(bytes);

Assert.NotNull(manifest);
Assert.Equal(3, manifest.FormatVersion);
Assert.Equal("289f771f-2c9a-4d73-9f3f-8492495a924d", manifest.FormatUuid);
Assert.Equal("test-tooth", manifest.Tooth);
Assert.Equal("1.0.0", manifest.Version);

Assert.NotNull(manifest.Variants);
Assert.Single(manifest.Variants);
PackageManifest.VariantType variant = manifest.Variants[0];
Assert.Equal("windows", variant.Platform);

Assert.NotNull(variant.Scripts);
Assert.Equal(new[] { "echo pre-install" }, variant.Scripts.PreInstall);
Assert.Equal(new[] { "echo custom" }, variant.Scripts.AdditionalScripts["custom_script"]);
}

[Fact]
public void FromBytes_InvalidAdditionalScripts_ThrowsJsonException()
public void FromBytes_InvalidAdditionalScriptKey_ThrowsJsonException()
{
byte[] bytes = """
{
Expand All @@ -220,7 +311,7 @@ public void FromBytes_InvalidAdditionalScripts_ThrowsJsonException()
"platform": "windows",
"scripts": {
"pre_install": ["echo pre-install"],
"invalid_script": "echo invalid"
"invalid-script": ["echo invalid"]
}
}
]
Expand All @@ -229,4 +320,30 @@ public void FromBytes_InvalidAdditionalScripts_ThrowsJsonException()

Assert.Throws<JsonException>(() => PackageManifest.FromBytes(bytes));
}

[Theory]
[InlineData("\"echo valid\"")]
[InlineData("[0]")]
public void FromBytes_InvalidAdditionalScriptValue_ThrowsJsonException(object invalidScript)
{
byte[] bytes = Encoding.UTF8.GetBytes($$"""
{
"format_version": 3,
"format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d",
"tooth": "test-tooth",
"version": "1.0.0",
"variants": [
{
"platform": "windows",
"scripts": {
"pre_install": ["echo pre-install"],
"invalid_script": {{invalidScript}}
}
}
]
}
""");

Assert.Throws<JsonException>(() => PackageManifest.FromBytes(bytes));
}
}
Loading

0 comments on commit b429aa6

Please sign in to comment.