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.

124 lines
4.0 KiB

  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package present
  5. import (
  6. "fmt"
  7. "reflect"
  8. "testing"
  9. )
  10. func TestSplit(t *testing.T) {
  11. var tests = []struct {
  12. in string
  13. out []string
  14. }{
  15. {"", []string{}},
  16. {" ", []string{" "}},
  17. {"abc", []string{"abc"}},
  18. {"abc def", []string{"abc", " ", "def"}},
  19. {"abc def ", []string{"abc", " ", "def", " "}},
  20. {"hey [[http://golang.org][Gophers]] around",
  21. []string{"hey", " ", "[[http://golang.org][Gophers]]", " ", "around"}},
  22. {"A [[http://golang.org/doc][two words]] link",
  23. []string{"A", " ", "[[http://golang.org/doc][two words]]", " ", "link"}},
  24. {"Visit [[http://golang.org/doc]] now",
  25. []string{"Visit", " ", "[[http://golang.org/doc]]", " ", "now"}},
  26. {"not [[http://golang.org/doc][a [[link]] ]] around",
  27. []string{"not", " ", "[[http://golang.org/doc][a [[link]]", " ", "]]", " ", "around"}},
  28. {"[[http://golang.org][foo bar]]",
  29. []string{"[[http://golang.org][foo bar]]"}},
  30. {"ends with [[http://golang.org][link]]",
  31. []string{"ends", " ", "with", " ", "[[http://golang.org][link]]"}},
  32. {"my talk ([[http://talks.golang.org/][slides here]])",
  33. []string{"my", " ", "talk", " ", "(", "[[http://talks.golang.org/][slides here]]", ")"}},
  34. }
  35. for _, test := range tests {
  36. out := split(test.in)
  37. if !reflect.DeepEqual(out, test.out) {
  38. t.Errorf("split(%q):\ngot\t%q\nwant\t%q", test.in, out, test.out)
  39. }
  40. }
  41. }
  42. func TestFont(t *testing.T) {
  43. var tests = []struct {
  44. in string
  45. out string
  46. }{
  47. {"", ""},
  48. {" ", " "},
  49. {"\tx", "\tx"},
  50. {"_a_", "<i>a</i>"},
  51. {"*a*", "<b>a</b>"},
  52. {"`a`", "<code>a</code>"},
  53. {"_a_b_", "<i>a b</i>"},
  54. {"_a__b_", "<i>a_b</i>"},
  55. {"_a___b_", "<i>a_ b</i>"},
  56. {"*a**b*?", "<b>a*b</b>?"},
  57. {"_a_<>_b_.", "<i>a <> b</i>."},
  58. {"(_a_)", "(<i>a</i>)"},
  59. {"((_a_), _b_, _c_).", "((<i>a</i>), <i>b</i>, <i>c</i>)."},
  60. {"(_a)", "(_a)"},
  61. {"(_a)", "(_a)"},
  62. {"_Why_use_scoped__ptr_? Use plain ***ptr* instead.", "<i>Why use scoped_ptr</i>? Use plain <b>*ptr</b> instead."},
  63. {"_hey_ [[http://golang.org][*Gophers*]] *around*",
  64. `<i>hey</i> <a href="http://golang.org" target="_blank"><b>Gophers</b></a> <b>around</b>`},
  65. {"_hey_ [[http://golang.org][so _many_ *Gophers*]] *around*",
  66. `<i>hey</i> <a href="http://golang.org" target="_blank">so <i>many</i> <b>Gophers</b></a> <b>around</b>`},
  67. {"Visit [[http://golang.org]] now",
  68. `Visit <a href="http://golang.org" target="_blank">golang.org</a> now`},
  69. {"my talk ([[http://talks.golang.org/][slides here]])",
  70. `my talk (<a href="http://talks.golang.org/" target="_blank">slides here</a>)`},
  71. {"Markup—_especially_italic_text_—can easily be overused.",
  72. `Markup—<i>especially italic text</i>—can easily be overused.`},
  73. {"`go`get`'s codebase", // ascii U+0027 ' before s
  74. `<code>go get</code>'s codebase`},
  75. {"`go`get`’s codebase", // unicode right single quote U+2019 ’ before s
  76. `<code>go get</code>’s codebase`},
  77. {"a_variable_name",
  78. `a_variable_name`},
  79. }
  80. for _, test := range tests {
  81. out := font(test.in)
  82. if out != test.out {
  83. t.Errorf("font(%q):\ngot\t%q\nwant\t%q", test.in, out, test.out)
  84. }
  85. }
  86. }
  87. func TestStyle(t *testing.T) {
  88. var tests = []struct {
  89. in string
  90. out string
  91. }{
  92. {"", ""},
  93. {" ", " "},
  94. {"\tx", "\tx"},
  95. {"_a_", "<i>a</i>"},
  96. {"*a*", "<b>a</b>"},
  97. {"`a`", "<code>a</code>"},
  98. {"_a_b_", "<i>a b</i>"},
  99. {"_a__b_", "<i>a_b</i>"},
  100. {"_a___b_", "<i>a_ b</i>"},
  101. {"*a**b*?", "<b>a*b</b>?"},
  102. {"_a_<>_b_.", "<i>a &lt;&gt; b</i>."},
  103. {"(_a_<>_b_)", "(<i>a &lt;&gt; b</i>)"},
  104. {"((_a_), _b_, _c_).", "((<i>a</i>), <i>b</i>, <i>c</i>)."},
  105. {"(_a)", "(_a)"},
  106. }
  107. for _, test := range tests {
  108. out := string(Style(test.in))
  109. if out != test.out {
  110. t.Errorf("style(%q):\ngot\t%q\nwant\t%q", test.in, out, test.out)
  111. }
  112. }
  113. }
  114. func ExampleStyle() {
  115. const s = "*Gophers* are _clearly_ > *cats*!"
  116. fmt.Println(Style(s))
  117. // Output: <b>Gophers</b> are <i>clearly</i> &gt; <b>cats</b>!
  118. }