This change fixes a whole host of issues with our current token binding logic. There are two primary aspects of this change: First, the prior token syntax was ambiguous, due to our choice of delimiter characters. For instance, "/" could be used both as a module member delimiter, in addition to being a valid character for sub-modules. The result is that we could not look at a token and know for certain which kind it is. There was also some annoyance with "." being the delimiter for class members in addition to being the leading character for special names like ".this", ".super", and ".ctor". Now, we just use ":" as the delimiter character for everything. The result is unambiguous. Second, the simplistic token table lookup really doesn't work. This is for three reasons: 1) decorated types like arrays, maps, pointers, and functions shouldn't need token lookup in the classical sense; 2) largely because of decorated naming, the mapping of token pieces to symbolic information isn't straightforward and requires parsing; 3) default modules need to be expanded and the old method only worked for simple cases and, in particular, would not work when combined with decorated names.
49 lines
1.5 KiB
Go
49 lines
1.5 KiB
Go
// Copyright 2016 Marapongo, Inc. All rights reserved.
|
|
|
|
package tokens
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestTokens(t *testing.T) {
|
|
// Package tokens/names.
|
|
p := "test/package"
|
|
assert.False(t, IsName(p))
|
|
assert.True(t, IsQName(p))
|
|
pkg := NewPackageToken(PackageName(p))
|
|
assert.Equal(t, p, pkg.Name().String())
|
|
assert.Equal(t, p, pkg.String())
|
|
|
|
// Module tokens/names.
|
|
m := "my/module"
|
|
assert.False(t, IsName(m))
|
|
assert.True(t, IsQName(m))
|
|
mod := NewModuleToken(pkg, ModuleName(m))
|
|
assert.Equal(t, m, mod.Name().String())
|
|
assert.Equal(t, p, mod.Package().Name().String())
|
|
assert.Equal(t, p+TokenDelimiter+m, mod.String())
|
|
|
|
// Module member tokens/names.
|
|
mm := "memby"
|
|
assert.True(t, IsName(mm))
|
|
assert.True(t, IsQName(mm))
|
|
modm := NewModuleMemberToken(mod, ModuleMemberName(mm))
|
|
assert.Equal(t, mm, modm.Name().String())
|
|
assert.Equal(t, m, modm.Module().Name().String())
|
|
assert.Equal(t, p, modm.Module().Package().Name().String())
|
|
assert.Equal(t, p+TokenDelimiter+m+TokenDelimiter+mm, modm.String())
|
|
|
|
// Class member tokens/names.
|
|
cm := "property"
|
|
assert.True(t, IsName(cm))
|
|
assert.True(t, IsQName(cm))
|
|
clm := NewClassMemberToken(Type(modm), ClassMemberName(cm))
|
|
assert.Equal(t, cm, clm.Name().String())
|
|
assert.Equal(t, mm, clm.Class().Name().String())
|
|
assert.Equal(t, m, clm.Class().Module().Name().String())
|
|
assert.Equal(t, p, clm.Class().Module().Package().Name().String())
|
|
assert.Equal(t, p+TokenDelimiter+m+TokenDelimiter+mm+TokenDelimiter+cm, clm.String())
|
|
}
|