public void ParseNextConcept_SameKeyWordDifferentContext() { string dsl = "concept simple simpledata; concept ext extdata extdata2;"; var syntax = DslSyntaxHelper.CreateDslSyntax(typeof(SimpleConceptInfo), typeof(ExtendedConceptInfo)); var conceptParsersList = new List <IConceptParser>() { new GenericParserHelper <SimpleConceptInfo>(syntax, "concept"), new GenericParserHelper <ExtendedConceptInfo>(syntax, "concept") }; var conceptParsers = new MultiDictionary <string, IConceptParser>(); conceptParsers.Add("concept", conceptParsersList); var noContext = new Stack <IConceptInfo>(); TokenReader tokenReader = TestTokenReader(dsl); IConceptInfo concept = new TestDslParser(dsl, syntax).ParseNextConcept(tokenReader, noContext, conceptParsers); Assert.AreEqual(typeof(SimpleConceptInfo), concept.GetType()); Assert.AreEqual("simple", (concept as SimpleConceptInfo).Name); Assert.AreEqual("simpledata", (concept as SimpleConceptInfo).Data); Assert.IsTrue(tokenReader.TryRead(";"), "Reading ';' between concepts."); concept = new TestDslParser(dsl, syntax).ParseNextConcept(tokenReader, noContext, conceptParsers); Assert.AreEqual(typeof(ExtendedConceptInfo), concept.GetType()); Assert.AreEqual("ext", (concept as ExtendedConceptInfo).Name); Assert.AreEqual("extdata", (concept as ExtendedConceptInfo).Data); Assert.AreEqual("extdata2", (concept as ExtendedConceptInfo).Data2); Assert.IsTrue(tokenReader.TryRead(";"), "Reading ';' after the concept."); }
public void ParseNextConcept_SameKeyWordDifferentContext_Enclosed() { string dsl = "concept name data { concept ref; }"; var syntax = DslSyntaxHelper.CreateDslSyntax(typeof(SimpleConceptInfo), typeof(EnclosedRefConceptInfo)); var conceptParsersList = new List <IConceptParser>() { new GenericParserHelper <SimpleConceptInfo>(syntax, "concept"), new GenericParserHelper <EnclosedRefConceptInfo>(syntax, "concept") }; var conceptParsers = new MultiDictionary <string, IConceptParser>(); conceptParsers.Add("concept", conceptParsersList); var context = new Stack <IConceptInfo>(); TokenReader tokenReader = TestTokenReader(dsl); IConceptInfo concept = new TestDslParser(dsl, syntax).ParseNextConcept(tokenReader, context, conceptParsers); Assert.AreEqual(typeof(SimpleConceptInfo), concept.GetType()); Assert.AreEqual("name", (concept as SimpleConceptInfo).Name); Assert.AreEqual("data", (concept as SimpleConceptInfo).Data); Assert.IsTrue(tokenReader.TryRead("{"), "Reading '{' between concepts."); context.Push(concept); concept = new TestDslParser(dsl, syntax).ParseNextConcept(tokenReader, context, conceptParsers); Assert.AreEqual(typeof(EnclosedRefConceptInfo), concept.GetType()); Assert.AreEqual("ref", (concept as EnclosedRefConceptInfo).Name); Assert.AreEqual("name", (concept as EnclosedRefConceptInfo).Reference.Name); Assert.AreEqual("data", (concept as EnclosedRefConceptInfo).Reference.Data); }
public void ParseNextConcept_SameKeyWordDifferentContext_Ambiguous() { string dsl = "concept simple data; concept ref simple;"; var conceptParsersList = new List <IConceptParser>() { new GenericParserHelper <SimpleConceptInfo>("concept"), new GenericParserHelper <RefConceptInfo>("concept") }; var conceptParsers = new MultiDictionary <string, IConceptParser> (); conceptParsers.Add("concept", conceptParsersList); var noContext = new Stack <IConceptInfo>(); TokenReader tokenReader = TestTokenReader(dsl); try { IConceptInfo concept = new TestDslParser(dsl).ParseNextConcept(tokenReader, noContext, conceptParsers); } catch (Exception e) { Assert.IsTrue(e.Message.Contains("SimpleConceptInfo"), "Possible interpretation is SimpleConceptInfo."); Assert.IsTrue(e.Message.Contains("RefConceptInfo"), "Possible interpretation is RefConceptInfo."); throw; } }
public void InitiallyParsedAlternativeInitializationConcept_Embedded() { string dsl = "SIMPLE s d { ALTER1 { ALTER2 d2; } }"; var syntax = new IConceptInfo[] { new SimpleConceptInfo(), new AlternativeConcept1(), new AlternativeConcept2() }; var parsedConcepts = new TestDslParser(dsl, syntax).GetConcepts(); var dslModelConcepts = DslModelFromConcepts(parsedConcepts); var s = dslModelConcepts.OfType <SimpleConceptInfo>().Single(); var a1 = dslModelConcepts.OfType <AlternativeConcept1>().Single(); var a2 = dslModelConcepts.OfType <AlternativeConcept2>().Single(); var r = dslModelConcepts.OfType <RefConceptInfo>().Single(); Assert.AreEqual("Rhetos.Dsl.Test.DslModelTest+SimpleConceptInfo Name=s Data=d", s.GetErrorDescription()); Assert.AreEqual("Rhetos.Dsl.Test.DslModelTest+AlternativeConcept1 Parent=s Name=a1 RefToParent=ref.s", a1.GetErrorDescription()); Assert.AreEqual("Rhetos.Dsl.Test.DslModelTest+AlternativeConcept2 Alter1=s.a1 Name=a2 Data=d2", a2.GetErrorDescription()); Assert.AreEqual("Rhetos.Dsl.Test.DslModelTest+RefConceptInfo Name=ref Reference=s", r.GetErrorDescription()); Assert.AreEqual("Rhetos.Dsl.Test.DslModelTest+SimpleConceptInfo Name=s Data=d", r.Reference.GetErrorDescription()); Assert.AreSame(s, a1.Parent); Assert.AreSame(r, a1.RefToParent); Assert.AreSame(a1, a2.Alter1); Assert.AreSame(s, r.Reference); Assert.AreEqual("alter1, alter2, InitializationConcept, REF, SIMPLE", TestUtility.DumpSorted(dslModelConcepts, c => c.GetKeywordOrTypeName())); }
public void AlternativeInitializationConcept_ErrorHandling() { string dsl = "alterror1;"; var grammar = new IConceptInfo[] { new AlternativeError1() }; // Parsing a concept with invalid DeclareNonparsableProperties TestUtility.ShouldFail( () => { var concepts = new TestDslParser(dsl, grammar).ParsedConcepts; }, "AlternativeError1", "invalid implementation", "Names", "does not exist", "DeclareNonparsableProperties"); }
public void AlternativeInitializationConcept_Embedded() { string dsl = "SIMPLE s d { ALTER1 { ALTER2 d2; } }"; var syntax = new IConceptInfo[] { new SimpleConceptInfo(), new AlternativeConcept1(), new AlternativeConcept2() }; var parsedNodes = new TestDslParser(dsl, syntax).GetConcepts(); var parsedConcepts = ConceptInfoHelper.ConvertNodesToConceptInfos(parsedNodes); // IAlternativeInitializationConcept should be parsed, but not yet initialized. Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+SimpleConceptInfo Name=s Data=d", parsedConcepts.OfType <SimpleConceptInfo>().Single().GetErrorDescription()); Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+AlternativeConcept1 Parent=s Name=<null> RefToParent=<null>", parsedConcepts.OfType <AlternativeConcept1>().Single().GetErrorDescription()); Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+AlternativeConcept2 Alter1=s.<null> Name=<null> Data=d2", parsedConcepts.OfType <AlternativeConcept2>().Single().GetErrorDescription()); Assert.AreEqual("alter1, alter2, SIMPLE", TestUtility.DumpSorted(parsedNodes, c => c.Concept.GetKeywordOrTypeName())); }
public void AlternativeInitializationConcept_Embedded() { string dsl = "SIMPLE s d { ALTER1 { ALTER2 d2; } }"; var grammar = new IConceptInfo[] { new SimpleConceptInfo(), new AlternativeConcept1(), new AlternativeConcept2() }; var parsedConcepts = new TestDslParser(dsl, grammar).ParsedConcepts; Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+SimpleConceptInfo Name=s Data=d", parsedConcepts.OfType <SimpleConceptInfo>().Single().GetErrorDescription()); Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+AlternativeConcept1 Parent=s Name=a1 RefToParent=ref.s", parsedConcepts.OfType <AlternativeConcept1>().Single().GetErrorDescription()); Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+AlternativeConcept2 Alter1=s.a1 Name=a2 Data=d2", parsedConcepts.OfType <AlternativeConcept2>().Single().GetErrorDescription()); Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+RefConceptInfo Name=ref Reference=s", parsedConcepts.OfType <RefConceptInfo>().Single().GetErrorDescription()); // Embedded concepts have the parent reference resolved during parsing, so the SimpleConceptInfo reference has a value of the Data property: Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+SimpleConceptInfo Name=s Data=d", parsedConcepts.OfType <RefConceptInfo>().Single().Reference.GetErrorDescription()); }
public void AlternativeInitializationConcept_Embedded() { string dsl = "SIMPLE s d { ALTER1 { ALTER2 d2; } }"; var grammar = new IConceptInfo[] { new SimpleConceptInfo(), new AlternativeConcept1(), new AlternativeConcept2() }; var parsedConcepts = new TestDslParser(dsl, grammar).ParsedConcepts; Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+SimpleConceptInfo Name=s Data=d", parsedConcepts.OfType<SimpleConceptInfo>().Single().GetErrorDescription()); Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+AlternativeConcept1 Parent=s Name=a1 RefToParent=ref.s", parsedConcepts.OfType<AlternativeConcept1>().Single().GetErrorDescription()); Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+AlternativeConcept2 Alter1=s.a1 Name=a2 Data=d2", parsedConcepts.OfType<AlternativeConcept2>().Single().GetErrorDescription()); Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+RefConceptInfo Name=ref Reference=s", parsedConcepts.OfType<RefConceptInfo>().Single().GetErrorDescription()); // Embedded concepts have the parent reference resolved during parsing, so the SimpleConceptInfo reference has a value of the Data property: Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+SimpleConceptInfo Name=s Data=d", parsedConcepts.OfType<RefConceptInfo>().Single().Reference.GetErrorDescription()); }
public void AlternativeInitializationConceptTest() { string dsl = "SIMPLE s d; ALTER1 s; ALTER2 s.a1 d2;"; var grammar = new IConceptInfo[] { new SimpleConceptInfo(), new AlternativeConcept1(), new AlternativeConcept2() }; var parsedConcepts = new TestDslParser(dsl, grammar).ParsedConcepts; Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+SimpleConceptInfo Name=s Data=d", parsedConcepts.OfType <SimpleConceptInfo>().Single().GetErrorDescription()); Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+AlternativeConcept1 Parent=s Name=a1 RefToParent=ref.s", parsedConcepts.OfType <AlternativeConcept1>().Single().GetErrorDescription()); Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+AlternativeConcept2 Alter1=s.a1 Name=a2 Data=d2", parsedConcepts.OfType <AlternativeConcept2>().Single().GetErrorDescription()); Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+RefConceptInfo Name=ref Reference=s", parsedConcepts.OfType <RefConceptInfo>().Single().GetErrorDescription()); // References are not yet resolved when evaluating IAlternativeInitializationConcept, so the SimpleConceptInfo reference does not have a value of the Data property: Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+SimpleConceptInfo Name=s Data=<null>", parsedConcepts.OfType <RefConceptInfo>().Single().Reference.GetErrorDescription(), "This test does not describe the wanted behavior, it just describes the current state of the parser."); }
public void AlternativeInitializationConceptTest() { string dsl = "SIMPLE s d; ALTER1 s; ALTER2 s.a1 d2;"; var grammar = new IConceptInfo[] { new SimpleConceptInfo(), new AlternativeConcept1(), new AlternativeConcept2() }; var parsedConcepts = new TestDslParser(dsl, grammar).ParsedConcepts; Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+SimpleConceptInfo Name=s Data=d", parsedConcepts.OfType<SimpleConceptInfo>().Single().GetErrorDescription()); Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+AlternativeConcept1 Parent=s Name=a1 RefToParent=ref.s", parsedConcepts.OfType<AlternativeConcept1>().Single().GetErrorDescription()); Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+AlternativeConcept2 Alter1=s.a1 Name=a2 Data=d2", parsedConcepts.OfType<AlternativeConcept2>().Single().GetErrorDescription()); Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+RefConceptInfo Name=ref Reference=s", parsedConcepts.OfType<RefConceptInfo>().Single().GetErrorDescription()); // References are not yet resolved when evaluating IAlternativeInitializationConcept, so the SimpleConceptInfo reference does not have a value of the Data property: Assert.AreEqual("Rhetos.Dsl.Test.DslParserTest+SimpleConceptInfo Name=s Data=<null>", parsedConcepts.OfType<RefConceptInfo>().Single().Reference.GetErrorDescription(), "This test does not describe the wanted behavior, it just describes the current state of the parser."); }
public void ParseNextConcept_SameKeyWordDifferentContext_Enclosed() { string dsl = "concept name data { concept ref; }"; var conceptParsers = new List<IConceptParser>() { new GenericParserHelper<SimpleConceptInfo>("concept"), new GenericParserHelper<EnclosedRefConceptInfo>("concept") }; var context = new Stack<IConceptInfo>(); TokenReader tokenReader = TestTokenReader(dsl); IConceptInfo concept = new TestDslParser(dsl).ParseNextConcept(tokenReader, context, conceptParsers); Assert.AreEqual(typeof(SimpleConceptInfo), concept.GetType()); Assert.AreEqual("name", (concept as SimpleConceptInfo).Name); Assert.AreEqual("data", (concept as SimpleConceptInfo).Data); tokenReader.Read("{", "Reading '{' between concepts."); context.Push(concept); concept = new TestDslParser(dsl).ParseNextConcept(tokenReader, context, conceptParsers); Assert.AreEqual(typeof(EnclosedRefConceptInfo), concept.GetType()); Assert.AreEqual("ref", (concept as EnclosedRefConceptInfo).Name); Assert.AreEqual("name", (concept as EnclosedRefConceptInfo).Reference.Name); Assert.AreEqual("data", (concept as EnclosedRefConceptInfo).Reference.Data); }
public void ParseNextConcept_SameKeyWordDifferentContext_Ambiguous() { string dsl = "concept simple data; concept ref simple;"; var conceptParsers = new List<IConceptParser>() { new GenericParserHelper<SimpleConceptInfo>("concept"), new GenericParserHelper<RefConceptInfo>("concept") }; var noContext = new Stack<IConceptInfo>(); TokenReader tokenReader = TestTokenReader(dsl); try { IConceptInfo concept = new TestDslParser(dsl).ParseNextConcept(tokenReader, noContext, conceptParsers); } catch (Exception e) { Assert.IsTrue(e.Message.Contains("SimpleConceptInfo"), "Possible interpretation is SimpleConceptInfo."); Assert.IsTrue(e.Message.Contains("RefConceptInfo"), "Possible interpretation is RefConceptInfo."); throw; } }
public void ParseNextConcept_SameKeyWordDifferentContext() { string dsl = "concept simple simpledata; concept ext extdata extdata2;"; var conceptParsers = new List<IConceptParser>() { new GenericParserHelper<SimpleConceptInfo>("concept"), new GenericParserHelper<ExtendedConceptInfo>("concept") }; var noContext = new Stack<IConceptInfo>(); TokenReader tokenReader = TestTokenReader(dsl); IConceptInfo concept = new TestDslParser(dsl).ParseNextConcept(tokenReader, noContext, conceptParsers); Assert.AreEqual(typeof(SimpleConceptInfo), concept.GetType()); Assert.AreEqual("simple", (concept as SimpleConceptInfo).Name); Assert.AreEqual("simpledata", (concept as SimpleConceptInfo).Data); tokenReader.Read(";", "Reading ';' between concepts."); concept = new TestDslParser(dsl).ParseNextConcept(tokenReader, noContext, conceptParsers); Assert.AreEqual(typeof(ExtendedConceptInfo), concept.GetType()); Assert.AreEqual("ext", (concept as ExtendedConceptInfo).Name); Assert.AreEqual("extdata", (concept as ExtendedConceptInfo).Data); Assert.AreEqual("extdata2", (concept as ExtendedConceptInfo).Data2); tokenReader.Read(";", "Reading ';' after the concept."); }
public void ParseNextConcept_PropagateExceptionIfKeywordRecognized() { string dsl = "a"; List<IConceptParser> conceptParsers = new List<IConceptParser>() { new TestErrorParser("a") }; TokenReader tokenReader = TestTokenReader(dsl); try { IConceptInfo actual = new TestDslParser(dsl).ParseNextConcept(tokenReader, null, conceptParsers); } catch (Exception e) { Console.WriteLine(e.Message); Assert.IsTrue(e.Message.Contains(TestErrorParser.ErrorMessage), "Exception must contain: " + TestErrorParser.ErrorMessage); throw; } }
public void ParseNextConcept_DontDescribeExceptionIfConceptNotRecognised() { string dsl = "a"; List<IConceptParser> conceptParsers = new List<IConceptParser>() { new TestErrorParser("b") }; TokenReader tokenReader = new TokenReader(Tokenizer.GetTokens(new DslSourceHelper(dsl)), 0); try { IConceptInfo actual = new TestDslParser(dsl).ParseNextConcept(tokenReader, null, conceptParsers); } catch (Exception e) { Console.WriteLine(e.Message); Assert.IsFalse(e.Message.Contains(TestErrorParser.ErrorMessage), "Exception must not contain: " + TestErrorParser.ErrorMessage); throw; } }