Skip to content

Commit

Permalink
feat: add more strict validation
Browse files Browse the repository at this point in the history
  • Loading branch information
futrime committed Jan 15, 2025
1 parent 0c2b41e commit e056397
Show file tree
Hide file tree
Showing 12 changed files with 645 additions and 107 deletions.
30 changes: 15 additions & 15 deletions Lip.Tests/PackageLockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void FromBytes_MinimumJson_Passes()
""");

// Act
var lockFile = PackageLock.FromBytes(bytes);
var lockFile = PackageLock.FromJsonBytes(bytes);

// Assert
Assert.Equal(3, lockFile.FormatVersion);
Expand Down Expand Up @@ -48,7 +48,7 @@ public void FromBytes_MaximumJson_Passes()
],
"locks": [
{
"package": "test/package",
"tooth": "test/package",
"variant": "default",
"version": "1.0.0"
}
Expand All @@ -57,7 +57,7 @@ public void FromBytes_MaximumJson_Passes()
""");

// Act
var lockFile = PackageLock.FromBytes(bytes);
var lockFile = PackageLock.FromJsonBytes(bytes);

// Assert
Assert.Equal(3, lockFile.FormatVersion);
Expand All @@ -74,7 +74,7 @@ public void FromBytes_NullJson_ThrowsArgumentException()

// Act & Assert
ArgumentException exception = Assert.Throws<ArgumentException>(
"bytes", () => PackageLock.FromBytes(bytes));
"bytes", () => PackageLock.FromJsonBytes(bytes));
Assert.Equal("Failed to deserialize package manifest. (Parameter 'bytes')", exception.Message);
}

Expand All @@ -93,7 +93,7 @@ public void FromBytes_InvalidFormatVersion_ThrowsArgumentException()

// Act & Assert
ArgumentException exception = Assert.Throws<ArgumentException>(
"value", () => PackageLock.FromBytes(bytes));
"value", () => PackageLock.FromJsonBytes(bytes));
Assert.Equal("Format version '0' is not equal to 3. (Parameter 'value')", exception.Message);
}

Expand All @@ -112,7 +112,7 @@ public void FromBytes_InvalidFormatUuid_ThrowsArgumentException()

// Act & Assert
ArgumentException exception = Assert.Throws<ArgumentException>(
"value", () => PackageLock.FromBytes(bytes));
"value", () => PackageLock.FromJsonBytes(bytes));
Assert.Equal(
"Format UUID 'invalid-uuid' is not equal to 289f771f-2c9a-4d73-9f3f-8492495a924d. (Parameter 'value')",
exception.Message);
Expand All @@ -131,7 +131,7 @@ public void ToBytes_MinimumJson_Passes()
};

// Act
byte[] bytes = lockFile.ToBytes();
byte[] bytes = lockFile.ToJsonBytes();

// Assert
Assert.Equal("""
Expand All @@ -156,21 +156,21 @@ public void ToBytes_MaximumJson_Passes()
new() {
FormatVersion = 3,
FormatUuid = "289f771f-2c9a-4d73-9f3f-8492495a924d",
Tooth = "test/package",
ToothPath = "test/package",
Version = "1.0.0"
}
],
Locks = [
new() {
Package = "test/package",
Variant = "default",
ToothPath = "test/package",
VariantLabel = "default",
Version = "1.0.0"
}
]
};

// Act
byte[] bytes = lockFile.ToBytes();
byte[] bytes = lockFile.ToJsonBytes();

// Assert
Assert.Equal("""
Expand All @@ -187,7 +187,7 @@ public void ToBytes_MaximumJson_Passes()
],
"locks": [
{
"package": "test/package",
"tooth": "test/package",
"variant": "default",
"version": "1.0.0"
}
Expand All @@ -202,7 +202,7 @@ public void LockType_Deserialize_MinimumJson_Passes()
// Arrange
string json = """
{
"package": "test/package",
"tooth": "test/package",
"variant": "default",
"version": "1.0.0"
}
Expand All @@ -213,8 +213,8 @@ public void LockType_Deserialize_MinimumJson_Passes()

// Assert
Assert.NotNull(lockType);
Assert.Equal("test/package", lockType.Package);
Assert.Equal("default", lockType.Variant);
Assert.Equal("test/package", lockType.ToothPath);
Assert.Equal("default", lockType.VariantLabel);
Assert.Equal("1.0.0", lockType.Version);
}
}
58 changes: 29 additions & 29 deletions Lip.Tests/PackageManifestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public void VariantType_Deserialize_MaximumJson_Passes()

// Assert.
Assert.NotNull(variant);
Assert.Equal("", variant.Label);
Assert.Equal("", variant.VariantLabel);
Assert.Equal("", variant.Platform);
Assert.Equal([], variant.Dependencies);
Assert.Equal([], variant.Assets);
Expand All @@ -376,12 +376,12 @@ public void FromBytes_MinimumJson_Passes()
""");

