aboutsummaryrefslogtreecommitdiff
path: root/cmd/planr/sub/evaluate.go
blob: 3f1718fa95eb6dcd8534ab98280e2ca7295f7abc (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
96
97
98
99
100
101
102
103
104
package sub

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

	"golang.flu0r1ne.net/planr"
)

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

func prettyPrint(results gradingResults, verbose, summarize bool) {
  for _, tr := range results.TestResults {
    opts := PRINT_CONCISE

    if verbose {
      opts = opts | PRINT_DESCRIPTION | PRINT_POINTS
    }

    tcPprint(tr, opts)
  }

  if summarize {
    printScoring(results.Score)
  }
}

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

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

  if file == "" {
    fmt.Println(string(res))
    return
  }


  if err := ioutil.WriteFile(file, res, 0664); err != nil {
    log.Fatalf("Could not write JSON file %v", err)
  }
}

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

  jsonOutput := f.Bool("json", false, "print json output")
  jsonFile   := f.String("json_to_file", "", "export json to a file")
  extra      := f.Bool("extra", false, "print extra grading information")

  dieIncompatibleVersion(cfg)

  f.Parse(params)

  if *jsonFile != "" && *jsonOutput {
    log.Fatalf("-json and -json_to_file are mutually exclusive")
  }

  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 || *jsonFile != "" {
    jsonPrint(results, *jsonFile)
  }

  if ! *jsonOutput {
    prettyPrint(results, *extra, summarizeScore)
  }
}