From bff41eef376445a88bb865f60896abd2549eae5c Mon Sep 17 00:00:00 2001 From: Martin Evans Date: Fri, 13 Oct 2023 01:36:48 +0100 Subject: [PATCH] Added some more coverage of `GrammarRule`, checking that invalid rules are rejected --- LLama.Unittest/GrammarParserTest.cs | 87 ++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/LLama.Unittest/GrammarParserTest.cs b/LLama.Unittest/GrammarParserTest.cs index 6d3adb82..40046732 100644 --- a/LLama.Unittest/GrammarParserTest.cs +++ b/LLama.Unittest/GrammarParserTest.cs @@ -1,4 +1,5 @@ -using LLama.Native; +using LLama.Exceptions; +using LLama.Native; using LLama.Grammars; namespace LLama.Unittest @@ -237,5 +238,89 @@ namespace LLama.Unittest } Assert.NotEmpty(state.Rules); } + + [Fact] + public void InvalidRuleNoElements() + { + Assert.Throws(() => + { + // ReSharper disable once ObjectCreationAsStatement + new GrammarRule("name", Array.Empty()); + }); + } + + [Fact] + public void InvalidRuleNoEndElement() + { + Assert.Throws(() => + { + // ReSharper disable once ObjectCreationAsStatement + new GrammarRule("name", new[] + { + new LLamaGrammarElement(LLamaGrammarElementType.ALT, 0) + }); + }); + } + + [Fact] + public void InvalidRuleExtraEndElement() + { + Assert.Throws(() => + { + // ReSharper disable once ObjectCreationAsStatement + new GrammarRule("name", new[] + { + new LLamaGrammarElement(LLamaGrammarElementType.END, 0), + new LLamaGrammarElement(LLamaGrammarElementType.ALT, 0), + new LLamaGrammarElement(LLamaGrammarElementType.END, 0) + }); + }); + } + + [Fact] + public void InvalidRuleMalformedRange() + { + Assert.Throws(() => + { + // ReSharper disable once ObjectCreationAsStatement + new GrammarRule("name", new[] + { + new LLamaGrammarElement(LLamaGrammarElementType.ALT, 0), + new LLamaGrammarElement(LLamaGrammarElementType.CHAR_RNG_UPPER, 0), + new LLamaGrammarElement(LLamaGrammarElementType.END, 0) + }); + }); + + + } + + [Fact] + public void InvalidRuleMalformedCharAlt() + { + Assert.Throws(() => + { + // ReSharper disable once ObjectCreationAsStatement + new GrammarRule("name", new[] + { + new LLamaGrammarElement(LLamaGrammarElementType.RULE_REF, 0), + new LLamaGrammarElement(LLamaGrammarElementType.CHAR_ALT, 0), + new LLamaGrammarElement(LLamaGrammarElementType.END, 0) + }); + }); + } + + [Fact] + public void InvalidRuleElement() + { + Assert.Throws(() => + { + // ReSharper disable once ObjectCreationAsStatement + new GrammarRule("name", new[] + { + new LLamaGrammarElement((LLamaGrammarElementType)99999, 0), + new LLamaGrammarElement(LLamaGrammarElementType.END, 0) + }); + }); + } } }