...

Source file src/github.com/jedib0t/go-pretty/v6/text/transformer_test.go

Documentation: github.com/jedib0t/go-pretty/v6/text

     1  package text
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestNewNumberTransformer(t *testing.T) {
    14  	signColorsMap := map[string]Colors{
    15  		"negative": colorsNumberNegative,
    16  		"positive": colorsNumberPositive,
    17  		"zero":     nil,
    18  		"nil":      nil,
    19  	}
    20  	colorValuesMap := map[string]map[interface{}]string{
    21  		"negative": {
    22  			int(-5):           "%05d",
    23  			int8(-5):          "%05d",
    24  			int16(-5):         "%05d",
    25  			int32(-5):         "%05d",
    26  			int64(-5):         "%05d",
    27  			float32(-5.55555): "%08.2f",
    28  			float64(-5.55555): "%08.2f",
    29  		},
    30  		"positive": {
    31  			int(5):           "%05d",
    32  			int8(5):          "%05d",
    33  			int16(5):         "%05d",
    34  			int32(5):         "%05d",
    35  			int64(5):         "%05d",
    36  			uint(5):          "%05d",
    37  			uint8(5):         "%05d",
    38  			uint16(5):        "%05d",
    39  			uint32(5):        "%05d",
    40  			uint64(5):        "%05d",
    41  			float32(5.55555): "%08.2f",
    42  			float64(5.55555): "%08.2f",
    43  		},
    44  		"zero": {
    45  			int(0):           "%05d",
    46  			int8(0):          "%05d",
    47  			int16(0):         "%05d",
    48  			int32(0):         "%05d",
    49  			int64(0):         "%05d",
    50  			uint(0):          "%05d",
    51  			uint8(0):         "%05d",
    52  			uint16(0):        "%05d",
    53  			uint32(0):        "%05d",
    54  			uint64(0):        "%05d",
    55  			float32(0.00000): "%08.2f",
    56  			float64(0.00000): "%08.2f",
    57  		},
    58  		"nil": {
    59  			nil: "%v",
    60  		},
    61  	}
    62  
    63  	for sign, valuesFormatMap := range colorValuesMap {
    64  		for value, format := range valuesFormatMap {
    65  			transformer := NewNumberTransformer(format)
    66  			expected := signColorsMap[sign].Sprintf(format, value)
    67  			if sign == "negative" {
    68  				expected = strings.Replace(expected, "-0", "-00", 1)
    69  			}
    70  			actual := transformer(value)
    71  			var kind reflect.Kind
    72  			if value != nil {
    73  				kind = reflect.TypeOf(value).Kind()
    74  			}
    75  			message := fmt.Sprintf("%s.%s: expected=%v, actual=%v; format=%#v",
    76  				sign, kind, expected, actual, format)
    77  
    78  			assert.Equal(t, expected, actual, message)
    79  		}
    80  	}
    81  
    82  	// invalid input
    83  	assert.Equal(t, "foo", NewNumberTransformer("%05d")("foo"))
    84  }
    85  
    86  type jsonTest struct {
    87  	Foo string       `json:"foo"`
    88  	Bar int32        `json:"bar"`
    89  	Baz float64      `json:"baz"`
    90  	Nan jsonNestTest `json:"nan"`
    91  }
    92  
    93  type jsonNestTest struct {
    94  	A string
    95  	B int32
    96  	C float64
    97  }
    98  
    99  func TestNewJSONTransformer(t *testing.T) {
   100  	transformer := NewJSONTransformer("", "    ")
   101  
   102  	// instance of a struct
   103  	inputObj := jsonTest{
   104  		Foo: "fooooooo",
   105  		Bar: 13,
   106  		Baz: 3.14,
   107  		Nan: jsonNestTest{
   108  			A: "a",
   109  			B: 2,
   110  			C: 3.0,
   111  		},
   112  	}
   113  	expectedOutput := `{
   114      "foo": "fooooooo",
   115      "bar": 13,
   116      "baz": 3.14,
   117      "nan": {
   118          "A": "a",
   119          "B": 2,
   120          "C": 3
   121      }
   122  }`
   123  	assert.Equal(t, expectedOutput, transformer(inputObj))
   124  
   125  	// numbers
   126  	assert.Equal(t, "1", transformer(int(1)))
   127  	assert.Equal(t, "1.2345", transformer(float32(1.2345)))
   128  
   129  	// slices
   130  	assert.Equal(t, "[\n    1,\n    2,\n    3\n]", transformer([]uint{1, 2, 3}))
   131  
   132  	// strings
   133  	assert.Equal(t, "\"foo\"", transformer("foo"))
   134  	assert.Equal(t, "\"{foo...\"", transformer("{foo...")) // malformed JSON
   135  
   136  	// strings with valid JSON
   137  	input := "{\"foo\":\"bar\",\"baz\":[1,2,3]}"
   138  	expectedOutput = `{
   139      "foo": "bar",
   140      "baz": [
   141          1,
   142          2,
   143          3
   144      ]
   145  }`
   146  	assert.Equal(t, expectedOutput, transformer(input))
   147  }
   148  
   149  func TestNewTimeTransformer(t *testing.T) {
   150  	inStr := "2010-11-12T13:14:15-07:00"
   151  	inTime, err := time.Parse(time.RFC3339, inStr)
   152  	assert.Nil(t, err)
   153  
   154  	location, err := time.LoadLocation("America/Los_Angeles")
   155  	assert.Nil(t, err)
   156  	transformer := NewTimeTransformer(time.RFC3339, location)
   157  	expected := "2010-11-12T12:14:15-08:00"
   158  	assert.Equal(t, expected, transformer(inStr))
   159  	assert.Equal(t, expected, transformer(inTime))
   160  	for _, possibleTimeLayout := range possibleTimeLayouts {
   161  		assert.Equal(t, expected, transformer(inTime.Format(possibleTimeLayout)), possibleTimeLayout)
   162  	}
   163  
   164  	location, err = time.LoadLocation("Asia/Singapore")
   165  	assert.Nil(t, err)
   166  	transformer = NewTimeTransformer(time.UnixDate, location)
   167  	expected = "Sat Nov 13 04:14:15 +08 2010"
   168  	assert.Equal(t, expected, transformer(inStr))
   169  	assert.Equal(t, expected, transformer(inTime))
   170  	for _, possibleTimeLayout := range possibleTimeLayouts {
   171  		assert.Equal(t, expected, transformer(inTime.Format(possibleTimeLayout)), possibleTimeLayout)
   172  	}
   173  
   174  	location, err = time.LoadLocation("Europe/London")
   175  	assert.Nil(t, err)
   176  	transformer = NewTimeTransformer(time.RFC3339, location)
   177  	expected = "2010-11-12T20:14:15Z"
   178  	assert.Equal(t, expected, transformer(inStr))
   179  	assert.Equal(t, expected, transformer(inTime))
   180  	for _, possibleTimeLayout := range possibleTimeLayouts {
   181  		assert.Equal(t, expected, transformer(inTime.Format(possibleTimeLayout)), possibleTimeLayout)
   182  	}
   183  }
   184  
   185  func TestNewUnixTimeTransformer(t *testing.T) {
   186  	inStr := "2010-11-12T13:14:15-07:00"
   187  	inTime, err := time.Parse(time.RFC3339, inStr)
   188  	assert.Nil(t, err)
   189  	inUnixTime := inTime.Unix()
   190  
   191  	location, err := time.LoadLocation("America/Los_Angeles")
   192  	assert.Nil(t, err)
   193  	transformer := NewUnixTimeTransformer(time.RFC3339, location)
   194  	expected := "2010-11-12T12:14:15-08:00"
   195  	assert.Equal(t, expected, transformer(fmt.Sprint(inUnixTime)), "seconds in string")
   196  	assert.Equal(t, expected, transformer(inUnixTime), "seconds")
   197  	assert.Equal(t, expected, transformer(inUnixTime*1000), "milliseconds")
   198  	assert.Equal(t, expected, transformer(inUnixTime*1000000), "microseconds")
   199  	assert.Equal(t, expected, transformer(inUnixTime*1000000000), "nanoseconds")
   200  
   201  	location, err = time.LoadLocation("Asia/Singapore")
   202  	assert.Nil(t, err)
   203  	transformer = NewUnixTimeTransformer(time.UnixDate, location)
   204  	expected = "Sat Nov 13 04:14:15 +08 2010"
   205  	assert.Equal(t, expected, transformer(fmt.Sprint(inUnixTime)), "seconds in string")
   206  	assert.Equal(t, expected, transformer(inUnixTime), "seconds")
   207  	assert.Equal(t, expected, transformer(inUnixTime*1000), "milliseconds")
   208  	assert.Equal(t, expected, transformer(inUnixTime*1000000), "microseconds")
   209  	assert.Equal(t, expected, transformer(inUnixTime*1000000000), "nanoseconds")
   210  
   211  	location, err = time.LoadLocation("Europe/London")
   212  	assert.Nil(t, err)
   213  	transformer = NewUnixTimeTransformer(time.RFC3339, location)
   214  	expected = "2010-11-12T20:14:15Z"
   215  	assert.Equal(t, expected, transformer(fmt.Sprint(inUnixTime)), "seconds in string")
   216  	assert.Equal(t, expected, transformer(inUnixTime), "seconds")
   217  	assert.Equal(t, expected, transformer(inUnixTime*1000), "milliseconds")
   218  	assert.Equal(t, expected, transformer(inUnixTime*1000000), "microseconds")
   219  	assert.Equal(t, expected, transformer(inUnixTime*1000000000), "nanoseconds")
   220  
   221  	assert.Equal(t, "0.123456", transformer(float32(0.123456)))
   222  }
   223  
   224  func TestNewURLTransformer(t *testing.T) {
   225  	url := "https://winter.is.coming"
   226  
   227  	transformer := NewURLTransformer()
   228  	assert.Equal(t, colorsURL.Sprint(url), transformer(url))
   229  
   230  	transformer2 := NewURLTransformer(FgRed, BgWhite, Bold)
   231  	assert.Equal(t, Colors{FgRed, BgWhite, Bold}.Sprint(url), transformer2(url))
   232  	assert.Equal(t, colorsURL.Sprint(url), transformer(url))
   233  }
   234  

View as plain text