| 365 | } |
| 366 | |
| 367 | func TestParseUnitPositive(t *testing.T) { |
| 368 | tests := []struct { |
| 369 | name string |
| 370 | str string |
| 371 | want []ast.Clause |
| 372 | }{ |
| 373 | { |
| 374 | name: "empty program 1", |
| 375 | str: "", |
| 376 | want: nil, |
| 377 | }, |
| 378 | { |
| 379 | name: "empty program 2", |
| 380 | str: " \n\n ", |
| 381 | want: nil, |
| 382 | }, |
| 383 | { |
| 384 | name: "one clause, no body.", |
| 385 | str: "foo(X).", |
| 386 | want: []ast.Clause{ast.NewClause(ast.NewAtom("foo", ast.Variable{"X"}), nil)}, |
| 387 | }, |
| 388 | { |
| 389 | name: "one clause, no body, wildcard.", |
| 390 | str: "foo(_).", |
| 391 | want: []ast.Clause{ast.NewClause(ast.NewAtom("foo", ast.Variable{"_"}), nil)}, |
| 392 | }, |
| 393 | { |
| 394 | name: "one clause, with body.", |
| 395 | str: "foo(X) ⟸ bar(X).", |
| 396 | want: []ast.Clause{ast.NewClause(ast.NewAtom("foo", ast.Variable{"X"}), []ast.Term{ast.NewAtom("bar", ast.Variable{"X"})})}, |
| 397 | }, |
| 398 | { |
| 399 | name: "one clause, with body, both contain '.', trailing comma", |
| 400 | str: "foo.bar(X) ⟸ bar.foo(X),.", |
| 401 | want: []ast.Clause{ast.NewClause(ast.NewAtom("foo.bar", ast.Variable{"X"}), []ast.Term{ast.NewAtom("bar.foo", ast.Variable{"X"})})}, |
| 402 | }, |
| 403 | { |
| 404 | name: "one clause, with body and one do-transform.", |
| 405 | str: "foo(X) ⟸ bar(X) |> do fn:party(), let Z = fn:foo(X).", |
| 406 | want: []ast.Clause{{ |
| 407 | ast.NewAtom("foo", ast.Variable{"X"}), |
| 408 | nil, |
| 409 | []ast.Term{ast.NewAtom("bar", ast.Variable{"X"})}, |
| 410 | &ast.Transform{ |
| 411 | []ast.TransformStmt{ |
| 412 | {nil, ast.ApplyFn{ast.FunctionSym{"fn:party", 0}, nil}}, |
| 413 | {&ast.Variable{"Z"}, ast.ApplyFn{ast.FunctionSym{"fn:foo", 1}, []ast.BaseTerm{ast.Variable{"X"}}}}, |
| 414 | }, |
| 415 | nil, |
| 416 | }, |
| 417 | }, |
| 418 | }, |
| 419 | }, |
| 420 | { |
| 421 | name: "one clause, with body and one do-transform and let-transform.", |
| 422 | str: "foo(X, ZZ) :- bar(X) |> do fn:party(), let Z = fn:foo(X) |> let ZZ = fn:mul(Z, 2).", |
| 423 | want: []ast.Clause{{ |
| 424 | ast.NewAtom("foo", ast.Variable{"X"}, ast.Variable{"ZZ"}), |