func BenchmarkExcerptPerform_RadiosOmission(b *testing.B) { exc := stringfy.NewExcerpt() exc.Options(stringfy.AddRadius(5), stringfy.AddOmission("(...)")) for i := 0; i < b.N; i++ { exc.Perform(text, "sit") } }
func Test_Truncate(t *testing.T) { tests := []struct { in string length int addLength bool omission string addOmission bool separator string addSeparator bool out string }{ { in: "Lorem ipsum.", out: "Lorem ipsum.", }, { in: "Lor", addLength: true, length: 3, out: "Lor", }, { in: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", out: "Lorem ipsum dolor sit amet,...", }, { in: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", addOmission: true, omission: "... (continued)", out: "Lorem ipsum dol... (continued)", }, { in: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", addLength: true, length: 13, addSeparator: true, separator: " ", out: "Lorem ipsum...", }, { in: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", addLength: true, length: 13, addSeparator: true, separator: " ", addOmission: true, omission: "... (continued) ...", out: "Lorem ipsum... (continued) ...", }, { in: "Lorem ipsum dolor sit amet, c", addLength: true, length: 13, addSeparator: true, separator: " ", addOmission: true, omission: "... (continued) ...", out: "Lorem ipsum dolor sit amet, c", }, } for _, test := range tests { tr := stringfy.NewTruncate() if test.addLength { tr.Options(stringfy.AddLength(test.length)) } if test.addOmission { tr.Options(stringfy.AddOmission(test.omission)) } if test.addSeparator { tr.Options(stringfy.AddSeparator(test.separator)) } trf := tr.Perform(test.in) if trf != test.out { t.Errorf("\nExpected: %s\nGot: %s", test.out, trf) } } }
func Test_Excerpt(t *testing.T) { tests := []struct { text string phrase string radious int addRadious bool omission string addOmission bool separator string addSeparator bool out string errorOut string }{ { text: text, phrase: "bhtk", addRadious: true, radious: 5, out: "", errorOut: "Phrase (bhtk) not found.", }, { text: text, phrase: "to", addRadious: true, radious: 5, out: "...rary to popu...", errorOut: "", }, { text: text, phrase: "tra", addRadious: true, radious: 5, out: "Contrary to...", errorOut: "", }, { text: text, phrase: "Rackham", addRadious: true, radious: 5, out: "...y H. Rackham.", errorOut: "", }, { text: text, phrase: "sit", addRadious: true, radious: 5, addOmission: true, omission: "(...)", out: "(...)olor sit amet(...)", errorOut: "", }, { text: text, phrase: "popular", addRadious: true, radious: 1, addSeparator: true, separator: " ", out: "...to popular belief,...", errorOut: "", }, { text: text, phrase: "to", addRadious: true, radious: 2, addSeparator: true, separator: " ", out: "Contrary to popular belief,...", errorOut: "", }, { text: text, phrase: "bhtk", addRadious: true, radious: 1, addSeparator: true, separator: " ", out: "", errorOut: "Phrase (bhtk) not found.", }, { text: text, phrase: "to", addRadious: true, radious: 5, addSeparator: true, separator: "-", out: "...rary to popu...", errorOut: "", }, { text: text, phrase: "to popular", addRadious: true, radious: 5, addSeparator: true, separator: " ", out: "", errorOut: "When composing the excerpt, your phrase should not contain more than one word.", }, { text: text, phrase: "to popular", addRadious: true, radious: 5, addSeparator: true, separator: "-", out: "", errorOut: "When composing the excerpt, your phrase should not contain more than one word.", }, } for _, test := range tests { exc := stringfy.NewExcerpt() if test.addRadious { exc.Options(stringfy.AddRadius(test.radious)) } if test.addOmission { exc.Options(stringfy.AddOmission(test.omission)) } if test.addSeparator { exc.Options(stringfy.AddSeparator(test.separator)) } excf, err := exc.Perform(test.text, test.phrase) if err != nil { if err.Error() != test.errorOut { t.Errorf("\nError Expected: %s\nError Got: %s", test.errorOut, err.Error()) } } if excf != test.out { t.Errorf("\nExpected: %s\nGot: %s", test.out, excf) } } }