Logger package for gitea repositories
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.3 KiB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package log
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. type testFlags struct {
  12. Flags Flags `json:"flags"`
  13. }
  14. func TestFlagsMarshalUnmarshalJSON(t *testing.T) {
  15. flagsBytes, err := json.Marshal(testFlags{
  16. Flags: LstdFlags,
  17. })
  18. assert.NoError(t, err)
  19. assert.Equal(t, string(makeTestFlagsBytes("stdflags")), string(flagsBytes))
  20. var testFlags testFlags
  21. err = json.Unmarshal(makeTestFlagsBytes(`XAXAXA`), &testFlags)
  22. assert.NoError(t, err)
  23. assert.Equal(t, Flags(0), testFlags.Flags)
  24. err = json.Unmarshal([]byte(fmt.Sprintf(`{"flags":%d}`, LstdFlags)), &testFlags)
  25. assert.NoError(t, err)
  26. assert.Equal(t, LstdFlags, testFlags.Flags)
  27. flagString := "date, shortfile, levelinitial"
  28. err = json.Unmarshal(makeTestFlagsBytes(flagString), &testFlags)
  29. assert.NoError(t, err)
  30. assert.Equal(t, FlagsFromString(flagString), testFlags.Flags)
  31. flagsBytes, err = json.Marshal(testFlags)
  32. assert.NoError(t, err)
  33. assert.Equal(t, string(makeTestFlagsBytes(flagString)), string(flagsBytes))
  34. }
  35. func makeTestFlagsBytes(flagsString string) []byte {
  36. return []byte(fmt.Sprintf(`{"flags":"%s"}`, flagsString))
  37. }