// Act.
var manifest = PackageManifest.FromBytes(bytes);
var manifest = PackageManifest.FromJsonBytes(bytes);

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

Expand All @@ -401,12 +401,12 @@ public void FromBytes_MaximumJson_Passes()
""");

// Act.
var manifest = PackageManifest.FromBytes(bytes);
var manifest = PackageManifest.FromJsonBytes(bytes);

// Assert.
Assert.Equal(3, manifest.FormatVersion);
Assert.Equal("289f771f-2c9a-4d73-9f3f-8492495a924d", manifest.FormatUuid);
Assert.Equal("", manifest.Tooth);
Assert.Equal("", manifest.ToothPath);
Assert.Equal("1.0.0", manifest.Version);
Assert.NotNull(manifest.Info);
Assert.NotNull(manifest.Variants);
Expand All @@ -420,7 +420,7 @@ public void FromBytes_NullJson_ThrowsArgumentException()
byte[] bytes = Encoding.UTF8.GetBytes("null");

// Act.
ArgumentException exception = Assert.Throws<ArgumentException>("bytes", () => PackageManifest.FromBytes(bytes));
ArgumentException exception = Assert.Throws<ArgumentException>("bytes", () => PackageManifest.FromJsonBytes(bytes));

// Assert.
Assert.Equal("Failed to deserialize package manifest. (Parameter 'bytes')", exception.Message);
Expand All @@ -440,7 +440,7 @@ public void FromBytes_InvalidFormatVersion_ThrowsArgumentException()
""");

// Act.
ArgumentException exception = Assert.Throws<ArgumentException>("value", () => PackageManifest.FromBytes(bytes));
ArgumentException exception = Assert.Throws<ArgumentException>("value", () => PackageManifest.FromJsonBytes(bytes));

// Assert.
Assert.Equal("Format version '0' is not equal to 3. (Parameter 'value')", exception.Message);
Expand All @@ -460,7 +460,7 @@ public void FromBytes_InvalidFormatUuid_ThrowsArgumentException()
""");

// Act.
ArgumentException exception = Assert.Throws<ArgumentException>("value", () => PackageManifest.FromBytes(bytes));
ArgumentException exception = Assert.Throws<ArgumentException>("value", () => PackageManifest.FromJsonBytes(bytes));

// Assert.
Assert.Equal("Format UUID 'invalid-uuid' is not equal to 289f771f-2c9a-4d73-9f3f-8492495a924d. (Parameter 'value')", exception.Message);
Expand All @@ -480,7 +480,7 @@ public void FromBytes_InvalidVersion_ThrowsArgumentException()
""");

// Act.
ArgumentException exception = Assert.Throws<ArgumentException>("value", () => PackageManifest.FromBytes(bytes));
ArgumentException exception = Assert.Throws<ArgumentException>("value", () => PackageManifest.FromJsonBytes(bytes));

