|
| 1 | +package unit1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "go/ast" |
| 7 | + "go/parser" |
| 8 | + "go/token" |
| 9 | + "io" |
| 10 | + "math" |
| 11 | + "os" |
| 12 | + "os/exec" |
| 13 | + "strconv" |
| 14 | + "strings" |
| 15 | + "testing" |
| 16 | + |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | +) |
| 19 | + |
| 20 | +func TestExercise0(t *testing.T) { |
| 21 | + codepath := "../../unit1/exercises/e0/main.go" |
| 22 | + var callVal string |
| 23 | + t.Run("Syntax", newTestExercise0_Syntax(codepath, &callVal)) |
| 24 | + t.Run("Output", newTestExercise0_Output(codepath, &callVal)) |
| 25 | +} |
| 26 | + |
| 27 | +func newTestExercise0_Syntax(codepath string, callValue *string) func(t *testing.T) { |
| 28 | + |
| 29 | + return func(t *testing.T) { |
| 30 | + sqrtDefinedinMainScope := false |
| 31 | + sqrtDefined := false |
| 32 | + sqrtArgIsInt := false |
| 33 | + sqrtArgIsFloat := false |
| 34 | + sqrtArgStringValue := "" |
| 35 | + |
| 36 | + fs := token.NewFileSet() |
| 37 | + f, err := parser.ParseFile(fs, codepath, nil, parser.AllErrors) |
| 38 | + if err != nil { |
| 39 | + t.Fatal("can't open file for parsing:", err) |
| 40 | + } |
| 41 | + |
| 42 | + ast.Inspect(f, func(n ast.Node) bool { |
| 43 | + if n == nil { |
| 44 | + return false |
| 45 | + } |
| 46 | + // t.Log(n, fmt.Sprintf("%T", n)) |
| 47 | + funcDecl, ok := n.(*ast.FuncDecl) |
| 48 | + if ok { |
| 49 | + if funcDecl.Name.Name == "Sqrt" { |
| 50 | + sqrtDefined = true |
| 51 | + return true |
| 52 | + } |
| 53 | + } |
| 54 | + callExpr, ok := n.(*ast.CallExpr) |
| 55 | + if ok { |
| 56 | + ident, ok := (callExpr.Fun).(*ast.Ident) |
| 57 | + if ok { |
| 58 | + if ident.String() == "Sqrt" { |
| 59 | + if !assert.Equal(t, 1, len(callExpr.Args), "wrong number of arguments in Sqrt call") { |
| 60 | + return false |
| 61 | + } |
| 62 | + arg := callExpr.Args[0] |
| 63 | + basicLit, ok := arg.(*ast.BasicLit) |
| 64 | + if ok { |
| 65 | + switch basicLit.Kind { |
| 66 | + case token.INT: |
| 67 | + sqrtArgIsInt = true |
| 68 | + sqrtArgStringValue = basicLit.Value |
| 69 | + case token.FLOAT: |
| 70 | + sqrtArgIsFloat = true |
| 71 | + sqrtArgStringValue = basicLit.Value |
| 72 | + default: |
| 73 | + assert.Fail(t, "Argument for Sqrt call must be a number") |
| 74 | + return false |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + return true |
| 83 | + }) |
| 84 | + |
| 85 | + for _, obj := range f.Scope.Objects { |
| 86 | + if obj.Kind == ast.Fun && obj.Name == "Sqrt" { |
| 87 | + sqrtDefinedinMainScope = true |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + assert.True(t, sqrtDefined, "Sqrt function is not defined") |
| 92 | + assert.True(t, sqrtDefinedinMainScope, "Sqrt function is not defined in main package") |
| 93 | + assert.True(t, sqrtArgIsFloat || sqrtArgIsInt, "Sqrt argument argument in Sqrt call must be a number") |
| 94 | + assert.NotEmpty(t, sqrtArgStringValue, "Sqart call argument must not be empty") |
| 95 | + |
| 96 | + *callValue = sqrtArgStringValue |
| 97 | + } |
| 98 | +} |
| 99 | +func newTestExercise0_Output(codepath string, callValue *string) func(t *testing.T) { |
| 100 | + return func(t *testing.T) { |
| 101 | + if !assert.NotNil(t, callValue, "cellValue must not be empty") { |
| 102 | + assert.FailNow(t, "test error: cellValue is empty") |
| 103 | + } |
| 104 | + t.Log("Trying to build and run file:", codepath) |
| 105 | + |
| 106 | + cmd := exec.Command("timeout", "10", "go", "run", codepath) |
| 107 | + tmpDir, err := os.MkdirTemp("", "gotest-*") |
| 108 | + if !assert.NoError(t, err) { |
| 109 | + assert.FailNow(t, "test error: cannot create temporary dir") |
| 110 | + } |
| 111 | + defer os.RemoveAll(tmpDir) |
| 112 | + |
| 113 | + cmd.Env = []string{ |
| 114 | + "PATH=" + os.Getenv("PATH"), |
| 115 | + "CGO_ENABLED=0", |
| 116 | + "GOCACHE=" + tmpDir, |
| 117 | + } |
| 118 | + |
| 119 | + outBuf := bytes.NewBuffer(nil) |
| 120 | + errBuf := bytes.NewBuffer(nil) |
| 121 | + cmd.Stdout = outBuf |
| 122 | + cmd.Stderr = errBuf |
| 123 | + |
| 124 | + err = cmd.Run() |
| 125 | + if !assert.NoError(t, err, errBuf.String()) { |
| 126 | + assert.FailNow(t, "test error: \"go run\" failed", err) |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + assert.NoError(t, err, "Can't compile and run code") |
| 131 | + assert.Empty(t, errBuf.String(), "stderr of your code must be empty", errBuf.String()) |
| 132 | + assert.NotEmpty(t, outBuf.String(), "stdout of your code must not be empty") |
| 133 | + |
| 134 | + arg, err := strconv.ParseFloat(*callValue, 64) |
| 135 | + if !assert.NoError(t, err) { |
| 136 | + assert.FailNow(t, "test error: cannot parse Sqrt argument value:", err) |
| 137 | + } |
| 138 | + |
| 139 | + epsilon := 0.0000000001 |
| 140 | + |
| 141 | + prevVal := arg |
| 142 | + curVal := 0.0 |
| 143 | + sqroot := math.Sqrt(arg) |
| 144 | + |
| 145 | + bfr := bufio.NewReader(outBuf) |
| 146 | + for { |
| 147 | + line, err := bfr.ReadString('\n') |
| 148 | + if err != nil && err != io.EOF { |
| 149 | + assert.FailNow(t, "test error: can't read output buffer", err) |
| 150 | + break |
| 151 | + } |
| 152 | + line = strings.TrimRight(line, "\r\n") |
| 153 | + if line == "" && err != io.EOF { |
| 154 | + continue |
| 155 | + } |
| 156 | + if err == io.EOF { |
| 157 | + break |
| 158 | + } |
| 159 | + curVal, err = strconv.ParseFloat(line, 64) |
| 160 | + if !assert.NoError(t, err) { |
| 161 | + assert.FailNow(t, "test error: cannot parse output line to number:", err) |
| 162 | + } |
| 163 | + assert.Greater(t, prevVal, curVal, "seems to be your code has error, previous iteration is less than following iteration", prevVal, curVal) |
| 164 | + assert.Greater(t, math.Abs(prevVal-curVal), epsilon, "seems to be your code stucked around one value", prevVal, curVal) |
| 165 | + } |
| 166 | + assert.Less(t, sqroot-curVal, epsilon, "seems to be your code has wrong computations, result value is far from real", sqroot, curVal) |
| 167 | + } |
| 168 | +} |
0 commit comments