go.dedis.ch/onet/v4@v4.0.0-pre1/app/io_test.go (about)

     1  package app
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"testing"
     7  
     8  	"io/ioutil"
     9  
    10  	"os"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  	"go.dedis.ch/onet/v4/log"
    15  )
    16  
    17  func TestInput(t *testing.T) {
    18  	setInput("Y")
    19  	assert.Equal(t, "Y", Input("def", "Question"))
    20  	assert.Equal(t, "\nQuestion [def]: ", getOutput())
    21  	setInput("")
    22  	assert.Equal(t, "def", Input("def", "Question"))
    23  	setInput("1\n2")
    24  	assert.Equal(t, "1", Input("", "Question1"))
    25  	assert.Equal(t, "2", Input("1", "Question2"))
    26  }
    27  
    28  func TestInputYN(t *testing.T) {
    29  	setInput("")
    30  	assert.True(t, InputYN(true))
    31  	setInput("")
    32  	assert.False(t, InputYN(false, "Are you sure?"))
    33  	assert.Equal(t, "\nAre you sure? [Ny]: ", getOutput())
    34  	setInput("")
    35  	assert.True(t, InputYN(true, "Are you sure?"))
    36  	assert.Equal(t, "\nAre you sure? [Yn]: ", getOutput(), "one")
    37  }
    38  
    39  func TestCopy(t *testing.T) {
    40  	tmp, err := ioutil.TempFile("", "copy")
    41  	log.ErrFatal(err)
    42  	_, err = tmp.Write([]byte{3, 1, 4, 5, 9, 2, 6})
    43  	log.ErrFatal(err)
    44  	log.ErrFatal(tmp.Close())
    45  	nsrc := tmp.Name()
    46  	ndst := nsrc + "1"
    47  	log.ErrFatal(Copy(ndst, nsrc))
    48  	stat, err := os.Stat(ndst)
    49  	log.ErrFatal(err)
    50  	require.Equal(t, int64(7), stat.Size())
    51  	log.ErrFatal(os.Remove(nsrc))
    52  	log.ErrFatal(os.Remove(ndst))
    53  }
    54  
    55  func setInput(s string) {
    56  	// Flush output
    57  	getOutput()
    58  	in = bufio.NewReader(bytes.NewReader([]byte(s + "\n")))
    59  }
    60  
    61  func getOutput() string {
    62  	out := o.Bytes()
    63  	o.Reset()
    64  	return string(out)
    65  }