MCPcopy
hub / github.com/google/mangle / TestParseUnitDecls

Function TestParseUnitDecls

parse/parse_test.go:306–365  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

304}
305
306func TestParseUnitDecls(t *testing.T) {
307 tests := []struct {
308 name string
309 str string
310 want []ast.Decl
311 }{
312 {
313 name: "Regular use",
314 str: "Use foo.bar!",
315 want: []ast.Decl{
316 makeDecl(t, ast.NewAtom("Package"), []ast.Atom{ast.NewAtom("name", ast.String(""))}, nil, nil),
317 makeDecl(t, ast.NewAtom("Use"), []ast.Atom{ast.NewAtom("name", ast.String("foo.bar"))}, nil, nil),
318 },
319 },
320 {
321 name: "Regular use with package",
322 str: "Package foo! Use bar!",
323 want: []ast.Decl{
324 makeDecl(t, ast.NewAtom("Package"), []ast.Atom{ast.NewAtom("name", ast.String("foo"))}, nil, nil),
325 makeDecl(t, ast.NewAtom("Use"), []ast.Atom{ast.NewAtom("name", ast.String("bar"))}, nil, nil),
326 },
327 },
328 {
329 name: "Use with atoms (trailing comma)",
330 str: `Use foo.bar [atom1("hello"), atom2(),]!`,
331 want: []ast.Decl{
332 makeDecl(t, ast.NewAtom("Package"), []ast.Atom{ast.NewAtom("name", ast.String(""))}, nil, nil),
333 makeDecl(t, ast.NewAtom("Use"), []ast.Atom{ast.NewAtom("name", ast.String("foo.bar")), ast.NewAtom("atom1", ast.String("hello")), ast.NewAtom("atom2")}, nil, nil),
334 },
335 },
336 {
337 name: "One decl",
338 str: `Decl pred().`,
339 want: []ast.Decl{
340 makeDecl(t, ast.NewAtom("Package"), []ast.Atom{ast.NewAtom("name", ast.String(""))}, nil, nil),
341 makeDecl(t, ast.NewAtom("pred"), nil, nil, nil),
342 },
343 },
344 {
345 name: "Multiple uses",
346 str: `Use foo! Use bar!`,
347 want: []ast.Decl{
348 makeDecl(t, ast.NewAtom("Package"), []ast.Atom{ast.NewAtom("name", ast.String(""))}, nil, nil),
349 makeDecl(t, ast.NewAtom("Use"), []ast.Atom{ast.NewAtom("name", ast.String("foo"))}, nil, nil),
350 makeDecl(t, ast.NewAtom("Use"), []ast.Atom{ast.NewAtom("name", ast.String("bar"))}, nil, nil),
351 },
352 },
353 }
354 for _, test := range tests {
355 t.Run(test.name, func(t *testing.T) {
356 got, err := Unit(strings.NewReader(test.str))
357 if err != nil {
358 t.Fatalf("Unit(%v) failed with %v", test.str, err)
359 }
360 if diff := cmp.Diff(test.want, got.Decls, cmp.Comparer(equals)); diff != "" {
361 t.Errorf("Unit(%v) = %v want %v diff %v", test.str, got, test.want, diff)
362 }
363 })

Callers

nothing calls this directly

Calls 4

NewAtomFunction · 0.92
StringFunction · 0.92
UnitFunction · 0.85
makeDeclFunction · 0.70

Tested by

no test coverage detected