summaryrefslogtreecommitdiff
path: root/adapters/gtest/results.go
blob: 14f5d1d162dd16b3dcb0b18666e22fcc7ab17c5b (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package gtest

import (
	"bytes"
	"encoding/json"
	"io"
	"time"
)

type gFailure struct {
  Failure string `json:"failure"`
  Type    string `json:"type"`
}

type gTestsuite struct {
  Name      string     `json:"name"`
  Status    string     `json:"status"`
  Result    string     `json:"result"`
  Timestamp time.Time  `json:"timestamp"`
  Time      string     `json:"time"`
  Classname string     `json:"classname"`
  Failures  []gFailure `json:"failures"`
}

type gTestsuites struct {
  Name      string       `json:"name"`
  Tests     int          `json:"tests"`
  Failures  int          `json:"failures"`
  Disabled  int          `json:"disabled"`
  Errors    int          `json:"errors"`
  Timestamp time.Time    `json:"timestamp"`
  Time      string       `json:"time"`
  Testsuite []gTestsuite `json:"testsuite"`
}

type gResults struct {
  Tests      int           `json:"tests"`
  Failures   int           `json:"failures"`
  Disabled   int           `json:"disabled"`
  Errors     int           `json:"errors"`
  Timestamp  time.Time     `json:"timestamp"`
  Time       string        `json:"time"`
  Name       string        `json:"name"`
  Testsuites []gTestsuites `json:"testsuites"`
}

type Result struct {
  id         string
  pass       bool
  failureMsg string
  testOutput string
}

func failureMsg(failures []gFailure) string {
  failure_msg := ""

  for _, failure := range failures {
    failure_msg += failure.Failure + "\n"
  }

  return failure_msg
}

func decodeResults(r io.Reader) ([]Result, error) {
  decoded := make([]Result, 0)
  
  var results gResults
  buf := bytes.Buffer{}

  if _, err := buf.ReadFrom(r); err != nil {
    return decoded, err
  }

  if err := json.Unmarshal(buf.Bytes(), &results); err != nil {
    return decoded, err
  }

  for _, suite := range results.Testsuites {
    for _, test := range suite.Testsuite {
      n := len(test.Failures)


      res := Result {
        id: suite.Name + "." + test.Name,
        pass: n == 0,
        failureMsg: failureMsg(test.Failures),
      }


      decoded = append(decoded, res)
    }
  }

  return decoded, nil
}