|
| 1 | +package entity_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/getfider/fider/app/models/entity" |
| 7 | + |
| 8 | + . "github.com/getfider/fider/app/pkg/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestComment_ParseMentions(t *testing.T) { |
| 12 | + RegisterT(t) |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + content string |
| 16 | + expected []entity.Mention |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "no mentions", |
| 20 | + content: "This is a regular comment \\\" just here", |
| 21 | + expected: []entity.Mention{}, |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "Simple mention", |
| 25 | + content: `Hello there @{"id":1,"name":"John Doe","isNew":false} how are you`, |
| 26 | + expected: []entity.Mention{{ID: 1, Name: "John Doe", IsNew: false}}, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "Simple mention 2", |
| 30 | + content: `Hello there @{"id":2,"name":"John Doe Smith","isNew":true} how are you`, |
| 31 | + expected: []entity.Mention{{ID: 2, Name: "John Doe Smith", IsNew: true}}, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "Multiple mentions", |
| 35 | + content: `Hello there @{"id":2,"name":"John Doe Smith","isNew":true} and @{"id":1,"name":"John Doe","isNew":false} how are you`, |
| 36 | + expected: []entity.Mention{{ID: 2, Name: "John Doe Smith", IsNew: true}, {ID: 1, Name: "John Doe", IsNew: false}}, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "Some odd JSON", |
| 40 | + content: `Hello there @{"id":2,name:"John Doe Smith","isNew":true} how are you`, |
| 41 | + expected: []entity.Mention{}, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + for _, tt := range tests { |
| 46 | + t.Run(tt.name, func(t *testing.T) { |
| 47 | + comment := &entity.Comment{Content: tt.content} |
| 48 | + comment.ParseMentions() |
| 49 | + Expect(comment.Mentions).Equals(tt.expected) |
| 50 | + }) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestStripMentionMetaData(t *testing.T) { |
| 55 | + RegisterT(t) |
| 56 | + |
| 57 | + for input, expected := range map[string]string{ |
| 58 | + `@{\"id\":1,\"name\":\"John Doe quoted\"}`: "@John Doe quoted", |
| 59 | + `@{"id":1,"name":"John Doe"}`: "@John Doe", |
| 60 | + `@{"id":1,"name":"JohnDoe"}`: "@JohnDoe", |
| 61 | + `@{\"id\":1,\"name\":\"JohnDoe quoted\"}`: "@JohnDoe quoted", |
| 62 | + `@{"id":1,"name":"John Smith Doe"}`: "@John Smith Doe", |
| 63 | + `@{\"id\":1,\"name\":\"John Smith Doe quoted\"}`: "@John Smith Doe quoted", |
| 64 | + "Hello there how are you": "Hello there how are you", |
| 65 | + `Hello there @{"id":1,"name":"John Doe"}`: "Hello there @John Doe", |
| 66 | + `Hello there @{"id":1,"name":"John Doe quoted"}`: "Hello there @John Doe quoted", |
| 67 | + `Hello both @{"id":1,"name":"John Doe"} and @{"id":2,"name":"John Smith"}`: "Hello both @John Doe and @John Smith", |
| 68 | + } { |
| 69 | + output := entity.CommentString(input).FormatMentionJson(func(mention entity.Mention) string { |
| 70 | + return mention.Name |
| 71 | + }) |
| 72 | + Expect(output).Equals(expected) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestStripMentionMetaDataDoesntBreakUserInput(t *testing.T) { |
| 77 | + RegisterT(t) |
| 78 | + |
| 79 | + for input, expected := range map[string]string{ |
| 80 | + `There is nothing here`: "There is nothing here", |
| 81 | + `There is nothing here {ok}`: "There is nothing here {ok}", |
| 82 | + `This is a message for {{matt}}`: "This is a message for {{matt}}", |
| 83 | + `This is a message for {{id:1,wiggles:true}}`: "This is a message for {{id:1,wiggles:true}}", |
| 84 | + `Although uncommon, someone could enter @{something} like this`: "Although uncommon, someone could enter @{something} like this", |
| 85 | + `Or @{"id":100,"wiggles":"yes"} something like this`: `Or @{"id":100,"wiggles":"yes"} something like this`, |
| 86 | + } { |
| 87 | + output := entity.CommentString(input).FormatMentionJson(func(mention entity.Mention) string { |
| 88 | + return mention.Name |
| 89 | + }) |
| 90 | + Expect(output).Equals(expected) |
| 91 | + } |
| 92 | +} |
0 commit comments