odf_test.go 3.9 KB

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