import ast
from Tensile.Configuration import ReadWriteTransformDict
from Tensile.Configuration import Parameter
from Tensile.Configuration import CallableParameter
from Tensile.Configuration import ExpressionEvaluator
from Tensile.Configuration import ProjectConfig

def test_ReadWriteTransformDict():
    def readXForm(obj, key):
        return obj.readNoTransform(key+"-Mod")

    def writeXForm(obj, key, value):
        obj.writeNoTransform(key+"-Mod", value)

    test = ReadWriteTransformDict(readXForm, writeXForm)

    # Test with the re-direction of transform funcs
    test["Key"] = 10
    assert test["Key"] == 10 and not "Key" in test
    assert test["Key"] == test.readWithTransform("Key")
    assert "Key-Mod" in test and test.readNoTransform("Key-Mod") == test["Key"]

    # Test bypass
    test.writeNoTransform("Key", 25)
    assert "Key" in test and test.readNoTransform("Key") == 25
    assert test["Key"] != test.readNoTransform("Key")

    # Test removal
    test.setReadTransform(None)
    test.setWriteTransform(None)

    # Test restoration of default behaviour
    test["Key"] = -5
    assert test["Key"] == -5 and test.readNoTransform("Key") == -5

def test_Parameter():
    name = "banana"
    initVal = 15
    defaultVal = 10
    descr = "I am a banana"
    initType = type(initVal)
    b = Parameter(name, initVal, defaultVal, descr)

    #attr
    assert b.value == initVal
    assert b.defaultValue == defaultVal
    assert b.name == name
    assert b.description == descr
    assert b.type == initType

    b.createAttr("color", "yellow")
    assert b.color == "yellow"

    # Accessor funcs
    assert b.getDefault() == defaultVal
    assert b.getValue() == initVal
    assert b.getDescription() == descr
    assert b.resetToDefault() == defaultVal