odf_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package odf
  2. import (
  3. "github.com/kpmy/odf/generators"
  4. "github.com/kpmy/odf/mappers"
  5. "github.com/kpmy/odf/mappers/attr"
  6. "github.com/kpmy/odf/model"
  7. _ "github.com/kpmy/odf/model/stub"
  8. "github.com/kpmy/odf/xmlns"
  9. "github.com/kpmy/odf/xmlns/fo"
  10. "github.com/kpmy/odf/xmlns/table"
  11. "github.com/kpmy/ypk/assert"
  12. "image/color"
  13. "os"
  14. "testing"
  15. )
  16. func TestModel(t *testing.T) {
  17. m := model.ModelFactory()
  18. if m == nil {
  19. t.Error("model is nil")
  20. }
  21. w := m.NewWriter()
  22. if w == nil {
  23. t.Error("writer is nil")
  24. }
  25. }
  26. func TestMappers(t *testing.T) {
  27. m := model.ModelFactory()
  28. fm := &mappers.Formatter{}
  29. fm.ConnectTo(m)
  30. fm.MimeType = xmlns.MimeText
  31. fm.Init()
  32. }
  33. func TestGenerators(t *testing.T) {
  34. output, _ := os.OpenFile("test-basics.odf", os.O_CREATE|os.O_WRONLY, 0666)
  35. m := model.ModelFactory()
  36. fm := &mappers.Formatter{}
  37. fm.ConnectTo(m)
  38. fm.MimeType = xmlns.MimeText
  39. fm.Init()
  40. generators.GeneratePackage(m, nil, output, fm.MimeType)
  41. assert.For(output.Close() == nil, 20)
  42. }
  43. func TestStructure(t *testing.T) {
  44. output, _ := os.OpenFile("test-text.odf", os.O_CREATE|os.O_WRONLY, 0666)
  45. m := model.ModelFactory()
  46. fm := &mappers.Formatter{}
  47. fm.ConnectTo(m)
  48. fm.MimeType = xmlns.MimeText
  49. fm.Init()
  50. fm.WriteString("Hello, World! \t \n \r фыва фыва \n фыва")
  51. generators.GeneratePackage(m, nil, output, fm.MimeType)
  52. assert.For(output.Close() == nil, 20)
  53. }
  54. func TestStylesMechanism(t *testing.T) {
  55. output, _ := os.OpenFile("test-styles.odf", os.O_CREATE|os.O_WRONLY, 0666)
  56. m := model.ModelFactory()
  57. fm := &mappers.Formatter{}
  58. fm.ConnectTo(m)
  59. fm.MimeType = xmlns.MimeText
  60. fm.Init()
  61. fm.RegisterFont("Arial", "Arial")
  62. fm.RegisterFont("Courier New", "Courier New")
  63. fm.SetDefaults(new(attr.TextAttributes).Size(18).FontFace("Courier New"))
  64. fm.SetDefaults(new(attr.TextAttributes).Size(16).FontFace("Courier New"))
  65. fm.WriteString("Hello, World!\n")
  66. fm.SetAttr(new(attr.TextAttributes).Size(32).FontFace("Arial"))
  67. fm.WriteString(`Hello, Go!`)
  68. fm.SetAttr(new(attr.TextAttributes).Size(36).FontFace("Courier New").Bold().Italic())
  69. fm.WriteString(` Hello, Again!`)
  70. fm.SetAttr(new(attr.TextAttributes).Size(32).FontFace("Arial")) //test attribute cache
  71. fm.SetAttr(new(attr.TextAttributes).Size(32).FontFace("Arial").Color(color.RGBA{0x00, 0xff, 0xff, 0xff}))
  72. fm.WriteString("\nNo, not you again!")
  73. fm.SetAttr(new(attr.ParagraphAttributes).AlignRight().PageBreak())
  74. fm.WritePara("Page break!\r")
  75. fm.SetAttr(nil)
  76. fm.WriteString(`Hello, Пщ!`)
  77. generators.GeneratePackage(m, nil, output, fm.MimeType)
  78. assert.For(output.Close() == nil, 20)
  79. }
  80. func TestTables(t *testing.T) {
  81. table := func(fm *mappers.Formatter) {
  82. tm := &mappers.TableMapper{}
  83. tm.ConnectTo(fm)
  84. tm.Write("test", 5, 10)
  85. tt := tm.List["test"]
  86. tm.WriteColumns(tt, 4)
  87. tm.WriteRows(tt, 3)
  88. tm.Span(tt, 1, 2, 1, 3)
  89. tm.Pos(tt, 0, 0).WritePara("Hello, table world!")
  90. tm.Pos(tt, 1, 2).WritePara("Hello, table world!")
  91. }
  92. {
  93. output, _ := os.OpenFile("test-odt-tables.odf", os.O_CREATE|os.O_WRONLY, 0666)
  94. m := model.ModelFactory()
  95. fm := &mappers.Formatter{}
  96. fm.ConnectTo(m)
  97. fm.MimeType = xmlns.MimeText
  98. fm.Init()
  99. table(fm)
  100. generators.GeneratePackage(m, nil, output, fm.MimeType)
  101. assert.For(output.Close() == nil, 20)
  102. }
  103. {
  104. output, _ := os.OpenFile("test-ods-tables.odf", os.O_CREATE|os.O_WRONLY, 0666)
  105. m := model.ModelFactory()
  106. fm := &mappers.Formatter{}
  107. fm.ConnectTo(m)
  108. fm.MimeType = xmlns.MimeSpreadsheet
  109. fm.Init()
  110. table(fm)
  111. generators.GeneratePackage(m, nil, output, fm.MimeType)
  112. assert.For(output.Close() == nil, 20)
  113. }
  114. }
  115. func TestDraw(t *testing.T) {
  116. const ImagePng xmlns.Mime = "image/png"
  117. output, _ := os.OpenFile("test-draw.odf", os.O_CREATE|os.O_WRONLY, 0666)
  118. m := model.ModelFactory()
  119. fm := &mappers.Formatter{}
  120. fm.ConnectTo(m)
  121. fm.MimeType = xmlns.MimeText
  122. fm.Init()
  123. embed := make(map[string]generators.Embeddable)
  124. {
  125. img, _ := os.Open("2go.png")
  126. d := mappers.NewDraw(img, ImagePng)
  127. url := d.WriteTo(fm, "Two Gophers", 6.07, 3.53) //magic? real size of `project.png`
  128. embed[url] = d
  129. }
  130. generators.GeneratePackage(m, embed, output, fm.MimeType)
  131. assert.For(output.Close() == nil, 20)
  132. }
  133. func TestTableStyles(t *testing.T) {
  134. output, _ := os.OpenFile("test-table-styles.odf", os.O_CREATE|os.O_WRONLY, 0666)
  135. m := model.ModelFactory()
  136. fm := &mappers.Formatter{}
  137. fm.ConnectTo(m)
  138. fm.MimeType = xmlns.MimeText
  139. fm.Init()
  140. fm.SetAttr(new(attr.TableAttributes).BorderModel(table.BorderModelCollapsing).AlignCenter().Width(10.0))
  141. fm.SetAttr(new(attr.TableRowAttributes).UseOptimalRowHeight()).SetAttr(new(attr.TableColumnAttributes).UseOptimalColumnWidth())
  142. fm.SetAttr(new(attr.TableCellAttributes).Border(attr.Border{Width: 0.01, Color: color.Black, Style: fo.Solid}))
  143. tm := &mappers.TableMapper{}
  144. tm.ConnectTo(fm)
  145. tm.Write("test", 5, 10)
  146. tt := tm.List["test"]
  147. tm.Pos(tt, 0, 0).WriteString("Hello!")
  148. generators.GeneratePackage(m, nil, output, fm.MimeType)
  149. assert.For(output.Close() == nil, 20)
  150. }