z_spec_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // Copyright 2014 The Go Authors.
  2. // See https://code.google.com/p/go/source/browse/CONTRIBUTORS
  3. // Licensed under the same terms as Go itself:
  4. // https://code.google.com/p/go/source/browse/LICENSE
  5. package http2
  6. import (
  7. "bytes"
  8. "encoding/xml"
  9. "flag"
  10. "fmt"
  11. "io"
  12. "os"
  13. "reflect"
  14. "regexp"
  15. "sort"
  16. "strconv"
  17. "strings"
  18. "testing"
  19. )
  20. var coverSpec = flag.Bool("coverspec", false, "Run spec coverage tests")
  21. // The global map of sentence coverage for the http2 spec.
  22. var defaultSpecCoverage specCoverage
  23. func init() {
  24. f, err := os.Open("testdata/draft-ietf-httpbis-http2.xml")
  25. if err != nil {
  26. panic(err)
  27. }
  28. defaultSpecCoverage = readSpecCov(f)
  29. }
  30. // specCover marks all sentences for section sec in defaultSpecCoverage. Sentences not
  31. // "covered" will be included in report outputed by TestSpecCoverage.
  32. func specCover(sec, sentences string) {
  33. defaultSpecCoverage.cover(sec, sentences)
  34. }
  35. type specPart struct {
  36. section string
  37. sentence string
  38. }
  39. func (ss specPart) Less(oo specPart) bool {
  40. atoi := func(s string) int {
  41. n, err := strconv.Atoi(s)
  42. if err != nil {
  43. panic(err)
  44. }
  45. return n
  46. }
  47. a := strings.Split(ss.section, ".")
  48. b := strings.Split(oo.section, ".")
  49. for i := 0; i < len(a); i++ {
  50. if i >= len(b) {
  51. return false
  52. }
  53. x, y := atoi(a[i]), atoi(b[i])
  54. if x < y {
  55. return true
  56. }
  57. }
  58. return false
  59. }
  60. type bySpecSection []specPart
  61. func (a bySpecSection) Len() int { return len(a) }
  62. func (a bySpecSection) Less(i, j int) bool { return a[i].Less(a[j]) }
  63. func (a bySpecSection) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  64. type specCoverage map[specPart]bool
  65. func readSection(sc specCoverage, d *xml.Decoder, sec []int) {
  66. var (
  67. buf = new(bytes.Buffer)
  68. sub = 0
  69. )
  70. for {
  71. tk, err := d.Token()
  72. if err != nil {
  73. if err == io.EOF {
  74. return
  75. }
  76. panic(err)
  77. }
  78. switch v := tk.(type) {
  79. case xml.StartElement:
  80. if skipElement(v) {
  81. if err := d.Skip(); err != nil {
  82. panic(err)
  83. }
  84. break
  85. }
  86. if v.Name.Local == "section" {
  87. sub++
  88. readSection(sc, d, append(sec, sub))
  89. }
  90. case xml.CharData:
  91. if len(sec) == 0 {
  92. break
  93. }
  94. buf.Write(v)
  95. case xml.EndElement:
  96. if v.Name.Local == "section" {
  97. ssec := fmt.Sprintf("%d", sec[0])
  98. for _, n := range sec[1:] {
  99. ssec = fmt.Sprintf("%s.%d", ssec, n)
  100. }
  101. sc.addSentences(ssec, buf.String())
  102. return
  103. }
  104. }
  105. }
  106. }
  107. var skipAnchor = map[string]bool{
  108. "intro": true,
  109. "Overview": true,
  110. }
  111. var skipTitle = map[string]bool{
  112. "Acknowledgements": true,
  113. "Change Log": true,
  114. "Document Organization": true,
  115. "Conventions and Terminology": true,
  116. }
  117. func skipElement(s xml.StartElement) bool {
  118. switch s.Name.Local {
  119. case "artwork":
  120. return true
  121. case "section":
  122. for _, attr := range s.Attr {
  123. switch attr.Name.Local {
  124. case "anchor":
  125. if skipAnchor[attr.Value] || strings.HasPrefix(attr.Value, "changes.since.") {
  126. return true
  127. }
  128. case "title":
  129. if skipTitle[attr.Value] {
  130. return true
  131. }
  132. }
  133. }
  134. }
  135. return false
  136. }
  137. func readSpecCov(r io.Reader) specCoverage {
  138. d := xml.NewDecoder(r)
  139. sc := specCoverage{}
  140. readSection(sc, d, nil)
  141. return sc
  142. }
  143. func (sc specCoverage) addSentences(sec string, sentence string) {
  144. for _, s := range parseSentences(sentence) {
  145. sc[specPart{sec, s}] = false
  146. }
  147. }
  148. func (sc specCoverage) cover(sec string, sentence string) {
  149. for _, s := range parseSentences(sentence) {
  150. p := specPart{sec, s}
  151. if _, ok := sc[p]; !ok {
  152. panic(fmt.Sprintf("Not found in spec: %q, %q", sec, s))
  153. }
  154. sc[specPart{sec, s}] = true
  155. }
  156. }
  157. var whitespaceRx = regexp.MustCompile(`\s+`)
  158. func parseSentences(sens string) []string {
  159. sens = strings.TrimSpace(sens)
  160. if sens == "" {
  161. return nil
  162. }
  163. ss := strings.Split(whitespaceRx.ReplaceAllString(sens, " "), ". ")
  164. for i, s := range ss {
  165. s = strings.TrimSpace(s)
  166. if !strings.HasSuffix(s, ".") {
  167. s += "."
  168. }
  169. ss[i] = s
  170. }
  171. return ss
  172. }
  173. func TestSpecParseSentences(t *testing.T) {
  174. tests := []struct {
  175. ss string
  176. want []string
  177. }{
  178. {"Sentence 1. Sentence 2.",
  179. []string{
  180. "Sentence 1.",
  181. "Sentence 2.",
  182. }},
  183. {"Sentence 1. \nSentence 2.\tSentence 3.",
  184. []string{
  185. "Sentence 1.",
  186. "Sentence 2.",
  187. "Sentence 3.",
  188. }},
  189. }
  190. for i, tt := range tests {
  191. got := parseSentences(tt.ss)
  192. if !reflect.DeepEqual(got, tt.want) {
  193. t.Errorf("%d: got = %q, want %q", i, got, tt.want)
  194. }
  195. }
  196. }
  197. func TestSpecBuildCoverageTable(t *testing.T) {
  198. testdata := `
  199. <rfc>
  200. <middle>
  201. <section anchor="foo" title="Introduction">
  202. <t>Foo.</t>
  203. <t><t>Sentence 1.
  204. Sentence 2
  205. . Sentence 3.</t></t>
  206. </section>
  207. <section anchor="bar" title="Introduction">
  208. <t>Bar.</t>
  209. <section anchor="bar" title="Introduction">
  210. <t>Baz.</t>
  211. </section>
  212. </section>
  213. </middle>
  214. </rfc>`
  215. got := readSpecCov(strings.NewReader(testdata))
  216. want := specCoverage{
  217. specPart{"1", "Foo."}: false,
  218. specPart{"1", "Sentence 1."}: false,
  219. specPart{"1", "Sentence 2."}: false,
  220. specPart{"1", "Sentence 3."}: false,
  221. specPart{"2", "Bar."}: false,
  222. specPart{"2.1", "Baz."}: false,
  223. }
  224. if !reflect.DeepEqual(got, want) {
  225. t.Errorf("got = %+v, want %+v", got, want)
  226. }
  227. }
  228. func TestSpecUncovered(t *testing.T) {
  229. testdata := `
  230. <rfc>
  231. <middle>
  232. <section anchor="foo" title="Introduction">
  233. <t>Foo.</t>
  234. <t><t>Sentence 1.</t></t>
  235. </section>
  236. </middle>
  237. </rfc>`
  238. sp := readSpecCov(strings.NewReader(testdata))
  239. sp.cover("1", "Foo. Sentence 1.")
  240. want := specCoverage{
  241. specPart{"1", "Foo."}: true,
  242. specPart{"1", "Sentence 1."}: true,
  243. }
  244. if !reflect.DeepEqual(sp, want) {
  245. t.Errorf("got = %+v, want %+v", sp, want)
  246. }
  247. defer func() {
  248. if err := recover(); err == nil {
  249. t.Error("expected panic")
  250. }
  251. }()
  252. sp.cover("1", "Not in spec.")
  253. }
  254. func TestSpecCoverage(t *testing.T) {
  255. var notCovered bySpecSection
  256. for p, covered := range defaultSpecCoverage {
  257. if !covered {
  258. notCovered = append(notCovered, p)
  259. }
  260. }
  261. if len(notCovered) == 0 {
  262. return
  263. }
  264. sort.Sort(notCovered)
  265. const shortLen = 5
  266. if testing.Short() && len(notCovered) > shortLen {
  267. notCovered = notCovered[:shortLen]
  268. }
  269. t.Logf("COVER REPORT:")
  270. fails := 0
  271. for _, p := range notCovered {
  272. t.Errorf("\tSECTION %s: %s", p.section, p.sentence)
  273. fails++
  274. }
  275. t.Logf("%d sections not covered", fails)
  276. }