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 BenchmarkExcerptPerform_Radious(b *testing.B) { exc := stringfy.NewExcerpt() exc.Options(stringfy.AddRadius(5)) for i := 0; i < b.N; i++ { exc.Perform(text, "to") } }
func BenchmarkExcerptPerform_RadiosSeparator(b *testing.B) { exc := stringfy.NewExcerpt() exc.Options(stringfy.AddRadius(1), stringfy.AddSeparator(" ")) for i := 0; i < b.N; i++ { exc.Perform(text, "popular") } }
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) } } }