From 33abd9ea7a170e1f3d63ce5b22368cea300a0fb3 Mon Sep 17 00:00:00 2001 From: Enrico Speranza Date: Mon, 4 Sep 2023 14:30:36 +0000 Subject: [PATCH] Add FileXXXParser --- opencrucible.go | 31 +++++++++++++++++++-- opencrucible_test.go | 64 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 79 insertions(+), 16 deletions(-) diff --git a/opencrucible.go b/opencrucible.go index 5fa4018..8ada5fb 100644 --- a/opencrucible.go +++ b/opencrucible.go @@ -45,6 +45,15 @@ func TXTParseToString(StreamToParse []byte) (string, error) { return txt, err } +func TXTFileParserToString(FileToParse string) (string, error) { + txt, err := os.ReadFile(FileToParse) + if err != nil { + return "", fmt.Errorf("error opening file: %s", err) + } + got, err := TXTParseToString(txt) + return got, err +} + func RTFParseToString(StreamToParse []byte) (string, error) { // If no StreamToDetect was given, return an error with a message. if len(StreamToParse) == 0 { @@ -54,8 +63,17 @@ func RTFParseToString(StreamToParse []byte) (string, error) { return rtf, err } +func RTFFileParserToString(FileToParse string) (string, error) { + rtf, err := os.ReadFile(FileToParse) + if err != nil { + return "", fmt.Errorf("error opening file: %s", err) + } + got, err := RTFParseToString(rtf) + return got, err +} + func DOCXFileParseToString(FileToParse string) (string, error) { - // extract text from an XLSX file + // extract text from an DOCX file file, err := os.Open(FileToParse) if err != nil { return "", fmt.Errorf("error opening file: %s", err) @@ -74,4 +92,13 @@ func ODTParseToString(StreamToParse []byte) (string, error) { } odt, err := cat.FromBytes(StreamToParse) return odt, err -} \ No newline at end of file +} + +func ODTFileParserToString(FileToParse string) (string, error) { + odt, err := os.ReadFile(FileToParse) + if err != nil { + return "", fmt.Errorf("error opening file: %s", err) + } + got, err := ODTParseToString(odt) + return got, err +} diff --git a/opencrucible_test.go b/opencrucible_test.go index 895a771..e37ccae 100644 --- a/opencrucible_test.go +++ b/opencrucible_test.go @@ -5,14 +5,14 @@ import ( "testing" ) -func TestTXTParse(t *testing.T) { +func TestTXTParser(t *testing.T) { txt, err := os.ReadFile("test_file/test_file_txt.txt") if err != nil { - panic(err) + t.Errorf("error loading file \n %s", err) } got, err := TXTParseToString(txt) if err != nil { - panic(err) + t.Errorf("unable to parse file \n %s", err) } want := "This is a test file to test library" t.Logf("Parsed: %s", got) @@ -21,14 +21,38 @@ func TestTXTParse(t *testing.T) { } } -func TestODTarse(t *testing.T) { +func TestTXTFileParser(t *testing.T) { + got, err := TXTFileParserToString("test_file/test_file_txt.txt") + if err != nil { + t.Errorf("error loading file \n %s", err) + } + want := "This is a test file to test library" + t.Logf("Parsed: %s", got) + if got != want { + t.Errorf("got %q, wanted %q", got, want) + } +} + +func TestODTParser(t *testing.T) { odt, err := os.ReadFile("test_file/test_file_odt.odt") if err != nil { - panic(err) + t.Errorf("error loading file \n %s", err) } got, err := ODTParseToString(odt) if err != nil { - panic(err) + t.Errorf("unable to parse file \n %s", err) + } + want := "This is a test for library\n" + t.Logf("Parsed: %s", got) + if got != want { + t.Errorf("got %q, wanted %q", got, want) + } +} + +func TestODTFileParser(t *testing.T) { + got, err := ODTFileParserToString("test_file/test_file_odt.odt") + if err != nil { + t.Errorf("error loading file \n %s", err) } want := "This is a test for library\n" t.Logf("Parsed: %s", got) @@ -37,14 +61,26 @@ func TestODTarse(t *testing.T) { } } -func TestRTFarse(t *testing.T) { +func TestRTFParser(t *testing.T) { rtf, err := os.ReadFile("test_file/test_file_rtf.rtf") if err != nil { - panic(err) + t.Errorf("error loading file \n %s", err) } got, err := RTFParseToString(rtf) if err != nil { - panic(err) + t.Errorf("unable to parse file \n %s", err) + } + want := "Normalheading 1heading 2heading 3heading 4heading 5heading 6TitleSubtitleThis is a test file to test library " + t.Logf("Parsed: %s", got) + if got != want { + t.Errorf("got %q, wanted %q", got, want) + } +} + +func TestRTFFileParser(t *testing.T) { + got, err := RTFFileParserToString("test_file/test_file_rtf.rtf") + if err != nil { + t.Errorf("error loading file \n %s", err) } want := "Normalheading 1heading 2heading 3heading 4heading 5heading 6TitleSubtitleThis is a test file to test library " t.Logf("Parsed: %s", got) @@ -53,10 +89,10 @@ func TestRTFarse(t *testing.T) { } } -func TestDOCXParse(t *testing.T) { +func TestDOCXFileParser(t *testing.T) { got, err := DOCXFileParseToString("test_file/test_file_docx.docx") if err != nil { - panic(err) + t.Errorf("error loading file \n %s", err) } want := "This is a test file to test library\n" t.Logf("Parsed: %s", got) @@ -65,14 +101,14 @@ func TestDOCXParse(t *testing.T) { } } -func TestDOCXMSParse(t *testing.T) { +func TestDOCXMSFileParser(t *testing.T) { got, err := DOCXFileParseToString("test_file/test_file_docx_ms.docx") if err != nil { - panic(err) + t.Errorf("error loading file \n %s", err) } want := "This is a test file to test library\n" t.Logf("Parsed: %s", got) if got != want { t.Errorf("got %q, wanted %q", got, want) } -} \ No newline at end of file +}