github.com/Ptt-official-app/go-bbs@v0.12.0/pttbbs/pttbss_append_new_line_test.go (about)

     1  package pttbbs
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  func TestNewComment(t *testing.T) {
    10  	c := Connector{"./testcase"}
    11  
    12  	fileName := "M.1606672292.A.B23"
    13  
    14  	expectedNewLine := "\u001B[1;31m→ \u001B[33mSYSOP\u001B[m\u001B[33m:test                                                   \u001B[m推 01/17 11:36"
    15  	boardPath := "./testcase/boards/t/test/"
    16  	filePath := boardPath + "/" + fileName
    17  	stat, _ := os.Stat(filePath)
    18  	oriFileSize := stat.Size()
    19  
    20  	err := c.AppendBoardArticleFile(filePath, []byte(expectedNewLine))
    21  
    22  	if err != nil {
    23  		t.Errorf("Unexpected Error happened: %s", err)
    24  	}
    25  
    26  	stat, _ = os.Stat(filePath)
    27  	newFileSize := stat.Size()
    28  	newLineSize := newFileSize - oriFileSize
    29  	buf := make([]byte, newLineSize)
    30  	fileHandle, _ := os.Open(filePath)
    31  	_, err = fileHandle.ReadAt(buf, oriFileSize)
    32  	actualNewLine := bytes.NewBuffer(buf)
    33  
    34  	if expectedNewLine != actualNewLine.String() {
    35  		t.Errorf("newline not matched, expected: %s, \ngot: %s", expectedNewLine, actualNewLine)
    36  	}
    37  }