• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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	"os"
22	"time"
23)
24
25// Results stores the top-level test results.
26type Results struct {
27	Version           int               `json:"version"`
28	Interrupted       bool              `json:"interrupted"`
29	PathDelimiter     string            `json:"path_delimiter"`
30	SecondsSinceEpoch float64           `json:"seconds_since_epoch"`
31	NumFailuresByType map[string]int    `json:"num_failures_by_type"`
32	Tests             map[string]Result `json:"tests"`
33}
34
35func NewResults() *Results {
36	return &Results{
37		Version:           3,
38		PathDelimiter:     ".",
39		SecondsSinceEpoch: float64(time.Now().UnixNano()) / float64(time.Second/time.Nanosecond),
40		NumFailuresByType: make(map[string]int),
41		Tests:             make(map[string]Result),
42	}
43}
44
45func (t *Results) addResult(name, result, expected string) {
46	if _, found := t.Tests[name]; found {
47		panic(name)
48	}
49	t.Tests[name] = Result{
50		Actual:       result,
51		Expected:     expected,
52		IsUnexpected: result != expected,
53	}
54	t.NumFailuresByType[result]++
55}
56
57// AddResult records a test result with the given result string. The test is a
58// failure if the result is not "PASS".
59func (t *Results) AddResult(name, result string) {
60	t.addResult(name, result, "PASS")
61}
62
63// AddSkip marks a test as being skipped. It is not considered a failure.
64func (t *Results) AddSkip(name string) {
65	t.addResult(name, "SKIP", "SKIP")
66}
67
68func (t *Results) HasUnexpectedResults() bool {
69	for _, r := range t.Tests {
70		if r.IsUnexpected {
71			return false
72		}
73	}
74	return true
75}
76
77func (t *Results) WriteToFile(name string) error {
78	file, err := os.Create(name)
79	if err != nil {
80		return err
81	}
82	defer file.Close()
83	out, err := json.MarshalIndent(t, "", "  ")
84	if err != nil {
85		return err
86	}
87	_, err = file.Write(out)
88	return err
89}
90
91type Result struct {
92	Actual       string `json:"actual"`
93	Expected     string `json:"expected"`
94	IsUnexpected bool   `json:"is_unexpected"`
95}
96