Provided tests for functions that can be tested (Closes #20).

This commit is contained in:
2025-01-29 17:45:08 +01:00
parent cd54fdf345
commit c1f2a28ac1
14 changed files with 676 additions and 1 deletions

90
export/Export_test.go Normal file
View File

@ -0,0 +1,90 @@
package export
import (
"testing"
"gitea.ligthert.net/golang/sudoku-funpark/controller"
"gitea.ligthert.net/golang/sudoku-funpark/outputter"
)
// This function will test the Export function.
// Starts off by creating all the structs and intefaces needed.
// Set all the needed values.
// Execute the Export function.
// Check if the output is a string.
// Check if the output for "human" "flat" and "json" are valid.
func TestExport(t *testing.T) {
// Create a new Export struct.
export := Export{}
// Create a new outputter struct.
outp := outputter.Outputter{}
// Set output type to "short".
outp.OutputType = "short"
// Create a new Controller struct and set the outputter.
controller := controller.Controller{Outputter: &outp}
// Set the Controller in the Export struct.
export.Controller = &controller
// Populate the Solutions slice.
controller.Solutions = append(controller.Solutions, []string{"123456789", "987654321", "123456789", "987654321", "123456789", "987654321", "123456789", "987654321", "123456789"})
// Set output type to "human".
controller.Output = "human"
// Execute the Export function.
render := export.Export()
// Check if the output is a string and not empty.
if render == "" {
t.Error("Expected a non-empty string, string was empty")
}
// Set the expected variable.
expected := "\nSolution #1:\n╔═══════════╗\n║123│456│789╢\n║987│654│321╢\n║123│456│789╢\n╟───┼───┼───╢\n║987│654│321╢\n║123│456│789╢\n║987│654│321╢\n╟───┼───┼───╢\n║123│456│789╢\n║987│654│321╢\n║123│456│789╢\n╚═══════════╝\n"
// Check if the output is the same as the expected variable.
if render != expected {
t.Error("Expected a ", expected, ", got", render)
}
// Set output type to "flat".
controller.Output = "flat"
// Execute the Export function.
render = export.Export()
// Check if the output for "flat" is non-empty.
if render == "" {
t.Error("Expected a non-empty string, string was empty")
}
expected = "\nSolution #1:\n[123456789 987654321 123456789 987654321 123456789 987654321 123456789 987654321 123456789]\n"
if render != expected {
t.Error("Expected a ", expected, ", got", render)
}
// Set output type to "json".
controller.Output = "json"
// Execute the Export function.
render = export.Export()
// Check if the output for "json" is valid.
if render == "" {
t.Error("Expected a non-empty string, got empty string")
}
// Set the expected variable.
expected = "[{\"order\":0,\"row1\":\"123456789\",\"row2\":\"987654321\",\"row3\":\"123456789\",\"row4\":\"987654321\",\"row5\":\"123456789\",\"row6\":\"987654321\",\"row7\":\"123456789\",\"row8\":\"987654321\",\"row9\":\"123456789\"}]"
// Check if what is rendered is the same as the expected variable.
if render != expected {
t.Error("Expected a ", expected, ", got", render)
}
}

39
export/renderFlat_test.go Normal file
View File

@ -0,0 +1,39 @@
package export
import (
"testing"
"gitea.ligthert.net/golang/sudoku-funpark/controller"
"gitea.ligthert.net/golang/sudoku-funpark/outputter"
)
// This function will test the renderFlat function.
// Then it will execute the renderFlat function.
// Check if the output is a string.
func TestRenderFlat(t *testing.T) {
// Create a new Export struct.
export := Export{}
// Create a new Controller struct.
// Set output type to "human".
outp := outputter.Outputter{}
outp.OutputType = "short"
controller := controller.Controller{Outputter: &outp}
export.Controller = &controller
controller.Solutions = append(controller.Solutions, []string{"123456789", "987654321", "123456789", "987654321", "123456789", "987654321", "123456789", "987654321", "123456789"})
// Execute the renderFlat function.
render := export.renderFlat()
// Check if the output is a string and not empty.
if render == "" {
t.Error("Expected a non-empty string, got", render)
}
// Check if the output is a string.
if render != "\nSolution #1:\n[123456789 987654321 123456789 987654321 123456789 987654321 123456789 987654321 123456789]\n" {
t.Error("Expected a string, got", render)
}
}

View File

@ -0,0 +1,45 @@
package export
import (
"strings"
"testing"
"gitea.ligthert.net/golang/sudoku-funpark/controller"
"gitea.ligthert.net/golang/sudoku-funpark/outputter"
)
// This function will test the renderHumanReadable function.
// Starts off by creating a new Export struct.
// Creates a new Controller struct.
// Adds a solution to the Controller struct.
// Then it will execute the renderHumanReadable function.
// Check if the output is a string.
func TestRenderHumanReadable(t *testing.T) {
// Create a new Export struct.
export := Export{}
// Create a new Controller struct.
// Set output type to "human".
outp := outputter.Outputter{}
outp.OutputType = "short"
controller := controller.Controller{Outputter: &outp}
export.Controller = &controller
controller.Solutions = append(controller.Solutions, []string{"123456789", "987654321", "123456789", "987654321", "123456789", "987654321", "123456789", "987654321", "123456789"})
// Execute the renderHumanReadable function.
render := export.renderHumanReadable()
expected := "\nSolution #1:\n╔═══════════╗\n║123│456│789╢\n║987│654│321╢\n║123│456│789╢\n╟───┼───┼───╢\n║987│654│321╢\n║123│456│789╢\n║987│654│321╢\n╟───┼───┼───╢\n║123│456│789╢\n║987│654│321╢\n║123│456│789╢\n╚═══════════╝\n"
// Check if the output is a string and not empty.
if render == "" {
t.Error("Expected a non-empty string, got", render)
}
// Check if the output is a string.
if strings.TrimSpace(render) != strings.TrimSpace(expected) {
t.Error("Expected a string, got", render)
}
}

54
export/renderJSON_test.go Normal file
View File

@ -0,0 +1,54 @@
package export
import (
"encoding/json"
"testing"
"gitea.ligthert.net/golang/sudoku-funpark/controller"
"gitea.ligthert.net/golang/sudoku-funpark/outputter"
)
// This function will test the renderJSON function.
// Starts off by creating a new Export struct.
// Creates a new Controller struct.
// Adds a solution to the Controller struct.
// Then it will execute the renderJSON function.
// Check if the output is a JSON string.
func TestRenderJSON(t *testing.T) {
// Create a new Export struct.
export := Export{}
outp := outputter.Outputter{}
outp.OutputType = "short"
export.Controller = &controller.Controller{Outputter: &outp}
// Create a new Controller struct.
controller := controller.Controller{}
controller.Solutions = append(controller.Solutions, []string{"123456789", "987654321", "123456789", "987654321", "123456789", "987654321", "123456789", "987654321", "123456789"})
// Add a solution to the Controller struct.
export.Controller = &controller
// Execute the renderJSON function.
render := export.renderJSON()
// Check if the output is a JSON string and not empty.
if render == "" {
t.Error("Expected a non-empty JSON string, got", render)
}
// Check if the output is a JSON string.\
if render != "[{\"order\":0,\"row1\":\"123456789\",\"row2\":\"987654321\",\"row3\":\"123456789\",\"row4\":\"987654321\",\"row5\":\"123456789\",\"row6\":\"987654321\",\"row7\":\"123456789\",\"row8\":\"987654321\",\"row9\":\"123456789\"}]" {
t.Error("Expected a JSON string, got", render)
}
// Check if the outpt is a valid JSON string.
// Check using the json.Unmarshal function.
// If the output is not a valid JSON string, the Unmarshal function will throw an error.
var result []map[string]interface{}
err := json.Unmarshal([]byte(render), &result)
if err != nil {
t.Error("Expected a valid JSON string, got", render)
}
}