Skip to content
This repository has been archived by the owner on May 30, 2021. It is now read-only.

Commit

Permalink
implement Article.Json and its test
Browse files Browse the repository at this point in the history
  • Loading branch information
sotetsuk committed May 6, 2016
1 parent 8115919 commit f50c4c7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
12 changes: 10 additions & 2 deletions article.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package goscholar

import "fmt"
import (
"fmt"
"encoding/json"
log "github.com/Sirupsen/logrus"
)

type Article struct {
Title *title
Expand Down Expand Up @@ -43,5 +47,9 @@ func (a *Article) String() string {
}

func (a *Article) Json() string {
return ""
bytes, err := json.Marshal(a)
if err != nil {
log.WithFields(log.Fields{"a": a, "err":err}).Error("Json encoding failed")
}
return string(bytes)
}
44 changes: 27 additions & 17 deletions article_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@ import (
"testing"
)

var a *Article

func init() {
a = &Article{
Title: &title{
Name: "Deep learning via Hessian-free optimization",
Url: "http://machinelearning.wustl.edu/mlpapers/paper_files/icml2010_Martens10.pdf"},
Year: "2010",
ClusterId: "15502119379559163003",
NumCite: "260",
NumVer: "9",
InfoId: "e6RSJHGXItcJ",
Link: &link{
Name: "wustl.edu",
Url: "http://machinelearning.wustl.edu/mlpapers/paper_files/icml2010_Martens10.pdf",
Format: "PDF",
},
}
}

func TestString(t *testing.T) {
expected := `-----------------------------------------------------------------------------
[Title]
Expand All @@ -25,25 +45,15 @@ func TestString(t *testing.T) {
Format: PDF
-----------------------------------------------------------------------------`

a := &Article{
Title: &title{
Name: "Deep learning via Hessian-free optimization",
Url: "http://machinelearning.wustl.edu/mlpapers/paper_files/icml2010_Martens10.pdf"},
Year: "2010",
ClusterId: "15502119379559163003",
NumCite: "260",
NumVer: "9",
InfoId: "e6RSJHGXItcJ",
Link: &link{
Name: "wustl.edu",
Url: "http://machinelearning.wustl.edu/mlpapers/paper_files/icml2010_Martens10.pdf",
Format: "PDF",
},
}

if actual := a.String(); actual != expected {
t.Error(TestErr{expected:expected, actual:actual})
}
}

func TestJson(t *testing.T) {}
func TestJson(t *testing.T) {
expected := `{"Title":{"Name":"Deep learning via Hessian-free optimization","Url":"http://machinelearning.wustl.edu/mlpapers/paper_files/icml2010_Martens10.pdf"},"Year":"2010","ClusterId":"15502119379559163003","NumCite":"260","NumVer":"9","InfoId":"e6RSJHGXItcJ","Link":{"Name":"wustl.edu","Url":"http://machinelearning.wustl.edu/mlpapers/paper_files/icml2010_Martens10.pdf","Format":"PDF"}}`

if actual := a.Json(); actual != expected {
t.Error(TestErr{expected:expected, actual:actual})
}
}

0 comments on commit f50c4c7

Please sign in to comment.