]> git.lizzy.rs Git - micro.git/blob - cmd/micro/util_test.go
Code optimisation (#1117)
[micro.git] / cmd / micro / util_test.go
1 package main
2
3 import (
4         "testing"
5 )
6
7 func TestNumOccurences(t *testing.T) {
8         var tests = []struct {
9                 inputStr  string
10                 inputChar byte
11                 want      int
12         }{
13                 {"aaaa", 'a', 4},
14                 {"\trfd\ta", '\t', 2},
15                 {"∆ƒ\tø ® \t\t", '\t', 3},
16         }
17         for _, test := range tests {
18                 if got := NumOccurrences(test.inputStr, test.inputChar); got != test.want {
19                         t.Errorf("NumOccurences(%s, %c) = %d", test.inputStr, test.inputChar, got)
20                 }
21         }
22 }
23
24 func TestSpaces(t *testing.T) {
25         var tests = []struct {
26                 input int
27                 want  string
28         }{
29                 {4, "    "},
30                 {0, ""},
31         }
32         for _, test := range tests {
33                 if got := Spaces(test.input); got != test.want {
34                         t.Errorf("Spaces(%d) = \"%s\"", test.input, got)
35                 }
36         }
37 }
38
39 func TestIsWordChar(t *testing.T) {
40         if IsWordChar("t") == false {
41                 t.Errorf("IsWordChar(t) = false")
42         }
43         if IsWordChar("T") == false {
44                 t.Errorf("IsWordChar(T) = false")
45         }
46         if IsWordChar("5") == false {
47                 t.Errorf("IsWordChar(5) = false")
48         }
49         if IsWordChar("_") == false {
50                 t.Errorf("IsWordChar(_) = false")
51         }
52         if IsWordChar("ß") == false {
53                 t.Errorf("IsWordChar(ß) = false")
54         }
55         if IsWordChar("~") == true {
56                 t.Errorf("IsWordChar(~) = true")
57         }
58         if IsWordChar(" ") == true {
59                 t.Errorf("IsWordChar( ) = true")
60         }
61         if IsWordChar(")") == true {
62                 t.Errorf("IsWordChar()) = true")
63         }
64         if IsWordChar("\n") == true {
65                 t.Errorf("IsWordChar(\n)) = true")
66         }
67 }
68
69 func TestStringWidth(t *testing.T) {
70         tabsize := 4
71         if w := StringWidth("1\t2", tabsize); w != 5 {
72                 t.Error("StringWidth 1 Failed. Got", w)
73         }
74         if w := StringWidth("\t", tabsize); w != 4 {
75                 t.Error("StringWidth 2 Failed. Got", w)
76         }
77         if w := StringWidth("1\t", tabsize); w != 4 {
78                 t.Error("StringWidth 3 Failed. Got", w)
79         }
80         if w := StringWidth("\t\t", tabsize); w != 8 {
81                 t.Error("StringWidth 4 Failed. Got", w)
82         }
83         if w := StringWidth("12\t2\t", tabsize); w != 8 {
84                 t.Error("StringWidth 5 Failed. Got", w)
85         }
86 }
87
88 func TestWidthOfLargeRunes(t *testing.T) {
89         tabsize := 4
90         if w := WidthOfLargeRunes("1\t2", tabsize); w != 2 {
91                 t.Error("WidthOfLargeRunes 1 Failed. Got", w)
92         }
93         if w := WidthOfLargeRunes("\t", tabsize); w != 3 {
94                 t.Error("WidthOfLargeRunes 2 Failed. Got", w)
95         }
96         if w := WidthOfLargeRunes("1\t", tabsize); w != 2 {
97                 t.Error("WidthOfLargeRunes 3 Failed. Got", w)
98         }
99         if w := WidthOfLargeRunes("\t\t", tabsize); w != 6 {
100                 t.Error("WidthOfLargeRunes 4 Failed. Got", w)
101         }
102         if w := WidthOfLargeRunes("12\t2\t", tabsize); w != 3 {
103                 t.Error("WidthOfLargeRunes 5 Failed. Got", w)
104         }
105 }