Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include enum subvalues in markdown documentation #39

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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