paperformatter/example_test.go

167 lines
3.5 KiB
Go
Raw Normal View History

2025-01-26 04:26:30 +00:00
package paperformatter_test
import (
"fmt"
"log"
"os"
"gitea.r8z.us/stwhite/paperformatter"
)
func ExampleFormatPapers() {
// Create a sample input file
input := `[
{
"paper": {
"title": "Example Research Paper",
"abstract": "This is a sample abstract for the example paper.",
"arxiv_id": "2301.0001"
},
"decision": "accept",
"explanation": "Well-written paper with novel contributions."
}
]`
inputFile := "example_input.json"
outputFile := "example_output.md"
// Write sample input
if err := os.WriteFile(inputFile, []byte(input), 0644); err != nil {
log.Fatal(err)
}
defer os.Remove(inputFile)
defer os.Remove(outputFile)
// Format the papers
if err := paperformatter.FormatPapers(inputFile, outputFile); err != nil {
log.Fatal(err)
}
// Read and print the output
output, err := os.ReadFile(outputFile)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(output))
// Output:
// # Accepted Papers
//
// ## Example Research Paper
//
// [arXiv:2301.0001](https://arxiv.org/abs/2301.0001)
//
// > This is a sample abstract for the example paper.
//
// **Decision:** accept
//
// **Explanation:** Well-written paper with novel contributions.
//
// # Rejected Papers
}
func ExampleFormatPapers_multipleEntries() {
// Create a sample input file with multiple entries
input := `[
{
"paper": {
"title": "First Paper",
"abstract": "Abstract for the first paper.",
"arxiv_id": "2301.0001"
},
"decision": "accept",
"explanation": "Good contribution."
},
{
"paper": {
"title": "Second Paper",
"abstract": "Abstract for the second paper.",
"arxiv_id": "2301.0002"
},
"decision": "reject",
"explanation": "Needs more work."
}
]`
inputFile := "example_multi_input.json"
outputFile := "example_multi_output.md"
if err := os.WriteFile(inputFile, []byte(input), 0644); err != nil {
log.Fatal(err)
}
defer os.Remove(inputFile)
defer os.Remove(outputFile)
if err := paperformatter.FormatPapers(inputFile, outputFile); err != nil {
log.Fatal(err)
}
output, err := os.ReadFile(outputFile)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(output))
// Output:
// # Accepted Papers
//
// ## First Paper
//
// [arXiv:2301.0001](https://arxiv.org/abs/2301.0001)
//
// > Abstract for the first paper.
//
// **Decision:** accept
//
// **Explanation:** Good contribution.
//
// # Rejected Papers
//
// ## Second Paper
//
// [arXiv:2301.0002](https://arxiv.org/abs/2301.0002)
//
// > Abstract for the second paper.
//
// **Decision:** reject
//
// **Explanation:** Needs more work.
}
func ExampleFormatPapers_errorHandling() {
// Example of handling different error types
input := `[
{
"paper": {
"title": "",
"abstract": "Abstract",
"arxiv_id": "2301.0001"
},
"decision": "accept",
"explanation": "Good paper"
}
]`
inputFile := "example_error_input.json"
outputFile := "example_error_output.md"
if err := os.WriteFile(inputFile, []byte(input), 0644); err != nil {
log.Fatal(err)
}
defer os.Remove(inputFile)
defer os.Remove(outputFile)
err := paperformatter.FormatPapers(inputFile, outputFile)
if err != nil {
switch e := err.(type) {
case *paperformatter.ValidationError:
fmt.Printf("Validation error: %v\n", e)
case *paperformatter.FileError:
fmt.Printf("File error: %v\n", e)
case *paperformatter.JSONError:
fmt.Printf("JSON error: %v\n", e)
default:
fmt.Printf("Error: %v\n", err)
}
}
// Output: Error: entry 1: validation error for title: title cannot be empty
}