Exit Full View

Feather2 / feather2-foocad / src / test / kotlin / uk.co.nickthecoder / feather / foocad / RealProblemsInExtensions.kt

package uk.co.nickthecoder.feather.foocad

import junit.framework.TestCase.assertEquals
import org.junit.Test

/**
 * While Feather was still in active development (i.e. unfinished),
 * I tried to compile existing feather scripts from FooCAD extensions.
 *
 * This helps me prioritise which features to implement, and gives me motivation to move forwards.
 * When all of my existing feather scripts compile, this version of Feather still won't be finished.
 * But it will allow me two switch to version 2, and therefore I can being to dog-food.
 * i.e. any bugs/missing features will need to be fixed asap, because my other projects will depend on it.
 */
class RealProblemsInExtensions {

    @Test
    fun arrangeToList() {
        val script = """
            class Arrange {
                // Failed to find arrange( double, List<Shape3d> )
                static fun arrange2( shapes: Shape3d... ) = arrange( 1.0, shapes.toList<Shape3d>() )

                static fun arrange( gap: double, shapes : List<Shape3d> ) : Shape3d {
                    return Cube(0) // A stub (not the real implementation
                }
                
            }
        """

        assertEquals( 1, extensionsConfig.compile(script).allClassNames.size )
    }

    @Test
    fun pathExtrudeArrayAccess() {
        val script = """
            
            class PathExtrude {
                // points[0] failed. The return type of path.points was ``List`` instead of ``List<Point2d>``
                static fun extrudePathScaled( profile : Shape2d, path : Path2d )
                    = PathExtrude.extrudePathScaled( profile, path, path.points[0].x )
                    
                static fun extrudePathScaled( profile : Shape2d, path : Path2d, size : double ) : Shape3d {
                    return Cube(0) // A stub (not the real implementation
                }
            }
            """

        // We are only testing that the compilation completed.
        assertEquals( 1, extensionsConfig.compile(script).allClassNames.size )

    }

    @Test
    fun abstractGetter() {
        val script = """
                        
            abstract class PieceOfWood() : Lazy3d() {

                abstract fun getWood() : Wood
               
                fun maxAlong(side : int) = if (side % 3 == 1) {
                    // Unknown identifier 'wood'.
                    wood.width
                } else {
                    size.z
                }
            }
            
            class Wood() {
                val width = 3
            }
            """

        // We are only testing that the compilation completed.
        assertEquals(2, extensionsConfig.compile(script).allClassNames.size)
    }

    @Test
    fun staticFieldUsingGetterMethod() {
        val script = """
            class SizeShapeExtension : AbstractShapeExtension( "Inspect" ) {
        
                // Field not found ZERO
                var rotate = Vector3.ZERO
            }
        """

        // We are only testing that the compilation completed.
        assertEquals(1, extensionsConfig.compile(script).allClassNames.size)
    }

    @Test
    fun fooLoop() {
        val script = """    
            class AroundPath2d( val path: Path2d ) {                
                init {
                    for (i in 1 until path.points.size() ) {
                        // Unknown identifier: 'i'
                        val next = path.points[i]
                    }
                }
            }
        """

        // We are only testing that the compilation completed.
        assertEquals(1, extensionsConfig.compile(script).allClassNames.size)
    }

    @Test
    fun slicerValues() {
        val script = """
            
            class PositionModel( wrapped : Model, val x : int, val y : int, val margin : Vector2 ) : WrappedModel( wrapped ) {

                override fun adjust( result : Shape3d ) : Shape3d {
                    // Function not found : slicerValues(). The method is on WrappedModel (the superclass)
                    // I didn't import ...foocad.build.util.Helper
                    var bedSize : Vector2 = slicerValues().bedSize ?: Vector2(150,150)

                    return Cube(0) // Dummy return value.
                }
            }
            """
        // We are only testing that the compilation completed.
        assertEquals(1, extensionsConfig.compile(script).allClassNames.size)
    }

    @Test
    fun listOfDouble() {
        val script = """
            class Test() {
                val list = listOf<double>()
            }
            """
        // We are only testing that the compilation completed.
        assertEquals(1, extensionsConfig.compile(script).allClassNames.size)
    }

    @Test
    fun functionArgument() {
        val script = """
            class InsertGCode {
            
                static fun pause( state : GCodeState ) {
                }
            
                static fun pauseAtHeight( gcode : GCode, heights : double... ) {
                    val list = listOf<double>()
                    for (height in heights) {
                        list.add(height)
                    }
                    // Function not found : insertAtHeight(GCode, Function{(GCodeState)->void}, List)
                    // list's type was List<double>, rather than List<Double>.
                    // Also, pause's return type argument was void, not Void.
                    insertAtHeight( gcode, ::pause, list )
                }
                
                static fun insertAtHeight( gcode : GCode, action : (GCodeState) -> void, heights: List<double> ) {
                }
            }
        """

        // We are only testing that the compilation completed.
        assertEquals(1, extensionsConfig.compile(script).allClassNames.size)
    }

    @Test
    fun quality() {
        val script = """
            class Test {
                fun test() {
                    Quality.minimumAngle = 1
                }
            }
        """

        assertEquals(1, extensionsConfig.compile(script).allClassNames.size)
    }

    @Test
    fun unaryMinus() {
        val script = """
            class Ears : Lazy3d(), Customisable {
                val ribMargin = Vector2(1.0,1.0)
                
                override fun build() : Shape3d {
                    return Square( 1 ).translate( -ribMargin ).extrude(1)
                }
            }
        """

        assertEquals(1, extensionsConfig.compile(script).allClassNames.size)
    }

}