Skip to content

Commit

Permalink
test: source generation test
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed Jul 25, 2024
1 parent e0ee763 commit 80b411a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2035,31 +2035,6 @@ public async Task JsonIgnoreCondition_LastOneWins()
Assert.Null(obj.MyString);
}

[Fact]
public void JsonIgnoreCondition_WhenWriting()
{
var json = JsonSerializer.Serialize(new JsonIgnoreCondition_WhenReadingWritingTestModel{ Age = 10, Name="Mike" });
Assert.Equal("""{"Age":10}""", json);
}

[Fact]
public void JsonIgnoreCondition_WhenReading()
{
var json = """{"Age":10, "Name":"Mike"}""";
var model = JsonSerializer.Deserialize<JsonIgnoreCondition_WhenReadingWritingTestModel>(json);
Assert.Equal("Mike", model.Name);
Assert.Equal(0, model.Age);
}

public class JsonIgnoreCondition_WhenReadingWritingTestModel
{
[JsonIgnore(Condition = JsonIgnoreCondition.WhenReading)]
public int Age { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWriting)]
public string Name { get; set; }
}


[Fact]
public async Task ClassWithComplexObjectsUsingIgnoreWhenWritingDefaultAttribute()
{
Expand Down Expand Up @@ -2190,6 +2165,30 @@ public async Task IgnoreConditionWhenWritingDefault_WinsOver_IgnoreReadOnlyField
Assert.Equal(@"{}", json);
}

[Fact]
public async Task JsonIgnoreCondition_WhenWriting()
{
var json = await Serializer.SerializeWrapper(new JsonIgnoreCondition_WhenReadingWritingTestModel{ Age = 10, Name = "Mike" });
Assert.Equal("""{"Age":10}""", json);
}

[Fact]
public async Task JsonIgnoreCondition_WhenReading()
{
var json = """{"Age":10, "Name":"Mike"}""";
var model = await Serializer.DeserializeWrapper<JsonIgnoreCondition_WhenReadingWritingTestModel>(json);
Assert.Equal("Mike", model.Name);
Assert.Equal(0, model.Age);
}

public class JsonIgnoreCondition_WhenReadingWritingTestModel
{
[JsonIgnore(Condition = JsonIgnoreCondition.WhenReading)]
public int Age { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWriting)]
public string? Name { get; set; }
}

public class ClassWithReadOnlyStringProperty
{
public string MyString { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ public override async Task ClassWithIgnoredAndPrivateMembers_DoesNotIncludeIgnor
[JsonSerializable(typeof(ClassWithProperty_IgnoreConditionAlways_Ctor))]
[JsonSerializable(typeof(ClassWithClassProperty_IgnoreConditionWhenWritingDefault_Ctor))]
[JsonSerializable(typeof(StructWithStructProperty_IgnoreConditionWhenWritingDefault_Ctor))]
[JsonSerializable(typeof(JsonIgnoreCondition_WhenReadingWritingTestModel))]
[JsonSerializable(typeof(SmallStructWithValueAndReferenceTypes))]
[JsonSerializable(typeof(WrapperForClassWithIgnoredUnsupportedDictionary))]
[JsonSerializable(typeof(Class1))]
Expand Down Expand Up @@ -613,6 +614,7 @@ partial class DefaultContextWithGlobalIgnoreSetting : JsonSerializerContext;
[JsonSerializable(typeof(ClassWithProperty_IgnoreConditionAlways_Ctor))]
[JsonSerializable(typeof(ClassWithClassProperty_IgnoreConditionWhenWritingDefault_Ctor))]
[JsonSerializable(typeof(StructWithStructProperty_IgnoreConditionWhenWritingDefault_Ctor))]
[JsonSerializable(typeof(JsonIgnoreCondition_WhenReadingWritingTestModel))]
[JsonSerializable(typeof(SmallStructWithValueAndReferenceTypes))]
[JsonSerializable(typeof(WrapperForClassWithIgnoredUnsupportedDictionary))]
[JsonSerializable(typeof(Class1))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ public enum ModifyJsonIgnore
[InlineData(JsonIgnoreCondition.Never, ModifyJsonIgnore.DontModify)]
[InlineData(JsonIgnoreCondition.WhenWritingDefault, ModifyJsonIgnore.NeverSerialize)]
[InlineData(JsonIgnoreCondition.WhenWritingNull, ModifyJsonIgnore.NeverSerialize)]
[InlineData(JsonIgnoreCondition.WhenWriting, ModifyJsonIgnore.NeverSerialize)]
[InlineData(JsonIgnoreCondition.Never, ModifyJsonIgnore.NeverSerialize)]
[InlineData(JsonIgnoreCondition.WhenWritingDefault, ModifyJsonIgnore.AlwaysSerialize)]
[InlineData(JsonIgnoreCondition.WhenWritingNull, ModifyJsonIgnore.AlwaysSerialize)]
Expand Down

0 comments on commit 80b411a

Please sign in to comment.