z_mirror/luapls
git clone https://git.raiguard.me/z_mirror/luapls
442023c
main
1package parser 2 3import ( 4"encoding/json" 5"os" 6"path/filepath" 7"strings" 8"testing" 9 10"github.com/stretchr/testify/assert" 11"github.com/stretchr/testify/require" 12) 13 14type TestSpec struct { 15Label string 16Input string `json:"input"` 17AST json.RawMessage 18Errors json.RawMessage `json:",omitempty"` 19} 20 21func TestParser (t * testing.T ) { 22dir ,err := os .ReadDir ("test_specs" ) 23require .NoError (t ,err ) 24for _ ,entry := range dir { 25bytes ,err := os .ReadFile (filepath .Join ("test_specs" ,entry .Name ())) 26var specs []TestSpec 27err = json .Unmarshal (bytes ,& specs ) 28if ! assert .NoError (t ,err ) { 29continue 30 } 31for _ ,spec := range specs { 32specLabel := strings .TrimSuffix (entry .Name (),".json" )+ "/" + spec .Label 33t .Run (specLabel ,func (t * testing.T ) {testSpec (t ,& spec ) }) 34 } 35 } 36} 37 38func testSpec (t * testing.T ,spec * TestSpec ) { 39p := New (spec .Input ) 40file := p .ParseFile () 41ast ,err := json .Marshal (& file .Block ) 42if ! assert .NoError (t ,err ) { 43return 44 } 45assert .JSONEq (t ,string (spec .AST ),string (ast )) 46 47if len (spec .Errors )== 0 { 48assert .Empty (t ,p .Errors ()) 49return 50 } 51 52errors ,err := json .Marshal (& file .Diagnostics ) 53if ! assert .NoError (t ,err ) { 54return 55 } 56assert .JSONEq (t ,string (spec .Errors ),string (errors )) 57}