-
Notifications
You must be signed in to change notification settings - Fork 1
/
testcase.go
61 lines (49 loc) · 1.26 KB
/
testcase.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package testcase
import (
"fmt"
"io"
"reflect"
"github.com/google/jsonapi"
)
// List is a list of TestCases, used for index action
type List struct {
TestCases []*TestCase
}
// TestCase represents a single TestCase
type TestCase struct {
ID string `jsonapi:"primary,test_cases"`
Name string `jsonapi:"attr,name"`
Description string `jsonapi:"attr,description"`
}
// Unmarshal unmarshals a list of TestCase records
func Unmarshal(input io.Reader) (List, error) {
items, err := jsonapi.UnmarshalManyPayload(input, reflect.TypeOf(new(TestCase)))
if err != nil {
return List{}, err
}
result := List{}
for _, item := range items {
testcase, ok := item.(*TestCase)
if !ok {
return List{}, fmt.Errorf("Type assertion failed")
}
result.TestCases = append(result.TestCases, testcase)
}
return result, nil
}
// FindByNameOrUID look up a TestCase by name in List
func (testcases List) FindByNameOrUID(nameOrUID string) TestCase {
// first, try to find test case by UID
for _, testCase := range testcases.TestCases {
if testCase.ID == nameOrUID {
return *testCase
}
}
// then, try to find case by name
for _, testCase := range testcases.TestCases {
if testCase.Name == nameOrUID {
return *testCase
}
}
return TestCase{}
}