From 46e4f483f6f34e259340d89373d28b7598cb5311 Mon Sep 17 00:00:00 2001 From: Daniel Fichtinger Date: Sun, 24 Nov 2024 17:46:36 -0500 Subject: [PATCH] created testing functions for conversion operations --- internal/convert/convert_test.go | 122 +++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 internal/convert/convert_test.go diff --git a/internal/convert/convert_test.go b/internal/convert/convert_test.go new file mode 100644 index 0000000..68c1134 --- /dev/null +++ b/internal/convert/convert_test.go @@ -0,0 +1,122 @@ +package convert_test + +import ( + "os" + "path/filepath" + "testing" + + "github.com/ficcdaf/zona/internal/convert" + "github.com/ficcdaf/zona/internal/util" +) + +func TestMdToHTML(t *testing.T) { + md := []byte("# Hello World\n\nThis is a test.") + expectedHTML := "

Hello World

\n

This is a test.

\n" + nExpectedHTML := util.NormalizeContent(expectedHTML) + html, err := convert.MdToHTML(md) + nHtml := util.NormalizeContent(string(html)) + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + if nHtml != nExpectedHTML { + t.Errorf("Expected:\n%s\nGot:\n%s", expectedHTML, html) + } +} + +func TestWriteFile(t *testing.T) { + path := filepath.Join(t.TempDir(), "test.txt") + content := []byte("Hello, World!") + + err := convert.WriteFile(content, path) + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + + // Verify file content + data, err := os.ReadFile(path) + if err != nil { + t.Fatalf("Error reading file: %v", err) + } + if string(data) != string(content) { + t.Errorf("Expected:\n%s\nGot:\n%s", content, data) + } +} + +func TestReadFile(t *testing.T) { + path := filepath.Join(t.TempDir(), "test.txt") + content := []byte("Hello, World!") + + err := os.WriteFile(path, content, 0644) + if err != nil { + t.Fatalf("Error writing file: %v", err) + } + + data, err := convert.ReadFile(path) + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + if string(data) != string(content) { + t.Errorf("Expected:\n%s\nGot:\n%s", content, data) + } +} + +func TestCopyFile(t *testing.T) { + src := filepath.Join(t.TempDir(), "source.txt") + dst := filepath.Join(t.TempDir(), "dest.txt") + content := []byte("File content for testing.") + + err := os.WriteFile(src, content, 0644) + if err != nil { + t.Fatalf("Error writing source file: %v", err) + } + + err = convert.CopyFile(src, dst) + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + + // Verify destination file content + data, err := os.ReadFile(dst) + if err != nil { + t.Fatalf("Error reading destination file: %v", err) + } + if string(data) != string(content) { + t.Errorf("Expected:\n%s\nGot:\n%s", content, data) + } +} + +func TestConvertFile(t *testing.T) { + src := filepath.Join(t.TempDir(), "test.md") + dst := filepath.Join(t.TempDir(), "test.html") + mdContent := []byte("# Test Title\n\nThis is Markdown content.") + nExpectedHTML := util.NormalizeContent("

Test Title

\n

This is Markdown content.

\n") + + err := os.WriteFile(src, mdContent, 0644) + if err != nil { + t.Fatalf("Error writing source Markdown file: %v", err) + } + + err = convert.ConvertFile(src, dst) + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + + // Verify destination HTML content + data, err := os.ReadFile(dst) + if err != nil { + t.Fatalf("Error reading HTML file: %v", err) + } + if util.NormalizeContent(string(data)) != nExpectedHTML { + t.Errorf("Expected:\n%s\nGot:\n%s", nExpectedHTML, data) + } +} + +func TestChangeExtension(t *testing.T) { + input := "test.md" + output := convert.ChangeExtension(input, ".html") + expected := "test.html" + + if output != expected { + t.Errorf("Expected %s, got %s", expected, output) + } +}