Browse Source

Make printprogramname subject, test & solution more consistent and slightly more difficult

content-update
Xavier Petit 4 years ago committed by xpetit
parent
commit
96af961a38
  1. 5
      subjects/printprogramname.en.md
  2. 29
      tests/go/printprogramname_test.go
  3. 3
      tests/go/solutions/printprogramname/programname.go

5
subjects/printprogramname.en.md

@ -9,6 +9,9 @@ Example of output :
```console
student@ubuntu:~/[[ROOT]]/printprogramname$ go build main.go
student@ubuntu:~/[[ROOT]]/printprogramname$ ./main
./main
main
student@ubuntu:~/[[ROOT]]/printprogramname$ go build
student@ubuntu:~/[[ROOT]]/printprogramname$ ./printprogramname
printprogramname
student@ubuntu:~/[[ROOT]]/printprogramname$
```

29
tests/go/printprogramname_test.go

@ -1,30 +1,19 @@
package student_test
import (
"strings"
"bytes"
"os/exec"
"testing"
"github.com/01-edu/z01"
)
func TestPrintProgramName(t *testing.T) {
exercise := strings.ToLower(
strings.TrimPrefix(t.Name(), "Test"))
out, err1 := z01.MainOut("./student/printprogramname")
if err1 != nil {
t.Fatalf(err1.Error())
}
correct, err2 := z01.MainOut("./solutions/printprogramname")
if err2 != nil {
t.Fatalf(err2.Error())
b, err := exec.Command("go", "run", "./student/printprogramname").Output()
if err != nil {
t.Fatal(err)
}
arrOut := strings.Split(out, "/")
ArrCor := strings.Split(correct, "/")
if ArrCor[len(ArrCor)-1] != arrOut[len(arrOut)-1] {
t.Fatalf("./%s prints %q instead of %q\n",
exercise, arrOut[len(arrOut)-1], ArrCor[len(ArrCor)-1])
if string(bytes.TrimSpace(b)) != "printprogramname" {
t.Fatal("Failed to print the program name")
}
}
// TODO: add more test cases (different program names), to do so compile then rename and test the binary several times

3
tests/go/solutions/printprogramname/programname.go

@ -3,8 +3,9 @@ package main
import (
"fmt"
"os"
"path/filepath"
)
func main() {
fmt.Println(os.Args[0])
fmt.Println(filepath.Base(os.Args[0]))
}

Loading…
Cancel
Save