A container registry that uses the AT Protocol for manifest storage and S3 for blob storage.
0
fork

Configure Feed

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

at 8d39daa09d8d9d5066c9fb336c61ef5bfb8a0b1f 160 lines 3.8 kB view raw
1package config 2 3import ( 4 "strings" 5 "testing" 6 "time" 7) 8 9type testConfig struct { 10 Name string `yaml:"name" comment:"The display name."` 11 Port int `yaml:"port" comment:"Listen port."` 12 Enabled bool `yaml:"enabled" comment:"Enable the service."` 13 Timeout time.Duration `yaml:"timeout" comment:"Request timeout."` 14 Internal string `yaml:"-"` 15 Nested testNested `yaml:"nested" comment:"Nested section."` 16} 17 18type testNested struct { 19 Endpoint string `yaml:"endpoint" comment:"API endpoint URL."` 20 Retries int `yaml:"retries" comment:"Number of retries."` 21} 22 23func TestMarshalCommentedYAML_CommentsRendered(t *testing.T) { 24 cfg := testConfig{ 25 Name: "myapp", 26 Port: 8080, 27 Enabled: true, 28 Timeout: 15 * time.Minute, 29 Nested: testNested{ 30 Endpoint: "https://api.example.com", 31 Retries: 3, 32 }, 33 } 34 35 out, err := MarshalCommentedYAML("Test Config", cfg) 36 if err != nil { 37 t.Fatalf("MarshalCommentedYAML() error: %v", err) 38 } 39 40 s := string(out) 41 42 // Title is rendered as head comment 43 if !strings.Contains(s, "# Test Config") { 44 t.Error("expected title comment in output") 45 } 46 47 // Field comments are rendered 48 for _, want := range []string{ 49 "# The display name.", 50 "# Listen port.", 51 "# Enable the service.", 52 "# Request timeout.", 53 "# Nested section.", 54 "# API endpoint URL.", 55 "# Number of retries.", 56 } { 57 if !strings.Contains(s, want) { 58 t.Errorf("expected comment %q in output", want) 59 } 60 } 61 62 // Field values are rendered 63 if !strings.Contains(s, "name: myapp") { 64 t.Error("expected name field value") 65 } 66 if !strings.Contains(s, "port: 8080") { 67 t.Error("expected port field value") 68 } 69 if !strings.Contains(s, "enabled: true") { 70 t.Error("expected enabled field value") 71 } 72} 73 74func TestMarshalCommentedYAML_DurationRendersAsString(t *testing.T) { 75 cfg := testConfig{ 76 Timeout: 15 * time.Minute, 77 } 78 79 out, err := MarshalCommentedYAML("", cfg) 80 if err != nil { 81 t.Fatalf("MarshalCommentedYAML() error: %v", err) 82 } 83 84 s := string(out) 85 if !strings.Contains(s, "timeout: 15m0s") { 86 t.Errorf("expected duration as string, got:\n%s", s) 87 } 88} 89 90func TestMarshalCommentedYAML_ZeroDuration(t *testing.T) { 91 cfg := testConfig{} 92 93 out, err := MarshalCommentedYAML("", cfg) 94 if err != nil { 95 t.Fatalf("MarshalCommentedYAML() error: %v", err) 96 } 97 98 s := string(out) 99 if !strings.Contains(s, "timeout: 0s") { 100 t.Errorf("expected zero duration as '0s', got:\n%s", s) 101 } 102} 103 104func TestMarshalCommentedYAML_YamlDashExcluded(t *testing.T) { 105 cfg := testConfig{ 106 Internal: "should-not-appear", 107 } 108 109 out, err := MarshalCommentedYAML("", cfg) 110 if err != nil { 111 t.Fatalf("MarshalCommentedYAML() error: %v", err) 112 } 113 114 s := string(out) 115 if strings.Contains(s, "should-not-appear") { 116 t.Error("yaml:\"-\" field should not appear in output") 117 } 118 if strings.Contains(s, "internal") { 119 t.Error("yaml:\"-\" field key should not appear in output") 120 } 121} 122 123func TestMarshalCommentedYAML_NestedStructs(t *testing.T) { 124 cfg := testConfig{ 125 Nested: testNested{ 126 Endpoint: "https://api.example.com", 127 Retries: 3, 128 }, 129 } 130 131 out, err := MarshalCommentedYAML("", cfg) 132 if err != nil { 133 t.Fatalf("MarshalCommentedYAML() error: %v", err) 134 } 135 136 s := string(out) 137 if !strings.Contains(s, "endpoint: https://api.example.com") { 138 t.Errorf("expected nested endpoint value in output:\n%s", s) 139 } 140 if !strings.Contains(s, "retries: 3") { 141 t.Errorf("expected nested retries value in output:\n%s", s) 142 } 143} 144 145func TestMarshalCommentedYAML_PointerStruct(t *testing.T) { 146 cfg := &testConfig{ 147 Name: "ptr-test", 148 Port: 9090, 149 } 150 151 out, err := MarshalCommentedYAML("Pointer Test", cfg) 152 if err != nil { 153 t.Fatalf("MarshalCommentedYAML() error: %v", err) 154 } 155 156 s := string(out) 157 if !strings.Contains(s, "name: ptr-test") { 158 t.Error("expected name field from pointer struct") 159 } 160}