Skip to content

Commit fd7267a

Browse files
authored
Merge pull request #974 from Alex-Jordan/answerHints
fix bugs with answerHints from issue #964
2 parents c1a8ad8 + 3fba804 commit fd7267a

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

lib/Value.pm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,12 @@ sub inContext { my $self = shift; $self->context(@_); $self }
267267

268268
#############################################################
269269

270-
#
271270
#
272271
# The address of a Value object (actually ANY perl value).
273272
# Use this to compare two objects to see of they are
274273
# the same object (avoids automatic stringification).
275274
#
276-
sub address { oct(sprintf("0x%p", shift)) }
275+
sub address { Scalar::Util::refaddr(shift) }
277276

278277
sub isBlessed { (Scalar::Util::blessed(shift) // '') ne "" }
279278
sub blessedClass { Scalar::Util::blessed(shift) }

macros/answers/answerHints.pl

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ sub AnswerHints {
144144
cmp_options => [],
145145
@options,
146146
);
147-
next if $options{checkTypes} && $correct->type ne $student->type;
148147
next if !$options{processPreview} && $ans->{isPreview};
149148
$wrongList = [$wrongList] unless ref($wrongList) eq 'ARRAY';
150149

151150
foreach my $wrong (@{$wrongList}) {
152151
if (ref($wrong) eq 'CODE') {
153-
if (($ans->{score} < 1 || $options{checkCorrect})
152+
if ((!$options{checkTypes} || $correct->type eq $student->type)
153+
&& ($ans->{score} < 1 || $options{checkCorrect})
154154
&& ($ans->{ans_message} eq "" || $options{replaceMessage}))
155155
{
156156
# Make the call to run the function inside an eval to trap errors
@@ -166,16 +166,12 @@ sub AnswerHints {
166166
}
167167
}
168168
} else {
169-
$wrong = Value::makeValue($wrong);
170-
if (
171-
(
172-
$ans->{score} < 1
173-
|| $options{checkCorrect}
174-
|| AnswerHints::Compare($correct, $wrong, $ans)
175-
)
176-
&& ($ans->{ans_message} eq "" || $options{replaceMessage})
177-
&& AnswerHints::Compare($wrong, $student, $ans, @{ $options{cmp_options} })
178-
)
169+
unless (Value::isValue($wrong)) {
170+
$wrong = main::Formula($wrong);
171+
$wrong = $wrong->{tree}->Compute if $wrong->{tree}{canCompute};
172+
}
173+
if (($ans->{ans_message} eq "" || $options{replaceMessage})
174+
&& AnswerHints::Compare($wrong, $student, $ans, @{ $options{cmp_options} }))
179175
{
180176
$ans->{ans_message} = $ans->{error_message} = $message;
181177
$ans->{score} = $options{score} if defined $options{score};
@@ -197,19 +193,19 @@ package AnswerHints;
197193
# and returns true if the two values match and false otherwise.
198194
#
199195
sub Compare {
200-
my $self = shift;
201-
my $other = shift;
202-
my $ans = shift;
203-
$ans = bless { %{$ans}, @_ }, ref($ans); # make a copy
196+
my ($self, $other, $ans, @options) = @_;
197+
return 0 unless $self->typeMatch($other); # make sure these can be compared
198+
$ans = bless { %{$ans}, @options }, ref($ans); # make a copy
204199
$ans->{typeError} = 0;
205200
$ans->{ans_message} = $ans->{error_message} = "";
206201
$ans->{score} = 0;
207-
if (sprintf("%p", $self) ne sprintf("%p", $ans->{correct_value})) {
202+
203+
if ($self->address != $ans->{correct_value}->address) {
208204
$ans->{correct_ans} = $self->string;
209205
$ans->{correct_value} = $self;
210206
$ans->{correct_formula} = Value->Package("Formula")->new($self);
211207
}
212-
if (sprintf("%p", $other) ne sprintf("%p", $ans->{student_value})) {
208+
if ($other->address != $ans->{student_value}->address) {
213209
$ans->{student_ans} = $other->string;
214210
$ans->{student_value} = $other;
215211
$ans->{student_formula} = Value->Package("Formula")->new($other);

0 commit comments

Comments
 (0)