Skip to content

Commit

Permalink
add tests to check import errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed May 21, 2017
1 parent 56457f4 commit 79d7d97
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 6 deletions.
1 change: 0 additions & 1 deletion next/compiler/driver/mediator.go
Expand Up @@ -70,7 +70,6 @@ func (d *Driver) Lex() ([]*syntax.Token, error) {
tok = append(tok, t)
switch t.Kind {
case syntax.TokenEOF, syntax.TokenIllegal:
close(l.Tokens)
return tok, err
}
}
Expand Down
3 changes: 0 additions & 3 deletions next/compiler/syntax/example_test.go
Expand Up @@ -25,9 +25,6 @@ func Example() {
fmt.Fprintln(os.Stderr, e)
}

// Lexed tokens will be sent to Tokens channel. Ensure to close it after lexing is done
defer close(lexer.Tokens)

// Lexing can be started in another goroutine.
go lexer.Lex()

Expand Down
5 changes: 5 additions & 0 deletions next/compiler/syntax/import.go
@@ -1,6 +1,7 @@
package syntax

import (
"fmt"
"github.com/rhysd/Dachs/next/compiler/ast"
"github.com/rhysd/Dachs/next/compiler/prelude"
"os"
Expand Down Expand Up @@ -50,6 +51,10 @@ func newImportResolver() (*importResolver, error) {
func (res *importResolver) resolveInDir(dir string, node *ast.Import) (*ast.Module, error) {
file := filepath.Join(dir, filepath.Join(node.Parents...)) + ".dcs"

if node.StartPos.File.Name == file {
return nil, fmt.Errorf("Cannot import myself")
}

ns := ""
names := node.Imported
all := false
Expand Down
24 changes: 24 additions & 0 deletions next/compiler/syntax/import_test.go
Expand Up @@ -243,3 +243,27 @@ func TestImportExternalLib(t *testing.T) {
t.Error(msg)
}
}

func TestImportFailed(t *testing.T) {
cases := []struct {
what string
expected string
}{
{"error_myself", "Cannot import myself"},
{"error_unknown", "Cannot import '.unknown.foo'"},
{"error_unknown_external", "Cannot import 'unknown.lib'"},
{"error_parse", "Cannot import '.foo.piyo'"},
}

for _, tc := range cases {
t.Run(tc.what, func(t *testing.T) {
_, err := testParseAndResolveModules(tc.what)
if err == nil {
t.Fatal("Importing myself should cause an error", tc.what)
}
if !strings.Contains(err.Error(), tc.expected) {
t.Errorf("Expected error message '%s' but actually it was '%s'", tc.expected, err.Error())
}
})
}
}
1 change: 0 additions & 1 deletion next/compiler/syntax/lexer_test.go
Expand Up @@ -12,7 +12,6 @@ import (

func testLex(s *prelude.Source) ([]*Token, error) {
l := NewLexer(s)
defer close(l.Tokens)

var err error
l.Error = func(e *prelude.Error) { err = e }
Expand Down
1 change: 0 additions & 1 deletion next/compiler/syntax/parser.go
Expand Up @@ -68,7 +68,6 @@ func Parse(src *prelude.Source) (*ast.Program, error) {
l.Error = func(err *prelude.Error) {
lexErr = err
}
defer close(l.Tokens)
go l.Lex()
prog, err := ParseTokens(l.Tokens)
if lexErr != nil {
Expand Down
1 change: 1 addition & 0 deletions next/compiler/syntax/testdata/import/error_myself/main.dcs
@@ -0,0 +1 @@
import .myself.test
@@ -0,0 +1 @@
import .myself.{a, b, c}
4 changes: 4 additions & 0 deletions next/compiler/syntax/testdata/import/error_parse/foo.dcs
@@ -0,0 +1,4 @@
func piyo
!! parse error
aaa.
end
1 change: 1 addition & 0 deletions next/compiler/syntax/testdata/import/error_parse/main.dcs
@@ -0,0 +1 @@
import .foo.piyo
@@ -0,0 +1 @@
import .unknown.foo
@@ -0,0 +1 @@
import unknown.lib

0 comments on commit 79d7d97

Please sign in to comment.