Prechádzať zdrojové kódy

Fixup for #1618.

- Simplify the matcher for compilation errors.

- Various stylistic fixes.
Qi Xiao 1 rok pred
rodič
commit
93e28d846b

+ 1 - 1
pkg/eval/builtin_special.go

@@ -133,7 +133,7 @@ func compileDel(cp *compiler, fn *parse.Form) effectOp {
 		}
 		head, indices := cn.Indexings[0].Head, cn.Indexings[0].Indices
 		if head.Type == parse.Variable {
-			cp.errorpf(cn, "arguments to del must omit the dollar-sign")
+			cp.errorpf(cn, "arguments to del must omit the dollar sign")
 		} else if !parse.ValidLHSVariable(head, false) {
 			cp.errorpf(cn, delArgMsg)
 		}

+ 1 - 1
pkg/eval/builtin_special_test.go

@@ -224,7 +224,7 @@ func TestDel(t *testing.T) {
 		// Deleting variable in non-local namespace
 		That("var a: = (ns [&b=$nil])", "del a:b").DoesNotCompile("only variables in the local scope or E: can be deleted"),
 		// Variable name given with $
-		That("var x = 1; del $x").DoesNotCompile("arguments to del must omit the dollar-sign"),
+		That("var x = 1; del $x").DoesNotCompile("arguments to del must omit the dollar sign"),
 		// Variable name not given as a single primary expression
 		That("var ab = 1; del a'b'").DoesNotCompile("arguments to del must be variable or variable elements"),
 		// Variable name not a string

+ 1 - 1
pkg/eval/compile_effect_test.go

@@ -172,7 +172,7 @@ func TestCommand_LegacyTemporaryAssignment(t *testing.T) {
 		// Using syntax of temporary assignment for non-temporary assignment no
 		// longer compiles
 		That("x=y").DoesNotCompile(
-			MatchingRegexp{Pattern: "^using the syntax of temporary assignment"}),
+			`using the syntax of temporary assignment for non-temporary assignment is no longer supported; use "var" or "set" instead`),
 	)
 }
 

+ 1 - 1
pkg/eval/compile_value_test.go

@@ -129,7 +129,7 @@ func TestTilde_ErrorForCurrentUser(t *testing.T) {
 
 func TestWildcard(t *testing.T) {
 	Test(t,
-		That("put ***").DoesNotCompile("bad wildcard: \"***\""),
+		That("put ***").DoesNotCompile(`bad wildcard: "***"`),
 	)
 	// More tests in glob_test.go
 }

+ 3 - 11
pkg/eval/evaltest/evaltest.go

@@ -17,7 +17,6 @@ package evaltest
 
 import (
 	"bytes"
-	"fmt"
 	"os"
 	"reflect"
 	"strings"
@@ -123,16 +122,9 @@ func (c Case) Throws(reason error, stacks ...string) Case {
 }
 
 // DoesNotCompile returns an altered Case that requires the source code to fail
-// compilation with a specific error. It accepts a string or a MatchingRegexp{}.
-func (c Case) DoesNotCompile(ce any) Case {
-	switch v := ce.(type) {
-	case string:
-		c.want.CompilationError = stringError{isRegexp: false, str: v}
-	case MatchingRegexp:
-		c.want.CompilationError = stringError{isRegexp: true, str: v.Pattern}
-	default:
-		panic(fmt.Sprintf("unrecognized DoesNotCompile() arg type: %T", ce))
-	}
+// compilation with a specific error message.
+func (c Case) DoesNotCompile(msg string) Case {
+	c.want.CompilationError = compilationError{msg}
 	return c
 }
 

+ 8 - 22
pkg/eval/evaltest/matchers.go

@@ -42,32 +42,18 @@ func matchRegexp(p, s string) bool {
 
 type errorMatcher interface{ matchError(error) bool }
 
-// An errorMatcher for a string literal or regexp.
-type stringError struct {
-	isRegexp bool
-	str      string
+// An errorMatcher for compilation errors.
+type compilationError struct {
+	msg string
 }
 
-func (se stringError) Error() string {
-	if !se.isRegexp {
-		return "literal: " + se.str
-	}
-	return "regexp: " + se.str
+func (e compilationError) Error() string {
+	return "compilation error with message: " + e.msg
 }
 
-func (se stringError) matchError(e error) bool {
-	ce := eval.GetCompilationError(e)
-	if ce == nil {
-		return false
-	}
-	if !se.isRegexp {
-		return ce.Message == se.str
-	}
-	matched, err := regexp.MatchString(se.str, ce.Message)
-	if err != nil {
-		panic(err)
-	}
-	return matched
+func (e compilationError) matchError(e2 error) bool {
+	ce := eval.GetCompilationError(e2)
+	return ce != nil && ce.Message == e.msg
 }
 
 // An errorMatcher for exceptions.