File: tag_validation_test.go

package info (click to toggle)
golang-github-oschwald-maxminddb-golang-v2 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,132 kB
  • sloc: perl: 557; makefile: 3
file content (97 lines) | stat: -rw-r--r-- 2,467 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package decoder

import (
	"reflect"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestValidateTag(t *testing.T) {
	tests := []struct {
		name        string
		fieldName   string
		tag         string
		expectError bool
		description string
	}{
		{
			name:        "ValidTag",
			fieldName:   "TestField",
			tag:         "valid_field",
			expectError: false,
			description: "Valid tag should not error",
		},
		{
			name:        "IgnoreTag",
			fieldName:   "TestField",
			tag:         "-",
			expectError: false,
			description: "Ignore tag should not error",
		},
		{
			name:        "EmptyTag",
			fieldName:   "TestField",
			tag:         "",
			expectError: false,
			description: "Empty tag should not error",
		},
		{
			name:        "ComplexValidTag",
			fieldName:   "TestField",
			tag:         "some_complex_field_name_123",
			expectError: false,
			description: "Complex valid tag should not error",
		},
		{
			name:        "InvalidUTF8",
			fieldName:   "TestField",
			tag:         "field\xff\xfe",
			expectError: true,
			description: "Invalid UTF-8 should error",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// Create a mock struct field
			field := reflect.StructField{
				Name: tt.fieldName,
				Type: reflect.TypeFor[string](),
			}

			err := validateTag(field, tt.tag)

			if tt.expectError {
				require.Error(t, err, tt.description)
				assert.Contains(t, err.Error(), tt.fieldName, "Error should mention field name")
			} else {
				assert.NoError(t, err, tt.description)
			}
		})
	}
}

// TestTagValidationIntegration tests that tag validation works during field processing.
func TestTagValidationIntegration(t *testing.T) {
	// Test that makeStructFields processes tags without panicking
	// even when there are validation errors

	type TestStruct struct {
		ValidField   string `maxminddb:"valid"`
		IgnoredField string `maxminddb:"-"`
		NoTagField   string
	}

	// This should not panic even with invalid tags
	structType := reflect.TypeFor[TestStruct]()
	fields := makeStructFields(structType)

	// Verify that valid fields are still processed
	assert.Contains(t, fields.namedFields, "valid", "Valid field should be processed")
	assert.Contains(t, fields.namedFields, "NoTagField", "Field without tag should use field name")

	// The important thing is that it doesn't crash
	assert.NotNil(t, fields.namedFields, "Fields map should be created")
}