// Assert.
Assert.Equal("Version '0.0.0.0' is invalid. (Parameter 'value')", exception.Message);
Expand All @@ -494,7 +494,7 @@ public void GetSpecifiedVariant_NullVariants_ReturnsNull()
{
FormatVersion = 3,
FormatUuid = "289f771f-2c9a-4d73-9f3f-8492495a924d",
Tooth = "",
ToothPath = "",
Version = "1.0.0"
};
string variantLabel = "";
Expand All @@ -515,7 +515,7 @@ public void GetSpecifiedVariant_EmptyVariants_ReturnsNull()
{
FormatVersion = 3,
FormatUuid = "289f771f-2c9a-4d73-9f3f-8492495a924d",
Tooth = "",
ToothPath = "",
Version = "1.0.0",
Variants = []
};
Expand All @@ -541,13 +541,13 @@ public void GetSpecifiedVariant_SingleVariant_ReturnsVariant(
{
FormatVersion = 3,
FormatUuid = "289f771f-2c9a-4d73-9f3f-8492495a924d",
Tooth = "",
ToothPath = "",
Version = "1.0.0",
Variants =
[
new PackageManifest.VariantType
{
Label = manifestVariantLabel,
VariantLabel = manifestVariantLabel,
Platform = manifestPlatform,
}
]
Expand All @@ -558,7 +558,7 @@ public void GetSpecifiedVariant_SingleVariant_ReturnsVariant(

// Assert.
Assert.NotNull(variant);
Assert.Equal(variantLabel, variant.Label);
Assert.Equal(variantLabel, variant.VariantLabel);
Assert.Equal(platform, variant.Platform);
Assert.NotNull(variant.Dependencies);
Assert.Empty(variant.Dependencies);
Expand All @@ -585,7 +585,7 @@ public void GetSpecifiedVariant_SingleFullVariant_ReturnsVariant()
{
FormatVersion = 3,
FormatUuid = "289f771f-2c9a-4d73-9f3f-8492495a924d",
Tooth = "",
ToothPath = "",
Version = "1.0.0",
Variants =
[
Expand All @@ -604,7 +604,7 @@ public void GetSpecifiedVariant_SingleFullVariant_ReturnsVariant()

// Assert.
Assert.NotNull(variant);
Assert.Equal("", variant.Label);
Assert.Equal("", variant.VariantLabel);
Assert.Equal("platform", variant.Platform);
Assert.NotNull(variant.Dependencies);
Assert.Empty(variant.Dependencies);
Expand All @@ -631,7 +631,7 @@ public void GetSpecifiedVariant_SingleVariantWithScripts_ReturnsVariant()
{
FormatVersion = 3,
FormatUuid = "289f771f-2c9a-4d73-9f3f-8492495a924d",
Tooth = "",
ToothPath = "",
Version = "1.0.0",
Variants =
[
Expand Down Expand Up @@ -659,7 +659,7 @@ public void GetSpecifiedVariant_SingleVariantWithScripts_ReturnsVariant()

// Assert.
Assert.NotNull(variant);
Assert.Equal("", variant.Label);
Assert.Equal("", variant.VariantLabel);
Assert.Equal("platform", variant.Platform);
Assert.NotNull(variant.Dependencies);
Assert.Empty(variant.Dependencies);
Expand Down Expand Up @@ -692,13 +692,13 @@ public void GetSpecifiedVariant_WildcardOnlySingleVariant_ReturnsNull(
{
FormatVersion = 3,
FormatUuid = "289f771f-2c9a-4d73-9f3f-8492495a924d",
Tooth = "",
ToothPath = "",
Version = "1.0.0",
Variants =
[
new PackageManifest.VariantType
{
Label = manifestVariantLabel,
VariantLabel = manifestVariantLabel,
Platform = manifestPlatform,
}
]
Expand All @@ -723,13 +723,13 @@ public void GetSpecifiedVariant_MismatchedSingleVariant_ReturnsNull(
{
FormatVersion = 3,
FormatUuid = "289f771f-2c9a-4d73-9f3f-8492495a924d",
Tooth = "",
ToothPath = "",
Version = "1.0.0",
Variants =
[
new PackageManifest.VariantType
{
Label = manifestVariantLabel,
VariantLabel = manifestVariantLabel,
Platform = manifestPlatform,
}
]
Expand All @@ -750,7 +750,7 @@ public void GetSpecifiedVariant_MultipleVariants_ReturnsMergedVariant()
{
FormatVersion = 3,
FormatUuid = "289f771f-2c9a-4d73-9f3f-8492495a924d",
Tooth = "",
ToothPath = "",
Version = "1.0.0",
Variants =
[
Expand Down Expand Up @@ -826,7 +826,7 @@ public void GetSpecifiedVariant_MultipleVariants_ReturnsMergedVariant()

// Assert.
Assert.NotNull(variant);
Assert.Equal("", variant.Label);
Assert.Equal("", variant.VariantLabel);
Assert.Equal("platform", variant.Platform);
Assert.NotNull(variant.Dependencies);
Assert.Equal(2, variant.Dependencies.Count);
Expand Down Expand Up @@ -862,12 +862,12 @@ public void ToBytes_MinimumJson_Passes()
{
FormatVersion = 3,
FormatUuid = "289f771f-2c9a-4d73-9f3f-8492495a924d",
Tooth = "",
ToothPath = "",
Version = "1.0.0"
};

// Act.
byte[] bytes = manifest.ToBytes();
byte[] bytes = manifest.ToJsonBytes();

// Assert.
Assert.Equal("""
Expand All @@ -888,7 +888,7 @@ public void WithTemplateParsed_CommonInput_Passes()
{
FormatVersion = 3,
FormatUuid = "289f771f-2c9a-4d73-9f3f-8492495a924d",
Tooth = "",
ToothPath = "",
Version = "1.0.0",
Variants = [
new(){
Expand All @@ -908,7 +908,7 @@ public void WithTemplateParsed_CommonInput_Passes()
// Assert.
Assert.Equal(3, result.FormatVersion);
Assert.Equal("289f771f-2c9a-4d73-9f3f-8492495a924d", result.FormatUuid);
Assert.Equal("", result.Tooth);
Assert.Equal("", result.ToothPath);
Assert.Equal("1.0.0", result.Version);
Assert.NotNull(result.Variants);
Assert.Single(result.Variants);
Expand All @@ -925,7 +925,7 @@ public void WithTemplateParsed_InvalidTemplate_ThrowsArgumentException()
{
FormatVersion = 3,
FormatUuid = "289f771f-2c9a-4d73-9f3f-8492495a924d",
Tooth = "",
ToothPath = "",
Version = "1.0.0",
Variants = [
new(){
Expand Down
Loading

0 comments on commit e056397

Please sign in to comment.