-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: API improvements to open and close files (#4)
* io methods: read, save, open, write * methods ordering * some more tests
- Loading branch information
Showing
4 changed files
with
86 additions
and
26 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 |
---|---|---|
@@ -1,29 +1,60 @@ | ||
package comicinfo | ||
|
||
import ( | ||
"bytes" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestWriteRead(t *testing.T) { | ||
func TestSaveOpen(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
ci := ComicInfo{ | ||
ci := &ComicInfo{ | ||
Title: "Test", | ||
} | ||
|
||
path := filepath.Join(tmpDir, "test.xml") | ||
path := filepath.Join(tmpDir, "comicinfo.xml") | ||
|
||
err := Write(ci, path) | ||
err := Save(ci, path) | ||
if err != nil { | ||
t.Errorf("error writing ComicInfo: %v", err) | ||
t.Fatalf("error writing ComicInfo: %v", err) | ||
} | ||
|
||
ci2, err := Open(path) | ||
if err != nil { | ||
t.Errorf("error reading ComicInfo: %v", err) | ||
t.Fatalf("error reading ComicInfo: %v", err) | ||
} | ||
|
||
require.Equal(t, ci, ci2) | ||
} | ||
|
||
func TestWriteOpen(t *testing.T) { | ||
ci := &ComicInfo{ | ||
Title: "Test", | ||
} | ||
|
||
var buf bytes.Buffer | ||
|
||
err := Write(ci, &buf) | ||
if err != nil { | ||
t.Fatalf("error writing ComicInfo: %v", err) | ||
} | ||
|
||
ci2, err := Read(&buf) | ||
if err != nil { | ||
t.Fatalf("error reading ComicInfo: %v", err) | ||
} | ||
|
||
require.Equal(t, ci, ci2) | ||
} | ||
|
||
func TestReadError(t *testing.T) { | ||
_, err := Read(bytes.NewReader([]byte(""))) | ||
require.Error(t, err) | ||
} | ||
|
||
func TestOpenError(t *testing.T) { | ||
_, err := Open("nonexistent.xml") | ||
require.Error(t, err) | ||
} |
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