Skip to content

Commit

Permalink
Update lexer_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
tolgaOzen committed Jul 15, 2022
1 parent b13a2ed commit 3411099
Showing 1 changed file with 65 additions and 57 deletions.
122 changes: 65 additions & 57 deletions pkg/dsl/lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,76 @@ package lexer
import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/Permify/permify/pkg/dsl/token"
)

func TestNextToken(t *testing.T) {
str := "entity user {} `table:\"users\",identifier:\"id\"`\n entity organization {\n relation admin @user `rel:\"custom\"`\n relation member @user `rel:\"many-to-many\", table:\"org_members\", cols:\"org_id,user_id\"`\n action create_repository = admin or member\n action delete = admin \n} `table:\"organizations\", identifier:\"id\"`\n"
// TestLexer -
func TestLexer(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "lexer-suite")
}

tests := []struct {
expectedType token.Type
expectedLiteral string
}{
{token.ENTITY, "entity"},
{token.IDENT, "user"},
{token.LBRACE, "{"},
{token.RBRACE, "}"},
{token.OPTION, "table:\"users\",identifier:\"id\""},
{token.NEWLINE, "\n"},
{token.ENTITY, "entity"},
{token.IDENT, "organization"},
{token.LBRACE, "{"},
{token.NEWLINE, "\n"},
{token.RELATION, "relation"},
{token.IDENT, "admin"},
{token.SIGN, "@"},
{token.IDENT, "user"},
{token.OPTION, "rel:\"custom\""},
{token.NEWLINE, "\n"},
{token.RELATION, "relation"},
{token.IDENT, "member"},
{token.SIGN, "@"},
{token.IDENT, "user"},
{token.OPTION, "rel:\"many-to-many\", table:\"org_members\", cols:\"org_id,user_id\""},
{token.NEWLINE, "\n"},
{token.ACTION, "action"},
{token.IDENT, "create_repository"},
{token.ASSIGN, "="},
{token.IDENT, "admin"},
{token.OR, "or"},
{token.IDENT, "member"},
{token.ACTION, "action"},
{token.IDENT, "delete"},
{token.ASSIGN, "="},
{token.IDENT, "admin"},
{token.NEWLINE, "\n"},
{token.RBRACE, "}"},
{token.OPTION, "table:\"organizations\", identifier:\"id\""},
{token.NEWLINE, "\n"},
{token.EOF, ""},
}
var _ = Describe("lexer", func() {

l := NewLexer(str)
Context("NextToken", func() {

for i, tt := range tests {
lexeme := l.NextToken()
It("Success", func() {

if lexeme.Type != tt.expectedType {
t.Fatalf("tests[%d] - tokentype wrong. expected=%q, got=%q",
i, tt.expectedType, lexeme.Type)
}
str := "entity user {} `table:\"users\",identifier:\"id\"`\n entity organization {\n relation admin @user `rel:\"custom\"`\n relation member @user `rel:\"many-to-many\", table:\"org_members\", cols:\"org_id,user_id\"`\n action create_repository = admin or member\n action delete = admin \n} `table:\"organizations\", identifier:\"id\"`\n"

if lexeme.Literal != tt.expectedLiteral {
t.Fatalf("tests[%d] - literal wrong. expected=%q, got=%q",
i, tt.expectedLiteral, lexeme.Literal)
}
}
}
tests := []struct {
expectedType token.Type
expectedLiteral string
}{
{token.ENTITY, "entity"},
{token.IDENT, "user"},
{token.LBRACE, "{"},
{token.RBRACE, "}"},
{token.OPTION, "table:\"users\",identifier:\"id\""},
{token.NEWLINE, "\n"},
{token.ENTITY, "entity"},
{token.IDENT, "organization"},
{token.LBRACE, "{"},
{token.NEWLINE, "\n"},
{token.RELATION, "relation"},
{token.IDENT, "admin"},
{token.SIGN, "@"},
{token.IDENT, "user"},
{token.OPTION, "rel:\"custom\""},
{token.NEWLINE, "\n"},
{token.RELATION, "relation"},
{token.IDENT, "member"},
{token.SIGN, "@"},
{token.IDENT, "user"},
{token.OPTION, "rel:\"many-to-many\", table:\"org_members\", cols:\"org_id,user_id\""},
{token.NEWLINE, "\n"},
{token.ACTION, "action"},
{token.IDENT, "create_repository"},
{token.ASSIGN, "="},
{token.IDENT, "admin"},
{token.OR, "or"},
{token.IDENT, "member"},
{token.ACTION, "action"},
{token.IDENT, "delete"},
{token.ASSIGN, "="},
{token.IDENT, "admin"},
{token.NEWLINE, "\n"},
{token.RBRACE, "}"},
{token.OPTION, "table:\"organizations\", identifier:\"id\""},
{token.NEWLINE, "\n"},
{token.EOF, ""},
}

l := NewLexer(str)

for _, tt := range tests {
lexeme := l.NextToken()
Expect(lexeme.Type).Should(Equal(tt.expectedType))
Expect(lexeme.Literal).Should(Equal(tt.expectedLiteral))
}
})
})
})

0 comments on commit 3411099

Please sign in to comment.