github.com/mborho/rem@v0.17.0/line_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestParseLine(t *testing.T) {
    10  	line := "#foo#blafasel blubb"
    11  	l := &Line{}
    12  	l.read(line)
    13  
    14  	if l.tag != "foo" {
    15  		t.Errorf("Can't detect tag for %s", line)
    16  	}
    17  
    18  	if l.cmd != "blafasel blubb" {
    19  		t.Errorf("Can't detect tag for %s", line)
    20  	}
    21  
    22  	l = &Line{}
    23  	line = "ls -la"
    24  	l.read(line)
    25  	if l.tag != "" {
    26  		t.Errorf("Tag was found in %s", line)
    27  	}
    28  
    29  	if l.cmd != "ls -la" {
    30  		t.Errorf("Command was not found in %s", line)
    31  	}
    32  
    33  }
    34  
    35  /*func TestExecute(t *testing.T) {
    36  	l := &Line{
    37  		cmd: "/not/abdcdef",
    38  	}
    39  
    40  	err := l.execute()
    41  	if err != nil {
    42  		t.Errorf("Command was executed without error: %s", l.cmd)
    43  	}
    44  
    45  }*/
    46  
    47  func TestPrint(t *testing.T) {
    48  	// test without tag
    49  	l := &Line{
    50  		cmd: "echo foobar",
    51  	}
    52  	var b bytes.Buffer
    53  	l.print(&b, 2, false)
    54  
    55  	if strings.Compare(b.String(), " 2\techo foobar\n") != 0 {
    56  		t.Error("Line was printed incorrect.")
    57  	}
    58  
    59  	// test with tag but without line tag
    60  	l = &Line{
    61  		cmd: "foo",
    62  	}
    63  	var d bytes.Buffer
    64  	l.print(&d, 3, true)
    65  	if strings.Compare(d.String(), " 3\t - \tfoo\n") != 0 {
    66  		t.Error("Line was printed incorrect.")
    67  	}
    68  
    69  	// test with tag
    70  	l = &Line{
    71  		tag: "foo",
    72  		cmd: "bar",
    73  	}
    74  	var c bytes.Buffer
    75  	l.print(&c, 4, true)
    76  	if strings.Compare(c.String(), " 4\tfoo\tbar\n") != 0 {
    77  		t.Error("Line with tag was printed incorrect.")
    78  	}
    79  }