|
| 1 | +package processors |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestLineNumberer_Command(t *testing.T) { |
| 9 | + test := struct { |
| 10 | + alias []string |
| 11 | + description string |
| 12 | + filterValue string |
| 13 | + flags []Flag |
| 14 | + name string |
| 15 | + title string |
| 16 | + }{ |
| 17 | + alias: []string{"nl"}, |
| 18 | + description: "Prepends consecutive number to each input line", |
| 19 | + filterValue: "Line numberer", |
| 20 | + flags: nil, |
| 21 | + name: "number-lines", |
| 22 | + title: "Line numberer", |
| 23 | + } |
| 24 | + p := LineNumberer{} |
| 25 | + if got := p.Alias(); !reflect.DeepEqual(got, test.alias) { |
| 26 | + t.Errorf("Alias() = %v, want %v", got, test.alias) |
| 27 | + } |
| 28 | + if got := p.Description(); got != test.description { |
| 29 | + t.Errorf("Description() = %v, want %v", got, test.description) |
| 30 | + } |
| 31 | + if got := p.FilterValue(); got != test.filterValue { |
| 32 | + t.Errorf("FilterValue() = %v, want %v", got, test.filterValue) |
| 33 | + } |
| 34 | + if got := p.Flags(); !reflect.DeepEqual(got, test.flags) { |
| 35 | + t.Errorf("Flags() = %v, want %v", got, test.flags) |
| 36 | + } |
| 37 | + if got := p.Name(); got != test.name { |
| 38 | + t.Errorf("Name() = %v, want %v", got, test.name) |
| 39 | + } |
| 40 | + if got := p.Title(); got != test.title { |
| 41 | + t.Errorf("Title() = %v, want %v", got, test.title) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestLineNumberer_Transform(t *testing.T) { |
| 46 | + type args struct { |
| 47 | + data []byte |
| 48 | + in1 []Flag |
| 49 | + } |
| 50 | + tests := []struct { |
| 51 | + name string |
| 52 | + args args |
| 53 | + want string |
| 54 | + wantErr bool |
| 55 | + }{ |
| 56 | + { |
| 57 | + name: "Test empty string", |
| 58 | + args: args{data: []byte("")}, |
| 59 | + want: "", |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "String", |
| 63 | + args: args{data: []byte("this is string")}, |
| 64 | + want: "1. this is string", |
| 65 | + }, { |
| 66 | + name: "Emoji", |
| 67 | + args: args{data: []byte("😃😇🙃🙂😉😌😙😗🇮🇳")}, |
| 68 | + want: "1. 😃😇🙃🙂😉😌😙😗🇮🇳", |
| 69 | + }, { |
| 70 | + name: "Multi line string", |
| 71 | + args: args{data: []byte("Hello\nfoo\nbar\nfoo\nf00\n%*&^*&^&\n***")}, |
| 72 | + want: "1. Hello\n2. foo\n3. bar\n4. foo\n5. f00\n6. %*&^*&^&\n7. ***", |
| 73 | + }, { |
| 74 | + name: "Multi line string ending with \\n", |
| 75 | + args: args{data: []byte("Hello\nfoo\nbar\n")}, |
| 76 | + want: "1. Hello\n2. foo\n3. bar\n", |
| 77 | + }, { |
| 78 | + name: "Multi line string with padding", |
| 79 | + args: args{data: []byte("Hello\nfoo\nbar\nfoo\nf00\n%*&^*&^&\n***\neight\nnine\nten")}, |
| 80 | + want: " 1. Hello\n 2. foo\n 3. bar\n 4. foo\n 5. f00\n 6. %*&^*&^&\n 7. ***\n 8. eight\n 9. nine\n10. ten", |
| 81 | + }, { |
| 82 | + name: "Test multi lingual character", |
| 83 | + args: args{data: []byte("नमस्ते")}, |
| 84 | + want: "1. नमस्ते", |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "Lines with empty lines between", |
| 88 | + args: args{data: []byte("this is string\n\nfollowed by empty line and another")}, |
| 89 | + want: "1. this is string\n\n2. followed by empty line and another", |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "Lines with first empty line", |
| 93 | + args: args{data: []byte("\nthis is string after newline\n\nfollowed by empty line and another")}, |
| 94 | + want: "\n1. this is string after newline\n\n2. followed by empty line and another", |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "Lines with space-only lines between", |
| 98 | + args: args{data: []byte("this is string\n \nfollowed by three-space line and another")}, |
| 99 | + want: "1. this is string\n2. \n3. followed by three-space line and another", |
| 100 | + }, |
| 101 | + } |
| 102 | + for _, tt := range tests { |
| 103 | + t.Run(tt.name, func(t *testing.T) { |
| 104 | + p := LineNumberer{} |
| 105 | + got, err := p.Transform(tt.args.data, tt.args.in1...) |
| 106 | + if (err != nil) != tt.wantErr { |
| 107 | + t.Errorf("Transform() error = %v, wantErr %v", err, tt.wantErr) |
| 108 | + return |
| 109 | + } |
| 110 | + if got != tt.want { |
| 111 | + t.Errorf("Transform() got = %v, want %v", got, tt.want) |
| 112 | + } |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
0 commit comments