Skip to content

Commit

Permalink
separate binPath and ephemeris files
Browse files Browse the repository at this point in the history
  • Loading branch information
indigofeather committed Feb 3, 2017
1 parent 17fefbc commit 3a6682e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 31 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ go get -u github.com/DestinyLab/go-swetest

## Usage

You can download the ephemeris files to specifically directory (default same as `binary`).

```go
package main

Expand All @@ -24,6 +26,7 @@ import (

func main() {
opt := []string{
"-edir./resources/",
"-b11.11.2017",
"-ut00:00:00",
"-p0",
Expand All @@ -44,3 +47,4 @@ func main() {

- Swetest test page: http://www.astro.com/swisseph/swetest.htm
- Help: http://www.astro.com/cgi/swetest.cgi?arg=-h&p=0
- ephemeris files: ftp://ftp.astro.com/pub/swisseph/ephe/
File renamed without changes.
49 changes: 39 additions & 10 deletions swetest.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
/*
Package swetest is the simple wrapper of swisseph library's swetest.
Swetest test page: http://www.astro.com/swisseph/swetest.htm
Swetest:
test page: http://www.astro.com/swisseph/swetest.htm
Help: http://www.astro.com/cgi/swetest.cgi?arg=-h&p=0
ephemeris files: ftp://ftp.astro.com/pub/swisseph/ephe/
Help: http://www.astro.com/cgi/swetest.cgi?arg=-h&p=0
Example:
package main
import (
"fmt"
"log"
"github.com/DestinyLab/go-swetest"
)
func main() {
opt := []string{
"-edir./resources/",
"-b11.11.2017",
"-ut00:00:00",
"-p0",
"-fZ",
"-eswe",
"-head",
}
s := swetest.New()
res, err := s.Query(opt)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", res)
}
*/
package swetest

Expand All @@ -15,26 +44,26 @@ import (

// A Swetest returns what you queryed to swisseph library's swetest.
type Swetest struct {
path string
binPath string
}

// New creates an instance of swetest.
func New() *Swetest {
return &Swetest{path: getDefaultPath()}
return &Swetest{binPath: getDefaultBinPath()}
}

// SetPath set the path of resources.
func (s *Swetest) SetPath(p string) {
s.path = p
// SetBinPath set the path to swetest(executable binary).
func (s *Swetest) SetBinPath(p string) {
s.binPath = p
}

// Query query what you want.
func (s *Swetest) Query(q []string) ([]byte, error) {
return exec.Command(s.path+"swetest", q[0:]...).Output()
return exec.Command(s.binPath+"swetest", q[0:]...).Output()
}

func getDefaultPath() string {
func getDefaultBinPath() string {
_, file, _, _ := runtime.Caller(0)

return filepath.Dir(file) + "/resources/"
return filepath.Dir(file)
}
44 changes: 23 additions & 21 deletions swetest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,61 @@ package swetest

import "testing"

var opt = []string{
"-edir./resources/",
"-b11.11.2016",
"-ut00:00:00",
"-p0",
"-fZ",
"-eswe",
"-head",
}

func TestNew(t *testing.T) {
New()
}

func TestSetPath(t *testing.T) {
func TestSetBinPath(t *testing.T) {
s := New()
s.SetPath("path/to")
s.SetBinPath("path/to")

if s.path != "path/to" {
t.Error("SetPath error")
if s.binPath != "path/to" {
t.Error("SetBinPath error")
}
}

func TestQuery(t *testing.T) {
opt := []string{`-b11.11.2016 -ut00:00:00 -p0 -fZ -eswe`}
s := New()
s.SetPath("./resources/")
_, err := s.Query(opt)
s.SetBinPath("./")
res, err := s.Query(opt)
if err != nil {
t.Error(err)
}
t.Log(string(res))
}

func Benchmark_New(b *testing.B) {
func BenchmarkNew(b *testing.B) {
for i := 0; i < b.N; i++ {
New()
}
}

func Benchmark_SetPath(b *testing.B) {
func BenchmarkSetPath(b *testing.B) {
s := New()
for i := 0; i < b.N; i++ {
s.SetPath("path/to")
s.SetBinPath("path/to")
}
}

func Benchmark_Query(b *testing.B) {
func BenchmarkQuery(b *testing.B) {
s := New()
opt := []string{
"-b11.11.2016",
"-ut00:00:00",
"-p0",
"-fZ",
"-eswe",
"-head",
}
for i := 0; i < b.N; i++ {
s.Query(opt)
}
}

func Benchmark_getDefaultPath(b *testing.B) {
func BenchmarkGetDefaultBinPath(b *testing.B) {
for i := 0; i < b.N; i++ {
getDefaultPath()
getDefaultBinPath()
}
}

0 comments on commit 3a6682e

Please sign in to comment.