github.com/honeycombio/honeytail@v1.9.0/parsers/htjson/htjson_test.go (about)

     1  package htjson
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  type testLineMap struct {
     9  	input    string
    10  	expected map[string]interface{}
    11  }
    12  
    13  var tlms = []testLineMap{
    14  	{ // strings, floats, and ints
    15  		input: `{"mystr": "myval", "myint": 3, "myfloat": 4.234}`,
    16  		expected: map[string]interface{}{
    17  			"mystr":   "myval",
    18  			"myint":   float64(3),
    19  			"myfloat": 4.234,
    20  		},
    21  	},
    22  	{ // time
    23  		input: `{"time": "2014-03-10 19:57:38.123456789 -0800 PST", "myint": 3, "myfloat": 4.234}`,
    24  		expected: map[string]interface{}{
    25  			"time":    "2014-03-10 19:57:38.123456789 -0800 PST",
    26  			"myint":   float64(3),
    27  			"myfloat": 4.234,
    28  		},
    29  	},
    30  	{ // non-flat json object
    31  		input: `{"array": [3, 4, 6], "obj": {"subkey":"subval"}, "myfloat": 4.234}`,
    32  		expected: map[string]interface{}{
    33  			"array":   []interface{}{float64(3), float64(4), float64(6)},
    34  			"obj":     map[string]interface{}{"subkey": "subval"},
    35  			"myfloat": 4.234,
    36  		},
    37  	},
    38  }
    39  
    40  func TestParseLine(t *testing.T) {
    41  	jlp := JSONLineParser{}
    42  	for _, tlm := range tlms {
    43  		resp, err := jlp.ParseLine(tlm.input)
    44  		if err != nil {
    45  			t.Error("jlp.ParseLine unexpectedly returned error ", err)
    46  		}
    47  		if !reflect.DeepEqual(resp, tlm.expected) {
    48  			t.Errorf("response %+v didn't match expected %+v", resp, tlm.expected)
    49  		}
    50  	}
    51  }