Skip to content

Commit

Permalink
docs: add doc architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 8, 2024
1 parent 707f53e commit c959153
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 7 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<h1 align="center">UnitGen - Code Fine-tuning Data Framework</h1>

<h1 align="center">UnitGen</h1>
<p align="center">
<img src="docs/logo.svg" width="100" height="100" alt="Chapi Logo">
</p>
<p align="center">
<a href="https://github.com/unit-mesh/unit-gen/actions/workflows/build.yml">
<img src="https://github.com/unit-mesh/unit-gen/actions/workflows/build.yml/badge.svg" alt="CI/CD" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import org.archguard.rule.core.Rule
import org.archguard.rule.core.RuleContext

open class CommentRule : Rule() {
fun visitRoot(node: CodeDataStruct, comment: String, context: RuleContext, callback: IssueEmit) {
open fun visitRoot(node: CodeDataStruct, comment: String, context: RuleContext, callback: IssueEmit) {

}

fun visitFunction(node: CodeFunction, comment: String, context: RuleContext, callback: IssueEmit) {
open fun visitFunction(node: CodeFunction, comment: String, context: RuleContext, callback: IssueEmit) {

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package cc.unitmesh.quality.comment.rule

import chapi.domain.core.CodeFunction
import org.archguard.rule.core.IssueEmit
import org.archguard.rule.core.IssuePosition
import org.archguard.rule.core.RuleContext

/**
* Parse the documentation of the code and check whether the documentation is complete.
*
Expand All @@ -17,6 +22,23 @@ package cc.unitmesh.quality.comment.rule
*
* We can use this rule to check whether the documentation is complete.
*/
class MissingParameterDescRule: CommentRule() {
class MissingParameterDescRule : CommentRule() {
private val pattern = Regex("""@param\s+(\w+)\s+([^@]+)""")

override fun visitFunction(node: CodeFunction, comment: String, context: RuleContext, callback: IssueEmit) {
val matches = pattern.findAll(comment)

val nodeSize = node.Parameters.size

if (matches.count() != nodeSize) {
callback(this, IssuePosition())
}

val matchNames = matches.map { it.groupValues[1] }.toSet()
val nodeNames = node.Parameters.map { it.TypeType }.toSet()

if (matchNames != nodeNames) {
callback(this, IssuePosition())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package cc.unitmesh.quality.comment.rule;

import chapi.domain.core.CodeFunction
import chapi.domain.core.CodeProperty
import org.archguard.rule.core.IssueEmit
import org.archguard.rule.core.IssuePosition
import org.archguard.rule.core.Rule
import org.archguard.rule.core.RuleContext
import org.junit.jupiter.api.Test;

import kotlin.test.assertFalse
import kotlin.test.assertTrue

class MissingParameterDescRuleTest {

@Test
fun shouldNotEmitIssueWhenDocumentationIsComplete() {
// Given
val rule = MissingParameterDescRule()
val function = CodeFunction()
function.Parameters = listOf(CodeProperty(TypeType = "int", TypeValue = "a"), CodeProperty(TypeType = "int", TypeValue = "b"))
val comment = """
/**
* Sum a and b, and return the result.
* @param a the first number
* @param b the second number
* @return the result of a + b
*/
""".trimIndent()
val context = RuleContext()
val callback = IssueEmitCallback()

// When
rule.visitFunction(function, comment, context, callback)

// Then
assertFalse(callback.hasIssue())
}

@Test
fun shouldEmitIssueWhenParameterDescriptionIsMissing() {
// Given
val rule = MissingParameterDescRule()
val function = CodeFunction()
function.Parameters = listOf(CodeProperty(TypeType = "int", TypeValue = "a"), CodeProperty(TypeType = "int", TypeValue = "b"))
val comment = """
/**
* Sum a and b, and return the result.
* @param a the first number
* @return the result of a + b
*/
""".trimIndent()
val context = RuleContext()
val callback = IssueEmitCallback()

// When
rule.visitFunction(function, comment, context, callback)

// Then
assertTrue(callback.hasIssue())
}

@Test
fun shouldEmitIssueWhenParameterNamesDoNotMatch() {
// Given
val rule = MissingParameterDescRule()
val function = CodeFunction()
function.Parameters = listOf(CodeProperty(TypeType = "int", TypeValue = "a"), CodeProperty(TypeType = "int", TypeValue = "b"))
val comment = """
/**
* Sum a and b, and return the result.
* @param x the first number
* @param y the second number
* @return the result of a + b
*/
""".trimIndent()
val context = RuleContext()
val callback = IssueEmitCallback()

// When
rule.visitFunction(function, comment, context, callback)

// Then
assertTrue(callback.hasIssue())
}
}

class IssueEmitCallback : IssueEmit {
private var hasIssue = false

fun hasIssue(): Boolean {
return hasIssue
}

override fun invoke(rule: Rule, position: IssuePosition) {
hasIssue = true
}
}
6 changes: 4 additions & 2 deletions docs/home.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ nav_order: 1
permalink: /
---

<h1 align="center">UnitGen - Code Fine-tuning Data Framework</h1>

<h1 align="center">UnitGen</h1>
<p align="center">
<img src="docs/logo.svg" width="100" height="100" alt="Chapi Logo">
</p>
<p align="center">
<a href="https://github.com/unit-mesh/unit-gen/actions/workflows/build.yml">
<img src="https://github.com/unit-mesh/unit-gen/actions/workflows/build.yml/badge.svg" alt="CI/CD" />
Expand Down
60 changes: 60 additions & 0 deletions docs/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c959153

Please sign in to comment.