test_core_json.odin 1.6 KB

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