blob: b467f60a87d3d09c7b89581a64e2da6f1b4faa77 [file] [log] [blame]
// Copyright 2020 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 props
import (
"testing"
structpb "github.com/golang/protobuf/ptypes/struct"
buildbucketpb "go.chromium.org/luci/buildbucket/proto"
)
func TestGetStringInputProperty(t *testing.T) {
t.Parallel()
tests := []struct {
build *buildbucketpb.Build
expectedValue string
expectErr bool
}{
{
build: &buildbucketpb.Build{
Input: &buildbucketpb.Build_Input{
Properties: &structpb.Struct{
Fields: map[string]*structpb.Value{
"string": {
Kind: &structpb.Value_StringValue{
StringValue: "foo",
},
},
},
},
},
},
expectedValue: "foo",
},
{
build: &buildbucketpb.Build{
Input: &buildbucketpb.Build_Input{
Properties: &structpb.Struct{},
},
},
expectedValue: "",
},
{
build: &buildbucketpb.Build{
Input: &buildbucketpb.Build_Input{
Properties: &structpb.Struct{
Fields: map[string]*structpb.Value{
"string": {
Kind: &structpb.Value_NumberValue{
NumberValue: 123,
},
},
},
},
},
},
expectErr: true,
},
}
for _, test := range tests {
value, err := GetStringInputProperty(test.build, "string")
if err == nil {
if test.expectErr {
t.Fatalf("expected error, got nil")
}
} else if !test.expectErr {
t.Fatalf("got unexpected error %v", err)
}
if value != test.expectedValue {
t.Fatalf("expected string value %s, got %s", test.expectedValue, value)
}
}
}
func TestGetBoolInputProperty(t *testing.T) {
t.Parallel()
tests := []struct {
build *buildbucketpb.Build
expectValue bool
expectErr bool
}{
{
build: &buildbucketpb.Build{
Input: &buildbucketpb.Build_Input{
Properties: &structpb.Struct{
Fields: map[string]*structpb.Value{
"bool": {
Kind: &structpb.Value_BoolValue{
BoolValue: true,
},
},
},
},
},
},
expectValue: true,
},
{
build: &buildbucketpb.Build{
Input: &buildbucketpb.Build_Input{
Properties: &structpb.Struct{},
},
},
expectValue: false,
},
{
build: &buildbucketpb.Build{
Input: &buildbucketpb.Build_Input{
Properties: &structpb.Struct{
Fields: map[string]*structpb.Value{
"bool": {
Kind: &structpb.Value_NumberValue{
NumberValue: 123,
},
},
},
},
},
},
expectErr: true,
},
}
for _, test := range tests {
value, err := GetBoolInputProperty(test.build, "bool")
if err == nil {
if test.expectErr {
t.Fatalf("expected error, got nil")
}
} else if !test.expectErr {
t.Fatalf("got unexpected error %v", err)
}
if value != test.expectValue {
t.Fatalf("expected bool value %v, got %v", test.expectValue, value)
}
}
}
func TestSetInputProperty(t *testing.T) {
t.Parallel()
tests := []struct {
build *buildbucketpb.Build
key string
value interface{}
expectErr bool
}{
{
build: &buildbucketpb.Build{
Input: &buildbucketpb.Build_Input{
Properties: &structpb.Struct{},
},
},
key: "foo",
value: "bar",
},
{
build: &buildbucketpb.Build{
Input: &buildbucketpb.Build_Input{
Properties: &structpb.Struct{
Fields: map[string]*structpb.Value{
"foo": {
Kind: &structpb.Value_StructValue{
StructValue: &structpb.Struct{
Fields: map[string]*structpb.Value{
"foo": {Kind: &structpb.Value_StringValue{
StringValue: "",
}},
},
},
},
},
},
},
},
},
key: "foo",
value: "bar",
},
}
for _, test := range tests {
err := SetInputProperty(test.build, test.key, test.value)
if err == nil {
if test.expectErr {
t.Fatalf("expected error, got nil")
}
} else if !test.expectErr {
t.Fatalf("got unexpected error %v", err)
}
gotValue, err := GetStringInputProperty(test.build, test.key)
if err == nil {
if test.expectErr {
t.Fatalf("expected error, got nil")
}
} else if !test.expectErr {
t.Fatalf("got unexpected error %v", err)
}
if test.value != gotValue {
t.Fatalf("expected value %s, got %s", test.value, gotValue)
}
}
}