Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pythonised simpleIntegrationTest #536

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 72 additions & 53 deletions test/00/simpleIntegrationTest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,59 +30,78 @@ pass()

trap "fail" 1 2 3 15

cat >input.tcl <<EOF
source $here/test/assert.tcl

# arrange for the following code to executed after Minsky has started.
proc afterMinskyStarted {} {
# remove Destroy binding as it interferes with assert
bind . <Destroy> {}

minsky.load $here/examples/exponentialGrowth.mky

getValue :y
assert {[value.value]==1}

set item "minsky.canvas.item"
findObject VarConstant
set a [\$item.value]
assert {[minsky.canvas.item.value]!=0}

# setting this flag prevents an extra reset occurring - used to
# distinguish between static plot updates and simulation
running 1
minsky.reset
step
step
getValue :y
assert {[value.value]>0}
if {\$a<0} {
assert {[value.value]>0 && [value.value]<1}
} else {
assert {[value.value]>0 && [value.value]>1}
}

set v [value.value]

findObject IntOp
\$item.toggleCoupled

minsky.reset
step
step
getValue :y
if {\$a<0} {
assert {[value.value]>0 && [value.value]<1}
} else {
assert {[value.value]>0 && [value.value]>1}
}
assert "\[value.value\]==\$v"

tcl_exit
}

cat >input.py <<EOF
import sys
sys.path.insert(0, '$here')
from pyminsky import minsky, findObject

# Step 1: Function to access value by ID
def getValue(valueId):
try:
# Access valueId directly via minsky's interface
value = minsky.variableValues[valueId]
print(f"Found variable {valueId} with value: {value.value()}")
return value
except KeyError:
raise ValueError(f"Variable '{valueId}' not found in variableValues.")
except Exception as e:
raise ValueError(f"Error accessing variable '{valueId}': {e}")

# Load the model
minsky.load('$here/examples/exponentialGrowth.mky')

# Step 2: Verify initial value of ':y'
y_value = getValue(":y")
assert y_value.value() == 1, f"Expected initial value of :y to be 1, but got {y_value.value()}"

# Step 3: Find and check VarConstant
findObject("VarConstant")
var_constant_value = minsky.canvas.item.value()
assert var_constant_value != 0, "VarConstant item value is unexpectedly 0."
print(f"VarConstant value: {var_constant_value}")

# Step 4: Advance simulation and verify updated ':y'
def step():
"""Advance the simulation by one step."""
minsky.step()

# Run two simulation steps
minsky.running = True
minsky.reset()
step()
step()

y_value_after_step = getValue(":y")
assert y_value_after_step.value() > 0, "Value of :y after steps is not greater than 0."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original code was nuts. The assertion here is redundant with the assertions on lines 79 and 81.


# Conditional check based on VarConstant value
if var_constant_value < 0:
assert 0 < y_value_after_step.value() < 1, "Expected :y to be between 0 and 1 for negative VarConstant."
else:
assert y_value_after_step.value() > 1, "Expected :y to be greater than 1 for positive VarConstant."

stored_y_value = y_value_after_step.value()

# Step 5: Toggle coupling for IntOp
findObject("IntOp")
minsky.canvas.item.toggleCoupled()
print("Toggled coupled state for IntOp.")

# Step 6: Reset and rerun simulation
minsky.reset()
step()
step()

y_value_after_toggle = getValue(":y")
if var_constant_value < 0:
assert 0 < y_value_after_toggle.value() < 1, "Expected :y to be between 0 and 1 for negative VarConstant."
else:
assert y_value_after_toggle.value() > 1, "Expected :y to be greater than 1 for positive VarConstant."

assert y_value_after_toggle.value() == stored_y_value, "Value of :y does not match after toggling coupled state."
EOF
$here/gui-tk/minsky input.tcl
if test $? -ne 0; then fail; fi

python3 input.py
if [ $? -ne 0 ]; then fail; fi

pass
Loading