|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func Test_removeCharacters(t *testing.T) { |
| 10 | + type args struct { |
| 11 | + input string |
| 12 | + characters string |
| 13 | + } |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + args args |
| 17 | + want string |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "Characters removal: suffix", |
| 21 | + args: args{ |
| 22 | + input: "Załoenie;!", |
| 23 | + characters: ";!", |
| 24 | + }, |
| 25 | + want: "Załoenie", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "Characters removal: prefix", |
| 29 | + args: args{ |
| 30 | + input: ",#Załoenie", |
| 31 | + characters: ";!-,#", |
| 32 | + }, |
| 33 | + want: "Załoenie", |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "Characters removal: both", |
| 37 | + args: args{ |
| 38 | + input: "-!Załoenie;!", |
| 39 | + characters: ";!-", |
| 40 | + }, |
| 41 | + want: "Załoenie", |
| 42 | + }, |
| 43 | + } |
| 44 | + assert := assert.New(t) |
| 45 | + for _, tt := range tests { |
| 46 | + t.Run(tt.name, func(t *testing.T) { |
| 47 | + got := removeCharacters(tt.args.input, tt.args.characters) |
| 48 | + assert.Equal(tt.want, got, "Unexpected result in test: "+tt.name) |
| 49 | + }) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func Test_resultsArray_extractWords(t *testing.T) { |
| 54 | + type fields struct { |
| 55 | + Results []string |
| 56 | + } |
| 57 | + type args struct { |
| 58 | + s string |
| 59 | + } |
| 60 | + tests := []struct { |
| 61 | + name string |
| 62 | + fields fields |
| 63 | + args args |
| 64 | + wants []string |
| 65 | + }{ |
| 66 | + { |
| 67 | + name: "Simple sentence", |
| 68 | + args: args{ |
| 69 | + s: "Within this tutorial, we are going to look at how you can effectively read and write to files within your filesystem using the go programming language.", |
| 70 | + }, |
| 71 | + wants: []string{"within", "this", "tutorial", "we", "are", "going", "to", "look", "at", "how", "you", "can", "effectively", "read", "and", "write", "to", "files", "within", "your", "filesystem", "using", "the", "go", "programming", "language"}, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "Multiline sentence", |
| 75 | + args: args{ |
| 76 | + s: `The UK has recorded another five COVID deaths and 2,047 more cases in the latest daily figures. |
| 77 | +
|
| 78 | + It compares with seven deaths and 1,907 cases this time last week, while the latest seven-day rolling average is 11.3 and 2,080.`, |
| 79 | + }, |
| 80 | + wants: []string{"the", "uk", "has", "recorded", "another", "five", "covid", "deaths", "and", "2047", "more", "cases", "in", "the", "latest", "daily", "figures", "it", "compares", "with", "seven", "deaths", "and", "1907", "cases", "this", "time", "last", "week", "while", "the", "latest", "sevenday", "rolling", "average", "is", "113", "and", "2080"}, |
| 81 | + }, |
| 82 | + } |
| 83 | + assert := assert.New(t) |
| 84 | + for _, tt := range tests { |
| 85 | + t.Run(tt.name, func(t *testing.T) { |
| 86 | + r := &resultsArray{ |
| 87 | + Results: tt.fields.Results, |
| 88 | + } |
| 89 | + r.extractWords(tt.args.s) |
| 90 | + assert.Equal(tt.wants, r.Results, "Unexpected result in test: "+tt.name) |
| 91 | + }) |
| 92 | + } |
| 93 | +} |
0 commit comments