Skip to content

Commit

Permalink
added some tests for the code generation phase
Browse files Browse the repository at this point in the history
  • Loading branch information
ikuraj committed Sep 4, 2012
1 parent bf7da17 commit 9dd5172
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package ch.epfl.insynth.test.reconstructor

import java.{ util => ju, lang => jl }
import ch.epfl.insynth.reconstruction.intermediate.IntermediateTransformer
import ch.epfl.insynth.reconstruction.codegen.{ CleanCodeGenerator, ClassicStyleCodeGenerator, ApplyTransfromer }
import ch.epfl.insynth.reconstruction.combinator.Combinator
import ch.epfl.insynth.reconstruction.combinator.DeclarationTransformer
import ch.epfl.insynth.trees.{ Type }
import ch.epfl.insynth.{ env => InSynth }
import ch.epfl.scala.{ trees => Scala }
import ch.epfl.insynth.reconstruction.intermediate._
import org.junit.Test
import org.junit.Assert._
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.junit.runners.Parameterized.Parameters
import ch.epfl.insynth.trees.BottomType

class CodeGenerationTests {

import TreeExample._

@Test
def test1() = {

import Scala._
import ch.epfl.insynth.trees.TypeTransformer.transform

// declare object
val objectType = Const("ObjectName")
val objectDeclaration = new InSynth.Declaration(
"some.package.ObjectName", // full name
transform(objectType), // inSynth type
objectType // scala type
)

// declare method
val methodType = Method(objectType, List(), typeString)
val methodDeclaration = new InSynth.Declaration(
"apply", // full name
transform(methodType), methodType
)
// it is an apply method, in an object
methodDeclaration.setIsApply(true)
methodDeclaration.setBelongsToObject(true)
methodDeclaration.setObjectName("ObjectName")

// declare according function
val functionType = Function(List(), typeString)
val functionDeclaration = new InSynth.Declaration(
"ObjectName", // full name
transform(methodType), functionType
)

// declare nodes
val methodIdentifier = Identifier(methodType, DeclarationTransformer.fromInSynthDeclaration(methodDeclaration))
val methodApplicationNode = Application( methodType, List(Set(methodIdentifier), Set(NullLeaf)) )
val methodRootNode = Application(Scala.Function(List(typeString), typeBottom), List(Set(NullLeaf), Set(methodApplicationNode)) )

val functionIdentifier = Identifier(functionType, DeclarationTransformer.fromInSynthDeclaration(functionDeclaration))
val functionApplicationNode = Application( functionType, List(Set(functionIdentifier)) )
val functionRootNode = Application(Scala.Function(List(typeString), typeBottom), List(Set(NullLeaf), Set(functionApplicationNode)) )

// make appropriate code generator objects
val methodGenerator = new CleanCodeGenerator with ApplyTransfromer
val functionGenerator = new CleanCodeGenerator

// compare all generated outputs
for ((output1, output2) <-
methodGenerator.apply(methodRootNode) zip functionGenerator.apply(functionRootNode))
assertEquals(output1.toString, output2.toString)

// make appropriate code generator objects
val methodClassicGenerator = new ClassicStyleCodeGenerator with ApplyTransfromer

// compare all generated outputs
for ((output1, output2) <-
methodClassicGenerator.apply(methodRootNode) zip functionGenerator.apply(functionRootNode))
assertEquals(output1.toString, output2.toString)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ class CleanCodeGenerator extends CodeGenerator {
} else if (!decl.isMethod) {
assert(!decl.isConstructor)
// just a function
parenthesesRequired = params.tail.size >= 1 || ctx == SinglePar
//parenthesesRequired = params.tail.size >= 1 || ctx == SinglePar
parenthesesRequired = true
firstTermFunctionTransform
} else // if (decl.isMethod)
{
Expand Down

0 comments on commit 9dd5172

Please sign in to comment.