github.com/keakon/golog@v0.0.0-20230330091222-cac71197c18d/formatter_test.go (about)

     1  package golog
     2  
     3  import (
     4  	"bytes"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func TestParseFormat(t *testing.T) {
    10  	if ParseFormat("") != nil {
    11  		t.Error("ParseFormat empty string is not nil")
    12  	}
    13  
    14  	formatter := ParseFormat("%")
    15  	if len(formatter.formatParts) != 1 {
    16  		t.Error("ParseFormat % failed")
    17  	}
    18  	p, ok := formatter.formatParts[0].(*BytesFormatPart)
    19  	if !ok {
    20  		t.Error("ParseFormat % failed")
    21  	}
    22  	if string(p.bytes) != "%\n" {
    23  		t.Error("ParseFormat % failed")
    24  	}
    25  
    26  	formatter = ParseFormat("%%")
    27  	if len(formatter.formatParts) != 1 {
    28  		t.Error("ParseFormat % failed")
    29  	}
    30  	p, ok = formatter.formatParts[0].(*BytesFormatPart)
    31  	if !ok {
    32  		t.Error("ParseFormat % failed")
    33  	}
    34  	if string(p.bytes) != "%\n" {
    35  		t.Error("ParseFormat % failed")
    36  	}
    37  
    38  	formatter = ParseFormat("%a")
    39  	if len(formatter.formatParts) != 1 {
    40  		t.Error("ParseFormat %a failed")
    41  	}
    42  	p, ok = formatter.formatParts[0].(*BytesFormatPart)
    43  	if !ok {
    44  		t.Error("ParseFormat %a failed")
    45  	}
    46  	if string(p.bytes) != "%a\n" {
    47  		t.Error("ParseFormat %a failed")
    48  	}
    49  
    50  	formatter = ParseFormat("% %")
    51  	if len(formatter.formatParts) != 1 {
    52  		t.Error("ParseFormat % % failed")
    53  	}
    54  	p, ok = formatter.formatParts[0].(*BytesFormatPart)
    55  	if !ok {
    56  		t.Error("ParseFormat % % failed")
    57  	}
    58  	if string(p.bytes) != "% %\n" {
    59  		t.Error("ParseFormat % % failed")
    60  	}
    61  
    62  	formatter = ParseFormat("% %a")
    63  	if len(formatter.formatParts) != 1 {
    64  		t.Error("ParseFormat % %a failed")
    65  	}
    66  	p, ok = formatter.formatParts[0].(*BytesFormatPart)
    67  	if !ok {
    68  		t.Error("ParseFormat % %a failed")
    69  	}
    70  	if string(p.bytes) != "% %a\n" {
    71  		t.Error("ParseFormat % %a failed")
    72  	}
    73  
    74  	formatter = ParseFormat("abc")
    75  	if len(formatter.formatParts) != 1 {
    76  		t.Error("ParseFormat abc failed")
    77  	}
    78  	p, ok = formatter.formatParts[0].(*BytesFormatPart)
    79  	if !ok {
    80  		t.Error("ParseFormat abc failed")
    81  	}
    82  	if string(p.bytes) != "abc\n" {
    83  		t.Error("ParseFormat abc failed")
    84  	}
    85  
    86  	formatter = ParseFormat("%S")
    87  	if len(formatter.formatParts) != 2 {
    88  		t.Error("ParseFormat abc failed")
    89  	}
    90  	if _, ok = formatter.formatParts[0].(*FullSourceFormatPart); !ok {
    91  		t.Error("ParseFormat %S failed")
    92  	}
    93  	if _, ok = formatter.formatParts[1].(*ByteFormatPart); !ok {
    94  		t.Error("ParseFormat %S failed")
    95  	}
    96  
    97  	if len(DefaultFormatter.formatParts) != 11 {
    98  		t.Errorf("formatParts are %d", len(DefaultFormatter.formatParts))
    99  	}
   100  
   101  	part0, ok := DefaultFormatter.formatParts[0].(*ByteFormatPart)
   102  	if !ok {
   103  		t.Errorf("part0 is " + reflect.TypeOf(DefaultFormatter.formatParts[0]).String())
   104  	}
   105  	if part0.byte != '[' {
   106  		t.Errorf("byte of part0 is %d", part0.byte)
   107  	}
   108  
   109  	_, ok = DefaultFormatter.formatParts[1].(*LevelFormatPart)
   110  	if !ok {
   111  		t.Errorf("part1 is " + reflect.TypeOf(DefaultFormatter.formatParts[1]).String())
   112  	}
   113  
   114  	part2, ok := DefaultFormatter.formatParts[2].(*ByteFormatPart)
   115  	if !ok {
   116  		t.Errorf("part2 is " + reflect.TypeOf(DefaultFormatter.formatParts[2]).String())
   117  	}
   118  	if part2.byte != ' ' {
   119  		t.Errorf("byte of part2 is %d", part2.byte)
   120  	}
   121  
   122  	_, ok = DefaultFormatter.formatParts[3].(*DateFormatPart)
   123  	if !ok {
   124  		t.Errorf("part3 is " + reflect.TypeOf(DefaultFormatter.formatParts[3]).String())
   125  	}
   126  
   127  	part4, ok := DefaultFormatter.formatParts[4].(*ByteFormatPart)
   128  	if !ok {
   129  		t.Errorf("part4 is " + reflect.TypeOf(DefaultFormatter.formatParts[4]).String())
   130  	}
   131  	if part4.byte != ' ' {
   132  		t.Errorf("byte of part4 is %d", part4.byte)
   133  	}
   134  
   135  	_, ok = DefaultFormatter.formatParts[5].(*TimeFormatPart)
   136  	if !ok {
   137  		t.Errorf("part5 is " + reflect.TypeOf(DefaultFormatter.formatParts[5]).String())
   138  	}
   139  
   140  	part6, ok := DefaultFormatter.formatParts[6].(*ByteFormatPart)
   141  	if !ok {
   142  		t.Errorf("part6 is " + reflect.TypeOf(DefaultFormatter.formatParts[6]).String())
   143  	}
   144  	if part6.byte != ' ' {
   145  		t.Errorf("byte of part6 is %d", part6.byte)
   146  	}
   147  
   148  	_, ok = DefaultFormatter.formatParts[7].(*SourceFormatPart)
   149  	if !ok {
   150  		t.Errorf("part7 is " + reflect.TypeOf(DefaultFormatter.formatParts[7]).String())
   151  	}
   152  
   153  	part8, ok := DefaultFormatter.formatParts[8].(*BytesFormatPart)
   154  	if !ok {
   155  		t.Errorf("part8 is " + reflect.TypeOf(DefaultFormatter.formatParts[8]).String())
   156  	}
   157  	bs := part8.bytes
   158  	if len(bs) != 2 || bs[0] != ']' || bs[1] != ' ' {
   159  		t.Errorf("bytes of part8 is " + string(part8.bytes))
   160  	}
   161  
   162  	_, ok = DefaultFormatter.formatParts[9].(*MessageFormatPart)
   163  	if !ok {
   164  		t.Errorf("part9 is " + reflect.TypeOf(DefaultFormatter.formatParts[9]).String())
   165  	}
   166  
   167  	part10, ok := DefaultFormatter.formatParts[10].(*ByteFormatPart)
   168  	if !ok {
   169  		t.Errorf("part10 is " + reflect.TypeOf(DefaultFormatter.formatParts[10]).String())
   170  	}
   171  	if part10.byte != '\n' {
   172  		t.Errorf("byte of part6 is %d", part6.byte)
   173  	}
   174  }
   175  
   176  func TestByteFormatPart(t *testing.T) {
   177  	buf := &bytes.Buffer{}
   178  	part := ByteFormatPart{'a'}
   179  	part.Format(nil, buf)
   180  	bs := buf.String()
   181  	if bs != "a" {
   182  		t.Error()
   183  	}
   184  }
   185  
   186  func TestBytesFormatPart(t *testing.T) {
   187  	buf := &bytes.Buffer{}
   188  	part := BytesFormatPart{[]byte("abc")}
   189  	part.Format(nil, buf)
   190  	bs := buf.String()
   191  	if bs != "abc" {
   192  		t.Error()
   193  	}
   194  }
   195  
   196  func TestLevelFormatPart(t *testing.T) {
   197  	r := &Record{}
   198  	buf := &bytes.Buffer{}
   199  	part := LevelFormatPart{}
   200  	part.Format(r, buf)
   201  	bs := buf.String()
   202  	if bs != "D" {
   203  		t.Error()
   204  	}
   205  
   206  	r.level = InfoLevel
   207  	buf.Reset()
   208  	part.Format(r, buf)
   209  	bs = buf.String()
   210  	if bs != "I" {
   211  		t.Error()
   212  	}
   213  }
   214  
   215  func TestTimeFormatPart(t *testing.T) {
   216  	r := &Record{
   217  		time: "16:12:34",
   218  		date: "2018-11-19",
   219  	}
   220  	buf := &bytes.Buffer{}
   221  	part := TimeFormatPart{}
   222  	part.Format(r, buf)
   223  	bs := buf.String()
   224  	if bs != "16:12:34" {
   225  		t.Error()
   226  	}
   227  }
   228  
   229  func TestDateFormatPart(t *testing.T) {
   230  	r := &Record{
   231  		time: "16:12:34",
   232  		date: "2018-11-19",
   233  	}
   234  	buf := &bytes.Buffer{}
   235  	part := DateFormatPart{}
   236  	part.Format(r, buf)
   237  	bs := buf.String()
   238  	if bs != "2018-11-19" {
   239  		t.Error()
   240  	}
   241  }
   242  
   243  func TestSourceFormatPart(t *testing.T) {
   244  	r := &Record{}
   245  	buf := &bytes.Buffer{}
   246  	part := SourceFormatPart{}
   247  	part.Format(r, buf)
   248  	bs := buf.String()
   249  	if bs != string(unknownFile) {
   250  		t.Error()
   251  	}
   252  
   253  	r.file = "/test/test.go"
   254  	r.line = 10
   255  	buf.Reset()
   256  	part.Format(r, buf)
   257  	bs = buf.String()
   258  	if bs != "test:10" {
   259  		t.Error()
   260  	}
   261  }
   262  
   263  func TestFullSourceFormatPart(t *testing.T) {
   264  	r := &Record{}
   265  	buf := &bytes.Buffer{}
   266  	part := FullSourceFormatPart{}
   267  	part.Format(r, buf)
   268  	bs := buf.String()
   269  	if bs != string(unknownFile) {
   270  		t.Error()
   271  	}
   272  
   273  	r.file = "/test/test.go"
   274  	r.line = 10
   275  	buf.Reset()
   276  	part.Format(r, buf)
   277  	bs = buf.String()
   278  	if bs != "/test/test.go:10" {
   279  		t.Error()
   280  	}
   281  }
   282  
   283  func TestMessageFormatPart(t *testing.T) {
   284  	r := &Record{}
   285  	buf := &bytes.Buffer{}
   286  	part := MessageFormatPart{}
   287  	part.Format(r, buf)
   288  	bs := buf.String()
   289  	if bs != "" {
   290  		t.Error()
   291  	}
   292  
   293  	r.message = "abc"
   294  	buf.Reset()
   295  	part.Format(r, buf)
   296  	bs = buf.String()
   297  	if bs != "abc" {
   298  		t.Error()
   299  	}
   300  
   301  	r.message = "abc %d %d"
   302  	r.args = []interface{}{1, 2}
   303  	buf.Reset()
   304  	part.Format(r, buf)
   305  	bs = buf.String()
   306  	if bs != "abc 1 2" {
   307  		t.Error()
   308  	}
   309  
   310  	r.message = ""
   311  	r.args = []interface{}{1, 2}
   312  	buf.Reset()
   313  	part.Format(r, buf)
   314  	bs = buf.String()
   315  	if bs != "1 2" {
   316  		t.Error()
   317  	}
   318  }