Browse Source

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

pull/533/head
Xavier Petit 5 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 ```console
student@ubuntu:~/[[ROOT]]/printprogramname$ go build main.go student@ubuntu:~/[[ROOT]]/printprogramname$ go build main.go
student@ubuntu:~/[[ROOT]]/printprogramname$ ./main student@ubuntu:~/[[ROOT]]/printprogramname$ ./main
./main main
student@ubuntu:~/[[ROOT]]/printprogramname$ go build
student@ubuntu:~/[[ROOT]]/printprogramname$ ./printprogramname
printprogramname
student@ubuntu:~/[[ROOT]]/printprogramname$ student@ubuntu:~/[[ROOT]]/printprogramname$
``` ```

29
tests/go/printprogramname_test.go

@ -1,30 +1,19 @@
package student_test package student_test
import ( import (
"strings" "bytes"
"os/exec"
"testing" "testing"
"github.com/01-edu/z01"
) )
func TestPrintProgramName(t *testing.T) { func TestPrintProgramName(t *testing.T) {
exercise := strings.ToLower( b, err := exec.Command("go", "run", "./student/printprogramname").Output()
strings.TrimPrefix(t.Name(), "Test")) if err != nil {
out, err1 := z01.MainOut("./student/printprogramname") t.Fatal(err)
if err1 != nil {
t.Fatalf(err1.Error())
}
correct, err2 := z01.MainOut("./solutions/printprogramname")
if err2 != nil {
t.Fatalf(err2.Error())
} }
if string(bytes.TrimSpace(b)) != "printprogramname" {
arrOut := strings.Split(out, "/") t.Fatal("Failed to print the program name")
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])
} }
} }
// 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 ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
) )
func main() { func main() {
fmt.Println(os.Args[0]) fmt.Println(filepath.Base(os.Args[0]))
} }

Loading…
Cancel
Save