You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.4 KiB
Go

package shell
import (
"bufio"
"fmt"
"io"
"strings"
"testing"
"git.snorba.art/hugo/nssh/commands/shell/builtins"
"github.com/stretchr/testify/suite"
)
type QuotesTest struct {
suite.Suite
}
func (t *QuotesTest) TestParse() {
expectedContents := fmt.Sprintf("'%s'", "word1 word2 word3\n word4")
parser := &BaseParser{&QuoteParameters{}}
reader := bufio.NewReader(strings.NewReader(expectedContents))
iterator, err := NewCharIterator(reader)
t.NoError(err)
token, err := parser.Parse(iterator, &CharCollection{})
t.ErrorIs(err, io.EOF)
t.Equal(expectedContents, token.String())
expanded, _ := token.(Expandable).Expand(nil, nil, nil)
t.Equal("word1 word2 word3\n word4", strings.Join(expanded, " "))
}
func (t *QuotesTest) TestExpandVariables() {
parser := &BaseParser{&QuoteParameters{}}
reader := bufio.NewReader(
strings.NewReader("This is a $variable ${variable2} $variable"),
)
state := NewShellState()
state.SetVariable("variable", "supergood")
state.SetVariable("variable2", "thing")
echo := &builtins.Echo{}
state.SetCommand(echo.Name(), echo)
iterator, err := NewCharIterator(reader)
t.NoError(err)
token, err := parser.Parse(iterator, &CharCollection{})
t.ErrorIs(err, io.EOF)
expanded, _ := token.(Expandable).Expand(state, nil, nil)
t.Equal("This is a supergood thing supergood", expanded[0])
}
func TestQuotesTest(t *testing.T) {
suite.Run(t, new(QuotesTest))
}