z_spec_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 struct {
  65. m map[specPart]bool
  66. d *xml.Decoder
  67. }
  68. func joinSection(sec []int) string {
  69. s := fmt.Sprintf("%d", sec[0])
  70. for _, n := range sec[1:] {
  71. s = fmt.Sprintf("%s.%d", s, n)
  72. }
  73. return s
  74. }
  75. func (sc specCoverage) readSection(sec []int) {
  76. var (
  77. buf = new(bytes.Buffer)
  78. sub = 0
  79. )
  80. for {
  81. tk, err := sc.d.Token()
  82. if err != nil {
  83. if err == io.EOF {
  84. return
  85. }
  86. panic(err)
  87. }
  88. switch v := tk.(type) {
  89. case xml.StartElement:
  90. if skipElement(v) {
  91. if err := sc.d.Skip(); err != nil {
  92. panic(err)
  93. }
  94. break
  95. }
  96. if v.Name.Local == "section" {
  97. sub++
  98. sc.readSection(append(sec, sub))
  99. }
  100. case xml.CharData:
  101. if len(sec) == 0 {
  102. break
  103. }
  104. buf.Write(v)
  105. case xml.EndElement:
  106. if v.Name.Local == "section" {
  107. sc.addSentences(joinSection(sec), buf.String())
  108. return
  109. }
  110. }
  111. }
  112. }
  113. var skipAnchor = map[string]bool{
  114. "intro": true,
  115. "Overview": true,
  116. }
  117. var skipTitle = map[string]bool{
  118. "Acknowledgements": true,
  119. "Change Log": true,
  120. "Document Organization": true,
  121. "Conventions and Terminology": true,
  122. }
  123. func skipElement(s xml.StartElement) bool {
  124. switch s.Name.Local {
  125. case "artwork":
  126. return true
  127. case "section":
  128. for _, attr := range s.Attr {
  129. switch attr.Name.Local {
  130. case "anchor":
  131. if skipAnchor[attr.Value] || strings.HasPrefix(attr.Value, "changes.since.") {
  132. return true
  133. }
  134. case "title":
  135. if skipTitle[attr.Value] {
  136. return true
  137. }
  138. }
  139. }
  140. }
  141. return false
  142. }
  143. func readSpecCov(r io.Reader) specCoverage {
  144. sc := specCoverage{
  145. m: map[specPart]bool{},
  146. d: xml.NewDecoder(r)}
  147. sc.readSection(nil)
  148. return sc
  149. }
  150. func (sc specCoverage) addSentences(sec string, sentence string) {
  151. for _, s := range parseSentences(sentence) {
  152. sc.m[specPart{sec, s}] = false
  153. }
  154. }
  155. func (sc specCoverage) cover(sec string, sentence string) {
  156. for _, s := range parseSentences(sentence) {
  157. p := specPart{sec, s}
  158. if _, ok := sc.m[p]; !ok {
  159. panic(fmt.Sprintf("Not found in spec: %q, %q", sec, s))
  160. }
  161. sc.m[specPart{sec, s}] = true
  162. }
  163. }
  164. func (sc specCoverage) uncovered() []specPart {
  165. var a []specPart
  166. for p, covered := range sc.m {
  167. if !covered {
  168. a = append(a, p)
  169. }
  170. }
  171. return a
  172. }
  173. var whitespaceRx = regexp.MustCompile(`\s+`)
  174. func parseSentences(sens string) []string {
  175. sens = strings.TrimSpace(sens)
  176. if sens == "" {
  177. return nil
  178. }
  179. ss := strings.Split(whitespaceRx.ReplaceAllString(sens, " "), ". ")
  180. for i, s := range ss {
  181. s = strings.TrimSpace(s)
  182. if !strings.HasSuffix(s, ".") {
  183. s += "."
  184. }
  185. ss[i] = s
  186. }
  187. return ss
  188. }
  189. func TestSpecParseSentences(t *testing.T) {
  190. tests := []struct {
  191. ss string
  192. want []string
  193. }{
  194. {"Sentence 1. Sentence 2.",
  195. []string{
  196. "Sentence 1.",
  197. "Sentence 2.",
  198. }},
  199. {"Sentence 1. \nSentence 2.\tSentence 3.",
  200. []string{
  201. "Sentence 1.",
  202. "Sentence 2.",
  203. "Sentence 3.",
  204. }},
  205. }
  206. for i, tt := range tests {
  207. got := parseSentences(tt.ss)
  208. if !reflect.DeepEqual(got, tt.want) {
  209. t.Errorf("%d: got = %q, want %q", i, got, tt.want)
  210. }
  211. }
  212. }
  213. func TestSpecBuildCoverageTable(t *testing.T) {
  214. testdata := `
  215. <rfc>
  216. <middle>
  217. <section anchor="foo" title="Introduction">
  218. <t>Foo.</t>
  219. <t><t>Sentence 1.
  220. Sentence 2
  221. . Sentence 3.</t></t>
  222. </section>
  223. <section anchor="bar" title="Introduction">
  224. <t>Bar.</t>
  225. <section anchor="bar" title="Introduction">
  226. <t>Baz.</t>
  227. </section>
  228. </section>
  229. </middle>
  230. </rfc>`
  231. got := readSpecCov(strings.NewReader(testdata))
  232. want := specCoverage{
  233. m: map[specPart]bool{
  234. specPart{"1", "Foo."}: false,
  235. specPart{"1", "Sentence 1."}: false,
  236. specPart{"1", "Sentence 2."}: false,
  237. specPart{"1", "Sentence 3."}: false,
  238. specPart{"2", "Bar."}: false,
  239. specPart{"2.1", "Baz."}: false,
  240. },
  241. }
  242. if !reflect.DeepEqual(got, want) {
  243. t.Errorf("got = %+v, want %+v", got, want)
  244. }
  245. }
  246. func TestSpecUncovered(t *testing.T) {
  247. testdata := `
  248. <rfc>
  249. <middle>
  250. <section anchor="foo" title="Introduction">
  251. <t>Foo.</t>
  252. <t><t>Sentence 1.</t></t>
  253. </section>
  254. </middle>
  255. </rfc>`
  256. sp := readSpecCov(strings.NewReader(testdata))
  257. sp.cover("1", "Foo. Sentence 1.")
  258. want := specCoverage{
  259. m: map[specPart]bool{
  260. specPart{"1", "Foo."}: true,
  261. specPart{"1", "Sentence 1."}: true,
  262. },
  263. }
  264. if !reflect.DeepEqual(sp, want) {
  265. t.Errorf("got = %+v, want %+v", sp, want)
  266. }
  267. defer func() {
  268. if err := recover(); err == nil {
  269. t.Error("expected panic")
  270. }
  271. }()
  272. sp.cover("1", "Not in spec.")
  273. }
  274. func TestSpecCoverage(t *testing.T) {
  275. uncovered := defaultSpecCoverage.uncovered()
  276. if len(uncovered) == 0 {
  277. return
  278. }
  279. sort.Sort(bySpecSection(uncovered))
  280. const shortLen = 5
  281. if testing.Short() && len(uncovered) > shortLen {
  282. uncovered = uncovered[:shortLen]
  283. }
  284. t.Logf("COVER REPORT:")
  285. fails := 0
  286. for _, p := range uncovered {
  287. t.Errorf("\tSECTION %s: %s", p.section, p.sentence)
  288. fails++
  289. }
  290. t.Logf("%d sections not covered", fails)
  291. }