summaryrefslogtreecommitdiff
path: root/cmd/planr/sub/evaluate.go
blob: 21481a1713bb876f5373b613adff74700313289a (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
package sub

import (
	"encoding/json"
	"fmt"
	"log"
        "flag"

	"golang.flu0r1ne.net/planr"
)

type gradingResults struct {
  TestResults []planr.TestResult
  Score       planr.Scoring
}

func prettyPrint(results gradingResults, summarize bool) {
  for _, tr := range results.TestResults {
    tcPprint(tr)
  }

  if summarize {
    printScoring(results.Score)
  }
}

func jsonPrint(results gradingResults) {
  res, err := json.Marshal(results)

  if err != nil {
    log.Fatalf("Error printing JSON: %v\n", err)
  }

  fmt.Println(string(res))
}

func Evaluate(runner planr.Runner, params []string, cfg planr.Config) {
  f := flag.NewFlagSet("evaluate", flag.ExitOnError)

  jsonOutput := f.Bool("json", false, "print json output")

  dieIncompatibleVersion(cfg)

  f.Parse(params)

  tcs := runner.CollectCases()

  // Filter those tests which patch IDs in params
  filteredTcs := make([]planr.TestCase, 0)
  summarizeScore := false
  if f.NArg() > 0 {
    ids := f.Args()

    membershipFun := make(map[string] bool, 0)
    for _, id := range ids {
      membershipFun[id] = true
    }

    for i := range tcs {
      if membershipFun[tcs[i].Cname] {
        filteredTcs = append(filteredTcs, tcs[i])
      }
    }
  } else {
    summarizeScore = true
    filteredTcs = tcs
  }

  trs := runner.Evaluate(filteredTcs)
  
  results := gradingResults {
    TestResults: trs,
    Score:       planr.Score(trs),
  }
  
  if *jsonOutput {
    jsonPrint(results)
  } else {
    prettyPrint(results, summarizeScore)
  }
}