Shopify / ruby-lsp

An opinionated language server for Ruby
https://shopify.github.io/ruby-lsp/
MIT License
1.5k stars 140 forks source link

Create test framework for grammar rules #2249

Closed vinistock closed 1 week ago

vinistock commented 2 months ago

Currently, there are no tests for our grammar files. Not having tests is always bad, but it's especially worse for this case since the Ruby grammar file is immense and filled with complex regexes that try to understand Ruby's ambiguous syntax.

Let's try to design a little test framework that allows us to assert the token types for a string of Ruby code. The framework should apply the grammar rules on the given source string and then somehow allow us to assert the tokens based on positions.

Something like this:

test("Class declaration tokens", () => {
  const tokenTypes = applyGrammar("class Foo < Bar; end");
  assert.strictEqual(tokenTypes[0].type, "keyword");
  assert.strictEqual(tokenTypes[1].type, "class.declaration");
});
borama commented 1 month ago

FWIW, I like the https://github.com/PanAeon/vscode-tmgrammar-test approach and I've had great success with building a test suite for my VS Code Slim extension using it. I may even try to start building some tests if there is interest.

andyw8 commented 1 week ago

I hadn't realised the extension already has something for testing grammar rules:

https://github.com/Shopify/ruby-lsp/pull/2493

vinistock commented 1 week ago

Indeed, I forgot we already have a setup for testing grammar rules. I'll close this issue then.