Skip to content

Commit

Permalink
feat: add tests for genAccountField
Browse files Browse the repository at this point in the history
  • Loading branch information
sol-mocha committed Feb 1, 2023
1 parent f6fff6a commit 219ba33
Showing 1 changed file with 67 additions and 5 deletions.
72 changes: 67 additions & 5 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import (

. "github.com/dave/jennifer/jen"
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type jsonToSource struct {
from string
expected string
}

func Test_genTypeName(t *testing.T) {
type jsonToSource struct {
from string
Expand Down Expand Up @@ -155,10 +160,6 @@ func Test_genTypeName(t *testing.T) {
}

func Test_genField(t *testing.T) {
type jsonToSource struct {
from string
expected string
}

tests := []jsonToSource{
{
Expand Down Expand Up @@ -186,6 +187,67 @@ func Test_genField(t *testing.T) {
}
}

func Test_genAccountField(t *testing.T) {
basicTest := `{
"name": "authorityBefore",
"isMut": false,
"isSigner": true
}`
compositeTest := `{
"name": "marketGroup",
"accounts": [
{
"name": "marketMarket",
"isMut": true,
"isSigner": false
},
{
"name": "foo",
"isMut": true,
"isSigner": false
},
{
"name": "subMarket",
"accounts": [
{
"name": "subMarketMarket",
"isMut": true,
"isSigner": false
},
{
"name": "openOrders",
"isMut": true,
"isSigner": false
}
]
}
]
}`

tests := []jsonToSource{
{
basicTest,
"var thing struct {\n\tAuthorityBefore solanago.PublicKey\n}",
},
{
compositeTest,
"var thing struct {\n\tMarketGroup struct {\n\t\tMarketMarket solanago.PublicKey\n\t\tFoo solanago.PublicKey\n\t\tSubMarket struct {\n\t\t\tSubMarketMarket solanago.PublicKey\n\t\t\tOpenOrders solanago.PublicKey\n\t\t}\n\t}\n}",
},
}
{
for _, scenario := range tests {
var target IdlAccountItem
err := json.Unmarshal([]byte(scenario.from), &target)
require.NoError(t, err)
code := Var().Id("thing").Struct(
genAccountField(target),
)
got := codeToString(code)
require.Equal(t, scenario.expected, got)
}
}
}

func Test_IdlAccountItemSlice_Walk(t *testing.T) {
data := `[
{
Expand Down

0 comments on commit 219ba33

Please sign in to comment.