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 #20 from Thunnini/supoort-test
Browse files Browse the repository at this point in the history
Initial support testing
  • Loading branch information
TanNgocDo committed Dec 1, 2018
2 parents 383df96 + 5fe6160 commit b04c58e
Show file tree
Hide file tree
Showing 9 changed files with 343 additions and 2 deletions.
188 changes: 188 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
"signed-varint": "^2.0.1",
"varint": "^5.0.0"
},
"devDependencies": {},
"devDependencies": {
"mocha": "^5.2.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha"
},
"repository": {
"type": "git",
Expand Down
85 changes: 85 additions & 0 deletions test/encode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const {
Codec,
TypeFactory,
Utils,
Types,
WireTypes
} = require('../src/index')
const { toHex } = require('./util')
const assert = require('assert')

describe('Test encode primitive type int', () => {
let codec = new Codec()
const Int8 = TypeFactory.create('Int8', [
{
name: 'int8',
type: Types.Int8,
}
], Types.Int8)

const Int16 = TypeFactory.create('Int16', [
{
name: 'int16',
type: Types.Int16,
}
], Types.Int16)

const Int32 = TypeFactory.create('int32', [
{
name: 'int32',
type: Types.Int32,
}
], Types.Int32)

const Int64 = TypeFactory.create('int64', [
{
name: 'int64',
type: Types.Int64,
}
], Types.Int64)

/*
Encode int8(123) 02f601
Encode int16(12345) 03f2c001
Encode int32(1234567) 0387ad4b
Encode int64(123456789) 04959aef3a
*/
it('result of int8 should match', () => {
let int8 = new Int8(123)
assert.equal(toHex(codec.marshalBinary(int8)), '02f601')
})

it('result of int16 should match', () => {
let int16 = new Int16(12345)
assert.equal(toHex(codec.marshalBinary(int16)), '03f2c001')
})

it('result of int32 should match', () => {
let int32 = new Int32(1234567)
assert.equal(toHex(codec.marshalBinary(int32)), '0387ad4b')
})

it('result of int64 should match', () => {
let int64 = new Int64(123456789)
assert.equal(toHex(codec.marshalBinary(int64)), '04959aef3a')
})
})


describe('Test encode primitive type string', () => {
let codec = new Codec()
const Str = TypeFactory.create('Str', [
{
name: 'str',
type: Types.String,
}
], Types.String)

/*
Encode string(teststring유니코드) 171674657374737472696e67ec9ca0eb8b88ecbd94eb939c
*/
it('result of string should match', () => {
let str = new Str('teststring유니코드')
assert.equal(toHex(codec.marshalBinary(str)), '171674657374737472696e67ec9ca0eb8b88ecbd94eb939c')
})
})
6 changes: 6 additions & 0 deletions test/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module github.com/TanNgocDo/Js-Amino/test

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/tendermint/go-amino v0.14.1
)
4 changes: 4 additions & 0 deletions test/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/tendermint/go-amino v0.14.1 h1:o2WudxNfdLNBwMyl2dqOJxiro5rfrEaU0Ugs6offJMk=
github.com/tendermint/go-amino v0.14.1/go.mod h1:i/UKE5Uocn+argJJBb12qTZsCDBcAYMbR92AaJVmKso=
9 changes: 9 additions & 0 deletions test/main/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"github.com/TanNgocDo/Js-Amino/test"
)

func main() {
test.PrintPrimitive()
}
29 changes: 29 additions & 0 deletions test/primitive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package test

import (
"encoding/hex"
"fmt"

"github.com/tendermint/go-amino"
)

func PrintPrimitive() {
fmt.Println("test")

cdc := amino.NewCodec()

bz := cdc.MustMarshalBinaryLengthPrefixed(int8(123))
fmt.Println("Encode int8(123)", hex.EncodeToString(bz))

bz = cdc.MustMarshalBinaryLengthPrefixed(int16(12345))
fmt.Println("Encode int16(12345)", hex.EncodeToString(bz))

bz = cdc.MustMarshalBinaryLengthPrefixed(int32(1234567))
fmt.Println("Encode int32(1234567)", hex.EncodeToString(bz))

bz = cdc.MustMarshalBinaryLengthPrefixed(int64(123456789))
fmt.Println("Encode int64(123456789)", hex.EncodeToString(bz))

bz = cdc.MustMarshalBinaryLengthPrefixed("teststring유니코드")
fmt.Println("Encode string(teststring유니코드)", hex.EncodeToString(bz))
}
3 changes: 3 additions & 0 deletions test/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
To make examples for test in go-amino, we use [go modules](https://github.com/golang/go/wiki/Modules), so you need golang above v1.11

To show examples in go-amino, type go run .\main\main.go
15 changes: 15 additions & 0 deletions test/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const toHex = (buffer) => {
let s = ''
buffer.forEach((b) => {
b = b.toString(16)
if (b.length == 1) {
b = '0' + b
}
s += b
})
return s
}

module.exports = {
toHex
}

0 comments on commit b04c58e

Please sign in to comment.