gophernotes/gophernotes_test.go

114 lines
2.1 KiB
Go
Raw Permalink Normal View History

2016-01-23 18:43:44 -06:00
package main
import (
"testing"
2016-07-29 06:10:43 -05:00
2016-07-29 17:24:05 -05:00
repl "github.com/gopherds/gophernotes/internal/repl"
2016-07-29 06:10:43 -05:00
"github.com/stretchr/testify/assert"
2016-01-23 18:43:44 -06:00
)
// noError is a helper function for testing
func noError(t *testing.T, err error) {
if err != nil {
t.Fatal(err)
}
}
// TestStub is a canary test to make sure testing and testify are good to go
func TestStub(t *testing.T) {
assert.True(t, true, "This is good. Canary test passing")
}
// TestRun_import tests importing and use of a package
func TestRun_import(t *testing.T) {
s, err := repl.NewSession()
noError(t, err)
codes := []string{
":import encoding/json",
"b, err := json.Marshal(nil)",
"string(b)",
}
for _, code := range codes {
2016-08-10 06:14:39 -05:00
_, _, err := s.Eval(code)
2016-01-23 18:43:44 -06:00
noError(t, err)
}
}
// TestRun_QuickFix_evaluated_but_not_used makes sure errors are not thrown for
// evaluations of variables that aren't used
func TestRun_QuickFix_evaluated_but_not_used(t *testing.T) {
s, err := repl.NewSession()
noError(t, err)
codes := []string{
`[]byte("")`,
`make([]int, 0)`,
`1+1`,
`func() {}`,
`(4 & (1 << 1))`,
`1`,
}
for _, code := range codes {
2016-08-10 06:14:39 -05:00
_, _, err := s.Eval(code)
2016-01-23 18:43:44 -06:00
noError(t, err)
}
}
// TestRun_QuickFix_used_as_value tests assignment of values to variables
// and subsequent use
func TestRun_QuickFix_used_as_value(t *testing.T) {
s, err := repl.NewSession()
noError(t, err)
codes := []string{
`:import log`,
`a := 1`,
`log.SetPrefix("")`,
}
for _, code := range codes {
2016-08-10 06:14:39 -05:00
_, _, err := s.Eval(code)
2016-01-23 18:43:44 -06:00
noError(t, err)
}
}
// TestRun_Copy tests a copy within the replpkg
func TestRun_Copy(t *testing.T) {
s, err := repl.NewSession()
noError(t, err)
codes := []string{
`a := []string{"hello", "world"}`,
`b := []string{"goodbye", "world"}`,
`copy(a, b)`,
`if (a[0] != "goodbye") {
panic("should be copied")
}`,
}
for _, code := range codes {
2016-08-10 06:14:39 -05:00
_, _, err := s.Eval(code)
2016-01-23 18:43:44 -06:00
noError(t, err)
}
}
// TestRun_Const tests a constant within the replpkg
func TestRun_Const(t *testing.T) {
s, err := repl.NewSession()
noError(t, err)
codes := []string{
`const ( a = iota; b )`,
`a`,
`b`,
}
for _, code := range codes {
2016-08-10 06:14:39 -05:00
_, _, err := s.Eval(code)
2016-01-23 18:43:44 -06:00
noError(t, err)
}
2016-01-24 00:16:23 -06:00
}