-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into feature/execution-databse
- Loading branch information
Showing
3 changed files
with
91 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package xjson | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
) | ||
|
||
// ReadFile reads the file named by filename and returns the contents. | ||
// It returns error if file is not well formated json. | ||
func ReadFile(filename string) ([]byte, error) { | ||
content, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var raw json.RawMessage | ||
if err := json.Unmarshal(content, &raw); err != nil { | ||
return nil, err | ||
} | ||
return raw, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package xjson | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
) | ||
|
||
const testfilenmae = "data.test" | ||
|
||
func TestReadFile(t *testing.T) { | ||
if err := ioutil.WriteFile(testfilenmae, []byte("{}"), os.ModePerm); err != nil { | ||
t.Fatalf("write error: %s", err) | ||
} | ||
defer os.Remove(testfilenmae) | ||
|
||
content, err := ReadFile(testfilenmae) | ||
if err != nil { | ||
t.Fatalf("read error: %s", err) | ||
} | ||
|
||
if string(content) != "{}" { | ||
t.Fatalf("invalid content - got: %s, want: {}", string(content)) | ||
} | ||
} | ||
|
||
func TestReadFileNoFile(t *testing.T) { | ||
_, err := ReadFile("nodata.test") | ||
if _, ok := err.(*os.PathError); !ok { | ||
t.Fatalf("read expected os.PathError - got: %s", err) | ||
} | ||
} | ||
|
||
func TestReadFileInvalidJSON(t *testing.T) { | ||
if err := ioutil.WriteFile(testfilenmae, []byte("{"), os.ModePerm); err != nil { | ||
t.Fatalf("write error: %s", err) | ||
} | ||
defer os.Remove(testfilenmae) | ||
|
||
_, err := ReadFile(testfilenmae) | ||
if _, ok := err.(*json.SyntaxError); !ok { | ||
t.Fatalf("read expected json.SyntaxError - got: %s", err) | ||
} | ||
} |