Skip to content

Commit

Permalink
feat: add first add sample #22
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 9, 2022
1 parent 34755a3 commit d7389cb
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ package chapi.ast.kotlinast
import hello.Human
class Person(val name: String, val age: Int = 0) : Human {
var height: Int = 0
private var weight: Int = 0
class Person(val name: String, val age: IntValue = 0) : Human {
var height: IntValue = 0
private var weight: IntValue = 0
fun setWeight(weight: Int) {
fun setWeight(weight: IntValue) {
this.weight = weight
}
}
Expand All @@ -146,19 +146,19 @@ class Person(val name: String, val age: Int = 0) : Human {
assertEquals(Modifiers[0], "val")
}
codeContainer.DataStructures[0].Fields[1].run {
assertEquals(TypeType, "kotlin.Int")
assertEquals(TypeType, "kotlin.IntValue")
assertEquals(TypeValue, "0")
assertEquals(TypeKey, "age")
assertEquals(Modifiers[0], "val")
}
codeContainer.DataStructures[0].Fields[2].run {
assertEquals(TypeType, "kotlin.Int")
assertEquals(TypeType, "kotlin.IntValue")
assertEquals(TypeValue, "0")
assertEquals(TypeKey, "height")
assertEquals(Modifiers[0], "var")
}
codeContainer.DataStructures[0].Fields[3].run {
assertEquals(TypeType, "kotlin.Int")
assertEquals(TypeType, "kotlin.IntValue")
assertEquals(TypeValue, "0")
assertEquals(TypeKey, "weight")
assertEquals(Modifiers[0], "private")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ import org.springframework.web.client.RestTemplate
@Component
class QualityGateClientImpl(@Value val baseUrl: String) : QualityGateClient {
class QualityGateClientImpl(@CustomValueType val baseUrl: String) : QualityGateClient {
override fun getQualityGate(qualityGateName: String): CouplingQualityGate {
RestTemplate().getForObject("/api/quality-gate-profile/mcc", CouplingQualityGate::class.java)
}
Expand All @@ -195,7 +195,7 @@ package chapi.ast.kotlinast
import org.springframework.web.client.RestTemplate
@Component
class QualityGateClientImpl(@Value("\${'$'}{client.host}") val baseUrl: String) : QualityGateClient {
class QualityGateClientImpl(@CustomValueType("\${'$'}{client.host}") val baseUrl: String) : QualityGateClient {
override fun getQualityGate(qualityGateName: String): CouplingQualityGate {
val couplingQualityGate = RestTemplate().getForObject(baseUrl + "/api/quality-gate-profile/${'$'}qualityGateName", CouplingQualityGate::class.java)
return couplingQualityGate ?: CouplingQualityGate(null, qualityGateName, emptyList(), null, null)
Expand All @@ -218,7 +218,7 @@ package com.thoughtworks.archguard.packages.domain
import org.springframework.web.client.RestTemplate
class PackageStore {
fun addEdge(a: String, b: String, num: Int) {
fun addEdge(a: String, b: String, num: IntValue) {
val aId = getNodeId(a)
val bId = getNodeId(b)
packageEdges.add(PackageEdge(aId, bId, num))
Expand All @@ -240,7 +240,7 @@ package com.thoughtworks.archguard.packages.domain
import org.springframework.web.client.RestTemplate
class SizingRepositoryImpl(val jdbi: Jdbi) : SizingRepository {
override fun getClassSizingListAboveMethodCountThresholdCount(systemId: Long, threshold: Int): Long {
override fun getClassSizingListAboveMethodCountThresholdCount(systemId: Long, threshold: IntValue): Long {
return jdbi.withHandle<Long, Exception> {
val table = "select count(name) as count from code_class where system_id = :systemId and is_test=false and loc is not NULL " +
"group by class_name " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ class ClassController(val service: ClassService) {
fun getDependencies(@PathVariable("systemId") systemId: Long,
@PathVariable("name") name: String,
@RequestParam(value = "module", required = false, defaultValue = "") module: String,
@RequestParam("deep", required = false, defaultValue = "3") deep: Int): JClass {
@RequestParam("deep", required = false, defaultValue = "3") deep: IntValue): JClass {
return service.getDependencies(systemId, module, name, deep)
}
@GetMapping("/{name}/invokes")
fun getInvokes(@PathVariable("systemId") systemId: Long,
@PathVariable("name") name: String,
@RequestParam(value = "module", required = false, defaultValue = "") module: String,
@RequestParam(value = "deep", required = false, defaultValue = "3") deep: Int,
@RequestParam(value = "callerDeep", required = false) callerDeep: Int?,
@RequestParam(value = "calleeDeep", required = false) calleeDeep: Int?,
@RequestParam(value = "deep", required = false, defaultValue = "3") deep: IntValue,
@RequestParam(value = "callerDeep", required = false) callerDeep: IntValue?,
@RequestParam(value = "calleeDeep", required = false) calleeDeep: IntValue?,
@RequestParam(value = "needIncludeImpl", required = false, defaultValue = "true") needIncludeImpl: Boolean?): JClass {
return service.findInvokes(systemId, module, name, callerDeep ?: deep, calleeDeep ?: deep, needIncludeImpl
?: true)
Expand All @@ -54,7 +54,7 @@ class ClassController(val service: ClassService) {
fun getMethodsCallees(@PathVariable("systemId") systemId: Long,
@PathVariable("name") name: String,
@RequestParam(value = "module", required = false, defaultValue = "") module: String,
@RequestParam(value = "deep", required = false, defaultValue = "3") deep: Int,
@RequestParam(value = "deep", required = false, defaultValue = "3") deep: IntValue,
@RequestParam(value = "needParents", required = false, defaultValue = "true") needParents: Boolean,
@RequestParam(value = "needIncludeImpl", required = false, defaultValue = "true") needIncludeImpl: Boolean): JClass {
return service.findMethodsCallees(systemId, module, name, deep, needIncludeImpl, needParents)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ object HelloWorld {
@Test
internal fun shouldIdentClassName() {
val code = """
class Outer(i : Int) {
class Outer(i : IntValue) {
def foo(x : Inner.type) = x.getI
}
"""
Expand All @@ -39,24 +39,24 @@ class Outer(i : Int) {
@Test
internal fun shouldIdentClassParameters() {
val code = """
class Outer(i : Int) {
class Outer(i : IntValue) {
def foo(x : Inner.type) = x.getI
}
"""

val container = ScalaAnalyser().analysis(code, "hello.scala")
assertEquals(container.DataStructures.size, 1)
assertEquals(container.DataStructures[0].Parameters.size, 1)
assertEquals(container.DataStructures[0].Parameters[0].TypeType, "Int")
assertEquals(container.DataStructures[0].Parameters[0].TypeType, "IntValue")
assertEquals(container.DataStructures[0].Parameters[0].TypeValue, "i")
}

@Test
internal fun shouldIdentOutClassInnerObjectName() {
val code = """
class Outer(i : Int) {
class Outer(i : IntValue) {
object Inner {
def getI : Int = i
def getI : IntValue = i
}
def foo(x : Inner.type) = x.getI
}
Expand Down Expand Up @@ -89,7 +89,7 @@ trait Iterator[A] {
def next(): A
}
class IntIterator(to: Int) extends Iterator[Int] {
class IntIterator(to: IntValue) extends Iterator[IntValue] {
private var current = 0
override def hasNext: Boolean = current < to
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,41 @@ import java.util.function.IntBinaryOperator
// Postfix;
// }
sealed class Expression {
class BinOp(lhs: Expression, op: BinOpKind, rhs: Expression) : ExpressionNode
class UnOp(lhs: Expression, op: UnOpKind) : ExpressionNode
class Int(val value: Int?) : ExpressionNode
class BinOp(val lhs: ExpressionNode, val op: BinOpKind, val rhs: ExpressionNode) : ExpressionNode {
override fun toString(): String {
return "$lhs $op $rhs"
}
}

class UnOp(lhs: ExpressionNode, op: UnOpKind) : ExpressionNode
class IntValue(val value: kotlin.Int) : ExpressionNode {
override fun toString() = value.toString()
}

class CustomValueType(val value: ValueType) : ExpressionNode

}

sealed class BinOpKind {
object Add : BinOpKind() // +
object Sub : BinOpKind() // -
object Mul : BinOpKind() // *
object Div : BinOpKind() // /
object Pow : BinOpKind() // ^
object Eq : BinOpKind() // =
object Add : BinOpKind() {
override fun toString() = "+"
}

object Sub : BinOpKind() {
override fun toString() = "-"
}

object Mul : BinOpKind() {
override fun toString() = "*"
}

object Div : BinOpKind() {
override fun toString() = "/"
}

object Pow : BinOpKind() {
override fun toString() = "^"
}
}

sealed class UnOpKind {
Expand Down Expand Up @@ -53,6 +76,11 @@ interface ExpressionNode {
}


interface ValueType {
val name: String
val value: Any
}

interface Operator : ExpressionNode

enum class Arithmetics : BinaryOperator<Int>, IntBinaryOperator {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package chapi.domain.expr

import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

class ExpressionTest {
@Test
fun shouldAddChildren() {
fun shouldPresentationAdd() {
// addOp(1, 2)
val binOp = Expression.BinOp(
lhs = Expression.IntValue(1),
op = BinOpKind.Add,
rhs = Expression.IntValue(2)
)

assertEquals("1 + 2", binOp.toString())
}
}

0 comments on commit d7389cb

Please sign in to comment.