sudoku-funpark/export/renderJSON_test.go

55 lines
1.8 KiB
Go

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)
}
}