Exemplo n.º 1
0
func TestFileReaderOpenNonExistingFile(t *testing.T) {
	vanish.File(func(name string) {
		assert.Nil(t, os.Remove(name))

		fr := jsons.NewFileReader(name)

		assert.NotNil(t, fr.Open())
	})
}
Exemplo n.º 2
0
func TestFileReaderEmptyFile(t *testing.T) {
	vanish.File(func(name string) {
		fr := jsons.NewFileReader(name)

		assert.Nil(t, fr.Open())

		var m map[int]int

		assert.Equal(t, io.EOF, fr.Next(&m))
		assert.Nil(t, fr.Close())
	})
}
Exemplo n.º 3
0
func TestFileWriterCreateFileIfItDoesntExist(t *testing.T) {
	vanish.File(func(name string) {
		assert.Nil(t, os.Remove(name))

		fw := jsons.NewFileWriter(name)
		assert.NotNil(t, fw)

		assert.Nil(t, fw.Open())
		assert.Nil(t, fw.Close())

		_, err := os.Stat(name)
		assert.Nil(t, err)
	})
}
Exemplo n.º 4
0
func TestFileWriterAdd(t *testing.T) {
	vanish.File(func(name string) {
		fw := jsons.NewFileWriter(name)
		assert.NotNil(t, fw)

		assert.Nil(t, fw.Open())

		assert.Nil(t, fw.Add(map[string]string{"foo": "bar"}))
		assert.Nil(t, fw.Add(struct{ A float64 }{A: 42.3}))

		assert.Nil(t, fw.Close())

		content, err := ioutil.ReadFile(name)
		assert.Nil(t, err)

		assert.Equal(t, "{\"foo\":\"bar\"}\n{\"A\":42.3}\n", string(content))
	})
}
Exemplo n.º 5
0
func TestFileReaderNext(t *testing.T) {
	vanish.File(func(name string) {

		ioutil.WriteFile(name, []byte("{\"foo\": 17}\n"), 0644)

		fr := jsons.NewFileReader(name)

		assert.Nil(t, fr.Open())

		var m map[string]int

		assert.Nil(t, fr.Next(&m))
		assert.Equal(t, 17, m["foo"])

		assert.Equal(t, io.EOF, fr.Next(&m))
		assert.Nil(t, fr.Close())
	})
}