Skip to content

Commit

Permalink
allow simple expansion variables to start with a number
Browse files Browse the repository at this point in the history
Added tests for numbered expansion variables.
Fixed a formatting verb for `p.op` rune.
  • Loading branch information
Parzival-3141 committed Jan 17, 2024
1 parent 948c005 commit 9735229
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (p itemParamOp) Eval(mapping Getter, stream chan item) (string, error) {
return "", errors.New(val)
}

return "", fmt.Errorf("unexpected op: %s", p.op)
return "", fmt.Errorf("unexpected op: %q", p.op)
}

// Returns the evaluation of the stream items against the given mapping.
Expand Down Expand Up @@ -286,7 +286,7 @@ func lexStartExpansion(l *lexer) stateFn {
l.ignore()
l.depth++
return lexBracketName
case isAlpha(c):
case isAlphaNum(c):
return lexSimpleName
}
return nil // FIXME
Expand Down
26 changes: 26 additions & 0 deletions posix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var paramtests = []struct {
// Names, no brackets
{"$set", "yes", ""},
{"$set$set2", "yesyes-two", ""},
{"/home/$set/$set2", "/home/yes/yes-two", ""},

// Default
{"${set:-word}", "yes", ""},
Expand Down Expand Up @@ -211,3 +212,28 @@ func TestExpand_assign(t *testing.T) {
equals(t, "word", x)
equals(t, map[string]string{"unset": "word"}, mapping)
}

func TestExpand_shellPositionalArg(t *testing.T) {
mapping := map[string]string{
"1": "foo",
"2": "bar",
}

params := []struct {
in string
out string
}{
{"$1$2", "foobar"},
{"/home/$1/$2", "/home/foo/bar"},
}

for _, tt := range params {
x, err := Expand(tt.in, Map(mapping))
if err != nil {
t.Errorf("pattern %#v should not have produced an error, but got: %s", tt.in, err)
}
if x != tt.out {
t.Errorf("pattern %#v should expand to %#v, but got %#v", tt.in, tt.out, x)
}
}
}

0 comments on commit 9735229

Please sign in to comment.