Approval-based snapshot testing library for Go (mirror)
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

test: more complex test

+1460
+85
__snapshots__/test_complex_j_s_o_n_structure.snap
··· 1 + --- 2 + title: Complex JSON Structure 3 + test_name: TestComplexJSONStructure 4 + file_path: 5 + func_name: 6 + version: 0.1.0 7 + --- 8 + map[string]interface{}{ 9 + "api": map[string]interface{}{ 10 + "version": "2.0", 11 + "endpoints": []interface{}{ 12 + map[string]interface{}{ 13 + "rate_limit": map[string]interface{}{ 14 + "requests": 100.0, 15 + "window": "1m", 16 + }, 17 + "responses": map[string]interface{}{ 18 + "200": map[string]interface{}{ 19 + "description": "Success", 20 + "schema": map[string]interface{}{ 21 + "type": "array", 22 + "items": map[string]interface{}{ 23 + "type": "object", 24 + "properties": map[string]interface{}{ 25 + "id": map[string]interface{}{ 26 + "type": "integer", 27 + }, 28 + "name": map[string]interface{}{ 29 + "type": "string", 30 + }, 31 + }, 32 + }, 33 + }, 34 + }, 35 + "401": map[string]interface{}{ 36 + "description": "Unauthorized", 37 + }, 38 + }, 39 + "path": "/users", 40 + "method": "GET", 41 + "auth_required": true, 42 + }, 43 + map[string]interface{}{ 44 + "auth_required": true, 45 + "rate_limit": map[string]interface{}{ 46 + "requests": 50.0, 47 + "window": "1m", 48 + }, 49 + "path": "/users/{id}", 50 + "method": "POST", 51 + }, 52 + }, 53 + "models": map[string]interface{}{ 54 + "User": map[string]interface{}{ 55 + "properties": map[string]interface{}{ 56 + "created_at": map[string]interface{}{ 57 + "type": "string", 58 + "format": "date-time", 59 + }, 60 + "roles": map[string]interface{}{ 61 + "type": "array", 62 + "items": map[string]interface{}{ 63 + "type": "string", 64 + }, 65 + }, 66 + "id": map[string]interface{}{ 67 + "type": "integer", 68 + }, 69 + "username": map[string]interface{}{ 70 + "type": "string", 71 + }, 72 + "email": map[string]interface{}{ 73 + "type": "string", 74 + "format": "email", 75 + }, 76 + }, 77 + "required": []interface{}{ 78 + "id", 79 + "username", 80 + "email", 81 + }, 82 + }, 83 + }, 84 + }, 85 + }
+88
__snapshots__/test_complex_nested_structure.snap
··· 1 + --- 2 + title: Complex Nested Structure 3 + test_name: TestComplexNestedStructure 4 + file_path: 5 + func_name: 6 + version: 0.1.0 7 + --- 8 + freeze_test.Post{ 9 + ID: 100, 10 + Title: "Introduction to Go Snapshot Testing", 11 + Content: "This is a comprehensive guide to snapshot testing in Go...", 12 + Author: freeze_test.User{ 13 + ID: 1, 14 + Username: "john_doe", 15 + Email: "john@example.com", 16 + Active: true, 17 + CreatedAt: time.Time{ 18 + wall: 0x0, 19 + ext: 63809375400, 20 + loc: (*time.Location)(nil), 21 + }, 22 + Roles: []string{ 23 + "admin", 24 + "moderator", 25 + "user", 26 + }, 27 + Metadata: map[string]interface{}{ 28 + "language": "en", 29 + "preferences": map[string]interface{}{ 30 + "email_frequency": "weekly", 31 + "notifications": true, 32 + }, 33 + "theme": "dark", 34 + "notifications": true, 35 + }, 36 + }, 37 + Tags: []string{ 38 + "go", 39 + "testing", 40 + "snapshots", 41 + "best-practices", 42 + }, 43 + Comments: []freeze_test.Comment{ 44 + { 45 + ID: 1, 46 + Author: "alice", 47 + Content: "Great post!", 48 + CreatedAt: time.Time{ 49 + wall: 0x0, 50 + ext: 63810858120, 51 + loc: (*time.Location)(nil), 52 + }, 53 + Replies: []freeze_test.Comment{ 54 + { 55 + ID: 2, 56 + Author: "bob", 57 + Content: "I agree!", 58 + CreatedAt: time.Time{ 59 + wall: 0x0, 60 + ext: 63810863100, 61 + loc: (*time.Location)(nil), 62 + }, 63 + Replies: []freeze_test.Comment{ 64 + }, 65 + }, 66 + }, 67 + }, 68 + { 69 + ID: 3, 70 + Author: "charlie", 71 + Content: "Thanks for sharing!", 72 + CreatedAt: time.Time{ 73 + wall: 0x0, 74 + ext: 63810927000, 75 + loc: (*time.Location)(nil), 76 + }, 77 + Replies: []freeze_test.Comment{ 78 + }, 79 + }, 80 + }, 81 + Likes: 42, 82 + Published: true, 83 + CreatedAt: time.Time{ 84 + wall: 0x0, 85 + ext: 63809802000, 86 + loc: (*time.Location)(nil), 87 + }, 88 + }
+16
__snapshots__/test_deeply_nested_j_s_o_n.snap
··· 1 + --- 2 + title: Deeply Nested JSON 3 + test_name: TestDeeplyNestedJSON 4 + file_path: 5 + func_name: 6 + version: 0.1.0 7 + --- 8 + map[string]interface{}{ 9 + "L2": map[string]interface{}{ 10 + "L3": map[string]interface{}{ 11 + "L4": map[string]interface{}{ 12 + "Value": "deep value", 13 + }, 14 + }, 15 + }, 16 + }
+24
__snapshots__/test_go_struct_marshalled_to_j_s_o_n.snap
··· 1 + --- 2 + title: Go Struct Marshalled to JSON 3 + test_name: TestGoStructMarshalledToJSON 4 + file_path: 5 + func_name: 6 + version: 0.1.0 7 + --- 8 + map[string]interface{}{ 9 + "name": "Jane Smith", 10 + "email": "jane@example.com", 11 + "phone": "+1-555-0123", 12 + "address": map[string]interface{}{ 13 + "street": "456 Oak Ave", 14 + "city": "San Francisco", 15 + "zip": "94102", 16 + }, 17 + "tags": []interface{}{ 18 + "vip", 19 + "verified", 20 + "premium", 21 + }, 22 + "active": true, 23 + "created_at": "2023-06-15T14:30:00Z", 24 + }
+35
__snapshots__/test_j_s_o_n_array_of_objects.snap
··· 1 + --- 2 + title: JSON Array of Objects 3 + test_name: TestJSONArrayOfObjects 4 + file_path: 5 + func_name: 6 + version: 0.1.0 7 + --- 8 + []interface{}{ 9 + map[string]interface{}{ 10 + "type": "user", 11 + "id": 1.0, 12 + "data": map[string]interface{}{ 13 + "role": "admin", 14 + "name": "Alice", 15 + }, 16 + }, 17 + map[string]interface{}{ 18 + "type": "post", 19 + "id": 100.0, 20 + "data": map[string]interface{}{ 21 + "likes": 42.0, 22 + "title": "First Post", 23 + "author_id": 1.0, 24 + }, 25 + }, 26 + map[string]interface{}{ 27 + "type": "comment", 28 + "id": 500.0, 29 + "data": map[string]interface{}{ 30 + "post_id": 100.0, 31 + "content": "Great post!", 32 + "author_id": 2.0, 33 + }, 34 + }, 35 + }
+25
__snapshots__/test_j_s_o_n_numbers.snap
··· 1 + --- 2 + title: JSON Numbers 3 + test_name: TestJSONNumbers 4 + file_path: 5 + func_name: 6 + version: 0.1.0 7 + --- 8 + map[string]interface{}{ 9 + "floats": map[string]interface{}{ 10 + "pi": 3.14159265359, 11 + "scientific": 0.000123, 12 + "negative_float": -42.5, 13 + "small": 0.0001, 14 + }, 15 + "edge_cases": map[string]interface{}{ 16 + "one": 1.0, 17 + "minus_one": -1.0, 18 + }, 19 + "integers": map[string]interface{}{ 20 + "zero": 0.0, 21 + "positive": 42.0, 22 + "negative": -100.0, 23 + "large": 9.999999999999e+12.0, 24 + }, 25 + }
+27
__snapshots__/test_j_s_o_n_object.snap
··· 1 + --- 2 + title: JSON Object 3 + test_name: TestJSONObject 4 + file_path: 5 + func_name: 6 + version: 0.1.0 7 + --- 8 + map[string]interface{}{ 9 + "user": map[string]interface{}{ 10 + "roles": []interface{}{ 11 + "user", 12 + "admin", 13 + }, 14 + "created_at": "2023-01-15T10:30:00Z", 15 + "id": 1.0, 16 + "username": "john_doe", 17 + "email": "john@example.com", 18 + "profile": map[string]interface{}{ 19 + "first_name": "John", 20 + "last_name": "Doe", 21 + "bio": "Software engineer", 22 + "verified": true, 23 + }, 24 + }, 25 + "status": "success", 26 + "message": nil, 27 + }
+55
__snapshots__/test_j_s_o_n_with_mixed_arrays.snap
··· 1 + --- 2 + title: JSON with Mixed Arrays 3 + test_name: TestJSONWithMixedArrays 4 + file_path: 5 + func_name: 6 + version: 0.1.0 7 + --- 8 + map[string]interface{}{ 9 + "heterogeneous_array": []interface{}{ 10 + "string", 11 + 42.0, 12 + 3.14, 13 + true, 14 + nil, 15 + map[string]interface{}{ 16 + "object": "value", 17 + }, 18 + []interface{}{ 19 + 1.0, 20 + 2.0, 21 + 3.0, 22 + }, 23 + }, 24 + "matrix": []interface{}{ 25 + []interface{}{ 26 + 1.0, 27 + 2.0, 28 + 3.0, 29 + }, 30 + []interface{}{ 31 + 4.0, 32 + 5.0, 33 + 6.0, 34 + }, 35 + []interface{}{ 36 + 7.0, 37 + 8.0, 38 + 9.0, 39 + }, 40 + }, 41 + "object_array": []interface{}{ 42 + map[string]interface{}{ 43 + "id": 1.0, 44 + "name": "Item 1", 45 + }, 46 + map[string]interface{}{ 47 + "name": "Item 2", 48 + "id": 2.0, 49 + }, 50 + map[string]interface{}{ 51 + "name": "Item 3", 52 + "id": 3.0, 53 + }, 54 + }, 55 + }
+17
__snapshots__/test_j_s_o_n_with_special_characters.snap
··· 1 + --- 2 + title: JSON with Special Characters 3 + test_name: TestJSONWithSpecialCharacters 4 + file_path: 5 + func_name: 6 + version: 0.1.0 7 + --- 8 + map[string]interface{}{ 9 + "tabs": "col1\tcol2\tcol3", 10 + "backslash": "path\\to\\file", 11 + "english": "Hello, World!", 12 + "unicode": "こんにちは 世界 🌍", 13 + "emoji": "😀 😃 😄 😁 😆", 14 + "special_chars": "!@#$%^&*()_+-=[]{}|;:,.<>?", 15 + "escaped": "quotes: \"double\" and 'single'", 16 + "newlines": "line1\nline2\rline3\r\nline4", 17 + }
+31
__snapshots__/test_j_s_o_n_with_various_types.snap
··· 1 + --- 2 + title: JSON with Various Types 3 + test_name: TestJSONWithVariousTypes 4 + file_path: 5 + func_name: 6 + version: 0.1.0 7 + --- 8 + map[string]interface{}{ 9 + "float": 3.14159, 10 + "boolean_true": true, 11 + "null_value": nil, 12 + "array": []interface{}{ 13 + 1.0, 14 + 2.0, 15 + 3.0, 16 + "four", 17 + 5.5, 18 + }, 19 + "empty_array": []interface{}{ 20 + }, 21 + "empty_object": map[string]interface{}{ 22 + }, 23 + "escaped_string": "line1\nline2\ttab", 24 + "string": "hello world", 25 + "integer": 42.0, 26 + "boolean_false": false, 27 + "object": map[string]interface{}{ 28 + "nested": "value", 29 + "count": 10.0, 30 + }, 31 + }
+55
__snapshots__/test_large_j_s_o_n.snap
··· 1 + --- 2 + title: Large JSON Structure 3 + test_name: TestLargeJSON 4 + file_path: 5 + func_name: 6 + version: 0.1.0 7 + --- 8 + map[string]interface{}{ 9 + "total": 1179.97, 10 + "status": "shipped", 11 + "created_at": "2023-01-28T14:30:00Z", 12 + "shipped_at": "2023-02-01T10:00:00Z", 13 + "delivered_at": nil, 14 + "id": 1001.0, 15 + "customer_id": 42.0, 16 + "products": []interface{}{ 17 + map[string]interface{}{ 18 + "price": 999.99, 19 + "stock": 5.0, 20 + "in_stock": true, 21 + "tags": []interface{}{ 22 + "electronics", 23 + "computers", 24 + "laptops", 25 + }, 26 + "id": 1.0, 27 + "name": "Laptop", 28 + "description": "High-performance laptop", 29 + }, 30 + map[string]interface{}{ 31 + "name": "Mouse", 32 + "description": "Wireless mouse", 33 + "price": 29.99, 34 + "stock": 50.0, 35 + "in_stock": true, 36 + "tags": []interface{}{ 37 + "electronics", 38 + "accessories", 39 + }, 40 + "id": 2.0, 41 + }, 42 + map[string]interface{}{ 43 + "description": "Mechanical keyboard", 44 + "price": 149.99, 45 + "stock": 0.0, 46 + "in_stock": false, 47 + "tags": []interface{}{ 48 + "electronics", 49 + "accessories", 50 + }, 51 + "id": 3.0, 52 + "name": "Keyboard", 53 + }, 54 + }, 55 + }
+65
__snapshots__/test_multiple_complex_structures.snap
··· 1 + --- 2 + title: Multiple Complex Structures 3 + test_name: TestMultipleComplexStructures 4 + file_path: 5 + func_name: 6 + version: 0.1.0 7 + --- 8 + []freeze_test.User{ 9 + { 10 + ID: 1, 11 + Username: "alice", 12 + Email: "alice@example.com", 13 + Active: true, 14 + CreatedAt: time.Time{ 15 + wall: 0x0, 16 + ext: 0, 17 + loc: (*time.Location)(nil), 18 + }, 19 + Roles: []string{ 20 + "user", 21 + "moderator", 22 + }, 23 + Metadata: map[string]interface{}{ 24 + "verified": true, 25 + "badge": "verified", 26 + }, 27 + }, 28 + { 29 + ID: 2, 30 + Username: "bob", 31 + Email: "bob@example.com", 32 + Active: false, 33 + CreatedAt: time.Time{ 34 + wall: 0x0, 35 + ext: 0, 36 + loc: (*time.Location)(nil), 37 + }, 38 + Roles: []string{ 39 + "user", 40 + }, 41 + Metadata: map[string]interface{}{ 42 + "verified": false, 43 + "avatar": "https://example.com/bob.jpg", 44 + }, 45 + }, 46 + { 47 + ID: 3, 48 + Username: "charlie", 49 + Email: "charlie@example.com", 50 + Active: true, 51 + CreatedAt: time.Time{ 52 + wall: 0x0, 53 + ext: 0, 54 + loc: (*time.Location)(nil), 55 + }, 56 + Roles: []string{ 57 + "user", 58 + "admin", 59 + }, 60 + Metadata: map[string]interface{}{ 61 + "verified": true, 62 + "account_age_days": 365, 63 + }, 64 + }, 65 + }
+52
__snapshots__/test_nested_maps_and_slices.snap
··· 1 + --- 2 + title: Nested Maps and Slices 3 + test_name: TestNestedMapsAndSlices 4 + file_path: 5 + func_name: 6 + version: 0.1.0 7 + --- 8 + map[string]interface{}{ 9 + "users": map[string]interface{}{ 10 + "active": []map[string]interface{}{ 11 + { 12 + "id": 1, 13 + "name": "Alice", 14 + "verified": true, 15 + }, 16 + { 17 + "id": 2, 18 + "name": "Bob", 19 + "verified": false, 20 + }, 21 + }, 22 + "inactive": []map[string]interface{}{ 23 + { 24 + "id": 3, 25 + "name": "Charlie", 26 + }, 27 + }, 28 + }, 29 + "posts": map[string]interface{}{ 30 + "published": 42, 31 + "drafts": 5, 32 + "categories": []string{ 33 + "tech", 34 + "lifestyle", 35 + "news", 36 + }, 37 + }, 38 + "stats": map[string]interface{}{ 39 + "daily": map[string]interface{}{ 40 + "views": 1500, 41 + "clicks": 320, 42 + "conversions": map[string]interface{}{ 43 + "total": 45, 44 + "by_source": map[string]int{ 45 + "paid": 15, 46 + "referral": 5, 47 + "organic": 25, 48 + }, 49 + }, 50 + }, 51 + }, 52 + }
+39
__snapshots__/test_structure_with_empty_values.snap
··· 1 + --- 2 + title: Structure with Empty Values 3 + test_name: TestStructureWithEmptyValues 4 + file_path: 5 + func_name: 6 + version: 0.1.0 7 + --- 8 + []freeze_test.Container{ 9 + { 10 + Items: []string{ 11 + }, 12 + Tags: map[string]string{ 13 + }, 14 + OptionalID: (*int)(nil), 15 + Count: 0, 16 + Active: false, 17 + }, 18 + { 19 + Items: []string(nil), 20 + Tags: map[string]string(nil), 21 + OptionalID: (*int)(nil), 22 + Count: 0, 23 + Active: true, 24 + }, 25 + { 26 + Items: []string{ 27 + "a", 28 + "b", 29 + "c", 30 + }, 31 + Tags: map[string]string{ 32 + "type": "test", 33 + "env": "dev", 34 + }, 35 + OptionalID: &int(42), 36 + Count: 3, 37 + Active: true, 38 + }, 39 + }
+77
__snapshots__/test_structure_with_interface.snap
··· 1 + --- 2 + title: Structure with Interface Fields 3 + test_name: TestStructureWithInterface 4 + file_path: 5 + func_name: 6 + version: 0.1.0 7 + --- 8 + []freeze_test.Response{ 9 + { 10 + Status: "success", 11 + Message: "User retrieved", 12 + Data: freeze_test.User{ 13 + ID: 1, 14 + Username: "john", 15 + Email: "john@example.com", 16 + Active: true, 17 + CreatedAt: time.Time{ 18 + wall: 0x0, 19 + ext: 0, 20 + loc: (*time.Location)(nil), 21 + }, 22 + Roles: []string(nil), 23 + Metadata: map[string]interface{}(nil), 24 + }, 25 + Meta: map[string]interface{}{ 26 + "request_id": "req-123", 27 + "timestamp": "2023-01-20T10:30:00Z", 28 + }, 29 + }, 30 + { 31 + Status: "error", 32 + Message: "User not found", 33 + Data: nil, 34 + Meta: map[string]interface{}{ 35 + "error_code": 404, 36 + "error_type": "NOT_FOUND", 37 + }, 38 + }, 39 + { 40 + Status: "success", 41 + Message: "Posts retrieved", 42 + Data: []freeze_test.Post{ 43 + { 44 + ID: 1, 45 + Title: "First Post", 46 + Content: "", 47 + Author: freeze_test.User{ 48 + ID: 0, 49 + Username: "", 50 + Email: "", 51 + Active: false, 52 + CreatedAt: time.Time{ 53 + wall: 0x0, 54 + ext: 0, 55 + loc: (*time.Location)(nil), 56 + }, 57 + Roles: []string(nil), 58 + Metadata: map[string]interface{}(nil), 59 + }, 60 + Tags: []string(nil), 61 + Comments: []freeze_test.Comment(nil), 62 + Likes: 0, 63 + Published: true, 64 + CreatedAt: time.Time{ 65 + wall: 0x0, 66 + ext: 0, 67 + loc: (*time.Location)(nil), 68 + }, 69 + }, 70 + }, 71 + Meta: map[string]interface{}{ 72 + "total_count": 10, 73 + "page": 1, 74 + "per_page": 20, 75 + }, 76 + }, 77 + }
+28
__snapshots__/test_structure_with_pointers.snap
··· 1 + --- 2 + title: Structure with Pointers 3 + test_name: TestStructureWithPointers 4 + file_path: 5 + func_name: 6 + version: 0.1.0 7 + --- 8 + freeze_test.Person{ 9 + Name: "John", 10 + Age: 35, 11 + Address: &freeze_test.Address{ 12 + Street: "123 Main St", 13 + City: "Boston", 14 + Zip: "02101", 15 + }, 16 + Manager: &freeze_test.Person{ 17 + Name: "Jane", 18 + Age: 30, 19 + Address: (*freeze_test.Address)(<already shown>), 20 + Manager: (*freeze_test.Person)(nil), 21 + Friends: []*freeze_test.Person(nil), 22 + Email: &string("jane@example.com"), 23 + }, 24 + Friends: []*freeze_test.Person{ 25 + (*freeze_test.Person)(<already shown>), 26 + }, 27 + Email: (*string)(nil), 28 + }
+741
freeze_test.go
··· 1 1 package freeze_test 2 2 3 3 import ( 4 + "encoding/json" 4 5 "os" 5 6 "path/filepath" 6 7 "strings" 7 8 "testing" 9 + "time" 8 10 9 11 "github.com/ptdewey/freeze" 10 12 "github.com/ptdewey/freeze/internal/api" ··· 207 209 snapshotDir := filepath.Join(cwd, "__snapshots__") 208 210 _ = os.RemoveAll(snapshotDir) 209 211 } 212 + 213 + // ============================================================================ 214 + // COMPLEX GO STRUCTURES TESTS 215 + // ============================================================================ 216 + 217 + // User represents a user in a system 218 + type User struct { 219 + ID int 220 + Username string 221 + Email string 222 + Active bool 223 + CreatedAt time.Time 224 + Roles []string 225 + Metadata map[string]interface{} 226 + } 227 + 228 + // Post represents a blog post 229 + type Post struct { 230 + ID int 231 + Title string 232 + Content string 233 + Author User 234 + Tags []string 235 + Comments []Comment 236 + Likes int 237 + Published bool 238 + CreatedAt time.Time 239 + } 240 + 241 + // Comment represents a comment on a post 242 + type Comment struct { 243 + ID int 244 + Author string 245 + Content string 246 + CreatedAt time.Time 247 + Replies []Comment 248 + } 249 + 250 + // TestComplexNestedStructure tests snapshot with deeply nested Go structures 251 + func TestComplexNestedStructure(t *testing.T) { 252 + user := User{ 253 + ID: 1, 254 + Username: "john_doe", 255 + Email: "john@example.com", 256 + Active: true, 257 + CreatedAt: time.Date(2023, 1, 15, 10, 30, 0, 0, time.UTC), 258 + Roles: []string{"admin", "moderator", "user"}, 259 + Metadata: map[string]interface{}{ 260 + "theme": "dark", 261 + "notifications": true, 262 + "language": "en", 263 + "preferences": map[string]interface{}{ 264 + "email_frequency": "weekly", 265 + "notifications": true, 266 + }, 267 + }, 268 + } 269 + 270 + comments := []Comment{ 271 + { 272 + ID: 1, 273 + Author: "alice", 274 + Content: "Great post!", 275 + CreatedAt: time.Date(2023, 2, 1, 14, 22, 0, 0, time.UTC), 276 + Replies: []Comment{ 277 + { 278 + ID: 2, 279 + Author: "bob", 280 + Content: "I agree!", 281 + CreatedAt: time.Date(2023, 2, 1, 15, 45, 0, 0, time.UTC), 282 + Replies: []Comment{}, 283 + }, 284 + }, 285 + }, 286 + { 287 + ID: 3, 288 + Author: "charlie", 289 + Content: "Thanks for sharing!", 290 + CreatedAt: time.Date(2023, 2, 2, 9, 30, 0, 0, time.UTC), 291 + Replies: []Comment{}, 292 + }, 293 + } 294 + 295 + post := Post{ 296 + ID: 100, 297 + Title: "Introduction to Go Snapshot Testing", 298 + Content: "This is a comprehensive guide to snapshot testing in Go...", 299 + Author: user, 300 + Tags: []string{"go", "testing", "snapshots", "best-practices"}, 301 + Comments: comments, 302 + Likes: 42, 303 + Published: true, 304 + CreatedAt: time.Date(2023, 1, 20, 9, 0, 0, 0, time.UTC), 305 + } 306 + 307 + freeze.Snap(t, "Complex Nested Structure", post) 308 + } 309 + 310 + // TestMultipleComplexStructures tests snapshot with multiple complex structures 311 + func TestMultipleComplexStructures(t *testing.T) { 312 + users := []User{ 313 + { 314 + ID: 1, 315 + Username: "alice", 316 + Email: "alice@example.com", 317 + Active: true, 318 + Roles: []string{"user", "moderator"}, 319 + Metadata: map[string]interface{}{ 320 + "verified": true, 321 + "badge": "verified", 322 + }, 323 + }, 324 + { 325 + ID: 2, 326 + Username: "bob", 327 + Email: "bob@example.com", 328 + Active: false, 329 + Roles: []string{"user"}, 330 + Metadata: map[string]interface{}{ 331 + "verified": false, 332 + "avatar": "https://example.com/bob.jpg", 333 + }, 334 + }, 335 + { 336 + ID: 3, 337 + Username: "charlie", 338 + Email: "charlie@example.com", 339 + Active: true, 340 + Roles: []string{"user", "admin"}, 341 + Metadata: map[string]interface{}{ 342 + "verified": true, 343 + "account_age_days": 365, 344 + }, 345 + }, 346 + } 347 + 348 + freeze.Snap(t, "Multiple Complex Structures", users) 349 + } 350 + 351 + // TestStructureWithInterface tests structures containing interface{} fields 352 + func TestStructureWithInterface(t *testing.T) { 353 + type Response struct { 354 + Status string 355 + Message string 356 + Data interface{} 357 + Meta map[string]interface{} 358 + } 359 + 360 + responses := []Response{ 361 + { 362 + Status: "success", 363 + Message: "User retrieved", 364 + Data: User{ 365 + ID: 1, 366 + Username: "john", 367 + Email: "john@example.com", 368 + Active: true, 369 + }, 370 + Meta: map[string]interface{}{ 371 + "request_id": "req-123", 372 + "timestamp": "2023-01-20T10:30:00Z", 373 + }, 374 + }, 375 + { 376 + Status: "error", 377 + Message: "User not found", 378 + Data: nil, 379 + Meta: map[string]interface{}{ 380 + "error_code": 404, 381 + "error_type": "NOT_FOUND", 382 + }, 383 + }, 384 + { 385 + Status: "success", 386 + Message: "Posts retrieved", 387 + Data: []Post{ 388 + { 389 + ID: 1, 390 + Title: "First Post", 391 + Published: true, 392 + }, 393 + }, 394 + Meta: map[string]interface{}{ 395 + "total_count": 10, 396 + "page": 1, 397 + "per_page": 20, 398 + }, 399 + }, 400 + } 401 + 402 + freeze.Snap(t, "Structure with Interface Fields", responses) 403 + } 404 + 405 + // TestNestedMapsAndSlices tests complex nested maps and slices 406 + func TestNestedMapsAndSlices(t *testing.T) { 407 + complexData := map[string]interface{}{ 408 + "users": map[string]interface{}{ 409 + "active": []map[string]interface{}{ 410 + { 411 + "id": 1, 412 + "name": "Alice", 413 + "verified": true, 414 + }, 415 + { 416 + "id": 2, 417 + "name": "Bob", 418 + "verified": false, 419 + }, 420 + }, 421 + "inactive": []map[string]interface{}{ 422 + { 423 + "id": 3, 424 + "name": "Charlie", 425 + }, 426 + }, 427 + }, 428 + "posts": map[string]interface{}{ 429 + "published": 42, 430 + "drafts": 5, 431 + "categories": []string{"tech", "lifestyle", "news"}, 432 + }, 433 + "stats": map[string]interface{}{ 434 + "daily": map[string]interface{}{ 435 + "views": 1500, 436 + "clicks": 320, 437 + "conversions": map[string]interface{}{ 438 + "total": 45, 439 + "by_source": map[string]int{ 440 + "organic": 25, 441 + "paid": 15, 442 + "referral": 5, 443 + }, 444 + }, 445 + }, 446 + }, 447 + } 448 + 449 + freeze.Snap(t, "Nested Maps and Slices", complexData) 450 + } 451 + 452 + // TestStructureWithPointers tests structures with pointer fields 453 + func TestStructureWithPointers(t *testing.T) { 454 + type Address struct { 455 + Street string 456 + City string 457 + Zip string 458 + } 459 + 460 + type Person struct { 461 + Name string 462 + Age int 463 + Address *Address 464 + Manager *Person 465 + Friends []*Person 466 + Email *string 467 + } 468 + 469 + addr1 := &Address{ 470 + Street: "123 Main St", 471 + City: "Boston", 472 + Zip: "02101", 473 + } 474 + 475 + email := "jane@example.com" 476 + 477 + person1 := Person{ 478 + Name: "Jane", 479 + Age: 30, 480 + Address: addr1, 481 + Email: &email, 482 + } 483 + 484 + person2 := Person{ 485 + Name: "John", 486 + Age: 35, 487 + Address: addr1, 488 + Manager: &person1, 489 + Friends: []*Person{&person1}, 490 + } 491 + 492 + freeze.Snap(t, "Structure with Pointers", person2) 493 + } 494 + 495 + // TestStructureWithEmptyValues tests structures with empty slices, maps, nil values 496 + func TestStructureWithEmptyValues(t *testing.T) { 497 + type Container struct { 498 + Items []string 499 + Tags map[string]string 500 + OptionalID *int 501 + Count int 502 + Active bool 503 + } 504 + 505 + containers := []Container{ 506 + { 507 + Items: []string{}, 508 + Tags: map[string]string{}, 509 + OptionalID: nil, 510 + Count: 0, 511 + Active: false, 512 + }, 513 + { 514 + Items: nil, 515 + Tags: nil, 516 + OptionalID: nil, 517 + Count: 0, 518 + Active: true, 519 + }, 520 + { 521 + Items: []string{"a", "b", "c"}, 522 + Tags: map[string]string{"type": "test", "env": "dev"}, 523 + OptionalID: intPtr(42), 524 + Count: 3, 525 + Active: true, 526 + }, 527 + } 528 + 529 + freeze.Snap(t, "Structure with Empty Values", containers) 530 + } 531 + 532 + // ============================================================================ 533 + // JSON OBJECT TESTS 534 + // ============================================================================ 535 + 536 + // TestJSONObject tests snapshot with JSON objects 537 + func TestJSONObject(t *testing.T) { 538 + jsonStr := `{ 539 + "user": { 540 + "id": 1, 541 + "username": "john_doe", 542 + "email": "john@example.com", 543 + "profile": { 544 + "first_name": "John", 545 + "last_name": "Doe", 546 + "bio": "Software engineer", 547 + "verified": true 548 + }, 549 + "roles": ["user", "admin"], 550 + "created_at": "2023-01-15T10:30:00Z" 551 + }, 552 + "status": "success", 553 + "message": null 554 + }` 555 + 556 + var data interface{} 557 + if err := json.Unmarshal([]byte(jsonStr), &data); err != nil { 558 + t.Fatalf("failed to unmarshal json: %v", err) 559 + } 560 + 561 + freeze.Snap(t, "JSON Object", data) 562 + } 563 + 564 + // TestComplexJSONStructure tests complex nested JSON structures 565 + func TestComplexJSONStructure(t *testing.T) { 566 + jsonStr := `{ 567 + "api": { 568 + "version": "2.0", 569 + "endpoints": [ 570 + { 571 + "path": "/users", 572 + "method": "GET", 573 + "auth_required": true, 574 + "rate_limit": { 575 + "requests": 100, 576 + "window": "1m" 577 + }, 578 + "responses": { 579 + "200": { 580 + "description": "Success", 581 + "schema": { 582 + "type": "array", 583 + "items": { 584 + "type": "object", 585 + "properties": { 586 + "id": {"type": "integer"}, 587 + "name": {"type": "string"} 588 + } 589 + } 590 + } 591 + }, 592 + "401": { 593 + "description": "Unauthorized" 594 + } 595 + } 596 + }, 597 + { 598 + "path": "/users/{id}", 599 + "method": "POST", 600 + "auth_required": true, 601 + "rate_limit": { 602 + "requests": 50, 603 + "window": "1m" 604 + } 605 + } 606 + ], 607 + "models": { 608 + "User": { 609 + "properties": { 610 + "id": {"type": "integer"}, 611 + "username": {"type": "string"}, 612 + "email": {"type": "string", "format": "email"}, 613 + "created_at": {"type": "string", "format": "date-time"}, 614 + "roles": { 615 + "type": "array", 616 + "items": {"type": "string"} 617 + } 618 + }, 619 + "required": ["id", "username", "email"] 620 + } 621 + } 622 + } 623 + }` 624 + 625 + var data interface{} 626 + if err := json.Unmarshal([]byte(jsonStr), &data); err != nil { 627 + t.Fatalf("failed to unmarshal json: %v", err) 628 + } 629 + 630 + freeze.Snap(t, "Complex JSON Structure", data) 631 + } 632 + 633 + // TestJSONArrayOfObjects tests JSON arrays with multiple object types 634 + func TestJSONArrayOfObjects(t *testing.T) { 635 + jsonStr := `[ 636 + { 637 + "type": "user", 638 + "id": 1, 639 + "data": { 640 + "name": "Alice", 641 + "role": "admin" 642 + } 643 + }, 644 + { 645 + "type": "post", 646 + "id": 100, 647 + "data": { 648 + "title": "First Post", 649 + "author_id": 1, 650 + "likes": 42 651 + } 652 + }, 653 + { 654 + "type": "comment", 655 + "id": 500, 656 + "data": { 657 + "content": "Great post!", 658 + "author_id": 2, 659 + "post_id": 100 660 + } 661 + } 662 + ]` 663 + 664 + var data interface{} 665 + if err := json.Unmarshal([]byte(jsonStr), &data); err != nil { 666 + t.Fatalf("failed to unmarshal json: %v", err) 667 + } 668 + 669 + freeze.Snap(t, "JSON Array of Objects", data) 670 + } 671 + 672 + // TestJSONWithVariousTypes tests JSON with various data types 673 + func TestJSONWithVariousTypes(t *testing.T) { 674 + jsonStr := `{ 675 + "string": "hello world", 676 + "integer": 42, 677 + "float": 3.14159, 678 + "boolean_true": true, 679 + "boolean_false": false, 680 + "null_value": null, 681 + "array": [1, 2, 3, "four", 5.5], 682 + "object": { 683 + "nested": "value", 684 + "count": 10 685 + }, 686 + "empty_array": [], 687 + "empty_object": {}, 688 + "escaped_string": "line1\nline2\ttab" 689 + }` 690 + 691 + var data interface{} 692 + if err := json.Unmarshal([]byte(jsonStr), &data); err != nil { 693 + t.Fatalf("failed to unmarshal json: %v", err) 694 + } 695 + 696 + freeze.Snap(t, "JSON with Various Types", data) 697 + } 698 + 699 + // TestJSONNumbers tests JSON with various number formats 700 + func TestJSONNumbers(t *testing.T) { 701 + jsonStr := `{ 702 + "integers": { 703 + "zero": 0, 704 + "positive": 42, 705 + "negative": -100, 706 + "large": 9999999999999 707 + }, 708 + "floats": { 709 + "small": 0.0001, 710 + "pi": 3.14159265359, 711 + "scientific": 1.23e-4, 712 + "negative_float": -42.5 713 + }, 714 + "edge_cases": { 715 + "one": 1, 716 + "minus_one": -1 717 + } 718 + }` 719 + 720 + var data interface{} 721 + if err := json.Unmarshal([]byte(jsonStr), &data); err != nil { 722 + t.Fatalf("failed to unmarshal json: %v", err) 723 + } 724 + 725 + freeze.Snap(t, "JSON Numbers", data) 726 + } 727 + 728 + // TestJSONWithSpecialCharacters tests JSON with special characters and unicode 729 + func TestJSONWithSpecialCharacters(t *testing.T) { 730 + jsonStr := `{ 731 + "english": "Hello, World!", 732 + "unicode": "こんにちは 世界 🌍", 733 + "emoji": "😀 😃 😄 😁 😆", 734 + "special_chars": "!@#$%^&*()_+-=[]{}|;:,.<>?", 735 + "escaped": "quotes: \"double\" and 'single'", 736 + "newlines": "line1\nline2\rline3\r\nline4", 737 + "tabs": "col1\tcol2\tcol3", 738 + "backslash": "path\\to\\file" 739 + }` 740 + 741 + var data interface{} 742 + if err := json.Unmarshal([]byte(jsonStr), &data); err != nil { 743 + t.Fatalf("failed to unmarshal json: %v", err) 744 + } 745 + 746 + freeze.Snap(t, "JSON with Special Characters", data) 747 + } 748 + 749 + // TestGoStructMarshalledToJSON tests Go struct marshalled to JSON 750 + func TestGoStructMarshalledToJSON(t *testing.T) { 751 + type Address struct { 752 + Street string `json:"street"` 753 + City string `json:"city"` 754 + Zip string `json:"zip"` 755 + } 756 + 757 + type Contact struct { 758 + Name string `json:"name"` 759 + Email string `json:"email"` 760 + Phone string `json:"phone"` 761 + Address Address `json:"address"` 762 + Tags []string `json:"tags"` 763 + Active bool `json:"active"` 764 + CreatedAt time.Time `json:"created_at"` 765 + } 766 + 767 + contact := Contact{ 768 + Name: "Jane Smith", 769 + Email: "jane@example.com", 770 + Phone: "+1-555-0123", 771 + Address: Address{ 772 + Street: "456 Oak Ave", 773 + City: "San Francisco", 774 + Zip: "94102", 775 + }, 776 + Tags: []string{"vip", "verified", "premium"}, 777 + Active: true, 778 + CreatedAt: time.Date(2023, 6, 15, 14, 30, 0, 0, time.UTC), 779 + } 780 + 781 + jsonBytes, err := json.MarshalIndent(contact, "", " ") 782 + if err != nil { 783 + t.Fatalf("failed to marshal json: %v", err) 784 + } 785 + 786 + var data interface{} 787 + if err := json.Unmarshal(jsonBytes, &data); err != nil { 788 + t.Fatalf("failed to unmarshal json: %v", err) 789 + } 790 + 791 + freeze.Snap(t, "Go Struct Marshalled to JSON", data) 792 + } 793 + 794 + // TestDeeplyNestedJSON tests deeply nested JSON structure 795 + func TestDeeplyNestedJSON(t *testing.T) { 796 + type Level4 struct { 797 + Value string 798 + } 799 + 800 + type Level3 struct { 801 + L4 Level4 802 + } 803 + 804 + type Level2 struct { 805 + L3 Level3 806 + } 807 + 808 + type Level1 struct { 809 + L2 Level2 810 + } 811 + 812 + l1 := Level1{ 813 + L2: Level2{ 814 + L3: Level3{ 815 + L4: Level4{ 816 + Value: "deep value", 817 + }, 818 + }, 819 + }, 820 + } 821 + 822 + jsonBytes, err := json.MarshalIndent(l1, "", " ") 823 + if err != nil { 824 + t.Fatalf("failed to marshal json: %v", err) 825 + } 826 + 827 + var data interface{} 828 + if err := json.Unmarshal(jsonBytes, &data); err != nil { 829 + t.Fatalf("failed to unmarshal json: %v", err) 830 + } 831 + 832 + freeze.Snap(t, "Deeply Nested JSON", data) 833 + } 834 + 835 + // TestLargeJSON tests larger JSON structure with many fields 836 + func TestLargeJSON(t *testing.T) { 837 + type Product struct { 838 + ID int `json:"id"` 839 + Name string `json:"name"` 840 + Description string `json:"description"` 841 + Price float64 `json:"price"` 842 + Stock int `json:"stock"` 843 + InStock bool `json:"in_stock"` 844 + Tags []string `json:"tags"` 845 + } 846 + 847 + type Order struct { 848 + ID int `json:"id"` 849 + CustomerID int `json:"customer_id"` 850 + Products []Product `json:"products"` 851 + Total float64 `json:"total"` 852 + Status string `json:"status"` 853 + CreatedAt time.Time `json:"created_at"` 854 + ShippedAt *time.Time `json:"shipped_at"` 855 + DeliveredAt *time.Time `json:"delivered_at"` 856 + } 857 + 858 + shippedTime := time.Date(2023, 2, 1, 10, 0, 0, 0, time.UTC) 859 + 860 + order := Order{ 861 + ID: 1001, 862 + CustomerID: 42, 863 + Products: []Product{ 864 + { 865 + ID: 1, 866 + Name: "Laptop", 867 + Description: "High-performance laptop", 868 + Price: 999.99, 869 + Stock: 5, 870 + InStock: true, 871 + Tags: []string{"electronics", "computers", "laptops"}, 872 + }, 873 + { 874 + ID: 2, 875 + Name: "Mouse", 876 + Description: "Wireless mouse", 877 + Price: 29.99, 878 + Stock: 50, 879 + InStock: true, 880 + Tags: []string{"electronics", "accessories"}, 881 + }, 882 + { 883 + ID: 3, 884 + Name: "Keyboard", 885 + Description: "Mechanical keyboard", 886 + Price: 149.99, 887 + Stock: 0, 888 + InStock: false, 889 + Tags: []string{"electronics", "accessories"}, 890 + }, 891 + }, 892 + Total: 1179.97, 893 + Status: "shipped", 894 + CreatedAt: time.Date(2023, 1, 28, 14, 30, 0, 0, time.UTC), 895 + ShippedAt: &shippedTime, 896 + DeliveredAt: nil, 897 + } 898 + 899 + jsonBytes, err := json.MarshalIndent(order, "", " ") 900 + if err != nil { 901 + t.Fatalf("failed to marshal json: %v", err) 902 + } 903 + 904 + var data interface{} 905 + if err := json.Unmarshal(jsonBytes, &data); err != nil { 906 + t.Fatalf("failed to unmarshal json: %v", err) 907 + } 908 + 909 + freeze.Snap(t, "Large JSON Structure", data) 910 + } 911 + 912 + // TestJSONWithMixedArrays tests JSON with arrays containing different types 913 + func TestJSONWithMixedArrays(t *testing.T) { 914 + jsonStr := `{ 915 + "heterogeneous_array": [ 916 + "string", 917 + 42, 918 + 3.14, 919 + true, 920 + null, 921 + {"object": "value"}, 922 + [1, 2, 3] 923 + ], 924 + "matrix": [ 925 + [1, 2, 3], 926 + [4, 5, 6], 927 + [7, 8, 9] 928 + ], 929 + "object_array": [ 930 + {"id": 1, "name": "Item 1"}, 931 + {"id": 2, "name": "Item 2"}, 932 + {"id": 3, "name": "Item 3"} 933 + ] 934 + }` 935 + 936 + var data interface{} 937 + if err := json.Unmarshal([]byte(jsonStr), &data); err != nil { 938 + t.Fatalf("failed to unmarshal json: %v", err) 939 + } 940 + 941 + freeze.Snap(t, "JSON with Mixed Arrays", data) 942 + } 943 + 944 + // ============================================================================ 945 + // HELPER FUNCTIONS 946 + // ============================================================================ 947 + 948 + func intPtr(i int) *int { 949 + return &i 950 + }