test_parser.odin 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package test_core_odin_parser
  2. import "base:runtime"
  3. import "core:fmt"
  4. import "core:log"
  5. import "core:odin/ast"
  6. import "core:odin/parser"
  7. import "core:odin/tokenizer"
  8. import "core:testing"
  9. @test
  10. test_parse_demo :: proc(t: ^testing.T) {
  11. context.allocator = context.temp_allocator
  12. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  13. pkg, ok := parser.parse_package_from_path(ODIN_ROOT + "examples/demo")
  14. testing.expect(t, ok, "parser.parse_package_from_path failed")
  15. for key, value in pkg.files {
  16. testing.expectf(t, value.syntax_error_count == 0, "%v should contain zero errors", key)
  17. }
  18. }
  19. @test
  20. test_parse_bitfield :: proc(t: ^testing.T) {
  21. context.allocator = context.temp_allocator
  22. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  23. file := ast.File{
  24. fullpath = "test.odin",
  25. src = `
  26. package main
  27. Foo :: bit_field uint {}
  28. Foo :: bit_field uint {hello: bool | 1}
  29. Foo :: bit_field uint {
  30. hello: bool | 1 ` + "`fmt:\"-\"`" + `,
  31. hello: bool | 5,
  32. }
  33. // Hellope 1.
  34. Foo :: bit_field uint {
  35. // Hellope 2.
  36. hello: bool | 1,
  37. hello: bool | 5, // Hellope 3.
  38. }
  39. `,
  40. }
  41. p := parser.default_parser()
  42. p.err = proc(pos: tokenizer.Pos, format: string, args: ..any) {
  43. message := fmt.tprintf(format, ..args)
  44. log.errorf("%s(%d:%d): %s", pos.file, pos.line, pos.column, message)
  45. }
  46. p.warn = proc(pos: tokenizer.Pos, format: string, args: ..any) {
  47. message := fmt.tprintf(format, ..args)
  48. log.warnf("%s(%d:%d): %s", pos.file, pos.line, pos.column, message)
  49. }
  50. ok := parser.parse_file(&p, &file)
  51. testing.expect(t, ok, "bad parse")
  52. }
  53. @test
  54. test_parse_parser :: proc(t: ^testing.T) {
  55. context.allocator = context.temp_allocator
  56. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  57. pkg, ok := parser.parse_package_from_path(ODIN_ROOT + "core/odin/parser")
  58. testing.expect(t, ok, "parser.parse_package_from_path failed")
  59. for key, value in pkg.files {
  60. testing.expectf(t, value.syntax_error_count == 0, "%v should contain zero errors", key)
  61. }
  62. }
  63. @test
  64. test_parse_stb_image :: proc(t: ^testing.T) {
  65. context.allocator = context.temp_allocator
  66. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  67. pkg, ok := parser.parse_package_from_path(ODIN_ROOT + "vendor/stb/image")
  68. testing.expect(t, ok, "parser.parse_package_from_path failed")
  69. for key, value in pkg.files {
  70. testing.expectf(t, value.syntax_error_count == 0, "%v should contain zero errors", key)
  71. }
  72. }