commit
eec6d38da6
@ -5,5 +5,5 @@ go:
|
||||
- 1.8
|
||||
- master
|
||||
script:
|
||||
- go test ./...
|
||||
|
||||
# The GOPATH is for testing #21
|
||||
- GOPATH="$GOPATH:/tmp/jsonenums-test/go1:/tmp/jsonenums-test/go2" go test ./...
|
||||
|
@ -41,6 +41,7 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalJSON is generated so ShirtSize satisfies json.Marshaler.
|
||||
func (r ShirtSize) MarshalJSON() ([]byte, error) {
|
||||
if s, ok := interface{}(r).(fmt.Stringer); ok {
|
||||
return json.Marshal(s.String())
|
||||
@ -52,6 +53,7 @@ func (r ShirtSize) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(s)
|
||||
}
|
||||
|
||||
// UnmarshalJSON is generated so ShirtSize satisfies json.Unmarshaler.
|
||||
func (r *ShirtSize) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(data, &s); err != nil {
|
||||
|
@ -44,6 +44,7 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalJSON is generated so WeekDay satisfies json.Marshaler.
|
||||
func (r WeekDay) MarshalJSON() ([]byte, error) {
|
||||
if s, ok := interface{}(r).(fmt.Stringer); ok {
|
||||
return json.Marshal(s.String())
|
||||
@ -55,6 +56,7 @@ func (r WeekDay) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(s)
|
||||
}
|
||||
|
||||
// UnmarshalJSON is generated so WeekDay satisfies json.Unmarshaler.
|
||||
func (r *WeekDay) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(data, &s); err != nil {
|
||||
|
@ -23,7 +23,6 @@ import (
|
||||
"go/token"
|
||||
"go/types"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/tools/go/loader"
|
||||
@ -39,20 +38,20 @@ type Package struct {
|
||||
|
||||
// ParsePackage parses the package in the given directory and returns it.
|
||||
func ParsePackage(directory string) (*Package, error) {
|
||||
relDir, err := filepath.Rel(filepath.Join(build.Default.GOPATH, "src"), directory)
|
||||
p, err := build.ImportDir(directory, build.FindOnly)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("provided directory not under GOPATH (%s): %v",
|
||||
build.Default.GOPATH, err)
|
||||
return nil, fmt.Errorf("provided directory (%s) may not under GOPATH (%s): %v",
|
||||
directory, build.Default.GOPATH, err)
|
||||
}
|
||||
|
||||
conf := loader.Config{TypeChecker: types.Config{FakeImportC: true}}
|
||||
conf.Import(relDir)
|
||||
conf.Import(p.ImportPath)
|
||||
program, err := conf.Load()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't load package: %v", err)
|
||||
}
|
||||
|
||||
pkgInfo := program.Package(relDir)
|
||||
pkgInfo := program.Package(p.ImportPath)
|
||||
return &Package{
|
||||
Name: pkgInfo.Pkg.Name(),
|
||||
files: pkgInfo.Files,
|
||||
|
52
parser/parser_test.go
Normal file
52
parser/parser_test.go
Normal file
@ -0,0 +1,52 @@
|
||||
// Copyright 2017 Google Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to writing, software distributed
|
||||
// under the License is distributed on a "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
// CONDITIONS OF ANY KIND, either express or implied.
|
||||
//
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package parser
|
||||
|
||||
import (
|
||||
"go/build"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func must(t *testing.T, err error) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
var fakeCode = `
|
||||
package main
|
||||
import "fmt"
|
||||
func main() {
|
||||
fmt.Println("Hello world!")
|
||||
}
|
||||
`
|
||||
|
||||
func TestParseFromMultipleGopath(t *testing.T) {
|
||||
gopaths := filepath.SplitList(build.Default.GOPATH)
|
||||
if len(gopaths) < 2 {
|
||||
t.Skipf("No multiple GOPATH (%s) exists, skiping..", build.Default.GOPATH)
|
||||
}
|
||||
gopath := gopaths[len(gopaths)-1]
|
||||
dir := filepath.Join(gopath, "src", "foo")
|
||||
defer func() { must(t, os.RemoveAll(dir)) }()
|
||||
must(t, os.MkdirAll(dir, 0755))
|
||||
must(t, ioutil.WriteFile(filepath.Join(dir, "main.go"), []byte(fakeCode), 0644))
|
||||
|
||||
if _, err := ParsePackage(dir); err != nil {
|
||||
t.Fatalf("Parse package (%v): %v", dir, err)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user