func testInputFile(sourceFilePath string, t *testing.T) { sourceDirPart, sourceFileNamePart := filepath.Split(sourceFilePath) parts := strings.Split(sourceFileNamePart, ".") testName := parts[0] outputFilePath := sourceDirPart + testName + ".out" input, errIn := util.ReadFile(sourceFilePath) if errIn != nil { t.Errorf("Error reading file <" + sourceFilePath + ">: " + errIn.Error()) return } expectedRaw, errOut := util.ReadFile(outputFilePath) if errOut != nil { t.Errorf("Error reading file <" + outputFilePath + ">: " + errOut.Error()) return } // Remove any carriage return line endings from .out file expectedWithUntrimmed := strings.Replace(expectedRaw, "\r", "", -1) expected := strings.TrimSpace(expectedWithUntrimmed) nodes, errors := parser.Parse(input, sourceFilePath) if errors.Len() != 0 { verify(t, sourceFilePath, input, expected, errors.String()) } else { e := interpreter.NewTopLevelMapEnv() var outputBuffer bytes.Buffer dummyReadLine := func() string { return "text from dummy read line" } var result ast.Node var evalError error for _, n := range nodes { result, evalError = interpreter.Eval(e, n, &outputBuffer, dummyReadLine) if evalError != nil { break } } actual := (&outputBuffer).String() if evalError == nil { //DEBUG fmt.Printf("RESULT(%v): %v\n", sourceFilePath, result) if result != nil { actual = actual + result.String() } } else { actual = actual + evalError.Error() } verify(t, sourceFilePath, input, expected, actual) } }
func loadFile(fileName string, env interpreter.Env, standardReadLine func() string) { if len(fileName) > 0 { content, err := util.ReadFile(fileName) if err != nil { fmt.Printf("Error while loading file <%v>: %v\n", fileName, err.Error()) } else { interpreter.ParseEvalPrint(env, content, standardReadLine, fileName, false) } } }
func primLoad(e Env, head ast.Node, args []ast.Node) ast.Node { arg := args[0] switch val := arg.(type) { case *ast.Str: fileName := val.Value if len(fileName) > 0 { content, err := util.ReadFile(fileName) if err != nil { panicEvalError( arg, fmt.Sprintf("Error while loading file <%v>: %v\n", fileName, err.Error())) } else { ParseEvalPrint(e, content, readLine, fileName, false) } } return &ast.Nil{} } panicEvalError(arg, "Argument to 'load' not a string: "+arg.String()) return nil }