github.com/splunk/dan1-qbec@v0.7.3/internal/sio/log_test.go (about)

     1  /*
     2     Copyright 2019 Splunk Inc.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package sio
    18  
    19  import (
    20  	"bytes"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestOutputWithoutColors(t *testing.T) {
    28  	var buf bytes.Buffer
    29  	orig := Output
    30  	origC := EnableColors
    31  	defer func() { Output = orig; EnableColors = origC }()
    32  	EnableColors = false
    33  	Output = &buf
    34  
    35  	Println("this", "is", "a", "message")
    36  	Warnln("this", "is", "a", "warning")
    37  	Errorln("this", "is", "an", "error")
    38  	Noticeln("this", "is", "a", "notice")
    39  	Debugln("this", "is", "an", "extra")
    40  
    41  	Printf("This is %s %s\n", "a", "message")
    42  	Warnf("This is %s %s\n", "a", "warning")
    43  	Errorf("This is %s %s\n", "an", "error")
    44  	Noticef("This is %s %s\n", "a", "notice")
    45  	Debugf("This is %s %s\n", "an", "extra")
    46  
    47  	a := assert.New(t)
    48  	lines := strings.Split(buf.String(), "\n")
    49  	a.Contains(lines, "this is a message")
    50  	a.Contains(lines, "this is a notice")
    51  	a.Contains(lines, "this is an extra")
    52  	a.Contains(lines, "[warn] this is a warning")
    53  	a.Contains(lines, unicodeX+" this is an error")
    54  	a.Contains(lines, "This is a message")
    55  	a.Contains(lines, "This is a notice")
    56  	a.Contains(lines, "This is an extra")
    57  	a.Contains(lines, "[warn] This is a warning")
    58  	a.Contains(lines, unicodeX+" This is an error")
    59  }
    60  
    61  func TestOutputWithColors(t *testing.T) {
    62  	var buf bytes.Buffer
    63  	orig := Output
    64  	origC := EnableColors
    65  	defer func() { Output = orig; EnableColors = origC }()
    66  	EnableColors = true
    67  	Output = &buf
    68  
    69  	Println("this", "is", "a", "message")
    70  	Noticeln("this", "is", "a", "notice")
    71  	Warnln("this", "is", "a", "warning")
    72  	Errorln("this", "is", "an", "error")
    73  	Debugln("this", "is", "an", "extra")
    74  
    75  	Printf("This is %s %s\n", "a", "message")
    76  	Noticef("This is %s %s\n", "a", "notice")
    77  	Warnf("This is %s %s\n", "a", "warning")
    78  	Errorf("This is %s %s\n", "an", "error")
    79  	Debugf("This is %s %s\n", "an", "extra")
    80  
    81  	a := assert.New(t)
    82  	s := buf.String()
    83  	a.Contains(s, "this is a message\n")
    84  	a.Contains(s, attrBold+"this is a notice\n"+codeReset)
    85  	a.Contains(s, colorMagenta+attrBold+"[warn] this is a warning\n"+codeReset)
    86  	a.Contains(s, colorRed+attrBold+unicodeX+" this is an error\n"+codeReset)
    87  	a.Contains(s, attrDim+"this is an extra\n"+codeReset)
    88  	a.Contains(s, "This is a message\n")
    89  	a.Contains(s, attrBold+"This is a notice\n"+codeReset)
    90  	a.Contains(s, attrDim+"This is an extra\n"+codeReset)
    91  	a.Contains(s, colorMagenta+attrBold+"[warn] This is a warning\n"+codeReset)
    92  	a.Contains(s, colorRed+attrBold+unicodeX+" This is an error\n"+codeReset)
    93  }