This repository has been archived by the owner on Nov 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copied from internal, but now with better semantics and preparing for not allowing string labels to bind. Ported most uses of internal.LabelName Change-Id: I35b178a2c03073a6a6df1f78e0bfe8eae7b882ad Reviewed-on: https://cue-review.googlesource.com/c/cue/+/3261 Reviewed-by: Marcel van Lohuizen <[email protected]>
- Loading branch information
Showing
7 changed files
with
165 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2019 CUE Authors | ||
// | ||
// 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. | ||
|
||
package ast_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"cuelang.org/go/cue/ast" | ||
"cuelang.org/go/cue/format" | ||
"cuelang.org/go/cue/token" | ||
"github.com/stretchr/testify/assert" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
func TestLabelName(t *testing.T) { | ||
testCases := []struct { | ||
in ast.Label | ||
out string | ||
isIdent bool | ||
err bool | ||
expr bool | ||
}{{ | ||
in: ast.NewString("foo-bar"), | ||
out: "foo-bar", | ||
isIdent: false, | ||
}, { | ||
in: ast.NewString("foo bar"), | ||
out: "foo bar", | ||
isIdent: false, | ||
}, { | ||
in: &ast.Ident{Name: "`foo`"}, | ||
out: "foo", | ||
isIdent: true, | ||
}, { | ||
in: &ast.Ident{Name: "`foo-bar`"}, | ||
out: "foo-bar", | ||
isIdent: true, | ||
}, { | ||
in: &ast.Ident{Name: "`foo-bar\x00`"}, | ||
out: "", | ||
isIdent: false, | ||
err: true, | ||
}, { | ||
in: &ast.Ident{Name: "`foo-bar\x00`"}, | ||
out: "", | ||
isIdent: false, | ||
err: true, | ||
}, { | ||
in: &ast.BasicLit{Kind: token.TRUE, Value: "true"}, | ||
out: "true", | ||
isIdent: true, | ||
}, { | ||
in: &ast.BasicLit{Kind: token.STRING, Value: `"foo`}, | ||
out: "", | ||
isIdent: false, | ||
err: true, | ||
}, { | ||
in: &ast.Interpolation{Elts: []ast.Expr{ast.NewString("foo")}}, | ||
out: "", | ||
isIdent: false, | ||
err: true, | ||
expr: true, | ||
}} | ||
for _, tc := range testCases { | ||
b, _ := format.Node(tc.in) | ||
t.Run(string(b), func(t *testing.T) { | ||
str, isIdent, err := ast.LabelName(tc.in) | ||
assert.Equal(t, tc.out, str, "value") | ||
assert.Equal(t, tc.isIdent, isIdent, "isIdent") | ||
assert.Equal(t, tc.err, err != nil, "err") | ||
assert.Equal(t, tc.expr, xerrors.Is(err, ast.ErrIsExpression), "expr") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters