@@ -262,3 +262,104 @@ func TestSeverityVarString(t *testing.T) {
262262 })
263263 }
264264}
265+
266+ func TestSeverityVarMarshalText (t * testing.T ) {
267+ for _ , test := range validEncodingTests {
268+ t .Run (test .Name , func (t * testing.T ) {
269+ var sev SeverityVar
270+ sev .Set (test .Severity )
271+ got , err := sev .MarshalText ()
272+ require .NoError (t , err )
273+ assert .Equal (t , test .Text , string (got ))
274+ })
275+ }
276+ }
277+
278+ func TestSeverityVarUnmarshalText (t * testing.T ) {
279+ for _ , test := range validDecodingTests {
280+ t .Run (test .Name , func (t * testing.T ) {
281+ var sev SeverityVar
282+ require .NoError (t , sev .UnmarshalText ([]byte (test .Text )))
283+
284+ got := Severity (int (sev .val .Load ()))
285+ const msg = "UnmarshalText(%q) != %d (%[2]s)"
286+ assert .Equalf (t , test .Severity , got , msg , test .Text , test .Severity )
287+ })
288+ }
289+ }
290+
291+ func TestSeverityVarUnmarshalTextError (t * testing.T ) {
292+ for _ , test := range invalidText {
293+ t .Run (test , func (t * testing.T ) {
294+ var sev SeverityVar
295+ err := sev .UnmarshalText ([]byte (test ))
296+ assert .Error (t , err )
297+ })
298+ }
299+ }
300+
301+ func TestSeverityVarAppendText (t * testing.T ) {
302+ tests := []struct {
303+ sev Severity
304+ prefix string
305+ expected string
306+ }{
307+ {SeverityInfo1 , "" , "INFO" },
308+ {SeverityError1 , "level=" , "level=ERROR" },
309+ {SeverityWarn2 , "severity:" , "severity:WARN2" },
310+ }
311+
312+ for _ , test := range tests {
313+ t .Run (test .expected , func (t * testing.T ) {
314+ var sev SeverityVar
315+ sev .Set (test .sev )
316+ result , err := sev .AppendText ([]byte (test .prefix ))
317+ require .NoError (t , err )
318+ assert .Equal (t , test .expected , string (result ))
319+ })
320+ }
321+ }
322+
323+ // Test JSON roundtrip for structures containing Severity.
324+ func TestSeverityJSONRoundtrip (t * testing.T ) {
325+ type Config struct {
326+ Level Severity `json:"level"`
327+ Name string `json:"name"`
328+ }
329+
330+ original := Config {
331+ Level : SeverityError1 ,
332+ Name : "test-config" ,
333+ }
334+
335+ // Marshal to JSON
336+ data , err := json .Marshal (original )
337+ require .NoError (t , err )
338+
339+ expectedJSON := `{"level":"ERROR","name":"test-config"}`
340+ assert .JSONEq (t , expectedJSON , string (data ))
341+
342+ // Unmarshal from JSON
343+ var decoded Config
344+ err = json .Unmarshal (data , & decoded )
345+ require .NoError (t , err )
346+ assert .Equal (t , original , decoded )
347+ }
348+
349+ // Test text marshaling roundtrip for SeverityVar.
350+ func TestSeverityVarTextRoundtrip (t * testing.T ) {
351+ original := SeverityWarn3
352+
353+ var sev SeverityVar
354+ sev .Set (original )
355+
356+ // Marshal to text.
357+ data , err := sev .MarshalText ()
358+ require .NoError (t , err )
359+ assert .Equal (t , "WARN3" , string (data ))
360+
361+ // Unmarshal from text
362+ var decoded SeverityVar
363+ require .NoError (t , decoded .UnmarshalText (data ))
364+ assert .Equal (t , original , Severity (int (decoded .val .Load ())))
365+ }
0 commit comments