Skip to content
This repository has been archived by the owner on Dec 22, 2019. It is now read-only.

Commit

Permalink
Merge pull request #26 from Thunnini/supoort-test
Browse files Browse the repository at this point in the history
Add test interface array
  • Loading branch information
TanNgocDo committed Dec 6, 2018
2 parents d2e9038 + 260f1de commit d68fe91
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 33 deletions.
34 changes: 34 additions & 0 deletions test/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
"github.com/tendermint/go-amino"
)

type Struct interface {
A()
}

type SimpleStruct struct {
Int8 int8
Int16 int16
Expand All @@ -15,6 +19,14 @@ type SimpleStruct struct {
Str string
}

func (s SimpleStruct) A() {}

type SimpleStruct2 struct {
Str string
}

func (s SimpleStruct2) A() {}

func PrintStruct() {
cdc := amino.NewCodec()

Expand All @@ -25,7 +37,9 @@ func PrintStruct() {
Int64: 123456789,
Str: "teststring유니코드",
}
cdc.RegisterInterface((*Struct)(nil), nil)
cdc.RegisterConcrete(SimpleStruct{}, "SimpleStruct", nil)
cdc.RegisterConcrete(SimpleStruct2{}, "SimpleStruct2", nil)

bz := cdc.MustMarshalBinaryLengthPrefixed(simpleStruct)
fmt.Println(`SimpleStruct{
Expand All @@ -35,4 +49,24 @@ func PrintStruct() {
Int64: 123456789,
Str: "teststring유니코드",
}`, hex.EncodeToString(bz))

simpleStruct2 := SimpleStruct2{
Str: "test",
}
simpleStruct2.Str = "test"
arrayInterface := make([]Struct, 0)
arrayInterface = append(arrayInterface, simpleStruct, simpleStruct2)
bz = cdc.MustMarshalBinaryLengthPrefixed(arrayInterface)
fmt.Println(`[]Struct{
SimpleStruct{
Int8: 123,
Int16: 12345,
Int32: 1234567,
Int64: 123456789,
Str: "teststring유니코드",
},
SimpleStruct2{
Str: "test",
}
}`, hex.EncodeToString(bz))
}
105 changes: 72 additions & 33 deletions test/struct.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,83 @@ const {
const { toHex } = require('./util')
const assert = require('assert')

describe('Test encode simple struct', () => {
let codec = new Codec()
const SimpleStruct = TypeFactory.create('SimpleStruct', [
{
name: 'int8',
type: Types.Int8,
},
{
name: 'int16',
type: Types.Int16,
},
{
name: 'int32',
type: Types.Int32,
},
{
name: 'int64',
type: Types.Int64,
},
{
name: 'str',
type: Types.String,
},
])
codec.registerConcrete(SimpleStruct, 'SimpleStruct')
let codec = new Codec()
const SimpleStruct = TypeFactory.create('SimpleStruct', [
{
name: 'int8',
type: Types.Int8,
},
{
name: 'int16',
type: Types.Int16,
},
{
name: 'int32',
type: Types.Int32,
},
{
name: 'int64',
type: Types.Int64,
},
{
name: 'str',
type: Types.String,
},
])
const SimpleStruct2 = TypeFactory.create('SimpleStruct2', [
{
name: 'str',
type: Types.String,
}
])
codec.registerConcrete(new SimpleStruct(), 'SimpleStruct')
codec.registerConcrete(new SimpleStruct2(), 'SimpleStruct2')

describe('Test encode simple struct', () => {
/*
cdc.RegisterConcrete(SimpleStruct{}, "SimpleStruct", nil)
SimpleStruct{
Int8: 123,
Int16: 12345,
Int32: 1234567,
Int64: 123456789,
Str: "teststring유니코드",
} 2c4a89c4bc08f60110f2c0011887ad4b20959aef3a2a1674657374737472696e67ec9ca0eb8b88ecbd94eb939c
cdc.RegisterConcrete(SimpleStruct{}, "SimpleStruct", nil)
SimpleStruct{
Int8: 123,
Int16: 12345,
Int32: 1234567,
Int64: 123456789,
Str: "teststring유니코드",
} 2c4a89c4bc08f60110f2c0011887ad4b20959aef3a2a1674657374737472696e67ec9ca0eb8b88ecbd94eb939c
*/
it('result of simple struct should match', () => {
let simpleStruct = new SimpleStruct(123, 12345, 1234567, 123456789, 'teststring유니코드')
assert.equal(toHex(codec.marshalBinary(simpleStruct)), '2c4a89c4bc08f60110f2c0011887ad4b20959aef3a2a1674657374737472696e67ec9ca0eb8b88ecbd94eb939c')
})
})

describe('Test encode array interface', () => {
const StructArray = TypeFactory.create('StructArray', [
{
name: 'array',
type: Types.ArrayInterface,
}
], Types.ArrayInterface)

/*
[]Struct{
SimpleStruct{
Int8: 123,
Int16: 12345,
Int32: 1234567,
Int64: 123456789,
Str: "teststring유니코드",
},
SimpleStruct2{
Str: "test",
}
} 3a0a2c4a89c4bc08f60110f2c0011887ad4b20959aef3a2a1674657374737472696e67ec9ca0eb8b88ecbd94eb939c0a0ad7618abe0a0474657374
*/
it('result of array interface should match', () => {
let structArray = new StructArray([
new SimpleStruct(123, 12345, 1234567, 123456789, 'teststring유니코드'),
new SimpleStruct2('test')
])
assert.equal(toHex(codec.marshalBinary(structArray)), '3a0a2c4a89c4bc08f60110f2c0011887ad4b20959aef3a2a1674657374737472696e67ec9ca0eb8b88ecbd94eb939c0a0ad7618abe0a0474657374')
})
})

0 comments on commit d68fe91

Please sign in to comment.