-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b07b667
commit 13795f1
Showing
3 changed files
with
127 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package repl | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func runReplWithString(t *testing.T, input string) (string, error) { | ||
t.Helper() | ||
inR, inW := io.Pipe() | ||
outR, outW := io.Pipe() | ||
|
||
// Start a goroutine to write the input string to the Stdin pipe | ||
go func() { | ||
defer inW.Close() | ||
_, _ = io.WriteString(inW, input) | ||
}() | ||
|
||
// Start a goroutine to run the REPL | ||
go func() { | ||
RunRepl("elps> ", WithStdin(inR), WithStderr(outW)) | ||
inR.Close() | ||
outW.Close() | ||
}() | ||
|
||
// Read the output from the Stderr pipe | ||
var output bytes.Buffer | ||
_, _ = io.Copy(&output, outR) | ||
outR.Close() | ||
|
||
return output.String(), nil | ||
} | ||
|
||
func TestRunRepl(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input string | ||
expected string | ||
}{ | ||
{ | ||
name: "Simple Addition", | ||
input: `(+ 1 1)`, | ||
expected: "2\n", | ||
}, | ||
{ | ||
name: "Error", | ||
input: `fnord`, | ||
expected: "unbound symbol", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got, err := runReplWithString(t, tc.input) | ||
require.NoError(t, err) | ||
require.Contains(t, got, tc.expected) | ||
}) | ||
} | ||
} |