oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1package docx_test
2
3import (
4 "archive/zip"
5 "bytes"
6 "io"
7 "strings"
8 "testing"
9
10 "github.com/carlosframework/docs/internal/docx"
11 "github.com/carlosframework/docs/internal/export"
12)
13
14func part(t *testing.T, data []byte, name string) string {
15 t.Helper()
16 z, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
17 if err != nil {
18 t.Fatal(err)
19 }
20 for _, f := range z.File {
21 if f.Name == name {
22 r, _ := f.Open()
23 b, _ := io.ReadAll(r)
24 return string(b)
25 }
26 }
27 t.Fatalf("no %s in the .docx", name)
28 return ""
29}
30
31// Paul's default 5: Carlito goes to Word as Calibri and Caladea as
32// Cambria, so the document breaks its lines in Word where it does here,
33// and both come back as themselves. Carlito is the default, so it is
34// the document's own default (w:docDefaults) and its runs carry no
35// mark either way.
36func TestCarlitoAndCaladeaRoundTripAsCalibriAndCambria(t *testing.T) {
37 doc, err := export.Parse("", `{"type":"doc","content":[
38 {"type":"heading","attrs":{"level":2},"content":[{"type":"text","text":"Minutes"}]},
39 {"type":"paragraph","content":[
40 {"type":"text","text":"Body text, "},
41 {"type":"text","text":"bold","marks":[{"type":"strong"}]},
42 {"type":"text","text":" and a Cambria twin","marks":[{"type":"font_caladea"},{"type":"em"}]},
43 {"type":"text","text":" and Inter at 14","marks":[{"type":"font_inter"},{"type":"size_14"}]},
44 {"type":"text","text":" and Arial's twin","marks":[{"type":"font_sans"}]}
45 ]}]}`)
46 if err != nil {
47 t.Fatal(err)
48 }
49 data, err := docx.Write(doc)
50 if err != nil {
51 t.Fatal(err)
52 }
53
54 styles := part(t, data, "word/styles.xml")
55 if !strings.Contains(styles, `<w:rPrDefault><w:rPr><w:rFonts w:ascii="Calibri"`) {
56 t.Errorf("the document default is not Calibri: %s", styles)
57 }
58 body := part(t, data, "word/document.xml")
59 if strings.Contains(body, "Carlito") || strings.Contains(body, "Caladea") {
60 t.Error("the open-source names leaked into the Word file; Word would substitute them")
61 }
62 if !strings.Contains(body, `<w:rFonts w:ascii="Cambria"`) {
63 t.Error("the Caladea run is not written as Cambria")
64 }
65 if !strings.Contains(body, `<w:rFonts w:ascii="Inter"`) || !strings.Contains(body, `<w:rFonts w:ascii="Arial"`) {
66 t.Error("Inter is named as itself and Liberation Sans as Arial")
67 }
68 table := part(t, data, "word/fontTable.xml")
69 for _, name := range []string{"Calibri", "Cambria", "Inter", "Arial"} {
70 if !strings.Contains(table, `<w:font w:name="`+name+`">`) {
71 t.Errorf("fontTable.xml does not name %s", name)
72 }
73 }
74 if !strings.Contains(table, `<w:font w:name="Inter"><w:altName w:val="Arial"/>`) {
75 t.Error("Inter does not tell Word what to fall back to")
76 }
77
78 res, err := docx.Import(data)
79 if err != nil {
80 t.Fatal(err)
81 }
82 back := res.Document
83 if len(res.Report.Items) > 0 {
84 t.Errorf("a document Docs wrote imports with losses: %+v", res.Report.Items)
85 }
86 runs := back.Root.Content[1].Content
87 want := []struct {
88 text string
89 marks string
90 }{
91 {"Body text, ", ""},
92 {"bold", "strong"},
93 {" and a Cambria twin", "font_caladea em"},
94 {" and Inter at 14", "font_inter size_14"},
95 {" and Arial's twin", "font_sans"},
96 }
97 if back.Root.Content[0].Type != "heading" || back.Root.Content[0].Attrs.Level != 2 {
98 t.Errorf("the heading came back as %+v", back.Root.Content[0])
99 }
100 if len(runs) != len(want) {
101 t.Fatalf("got %d runs back, want %d: %+v", len(runs), len(want), runs)
102 }
103 for i, w := range want {
104 var marks []string
105 for _, m := range runs[i].Marks {
106 marks = append(marks, m.Type)
107 }
108 if runs[i].Text != w.text || strings.Join(marks, " ") != w.marks {
109 t.Errorf("run %d came back %q %v, want %q [%s]", i, runs[i].Text, marks, w.text, w.marks)
110 }
111 }
112}
113
114// A Word document whose own default is Calibri, written by Word, is a
115// Carlito document here; one whose default is Cambria is Caladea
116// throughout; and a face Docs does not have is reported for the import
117// report rather than silently replaced.
118func TestImportingWordsDefaults(t *testing.T) {
119 for _, c := range []struct {
120 defaultFace, runFace string
121 marks string
122 unknown bool
123 }{
124 {"Calibri", "", "", false},
125 {"Cambria", "", "font_caladea", false},
126 {"Calibri", "Cambria", "font_caladea", false},
127 {"Cambria", "Calibri", "", false},
128 {"Calibri", "Wingdings", "", true},
129 } {
130 data := wordFile(t, c.defaultFace, c.runFace)
131 res, err := docx.Import(data)
132 if err != nil {
133 t.Fatal(err)
134 }
135 doc, unknown := res.Document, substituted(res.Report)
136 var marks []string
137 for _, m := range doc.Root.Content[0].Content[0].Marks {
138 marks = append(marks, m.Type)
139 }
140 if strings.Join(marks, " ") != c.marks {
141 t.Errorf("default %s, run %q: marks %v, want [%s]", c.defaultFace, c.runFace, marks, c.marks)
142 }
143 if unknown != c.unknown {
144 t.Errorf("default %s, run %q: substitution reported %v, want %v", c.defaultFace, c.runFace, unknown, c.unknown)
145 }
146 }
147}
148
149// wordFile is a minimal .docx the way Word writes one: the default in
150// styles.xml, an optional run override.
151func wordFile(t *testing.T, defaultFace, runFace string) []byte {
152 t.Helper()
153 var buf bytes.Buffer
154 z := zip.NewWriter(&buf)
155 w, _ := z.Create("word/styles.xml")
156 io.WriteString(w, `<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:docDefaults><w:rPrDefault><w:rPr><w:rFonts w:ascii="`+defaultFace+`" w:hAnsi="`+defaultFace+`"/></w:rPr></w:rPrDefault></w:docDefaults></w:styles>`)
157 rpr := ""
158 if runFace != "" {
159 rpr = `<w:rPr><w:rFonts w:ascii="` + runFace + `" w:hAnsi="` + runFace + `"/></w:rPr>`
160 }
161 w, _ = z.Create("word/document.xml")
162 io.WriteString(w, `<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p><w:r>`+rpr+`<w:t>Hello</w:t></w:r></w:p></w:body></w:document>`)
163 z.Close()
164 return buf.Bytes()
165}
166
167// substituted is whether the report says a face was substituted.
168func substituted(r docx.Report) bool {
169 for _, it := range r.Items {
170 if it.Code == docx.CodeFontSubstituted {
171 return true
172 }
173 }
174 return false
175}
176