aboutsummaryrefslogtreecommitdiff
path: root/cmd/planr/sub/evaluate.go
blob: 95f88d035b49387209643fa1a74bd4df665caaff (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
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) {
  for _, tr := range results.TestResults {
    tcPprint(tr)
  }

  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")

  f.Parse(params)

  dieIncompatibleVersion(cfg)

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