2018-05-22 19:43:36 +00:00
|
|
|
// Copyright 2016-2018, Pulumi Corporation.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2017-01-21 19:04:03 +00:00
|
|
|
|
|
|
|
package tokens
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTokens(t *testing.T) {
|
2017-06-01 21:01:26 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-01-21 19:04:03 +00:00
|
|
|
// Package tokens/names.
|
|
|
|
p := "test/package"
|
|
|
|
assert.False(t, IsName(p))
|
2022-01-21 19:23:14 +00:00
|
|
|
assert.True(t, IsQName(p))
|
2017-01-21 20:25:59 +00:00
|
|
|
pkg := NewPackageToken(PackageName(p))
|
|
|
|
assert.Equal(t, p, pkg.Name().String())
|
|
|
|
assert.Equal(t, p, pkg.String())
|
2017-04-19 17:53:14 +00:00
|
|
|
p2 := "test/my-package"
|
|
|
|
assert.False(t, IsName(p2))
|
2022-01-21 19:23:14 +00:00
|
|
|
assert.True(t, IsQName(p2))
|
2017-04-19 17:53:14 +00:00
|
|
|
pkg2 := NewPackageToken(PackageName(p2))
|
|
|
|
assert.Equal(t, p2, pkg2.Name().String())
|
|
|
|
assert.Equal(t, p2, pkg2.String())
|
2017-01-21 19:04:03 +00:00
|
|
|
|
|
|
|
// Module tokens/names.
|
|
|
|
m := "my/module"
|
|
|
|
assert.False(t, IsName(m))
|
|
|
|
assert.True(t, IsQName(m))
|
2017-01-21 20:25:59 +00: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 22:10:16 +00:00
|
|
|
assert.Equal(t, p+TokenDelimiter+m, mod.String())
|
2017-01-21 19:04:03 +00:00
|
|
|
|
|
|
|
// Module member tokens/names.
|
|
|
|
mm := "memby"
|
|
|
|
assert.True(t, IsName(mm))
|
|
|
|
assert.True(t, IsQName(mm))
|
2017-01-21 20:25:59 +00: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 22:10:16 +00:00
|
|
|
assert.Equal(t, p+TokenDelimiter+m+TokenDelimiter+mm, modm.String())
|
2017-01-21 19:04:03 +00:00
|
|
|
}
|
2023-04-13 21:33:18 +00:00
|
|
|
|
|
|
|
func TestTypeDisplayName(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
give Type
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "not enough parts",
|
|
|
|
give: "incomplete",
|
|
|
|
want: "incomplete",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "no name",
|
|
|
|
give: "pkg:mod:",
|
|
|
|
want: "pkg:mod:",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "no slash",
|
|
|
|
give: "pkg:mod:typ",
|
|
|
|
want: "pkg:mod:typ",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "bad casing",
|
|
|
|
give: "pkg:Mod/foo:typ",
|
|
|
|
want: "pkg:Mod/foo:typ",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "remove slash",
|
|
|
|
give: "pkg:mod/foo/bar:Bar",
|
|
|
|
want: "pkg:mod/foo:Bar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "remove up to last slash",
|
|
|
|
give: "pkg:mod/foo/bar/baz:Baz",
|
|
|
|
want: "pkg:mod/foo/bar:Baz",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
assert.Equal(t, tt.want, tt.give.DisplayName())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|