Export_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package export
  2. import (
  3. "testing"
  4. "gitea.ligthert.net/golang/sudoku-funpark/controller"
  5. "gitea.ligthert.net/golang/sudoku-funpark/outputter"
  6. )
  7. // This function will test the Export function.
  8. // Starts off by creating all the structs and intefaces needed.
  9. // Set all the needed values.
  10. // Execute the Export function.
  11. // Check if the output is a string.
  12. // Check if the output for "human" "flat" and "json" are valid.
  13. func TestExport(t *testing.T) {
  14. // Create a new Export struct.
  15. export := Export{}
  16. // Create a new outputter struct.
  17. outp := outputter.Outputter{}
  18. // Set output type to "short".
  19. outp.OutputType = "short"
  20. // Create a new Controller struct and set the outputter.
  21. controller := controller.Controller{Outputter: &outp}
  22. // Set the Controller in the Export struct.
  23. export.Controller = &controller
  24. // Populate the Solutions slice.
  25. controller.Solutions = append(controller.Solutions, []string{"123456789", "987654321", "123456789", "987654321", "123456789", "987654321", "123456789", "987654321", "123456789"})
  26. // Set output type to "human".
  27. controller.Output = "human"
  28. // Execute the Export function.
  29. render := export.Export()
  30. // Check if the output is a string and not empty.
  31. if render == "" {
  32. t.Error("Expected a non-empty string, string was empty")
  33. }
  34. // Set the expected variable.
  35. 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"
  36. // Check if the output is the same as the expected variable.
  37. if render != expected {
  38. t.Error("Expected a ", expected, ", got", render)
  39. }
  40. // Set output type to "flat".
  41. controller.Output = "flat"
  42. // Execute the Export function.
  43. render = export.Export()
  44. // Check if the output for "flat" is non-empty.
  45. if render == "" {
  46. t.Error("Expected a non-empty string, string was empty")
  47. }
  48. expected = "\nSolution #1:\n[123456789 987654321 123456789 987654321 123456789 987654321 123456789 987654321 123456789]\n"
  49. if render != expected {
  50. t.Error("Expected a ", expected, ", got", render)
  51. }
  52. // Set output type to "json".
  53. controller.Output = "json"
  54. // Execute the Export function.
  55. render = export.Export()
  56. // Check if the output for "json" is valid.
  57. if render == "" {
  58. t.Error("Expected a non-empty string, got empty string")
  59. }
  60. // Set the expected variable.
  61. expected = "[{\"order\":0,\"row1\":\"123456789\",\"row2\":\"987654321\",\"row3\":\"123456789\",\"row4\":\"987654321\",\"row5\":\"123456789\",\"row6\":\"987654321\",\"row7\":\"123456789\",\"row8\":\"987654321\",\"row9\":\"123456789\"}]"
  62. // Check if what is rendered is the same as the expected variable.
  63. if render != expected {
  64. t.Error("Expected a ", expected, ", got", render)
  65. }
  66. }