fortio.org/log@v1.12.2/special_test.go (about)

     1  package log
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"math"
     7  	"testing"
     8  )
     9  
    10  func Test_LogS_JSON_NaN(t *testing.T) {
    11  	// Setup
    12  	var b bytes.Buffer
    13  	w := bufio.NewWriter(&b)
    14  	SetLogLevel(LevelByName("Verbose"))
    15  	Config.LogFileAndLine = false
    16  	Config.JSON = true
    17  	Config.NoTimestamp = true
    18  	SetOutput(w)
    19  	// Start of the actual test
    20  	value := math.NaN()
    21  	zero := 0.0
    22  	S(Verbose, "Test NaN", Any("nan", value), Any("minus-inf", -1.0/zero))
    23  	_ = w.Flush()
    24  	actual := b.String()
    25  	// Note that we serialize that way but can't deserialize with go default json unmarshaller
    26  	expected := `{"level":"trace","msg":"Test NaN","nan":NaN,"minus-inf":-Inf}` + "\n"
    27  	if actual != expected {
    28  		t.Errorf("unexpected:\n%s\nvs:\n%s\n", actual, expected)
    29  	}
    30  }
    31  
    32  func Test_LogS_JSON_Array(t *testing.T) {
    33  	// Setup
    34  	var b bytes.Buffer
    35  	w := bufio.NewWriter(&b)
    36  	SetLogLevel(LevelByName("Verbose"))
    37  	Config.LogFileAndLine = false
    38  	Config.JSON = true
    39  	Config.NoTimestamp = true
    40  	SetOutput(w)
    41  	// Start of the actual test
    42  	S(Verbose, "Test Array", Any("arr", []interface{}{"x", 42, "y"}))
    43  	_ = w.Flush()
    44  	actual := b.String()
    45  	expected := `{"level":"trace","msg":"Test Array","arr":["x",42,"y"]}` + "\n"
    46  	if actual != expected {
    47  		t.Errorf("unexpected:\n%s\nvs:\n%s\n", actual, expected)
    48  	}
    49  }
    50  
    51  func Test_LogS_JSON_Map(t *testing.T) {
    52  	// Setup
    53  	var b bytes.Buffer
    54  	w := bufio.NewWriter(&b)
    55  	SetLogLevel(LevelByName("Verbose"))
    56  	Config.LogFileAndLine = false
    57  	Config.JSON = true
    58  	Config.NoTimestamp = true
    59  	SetOutput(w)
    60  	// Start of the actual test
    61  	tst := map[string]interface{}{
    62  		"str1": "val 1",
    63  		"subArray": []interface{}{
    64  			"x", 42, "y",
    65  		},
    66  		"number": 3.14,
    67  	}
    68  	S(Verbose, "Test Map", Any("map", tst), Int64("in64", 0), Bool("bool", true))
    69  	_ = w.Flush()
    70  	actual := b.String()
    71  	//nolint:lll
    72  	expected := `{"level":"trace","msg":"Test Map","map":{"number":3.14,"str1":"val 1","subArray":["x",42,"y"]},"in64":0,"bool":true}` +
    73  		"\n"
    74  	if actual != expected {
    75  		t.Errorf("unexpected:\n%s\nvs:\n%s\n", actual, expected)
    76  	}
    77  }