Skip to content

Commit

Permalink
Include enum subvalues in markdown documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
domi-b committed Nov 5, 2024
1 parent f4e0fcd commit 5d8a3da
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,54 @@ namespace Geowerkstatt.Interlis.LanguageServer.Visitors;
public class MarkdownDocumentationVisitorTest
{
private const string TestModel = """
INTERLIS 2.4;
MODEL TestModel (de) AT "http://models.geow.cloud" VERSION "1" =
TOPIC TestTopic =
CLASS TestClass =
attr1: TEXT*12;
attr2: MANDATORY BOOLEAN;
END TestClass;
END TestTopic;
END TestModel;
""";
INTERLIS 2.4;
MODEL TestModel (de) AT "http://models.geow.cloud" VERSION "1" =
TOPIC TestTopic =
CLASS TestClass =
attr1: TEXT*12;
attr2: MANDATORY BOOLEAN;
END TestClass;
END TestTopic;
END TestModel;
""";

private const string TestModelAssociation = """
INTERLIS 2.4;
MODEL TestModel (de) AT "http://models.geow.cloud" VERSION "1" =
TOPIC TestTopic =
CLASS ClassA =
attrA: TEXT*10;
END ClassA;
CLASS ClassB =
attrB: 10..20;
END ClassB;
ASSOCIATION Assoc1 =
AssocA -- {0..*} ClassA;
AssocB -<> {1} ClassB;
END Assoc1;
END TestTopic;
END TestModel;
""";
INTERLIS 2.4;
MODEL TestModel (de) AT "http://models.geow.cloud" VERSION "1" =
TOPIC TestTopic =
CLASS ClassA =
attrA: TEXT*10;
END ClassA;
CLASS ClassB =
attrB: 10..20;
END ClassB;
ASSOCIATION Assoc1 =
AssocA -- {0..*} ClassA;
AssocB -<> {1} ClassB;
END Assoc1;
END TestTopic;
END TestModel;
""";

private const string TestModelEnumeration = """
INTERLIS 2.4;
MODEL TestModel (de) AT "http://models.geow.cloud" VERSION "1" =
TOPIC TestTopic =
CLASS TestClass =
attr1: (
topValue1,
topValue2 (subValue1, subValue2, subValue3 (subSubValue1, subSubValue2)),
topValue3
);
END TestClass;
END TestTopic;
END TestModel;
""";

[TestMethod]
public void TestInterlisFile()
Expand All @@ -50,16 +66,16 @@ public void TestInterlisFile()
var documentation = visitor.GetDocumentation();

const string expected = """
# TestModel
## TestTopic
### TestClass
| Attributname | Kardinalität | Typ |
| --- | --- | --- |
| attr1 | 0..1 | Text [12] |
| attr2 | 1 | Boolean |
# TestModel
## TestTopic
### TestClass
| Attributname | Kardinalität | Typ |
| --- | --- | --- |
| attr1 | 0..1 | Text [12] |
| attr2 | 1 | Boolean |
""";
""";

Assert.AreEqual(expected.ReplaceLineEndings(), documentation.ReplaceLineEndings());
}
Expand All @@ -75,22 +91,46 @@ public void TestInterlisFileAssociation()
var documentation = visitor.GetDocumentation();

const string expected = """
# TestModel
## TestTopic
### ClassA
| Attributname | Kardinalität | Typ |
| --- | --- | --- |
| attrA | 0..1 | Text [10] |
| AssocB | 1 | ClassB |
# TestModel
## TestTopic
### ClassA
| Attributname | Kardinalität | Typ |
| --- | --- | --- |
| attrA | 0..1 | Text [10] |
| AssocB | 1 | ClassB |
### ClassB
| Attributname | Kardinalität | Typ |
| --- | --- | --- |
| attrB | 0..1 | 10..20 |
| AssocA | 0..n | ClassA |
### ClassB
| Attributname | Kardinalität | Typ |
| --- | --- | --- |
| attrB | 0..1 | 10..20 |
| AssocA | 0..n | ClassA |
""";
""";

Assert.AreEqual(expected.ReplaceLineEndings(), documentation.ReplaceLineEndings());
}

[TestMethod]
public void TestInterlisFileEnumeration()
{
var reader = new InterlisReader();
var interlisFile = reader.ReadFile(new StringReader(TestModelEnumeration));

var visitor = new MarkdownDocumentationVisitor();
visitor.VisitInterlisFile(interlisFile);
var documentation = visitor.GetDocumentation();

const string expected = """
# TestModel
## TestTopic
### TestClass
| Attributname | Kardinalität | Typ |
| --- | --- | --- |
| attr1 | 0..1 | (**topValue1**, **topValue2** (subValue1, subValue2, subValue3 (*subSubValue1*, *subSubValue2*)), **topValue3**) |
""";

Assert.AreEqual(expected.ReplaceLineEndings(), documentation.ReplaceLineEndings());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,29 @@ private static string CalculateCardinality(Cardinality? cardinality)
BlackboxType.BlackboxTypeKind.Xml => "Blackbox (XML)",
_ => "Blackbox",
},
EnumerationType enumerationType => $"({string.Join(", ", enumerationType.Values.Select(v => v.Name))})",
EnumerationType enumerationType => FormatEnumerationValues(enumerationType.Values),
ReferenceType referenceType => referenceType.Target.Value?.Path.Last(),
TypeRef typeRef => typeRef.Extends?.Path.Last(),
RoleType roleType => string.Join(", ", roleType.Targets.Select(target => target.Value?.Path.Last()).Where(target => target is not null)),
_ => type?.ToString(),
};
}

private static string FormatEnumerationValues(EnumerationValuesList enumerationValues, int depth = 0)
{
const string bold = "**";
const string italic = "*";

var formatting = depth switch
{
0 => bold,
1 => "",
_ => italic,
};
var formattedValues = enumerationValues.Select(v => $"{formatting}{v.Name}{formatting}{(v.SubValues.Count == 0 ? "" : " " + FormatEnumerationValues(v.SubValues, depth + 1))}");
return $"({string.Join(", ", formattedValues)})";
}

/// <summary>
/// Returns the generated markdown documentation of the visited elements.
/// </summary>
Expand Down

0 comments on commit 5d8a3da

Please sign in to comment.