z_spec_test.go 6.9 KB

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