2017-03-15 03:26:14 +01:00
|
|
|
// Copyright 2017 Pulumi, Inc. All rights reserved.
|
2017-01-21 20:04:03 +01:00
|
|
|
|
|
|
|
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))
|
2017-01-21 21:25:59 +01:00
|
|
|
pkg := NewPackageToken(PackageName(p))
|
|
|
|
assert.Equal(t, p, pkg.Name().String())
|
|
|
|
assert.Equal(t, p, pkg.String())
|
2017-01-21 20:04:03 +01:00
|
|
|
|
|
|
|
// Module tokens/names.
|
|
|
|
m := "my/module"
|
|
|
|
assert.False(t, IsName(m))
|
|
|
|
assert.True(t, IsQName(m))
|
2017-01-21 21:25:59 +01:00
|
|
|
mod := NewModuleToken(pkg, ModuleName(m))
|
|
|
|
assert.Equal(t, m, mod.Name().String())
|
|
|
|
assert.Equal(t, p, mod.Package().Name().String())
|
Implement structured token binding
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.
2017-02-08 23:10:16 +01:00
|
|
|
assert.Equal(t, p+TokenDelimiter+m, mod.String())
|
2017-01-21 20:04:03 +01:00
|
|
|
|
|
|
|
// Module member tokens/names.
|
|
|
|
mm := "memby"
|
|
|
|
assert.True(t, IsName(mm))
|
|
|
|
assert.True(t, IsQName(mm))
|
2017-01-21 21:25:59 +01:00
|
|
|
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())
|
Implement structured token binding
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.
2017-02-08 23:10:16 +01:00
|
|
|
assert.Equal(t, p+TokenDelimiter+m+TokenDelimiter+mm, modm.String())
|
2017-01-21 20:04:03 +01:00
|
|
|
|
|
|
|
// Class member tokens/names.
|
|
|
|
cm := "property"
|
|
|
|
assert.True(t, IsName(cm))
|
|
|
|
assert.True(t, IsQName(cm))
|
2017-01-21 21:25:59 +01:00
|
|
|
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())
|
Implement structured token binding
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.
2017-02-08 23:10:16 +01:00
|
|
|
assert.Equal(t, p+TokenDelimiter+m+TokenDelimiter+mm+TokenDelimiter+cm, clm.String())
|
2017-01-21 20:04:03 +01:00
|
|
|
}
|