github.com/loadoff/excl@v0.0.0-20171207172601-c6a9e4c4b4c4/shared_strings_test.go (about) 1 package excl 2 3 import ( 4 "bytes" 5 "encoding/xml" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "testing" 10 ) 11 12 func TestOpenSharedStrings(t *testing.T) { 13 os.Mkdir("temp/xl", 0755) 14 defer os.RemoveAll("temp/xl") 15 _, err := OpenSharedStrings("nopath") 16 if err == nil { 17 t.Error("sharedStrings.xml file should not be opened.") 18 } 19 ss, err := OpenSharedStrings("temp") 20 if ss == nil { 21 t.Error("structure should be created but [", err.Error(), "]") 22 } else { 23 if !isFileExist(filepath.Join("temp", "xl", "sharedStrings.xml")) { 24 t.Error("sharedStrings.xml file should be created.") 25 } 26 if ss.count != 0 { 27 t.Error("count should be 0 but [", ss.count, "]") 28 } 29 if !isFileExist(filepath.Join("temp", "xl", "__sharedStrings.xml")) { 30 t.Error("__sharedStrings.xml should be opened.") 31 } 32 ss.Close() 33 if isFileExist(filepath.Join("temp", "xl", "__sharedStrings.xml")) { 34 t.Error("__sharedStrings.xml should be removed.") 35 } 36 } 37 38 f, _ := os.Create(filepath.Join("temp", "xl", "sharedStrings.xml")) 39 f.Close() 40 _, err = OpenSharedStrings("temp") 41 if err == nil { 42 t.Error("sharedStrings.xml should not be parsed.") 43 } 44 45 f, _ = os.Create(filepath.Join("temp", "xl", "sharedStrings.xml")) 46 f.WriteString("<currupt></currupt>") 47 f.Close() 48 _, err = OpenSharedStrings("temp") 49 if err == nil { 50 t.Error("sharedStrings.xml file should be currupt.") 51 } 52 53 f, _ = os.Create(filepath.Join("temp", "xl", "sharedStrings.xml")) 54 f.WriteString("<sst><si><t></t></si><si><t></t></si></sst>") 55 f.Close() 56 ss, err = OpenSharedStrings("temp") 57 if err != nil { 58 t.Error("sharedStrings.xml should be opend.[", err.Error(), "]") 59 } 60 if ss.count != 2 { 61 t.Error("strings count should be 2 but [", ss.count, "]") 62 } 63 ss.Close() 64 f, _ = os.Create(filepath.Join("temp", "xl", "sharedStrings.xml")) 65 f.WriteString("<sst><separate_tag></separate_tag></sst>") 66 f.Close() 67 ss, err = OpenSharedStrings("temp") 68 if err == nil { 69 t.Error("sharedString.xml should not be opened because the file is currupt.") 70 } 71 } 72 73 func TestAddString(t *testing.T) { 74 os.Mkdir("temp/xl", 0755) 75 defer os.RemoveAll("temp/xl") 76 f, _ := os.Create(filepath.Join("temp", "xl", "sharedStrings.xml")) 77 f.WriteString("<sst><si></si></sst>") 78 f.Close() 79 ss, _ := OpenSharedStrings("temp") 80 if index := ss.AddString("hello world"); index != 1 { 81 t.Error("index should be 1 but [", index, "]") 82 } 83 ss.Close() 84 f, _ = os.Open(filepath.Join("temp", "xl", "sharedStrings.xml")) 85 b, _ := ioutil.ReadAll(f) 86 f.Close() 87 if string(b) != "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<sst><si></si><si><t>hello world</t></si></sst>" { 88 t.Error(string(b)) 89 } 90 } 91 92 func TestEscapeText(t *testing.T) { 93 buf := new(bytes.Buffer) 94 escapeText(buf, []byte("\"'&<>\t\n\rあいう")) 95 if string(buf.Bytes()) != ""'&<>	

あいう" { 96 t.Error("string should be "'&<>	

あいう but ", string(buf.Bytes())) 97 } 98 } 99 100 func BenchmarkAddString(b *testing.B) { 101 s := "あいうえお" 102 f, _ := os.Create("temp/sharedStrings.xml") 103 //defer f.Close() 104 //sharedStrings := &SharedStrings{tempFile: f} 105 for i := 1; i < 100000; i++ { 106 for j := 0; j < 20; j++ { 107 //sharedStrings.AddString("あいうえお") 108 //var b bytes.Buffer 109 f.WriteString("<si><t>") 110 xml.EscapeText(f, []byte(s)) 111 f.WriteString("</t></si>") 112 113 } 114 } 115 }