test_core_json.odin 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package test_core_json
  2. import "core:encoding/json"
  3. import "core:testing"
  4. import "core:fmt"
  5. import "core:os"
  6. TEST_count := 0
  7. TEST_fail := 0
  8. when ODIN_TEST {
  9. expect :: testing.expect
  10. log :: testing.log
  11. } else {
  12. expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) {
  13. fmt.printf("[%v] ", loc)
  14. TEST_count += 1
  15. if !condition {
  16. TEST_fail += 1
  17. fmt.println(message)
  18. return
  19. }
  20. fmt.println(" PASS")
  21. }
  22. log :: proc(t: ^testing.T, v: any, loc := #caller_location) {
  23. fmt.printf("[%v] ", loc)
  24. fmt.printf("log: %v\n", v)
  25. }
  26. }
  27. main :: proc() {
  28. t := testing.T{}
  29. parse_json(&t)
  30. marshal_json(&t)
  31. fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
  32. if TEST_fail > 0 {
  33. os.exit(1)
  34. }
  35. }
  36. @test
  37. parse_json :: proc(t: ^testing.T) {
  38. json_data := `
  39. {
  40. "firstName": "John",
  41. "lastName": "Smith",
  42. "isAlive": true,
  43. "age": 27,
  44. "address": {
  45. "streetAddress": "21 2nd Street",
  46. "city": "New York",
  47. "state": "NY",
  48. "postalCode": "10021-3100"
  49. },
  50. "phoneNumbers": [
  51. {
  52. "type": "home",
  53. "number": "212 555-1234"
  54. },
  55. {
  56. "type": "office",
  57. "number": "646 555-4567"
  58. }
  59. ],
  60. "children": [],
  61. "spouse": null
  62. }
  63. `
  64. _, err := json.parse(transmute([]u8)json_data)
  65. expect(t, err == .None, "expected json error to be none")
  66. }
  67. @test
  68. marshal_json :: proc(t: ^testing.T) {
  69. My_Struct :: struct {
  70. a: int,
  71. b: int,
  72. }
  73. my_struct := My_Struct {
  74. a = 2,
  75. b = 5,
  76. }
  77. _, err := json.marshal(my_struct)
  78. expect(t, err == .None, "expected json error to be none")
  79. }