...

Source file src/code.rocketnine.space/tslocum/desktop/entry_test.go

Documentation: code.rocketnine.space/tslocum/desktop

     1  package desktop
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  type testData struct {
    11  	Filename string
    12  	Entry    *Entry
    13  }
    14  
    15  var testCases = []*testData{
    16  	{Filename: "app-alacritty.desktop", Entry: &Entry{Type: Application, Name: "Alacritty", GenericName: "Terminal", Comment: "A cross-platform, GPU enhanced terminal emulator", Icon: "Alacritty", Path: "/home/test", Exec: "alacritty"}},
    17  	{Filename: "app-vim.desktop", Entry: &Entry{Type: Application, Name: "Vim", GenericName: "Text Editor", Comment: "Edit text files", Icon: "gvim", Exec: "vim %F", Terminal: true}},
    18  	{Filename: "app-vim-nodisplay.desktop", Entry: nil},
    19  	{Filename: "link-google.desktop", Entry: &Entry{Type: Link, Name: "Link to Google", Icon: "text-html", URL: "https://google.com"}},
    20  }
    21  
    22  func TestParse(t *testing.T) {
    23  	var (
    24  		buf   = make([]byte, bufferSize)
    25  		f     *os.File
    26  		entry *Entry
    27  		err   error
    28  	)
    29  	for _, c := range testCases {
    30  		f, err = os.OpenFile("test/"+c.Filename, os.O_RDONLY, 0644)
    31  		if err != nil {
    32  			t.Fatal(err)
    33  		}
    34  
    35  		entry, err = Parse(f, buf)
    36  		f.Close()
    37  		if err != nil {
    38  			t.Fatal(err)
    39  		}
    40  
    41  		if !reflect.DeepEqual(entry, c.Entry) {
    42  			t.Fatalf("%s: entry incorrect: got %#v, want %#v", f.Name(), entry, c.Entry)
    43  		}
    44  	}
    45  }
    46  
    47  func BenchmarkParse(b *testing.B) {
    48  	var (
    49  		buf   = make([]byte, bufferSize)
    50  		files = make([]*os.File, len(testCases))
    51  		entry *Entry
    52  		err   error
    53  	)
    54  	defer func() {
    55  		for _, f := range files {
    56  			f.Close()
    57  		}
    58  	}()
    59  
    60  	for i, c := range testCases {
    61  		f, err := os.OpenFile("test/"+c.Filename, os.O_RDONLY, 0644)
    62  		if err != nil {
    63  			b.Fatal(err)
    64  		}
    65  
    66  		files[i] = f
    67  	}
    68  
    69  	b.StopTimer()
    70  	b.ResetTimer()
    71  	for i := 0; i < b.N; i++ {
    72  		for ci, f := range files {
    73  			b.StartTimer()
    74  			entry, err = Parse(f, buf)
    75  			if err != nil {
    76  				b.Fatal(err)
    77  			}
    78  			b.StopTimer()
    79  
    80  			if !reflect.DeepEqual(entry, testCases[ci].Entry) {
    81  				b.Fatalf("%s: entry incorrect: got %#v, want %#v", f.Name(), entry, testCases[ci].Entry)
    82  			}
    83  
    84  			_, err = f.Seek(0, io.SeekStart)
    85  			if err != nil {
    86  				b.Fatal(err)
    87  			}
    88  		}
    89  	}
    90  }
    91  

View as plain text