Browse Source

Treats the case of not passing a golang source file

Also fixes the test and adds a test case for an empty file
content-update
Augusto 4 years ago committed by xpetit
parent
commit
8d1b051f4b
  1. 31
      go/tests/rc/rc.go
  2. 48
      go/tests/rc/rc_test.go
  3. 0
      rc/tests/empty

31
go/tests/rc/rc.go

@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
@ -90,32 +91,41 @@ passed to the program would not be allowed`,
flag.BoolVar(&casting, "cast", false, "Allowes casting")
flag.BoolVar(&noArrays, "no-arrays", false, "Disallowes all array types")
flag.BoolVar(&allowBuiltin, "allow-builtin", false, "Allowes all builtin functions and casting")
sort.Sort(sort.StringSlice(os.Args[1:]))
}
func main() {
flag.Parse()
filename := goFile(flag.Args())
if filename == "" {
fmt.Println("No file to analyse")
os.Exit(1)
}
if flag.NArg() < 1 {
fmt.Println("Not enough arguments: missing file")
os.Exit(1)
}
fmt.Println("Parsing:")
err := parseArgs(flag.Args()[1:], allowBuiltin, casting)
err := parseArgs(flag.Args(), allowBuiltin, casting)
if err != nil {
panic(err)
fmt.Println(err)
os.Exit(1)
}
filename := flag.Arg(0)
load := make(loadedSource)
currentPath := filepath.Dir(flag.Arg(0))
currentPath := filepath.Dir(filename)
err = loadProgram(currentPath, load)
if err != nil {
panic(err)
fmt.Println(err)
os.Exit(1)
}
fmt.Println("\tOk")
@ -130,6 +140,15 @@ func main() {
fmt.Println("\tOk")
}
func goFile(args []string) string {
for _, v := range args {
if strings.HasSuffix(v, ".go") {
return v
}
}
return ""
}
// Returns the smallest block containing the position pos. It can
// return nil if `pos` is not inside any ast.BlockStmt
func smallestBlock(pos token.Pos, blocks []*ast.BlockStmt) *ast.BlockStmt {

48
go/tests/rc/rc_test.go

@ -1,7 +1,7 @@
package main
import (
"fmt"
"sort"
"strings"
"testing"
@ -106,7 +106,7 @@ Cheating:
Cheating:
Ok
`,
`test/testingWrapping.go`: `Parsing:
`tests/testingWrapping.go`: `Parsing:
Ok
Cheating:
TYPE: NAME: LOCATION:
@ -117,10 +117,12 @@ Cheating:
illegal-access util.LenWrapperU tests/testingWrapping.go:8:9
illegal-definition Length tests/testingWrapping.go:7:1
`,
`test/testingWrapping.go len`: `Parsing:
`tests/testingWrapping.go len`: `Parsing:
Ok
Cheating:
Ok
`,
`tests/empty len`: `No file to analyse
`,
}
Compare(t, argsAndSolution)
@ -130,29 +132,27 @@ func Compare(t *testing.T, argsAndSol map[string]string) {
for args, sol := range argsAndSol {
a := strings.Split(args, " ")
out, err := z01.MainOut("../rc", a...)
if EqualResult(out, sol) && err != nil && EqualResult(err.Error(), sol) {
fmt.Println(args, "\nError:", err)
fmt.Println("Solution:", sol)
// fmt.Println("Out:", out)
t.Errorf("./rc %s prints %q\n instead of %q\n", args, out, sol)
if !EqualResult(sol, out) {
if err == nil {
t.Errorf("./rc %s prints %q\n instead of %q\n", args, out, sol)
}
if err != nil && !EqualResult(sol, err.Error()) {
t.Errorf("./rc %s prints %q\n instead of %q\n", args, err.Error(), sol)
}
}
}
}
func EqualResult(out, sol string) bool {
for _, v := range strings.Split(out, "\n") {
if strings.Contains(v, sol) {
return true
}
}
return false
}
func ExtractFile(args []string) string {
for _, v := range args {
if strings.HasSuffix(v, ".go") {
return v
}
}
return ""
func EqualResult(sol, out string) bool {
// split
solSli := strings.Split(sol, "\n")
outSli := strings.Split(out, "\n")
// sort
sort.Sort(sort.StringSlice(solSli))
sort.Sort(sort.StringSlice(outSli))
// join
sol = strings.Join(solSli, "\n")
out = strings.Join(outSli, "\n")
// compare
return sol == out
}

Loading…
Cancel
Save