1/* Copyright (c) 2018, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15// testresult is an implementation of Chromium's JSON test result format. See
16// https://chromium.googlesource.com/chromium/src/+/master/docs/testing/json_test_results_format.md
17package testresult
18
19import (
20	"encoding/json"
21	"fmt"
22	"os"
23	"time"
24)
25
26// Results stores the top-level test results.
27type Results struct {
28	Version           int               `json:"version"`
29	Interrupted       bool              `json:"interrupted"`
30	PathDelimiter     string            `json:"path_delimiter"`
31	SecondsSinceEpoch float64           `json:"seconds_since_epoch"`
32	NumFailuresByType map[string]int    `json:"num_failures_by_type"`
33	Tests             map[string]Result `json:"tests"`
34}
35
36func NewResults() *Results {
37	return &Results{
38		Version:           3,
39		PathDelimiter:     ".",
40		SecondsSinceEpoch: float64(time.Now().UnixNano()) / float64(time.Second/time.Nanosecond),
41		NumFailuresByType: make(map[string]int),
42		Tests:             make(map[string]Result),
43	}
44}
45
46func (t *Results) addResult(name, result, expected string) {
47	if _, found := t.Tests[name]; found {
48		panic(fmt.Sprintf("duplicate test name %q", name))
49	}
50	t.Tests[name] = Result{
51		Actual:       result,
52		Expected:     expected,
53		IsUnexpected: result != expected,
54	}
55	t.NumFailuresByType[result]++
56}
57
58// AddResult records a test result with the given result string. The test is a
59// failure if the result is not "PASS".
60func (t *Results) AddResult(name, result string) {
61	t.addResult(name, result, "PASS")
62}
63
64// AddSkip marks a test as being skipped. It is not considered a failure.
65func (t *Results) AddSkip(name string) {
66	t.addResult(name, "SKIP", "SKIP")
67}
68
69func (t *Results) HasUnexpectedResults() bool {
70	for _, r := range t.Tests {
71		if r.IsUnexpected {
72			return false
73		}
74	}
75	return true
76}
77
78func (t *Results) WriteToFile(name string) error {
79	file, err := os.Create(name)
80	if err != nil {
81		return err
82	}
83	defer file.Close()
84	out, err := json.MarshalIndent(t, "", "  ")
85	if err != nil {
86		return err
87	}
88	_, err = file.Write(out)
89	return err
90}
91
92type Result struct {
93	Actual       string `json:"actual"`
94	Expected     string `json:"expected"`
95	IsUnexpected bool   `json:"is_unexpected"`
96}
97