my CMS/Blog engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gen_test.go 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package main
  2. /*
  3. * This is freesofware under 2-clause BSD license, See LICENSE file
  4. * Copyright (c) 2013, Martin Angers
  5. */
  6. import (
  7. "bytes"
  8. "io/ioutil"
  9. "log"
  10. "sort"
  11. "testing"
  12. "time"
  13. )
  14. func mustParse(s string) time.Time {
  15. t, err := time.Parse("2006-01-02", s)
  16. if err != nil {
  17. panic(err)
  18. }
  19. return t
  20. }
  21. func TestSort(t *testing.T) {
  22. ps := make(sortablePosts, 5)
  23. ps[0] = &LongPost{
  24. ShortPost: &ShortPost{
  25. Title: "a",
  26. PubTime: mustParse("2012-01-07"),
  27. },
  28. }
  29. ps[1] = &LongPost{
  30. ShortPost: &ShortPost{
  31. Title: "b",
  32. PubTime: mustParse("2012-04-22"),
  33. },
  34. }
  35. ps[2] = &LongPost{
  36. ShortPost: &ShortPost{
  37. Title: "c",
  38. PubTime: mustParse("2012-01-01"),
  39. },
  40. }
  41. ps[3] = &LongPost{
  42. ShortPost: &ShortPost{
  43. Title: "d",
  44. PubTime: mustParse("2011-11-30"),
  45. },
  46. }
  47. ps[4] = &LongPost{
  48. ShortPost: &ShortPost{
  49. Title: "e",
  50. PubTime: mustParse("2012-12-01"),
  51. },
  52. }
  53. sort.Sort(ps)
  54. buf := bytes.NewBuffer(nil)
  55. for _, p := range ps {
  56. buf.WriteString(p.Title)
  57. }
  58. if buf.String() != "dcabe" {
  59. t.Errorf("expected 'dcabe', got %s", buf.String())
  60. }
  61. }
  62. func BenchmarkGenerateSite(b *testing.B) {
  63. b.StopTimer()
  64. log.SetOutput(ioutil.Discard)
  65. Options.RecentPostsCount = 5
  66. PublicDir = "./examples/amber/public"
  67. PostsDir = "./examples/amber/posts"
  68. TemplatesDir = "./examples/amber/templates"
  69. b.StartTimer()
  70. for i := 0; i < b.N; i++ {
  71. err := generateSite()
  72. if err != nil {
  73. b.Fatal(err)
  74. }
  75. }
  76. }