| // Copyright 2018 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file |
| package catapult_test |
| |
| import ( |
| "reflect" |
| "testing" |
| |
| "fuchsia.googlesource.com/testing/catapult" |
| "github.com/kr/pretty" |
| ) |
| |
| func TestAddHistogram(t *testing.T) { |
| t.Run("When the HistogramSet is empty", func(t *testing.T) { |
| histogramSet := catapult.HistogramSet{} |
| histogram := catapult.Histogram{Name: "test_histogram"} |
| |
| expectedJSON := []interface{}{histogram} |
| histogramSet.AddHistogram(histogram) |
| |
| if !reflect.DeepEqual(histogramSet.ToJSON(), expectedJSON) { |
| t.Errorf("Invalid HistogramSet. Got %+v. Expected %+v", |
| pretty.Sprint(histogramSet.ToJSON()), |
| pretty.Sprint(expectedJSON), |
| ) |
| } |
| }) |
| |
| t.Run("When the HistogramSet contains other Histograms", func(t *testing.T) { |
| histogramSet := catapult.HistogramSet{} |
| histogram1 := catapult.Histogram{Name: "test_histogram_1"} |
| histogram2 := catapult.Histogram{Name: "test_histogram_2"} |
| |
| expectedJSON := []interface{}{histogram1, histogram2} |
| |
| histogramSet.AddHistogram(histogram1) |
| histogramSet.AddHistogram(histogram2) |
| if !reflect.DeepEqual(histogramSet.ToJSON(), expectedJSON) { |
| t.Errorf("Invalid HistogramSet. Got %+v. Expected %+v", |
| pretty.Sprint(histogramSet.ToJSON()), |
| pretty.Sprint(expectedJSON), |
| ) |
| } |
| }) |
| |
| t.Run("When the HistogramSet contains other Diagnostics", func(t *testing.T) { |
| histogramSet := catapult.HistogramSet{} |
| histogram := catapult.Histogram{ |
| Name: "test_histogram", |
| Diagnostics: make(map[string]string), |
| } |
| diagnostic := &catapult.GenericSetDiagnostic{ |
| Type: catapult.DiagnosticTypeGenericSet, |
| Values: []interface{}{"test_value"}, |
| GUID: "test_guid", |
| } |
| |
| // Should return an error without modifying the HistogramSet |
| // because all Histograms must be added before any Diagnotics |
| // are added. |
| expectedJSON := []interface{}{diagnostic} |
| histogramSet.AddSharedDiagnostic("test_diagnostic", diagnostic) |
| if histogramSet.AddHistogram(histogram) == nil { |
| t.Errorf("expected an error when adding a Histogram after a Diagnostic") |
| } |
| |
| if !reflect.DeepEqual(histogramSet.ToJSON(), expectedJSON) { |
| t.Errorf("Invalid HistogramSet. Got %+v. Expected %+v", |
| pretty.Sprint(histogramSet.ToJSON()), |
| pretty.Sprint(expectedJSON), |
| ) |
| } |
| }) |
| } |
| |
| func TestAddSharedDiagnostic(t *testing.T) { |
| t.Run("When there are no Histograms in the set", func(t *testing.T) { |
| diagnostic := &catapult.GenericSetDiagnostic{ |
| Type: catapult.DiagnosticTypeGenericSet, |
| GUID: "test_guid", |
| Values: []interface{}{"test_value"}, |
| } |
| histogramSet := catapult.HistogramSet{} |
| histogramSet.AddSharedDiagnostic("test_diagnostic", diagnostic) |
| |
| expectedJSON := []interface{}{diagnostic} |
| if !reflect.DeepEqual(histogramSet.ToJSON(), expectedJSON) { |
| t.Errorf("Invalid HistogramSet. Got: %+v\nExpected: %+v", |
| pretty.Sprint(histogramSet.ToJSON()), |
| pretty.Sprint(expectedJSON)) |
| } |
| |
| }) |
| |
| t.Run("When there are existing Histograms in the set", func(t *testing.T) { |
| diagnostic := &catapult.GenericSetDiagnostic{ |
| Type: catapult.DiagnosticTypeGenericSet, |
| GUID: "test_guid", |
| Values: []interface{}{"test_value"}, |
| } |
| |
| histogramSet := catapult.HistogramSet{} |
| histogramSet.AddHistogram(catapult.Histogram{ |
| Name: "test_histogram_1", |
| Diagnostics: make(map[string]string), |
| }) |
| histogramSet.AddHistogram(catapult.Histogram{ |
| Name: "test_histogram_2", |
| // Intentionally leaving this Diagnostics map as nil to |
| // make sure the map still recieves a reference to the |
| // new Diagnostic. |
| }) |
| histogramSet.AddSharedDiagnostic("test_diagnostic", diagnostic) |
| |
| // Each histogram in the set should have a reference to the diagnostic in its |
| // Diagnostics map. |
| expectedJSON := []interface{}{ |
| diagnostic, |
| catapult.Histogram{ |
| Name: "test_histogram_1", |
| Diagnostics: map[string]string{"test_diagnostic": "test_guid"}, |
| }, |
| catapult.Histogram{ |
| Name: "test_histogram_2", |
| Diagnostics: map[string]string{"test_diagnostic": "test_guid"}, |
| }, |
| } |
| |
| if !reflect.DeepEqual(histogramSet.ToJSON(), expectedJSON) { |
| t.Errorf("Invalid HistogramSet. Got: %+v\nExpected: %+v", |
| pretty.Sprint(histogramSet.ToJSON()), |
| pretty.Sprint(expectedJSON)) |
| } |
| }) |
| } |