renderJSON_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package export
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "gitea.ligthert.net/golang/sudoku-funpark/controller"
  6. "gitea.ligthert.net/golang/sudoku-funpark/outputter"
  7. )
  8. // This function will test the renderJSON function.
  9. // Starts off by creating a new Export struct.
  10. // Creates a new Controller struct.
  11. // Adds a solution to the Controller struct.
  12. // Then it will execute the renderJSON function.
  13. // Check if the output is a JSON string.
  14. func TestRenderJSON(t *testing.T) {
  15. // Create a new Export struct.
  16. export := Export{}
  17. outp := outputter.Outputter{}
  18. outp.OutputType = "short"
  19. export.Controller = &controller.Controller{Outputter: &outp}
  20. // Create a new Controller struct.
  21. controller := controller.Controller{}
  22. controller.Solutions = append(controller.Solutions, []string{"123456789", "987654321", "123456789", "987654321", "123456789", "987654321", "123456789", "987654321", "123456789"})
  23. // Add a solution to the Controller struct.
  24. export.Controller = &controller
  25. // Execute the renderJSON function.
  26. render := export.renderJSON()
  27. // Check if the output is a JSON string and not empty.
  28. if render == "" {
  29. t.Error("Expected a non-empty JSON string, got", render)
  30. }
  31. // Check if the output is a JSON string.\
  32. if render != "[{\"order\":0,\"row1\":\"123456789\",\"row2\":\"987654321\",\"row3\":\"123456789\",\"row4\":\"987654321\",\"row5\":\"123456789\",\"row6\":\"987654321\",\"row7\":\"123456789\",\"row8\":\"987654321\",\"row9\":\"123456789\"}]" {
  33. t.Error("Expected a JSON string, got", render)
  34. }
  35. // Check if the outpt is a valid JSON string.
  36. // Check using the json.Unmarshal function.
  37. // If the output is not a valid JSON string, the Unmarshal function will throw an error.
  38. var result []map[string]interface{}
  39. err := json.Unmarshal([]byte(render), &result)
  40. if err != nil {
  41. t.Error("Expected a valid JSON string, got", render)
  42. }
  43. }