chaokunyang commented on code in PR #2587:
URL: https://github.com/apache/fory/pull/2587#discussion_r2331940169
##########
go/fory/tests/generator_test.go:
##########
@@ -57,7 +72,125 @@ func TestValidationDemo(t *testing.T) {
assert.Equal(t, original.A, result.A, "Field A should match after
round-trip")
assert.Equal(t, original.B, result.B, "Field B should match after
round-trip")
assert.Equal(t, original.C, result.C, "Field C should match after
round-trip")
+ assert.Equal(t, original.D, result.D, "Field D should match after
round-trip")
+ assert.Equal(t, original.E, result.E, "Field E should match after
round-trip")
+ assert.Equal(t, original.F, result.F, "Field F should match after
round-trip")
+ assert.Equal(t, original.G, result.G, "Field G should match after
round-trip")
+ assert.Equal(t, original.H, result.H, "Field H should match after
round-trip")
+ assert.Equal(t, original.I, result.I, "Field I should match after
round-trip")
+ // For time.Time, compare Unix timestamps since timezone info is lost
during serialization
+ assert.Equal(t, original.J.Unix(), result.J.Unix(), "Field J timestamps
should match after round-trip")
- // 5. Validate data integrity
+ // 5. Validate data integrity (excluding time field due to timezone
differences)
+ // Create copies without time field for comparison
+ originalCopy := *original
+ resultCopy := *result
+ originalCopy.J = time.Time{}
+ resultCopy.J = time.Time{}
+ assert.EqualValues(t, &originalCopy, &resultCopy, "Complete struct
(except time) should match after round-trip")
+}
+
+func TestSimpleStruct(t *testing.T) {
+ // 1. Create test instance
+ original := &SimpleStruct{
+ ID: 42, // int field
+ Name: "Test User", // string field
+ }
+
+ // Validate original data structure
+ assert.Equal(t, 42, original.ID, "Original ID should be 42")
+ assert.Equal(t, "Test User", original.Name, "Original Name should be
'Test User'")
+
+ // 2. Serialize using generated code
+ f := fory.NewFory(true)
+ data, err := f.Marshal(original)
+ require.NoError(t, err, "Serialization should not fail")
+ require.NotEmpty(t, data, "Serialized data should not be empty")
+ assert.Greater(t, len(data), 0, "Serialized data should have positive
length")
+
+ // 3. Deserialize using generated code
+ var result *SimpleStruct
+ err = f.Unmarshal(data, &result)
+ require.NoError(t, err, "Deserialization should not fail")
+ require.NotNil(t, result, "Deserialized result should not be nil")
+
+ // 4. Validate round-trip serialization
+ assert.Equal(t, original.ID, result.ID, "Field ID should match after
round-trip")
+ assert.Equal(t, original.Name, result.Name, "Field Name should match
after round-trip")
+
+ // 5. Validate complete data integrity
assert.EqualValues(t, original, result, "Complete struct should match
after round-trip")
}
+
+func TestCompoundStruct(t *testing.T) {
+ // 1. Create test instance with nested structs
+ original := &CompoundStruct{
+ ValidationData: ValidationDemo{
+ A: 123,
+ B: "Nested ValidationDemo",
+ C: 789,
+ D: []int32{10, 20, 30},
+ E: []string{"nested", "test"},
+ F: []bool{true, false},
+ G: map[string]int32{"nested": 100},
+ H: map[int32]string{1: "nested_value"},
+ I: map[string]bool{"nested_flag": true},
+ J: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC),
+ },
+ SimpleData: SimpleStruct{
+ ID: 999,
+ Name: "Nested SimpleStruct",
+ },
+ Count: 42,
+ }
+
+ // Validate original data structure
+ assert.Equal(t, int32(123), original.ValidationData.A, "Nested
ValidationDemo.A should be 123")
+ assert.Equal(t, "Nested ValidationDemo", original.ValidationData.B,
"Nested ValidationDemo.B should be correct")
+ assert.Equal(t, 999, original.SimpleData.ID, "Nested SimpleStruct.ID
should be 999")
+ assert.Equal(t, "Nested SimpleStruct", original.SimpleData.Name,
"Nested SimpleStruct.Name should be correct")
+ assert.Equal(t, 42, original.Count, "Count should be 42")
+
+ // 2. Serialize using generated code
+ f := fory.NewFory(true)
+ data, err := f.Marshal(original)
+ require.NoError(t, err, "Serialization should not fail")
+ require.NotEmpty(t, data, "Serialized data should not be empty")
+ assert.Greater(t, len(data), 0, "Serialized data should have positive
length")
+
+ // 3. Deserialize using generated code
+ var result *CompoundStruct
+ err = f.Unmarshal(data, &result)
+ require.NoError(t, err, "Deserialization should not fail")
+ require.NotNil(t, result, "Deserialized result should not be nil")
+
+ // 4. Validate round-trip serialization for nested structs
+ assert.Equal(t, original.ValidationData.A, result.ValidationData.A,
"Nested ValidationDemo.A should match after round-trip")
Review Comment:
Could we use `==` directly instead of comparing by fields manually?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]