| // Copyright 2022 The Fuchsia Authors. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package sizes |
| |
| import ( |
| "testing" |
| |
| "github.com/google/go-cmp/cmp" |
| ) |
| |
| func TestParseBinarySizes(t *testing.T) { |
| t.Parallel() |
| |
| tests := []struct { |
| name string |
| raw map[string]any |
| expected BinarySizes |
| expectErr bool |
| }{ |
| { |
| name: "two components", |
| raw: map[string]any{ |
| "componentA": float64(30), |
| "componentA.budget": float64(50), |
| "componentA.creepBudget": float64(10), |
| "componentA.owner": "owner@componentA.dev", |
| "componentB": float64(20), |
| "componentB.budget": float64(40), |
| "componentB.creepBudget": float64(5), |
| "componentB.owner": "owner@componentB.dev", |
| }, |
| expected: BinarySizes{ |
| "componentA": { |
| Size: 30, |
| Budget: 50, |
| CreepBudget: 10, |
| }, |
| "componentB": { |
| Size: 20, |
| Budget: 40, |
| CreepBudget: 5, |
| }, |
| }, |
| expectErr: false, |
| }, |
| { |
| name: "unexpected non-float64", |
| raw: map[string]any{ |
| "componentA": "30", |
| "componentA.creepBudget": float64(10), |
| "componentA.owner": "owner@componentA.dev", |
| }, |
| expected: nil, |
| expectErr: true, |
| }, |
| { |
| name: "missing budget", |
| raw: map[string]any{ |
| "componentA": float64(30), |
| "componentA.creepBudget": float64(10), |
| "componentA.owner": "owner@componentA.dev", |
| }, |
| expected: nil, |
| expectErr: true, |
| }, |
| { |
| name: "missing creep budget", |
| raw: map[string]any{ |
| "componentA": float64(30), |
| "componentA.budget": float64(50), |
| "componentA.owner": "owner@componentA.dev", |
| }, |
| expected: nil, |
| expectErr: true, |
| }, |
| } |
| |
| for _, test := range tests { |
| t.Run(test.name, func(t *testing.T) { |
| parsed, err := Parse(test.raw) |
| if err != nil { |
| if test.expectErr { |
| return |
| } |
| t.Fatalf("got unexpected error: %s", err) |
| } else if test.expectErr { |
| t.Fatalf("expected error, got nil") |
| } |
| if diff := cmp.Diff(test.expected, parsed); diff != "" { |
| t.Fatalf("different (-want +got):\n%s", diff) |
| } |
| }) |
| } |
| } |