|
5 | 5 | "testing"
|
6 | 6 |
|
7 | 7 | "github.com/gnolang/gno/gnovm"
|
| 8 | + "github.com/gnolang/gno/gnovm/pkg/doc" |
8 | 9 | abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
|
9 | 10 | "github.com/gnolang/gno/tm2/pkg/crypto"
|
10 | 11 | "github.com/gnolang/gno/tm2/pkg/std"
|
@@ -327,3 +328,118 @@ func TestVmHandlerQuery_File(t *testing.T) {
|
327 | 328 | })
|
328 | 329 | }
|
329 | 330 | }
|
| 331 | + |
| 332 | +func TestVmHandlerQuery_Doc(t *testing.T) { |
| 333 | + expected := &doc.JSONDocumentation{ |
| 334 | + PackagePath: "gno.land/r/hello", |
| 335 | + PackageLine: "package hello // import \"hello\"", |
| 336 | + PackageDoc: "hello is a package for testing\n", |
| 337 | + Values: []*doc.JSONValueDecl{ |
| 338 | + { |
| 339 | + Signature: "const prefix = \"Hello\"", |
| 340 | + Const: true, |
| 341 | + Doc: "The prefix for the hello message\n", |
| 342 | + Values: []*doc.JSONValue{ |
| 343 | + { |
| 344 | + Name: "prefix", |
| 345 | + Doc: "", |
| 346 | + Type: "", |
| 347 | + }, |
| 348 | + }, |
| 349 | + }, |
| 350 | + }, |
| 351 | + Funcs: []*doc.JSONFunc{ |
| 352 | + { |
| 353 | + Type: "", |
| 354 | + Name: "Hello", |
| 355 | + Signature: "func Hello(msg string) (res string)", |
| 356 | + Doc: "", |
| 357 | + Params: []*doc.JSONField{ |
| 358 | + {Name: "msg", Type: "string"}, |
| 359 | + }, |
| 360 | + Results: []*doc.JSONField{ |
| 361 | + {Name: "res", Type: "string"}, |
| 362 | + }, |
| 363 | + }, |
| 364 | + { |
| 365 | + Type: "myStruct", |
| 366 | + Name: "Foo", |
| 367 | + Signature: "func (ms myStruct) Foo() string", |
| 368 | + Doc: "", |
| 369 | + Params: []*doc.JSONField{}, |
| 370 | + Results: []*doc.JSONField{ |
| 371 | + {Name: "", Type: "string"}, |
| 372 | + }, |
| 373 | + }, |
| 374 | + }, |
| 375 | + Types: []*doc.JSONType{ |
| 376 | + { |
| 377 | + Name: "myStruct", |
| 378 | + Signature: "type myStruct struct{ a int }", |
| 379 | + Doc: "myStruct is a struct for testing\n", |
| 380 | + }, |
| 381 | + }, |
| 382 | + } |
| 383 | + |
| 384 | + tt := []struct { |
| 385 | + input []byte |
| 386 | + expectedResult string |
| 387 | + expectedErrorMatch string |
| 388 | + }{ |
| 389 | + // valid queries |
| 390 | + {input: []byte(`gno.land/r/hello`), expectedResult: expected.JSON()}, |
| 391 | + {input: []byte(`gno.land/r/doesnotexist`), expectedErrorMatch: `invalid package path`}, |
| 392 | + } |
| 393 | + |
| 394 | + for _, tc := range tt { |
| 395 | + name := string(tc.input) |
| 396 | + t.Run(name, func(t *testing.T) { |
| 397 | + env := setupTestEnv() |
| 398 | + ctx := env.vmk.MakeGnoTransactionStore(env.ctx) |
| 399 | + vmHandler := env.vmh |
| 400 | + |
| 401 | + // Give "addr1" some gnots. |
| 402 | + addr := crypto.AddressFromPreimage([]byte("addr1")) |
| 403 | + acc := env.acck.NewAccountWithAddress(ctx, addr) |
| 404 | + env.acck.SetAccount(ctx, acc) |
| 405 | + env.bank.SetCoins(ctx, addr, std.MustParseCoins("10000000ugnot")) |
| 406 | + assert.True(t, env.bank.GetCoins(ctx, addr).IsEqual(std.MustParseCoins("10000000ugnot"))) |
| 407 | + |
| 408 | + // Create test package. |
| 409 | + files := []*gnovm.MemFile{ |
| 410 | + {Name: "hello.gno", Body: ` |
| 411 | +// hello is a package for testing |
| 412 | +package hello |
| 413 | +
|
| 414 | +// myStruct is a struct for testing |
| 415 | +type myStruct struct{a int} |
| 416 | +func (ms myStruct) Foo() string { return "myStruct.Foo" } |
| 417 | +// The prefix for the hello message |
| 418 | +const prefix = "Hello" |
| 419 | +func Hello(msg string) (res string) { res = prefix+" "+msg; return } |
| 420 | +`}, |
| 421 | + } |
| 422 | + pkgPath := "gno.land/r/hello" |
| 423 | + msg1 := NewMsgAddPackage(addr, pkgPath, files) |
| 424 | + err := env.vmk.AddPackage(ctx, msg1) |
| 425 | + assert.NoError(t, err) |
| 426 | + |
| 427 | + req := abci.RequestQuery{ |
| 428 | + Path: "vm/qdoc", |
| 429 | + Data: tc.input, |
| 430 | + } |
| 431 | + |
| 432 | + res := vmHandler.Query(env.ctx, req) |
| 433 | + if tc.expectedErrorMatch == "" { |
| 434 | + assert.True(t, res.IsOK(), "should not have error") |
| 435 | + if tc.expectedResult != "" { |
| 436 | + assert.Equal(t, tc.expectedResult, string(res.Data)) |
| 437 | + } |
| 438 | + } else { |
| 439 | + assert.False(t, res.IsOK(), "should have an error") |
| 440 | + errmsg := res.Error.Error() |
| 441 | + assert.Regexp(t, tc.expectedErrorMatch, errmsg) |
| 442 | + } |
| 443 | + }) |
| 444 | + } |
| 445 | +} |
0 commit comments