-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wit-bindgen-go): align flags decoding
Flags encoding/decoding in Go is now rewritten to use the correct binary encoding. Flag structs now contain an addition ReadFromIndex method that can be used for decoding. This makes the generated code more testable.
- Loading branch information
1 parent
02c4350
commit 4fd3c34
Showing
6 changed files
with
178 additions
and
55 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,63 @@ | ||
//go:generate $WIT_BINDGEN_WRPC go --gofmt=false --world types --out-dir bindings/types --package github.com/wrpc/wrpc/tests/go/bindings/types ../wit | ||
|
||
package integration_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
wrpc "github.com/wrpc/wrpc/go" | ||
"github.com/wrpc/wrpc/tests/go/bindings/types/wrpc_test/integration/get_types" | ||
) | ||
|
||
type indexReader struct { | ||
*bytes.Buffer | ||
} | ||
|
||
func (r *indexReader) Index(path ...uint32) (wrpc.IndexReader, error) { | ||
panic("not implemented") | ||
} | ||
|
||
func TestTypes_Flags(t *testing.T) { | ||
t.Run("WriteToIndex", func(t *testing.T) { | ||
check := assert.New(t) | ||
|
||
flags := get_types.FeatureFlags{ | ||
ShowA: true, | ||
ShowC: true, | ||
} | ||
var b bytes.Buffer | ||
_, err := flags.WriteToIndex(&b) | ||
check.NoError(err) | ||
check.Equal([]byte{0b00000101}, b.Bytes()) | ||
}) | ||
|
||
t.Run("ReadFromIndex", func(t *testing.T) { | ||
check := assert.New(t) | ||
|
||
var flags get_types.FeatureFlags | ||
err := flags.ReadFromIndex(&indexReader{ | ||
Buffer: bytes.NewBuffer([]byte{0b00000101}), | ||
}) | ||
check.NoError(err) | ||
check.Equal(get_types.FeatureFlags{ | ||
ShowA: true, | ||
ShowC: true, | ||
}, flags) | ||
|
||
t.Run("invalid bit set", func(t *testing.T) { | ||
check := assert.New(t) | ||
|
||
var flags get_types.FeatureFlags | ||
err := flags.ReadFromIndex(&indexReader{ | ||
Buffer: bytes.NewBuffer([]byte{0b10000000}), | ||
}) | ||
if check.Error(err) { | ||
check.Equal("bit not associated with any flag is set", err.Error()) | ||
} | ||
check.Zero(flags) | ||
}) | ||
}) | ||
} |
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