github.com/kilpkonn/gtm-enhanced@v1.3.5/metric/metric_test.go (about)

     1  // Copyright 2016 Michael Schenk. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package metric
     6  
     7  import (
     8  	"path/filepath"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func TestAllocateTime(t *testing.T) {
    14  	cases := []struct {
    15  		metric   map[string]FileMetric
    16  		event    map[string]int
    17  		expected map[string]FileMetric
    18  	}{
    19  		{
    20  			map[string]FileMetric{},
    21  			map[string]int{filepath.Join("event", "event.go"): 1},
    22  			map[string]FileMetric{
    23  				"6f53bc90ba625b5afaac80b422b44f1f609d6367": {Updated: true, SourceFile: filepath.Join("event", "event.go"), TimeSpent: 60, Timeline: map[int64]int{int64(1): 60}}},
    24  		},
    25  		{
    26  			map[string]FileMetric{},
    27  			map[string]int{filepath.Join("event", "event.go"): 4, filepath.Join("event", "event_test.go"): 2},
    28  			map[string]FileMetric{
    29  				"6f53bc90ba625b5afaac80b422b44f1f609d6367": {Updated: true, SourceFile: filepath.Join("event", "event.go"), TimeSpent: 40, Timeline: map[int64]int{int64(1): 40}},
    30  				"e65b42b6bf1eda6349451b063d46134dd7ab9921": {Updated: true, SourceFile: filepath.Join("event", "event_test.go"), TimeSpent: 20, Timeline: map[int64]int{int64(1): 20}}},
    31  		},
    32  		{
    33  			map[string]FileMetric{"e65b42b6bf1eda6349451b063d46134dd7ab9921": {Updated: true, SourceFile: filepath.Join("event", "event_test.go"), TimeSpent: 60, Timeline: map[int64]int{int64(1): 60}}},
    34  			map[string]int{filepath.Join("event", "event.go"): 4, filepath.Join("event", "event_test.go"): 2},
    35  			map[string]FileMetric{
    36  				"6f53bc90ba625b5afaac80b422b44f1f609d6367": {Updated: true, SourceFile: filepath.Join("event", "event.go"), TimeSpent: 40, Timeline: map[int64]int{int64(1): 40}},
    37  				"e65b42b6bf1eda6349451b063d46134dd7ab9921": {Updated: true, SourceFile: filepath.Join("event", "event_test.go"), TimeSpent: 80, Timeline: map[int64]int{int64(1): 80}}},
    38  		},
    39  	}
    40  
    41  	for _, tc := range cases {
    42  		// copy metric map because it's updated in place during testing
    43  		metricOrig := map[string]FileMetric{}
    44  		for k, v := range tc.metric {
    45  			metricOrig[k] = v
    46  
    47  		}
    48  		if err := allocateTime(1, tc.metric, tc.event); err != nil {
    49  			t.Errorf("allocateTime(%+v, %+v) want error nil got %s", metricOrig, tc.event, err)
    50  		}
    51  
    52  		if !reflect.DeepEqual(tc.metric, tc.expected) {
    53  			t.Errorf("allocateTime(%+v, %+v)\nwant:\n%+v\ngot:\n%+v\n", metricOrig, tc.event, tc.expected, tc.metric)
    54  		}
    55  	}
    56  }
    57  
    58  func TestFileID(t *testing.T) {
    59  	want := "6f53bc90ba625b5afaac80b422b44f1f609d6367"
    60  	got := getFileID(filepath.Join("event", "event.go"))
    61  	if want != got {
    62  		t.Errorf("getFileID(%s), want %s, got %s", filepath.Join("event", "event.go"), want, got)
    63  
    64  	}
    65  }