Skip to content

Commit

Permalink
chore: add test ci
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Aug 21, 2023
1 parent 9932479 commit bf9fa7c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Unit Test

on: [push,pull_request]

jobs:
run-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: setup go env
uses: actions/setup-go@v4
with:
go-version: '1.19'
- name: run unit test
run: go test -race -coverprofile=coverage -covermode=atomic -v ./...
- name: upload code coverage
uses: codecov/codecov-action@v3
20 changes: 20 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
codecov:
require_ci_to_pass: true

coverage:
status:
project:
default:
target: 85%
threshold: 2%
base: auto
if_ci_failed: error
branches:
- 'master'

comment:
layout: 'reach, diff, flags, files'
behavior: default
require_changes: false
branches:
- 'master'
59 changes: 59 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package bencode

import (
"reflect"
"testing"
)

func TestDecodeNumeric(t *testing.T) {
input := "i-42e"
expected := -42
decoded, _ := Decode([]byte(input))

value, _ := decoded.(int)
if value != expected {
t.Fatalf("%d != %d", value, expected)
}
}

func TestDecodeStrin(t *testing.T) {
input := "4:spam"
expected := "spam"
decoded, _ := Decode([]byte(input))
value, _ := decoded.([]byte)
if string(value) != expected {
t.Fatalf("%s != %s", string(value), expected)
}
}

func TestDecodeList(t *testing.T) {
input := "l4:spami42ee"
expected1 := "spam"
var expected2 int64 = 42
decoded, _ := Decode([]byte(input))
value, _ := decoded.([]interface{})
n1 := string(reflect.ValueOf(value[0]).Bytes())
n2 := reflect.ValueOf(value[1]).Int()
if n1 != expected1 {
t.Fatalf("%s != %s", n1, expected1)
}
if n2 != expected2 {
t.Fatalf("%d != %d", n2, expected2)
}
}

func TestDecodeDirectory(t *testing.T) {
input := "d3:bar4:spam3:fooi42ee"
decoded, _ := Decode([]byte(input))
value, _ := decoded.(map[string]interface{})
expected1 := "spam"
var expected2 int64 = 42
n1 := string(reflect.ValueOf(value["bar"]).Bytes())
n2 := reflect.ValueOf(value["foo"]).Int()
if n1 != expected1 {
t.Fatalf("%s != %s", n1, expected1)
}
if n2 != expected2 {
t.Fatalf("%d != %d", n2, expected2)
}
}

0 comments on commit bf9fa7c

Please sign in to comment.