From: Matt S Trout Date: Mon, 9 Nov 2015 17:55:38 +0000 (+0000) Subject: implement ->equals for values X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9e13b86b57aaf5f701bd55cfe5fc4e1078c3809b;p=scpubgit%2FDX.git implement ->equals for values --- diff --git a/lib/DX/Role/Predicate.pm b/lib/DX/Role/Predicate.pm index c1bcc0b..b1c8181 100644 --- a/lib/DX/Role/Predicate.pm +++ b/lib/DX/Role/Predicate.pm @@ -18,7 +18,7 @@ sub _make_equal { if ($left->equals($right)) { return step( actions => [], - depends_on => [ $left, $right ], + depends_on => [ [ undef ,=> [ $left, $right ] ] ], ); } return ( diff --git a/lib/DX/Role/Value.pm b/lib/DX/Role/Value.pm index a5fc4a0..01fd710 100644 --- a/lib/DX/Role/Value.pm +++ b/lib/DX/Role/Value.pm @@ -23,4 +23,12 @@ sub but_set_identity_path { $self->but(identity_path => $path); } +requires 'to_data'; + +sub equals { + my ($self, $other) = @_; + require YAML; + YAML::Dump($self->to_data) eq YAML::Dump($other->to_data); +} + 1; diff --git a/lib/DX/Value/Dict.pm b/lib/DX/Value/Dict.pm index af579ea..85decc7 100644 --- a/lib/DX/Value/Dict.pm +++ b/lib/DX/Value/Dict.pm @@ -38,4 +38,10 @@ sub without_member_at { $self->but(members => \%members); } +sub to_data { + my ($self) = @_; + my $m = $self->members; + +{ map +($_ => $m->{$_}->to_data), $self->index_list }; +} + 1; diff --git a/lib/DX/Value/False.pm b/lib/DX/Value/False.pm index 0412671..2dee3f4 100644 --- a/lib/DX/Value/False.pm +++ b/lib/DX/Value/False.pm @@ -6,4 +6,6 @@ with 'DX::Role::BooleanValue'; sub is_true { 0 } +sub to_data { \0 } + 1; diff --git a/lib/DX/Value/Number.pm b/lib/DX/Value/Number.pm index 6564205..74e30ac 100644 --- a/lib/DX/Value/Number.pm +++ b/lib/DX/Value/Number.pm @@ -6,4 +6,6 @@ with 'DX::Role::Value'; has number_value => (is => 'ro', required => 1); +sub to_data { 0+$_[0]->number_value } + 1; diff --git a/lib/DX/Value/String.pm b/lib/DX/Value/String.pm index 78c5f43..e1a55b6 100644 --- a/lib/DX/Value/String.pm +++ b/lib/DX/Value/String.pm @@ -6,4 +6,6 @@ with 'DX::Role::Value'; has string_value => (is => 'ro', required => 1); +sub to_data { ''.$_[0]->string_value } + 1; diff --git a/lib/DX/Value/True.pm b/lib/DX/Value/True.pm index df3f842..55dee79 100644 --- a/lib/DX/Value/True.pm +++ b/lib/DX/Value/True.pm @@ -6,4 +6,6 @@ with 'DX::Role::BooleanValue'; sub is_true { 1 } +sub to_data { \1 } + 1; diff --git a/lib/DX/Value/Unset.pm b/lib/DX/Value/Unset.pm index a78f556..507bd4f 100644 --- a/lib/DX/Value/Unset.pm +++ b/lib/DX/Value/Unset.pm @@ -6,4 +6,8 @@ with 'DX::Role::Value'; sub is_set { 0 } +sub to_data { undef } + +sub equals { 0 } + 1; diff --git a/t/01basics.t b/t/01basics.t index 09af40e..2c6c307 100644 --- a/t/01basics.t +++ b/t/01basics.t @@ -30,6 +30,7 @@ my $hyp = use_module('DX::Hypothesis')->new( outstanding_propositions => [ proposition(is_dict => 'X'), proposition(member_at => 'X', string('a'), string('b')), + proposition(member_at => 'X', string('a'), string('b')), ], actions => [], ); @@ -40,5 +41,4 @@ my $ss = use_module('DX::SearchState')->new( ); #::Dwarn($ss->with_one_step->with_one_step); -require YAML; -warn deparse($ss->with_one_step->with_one_step); +warn deparse($ss->with_one_step->with_one_step->with_one_step);