Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
(fix) Check for duplicate clauses for same request, add dispatch tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jerome Simeon <jeromesimeon@me.com>
  • Loading branch information
jeromesimeon committed Feb 24, 2019
1 parent c54edcc commit 76d737d
Show file tree
Hide file tree
Showing 11 changed files with 9,252 additions and 8,961 deletions.
3 changes: 3 additions & 0 deletions examples/dispatchtest/logic2.ergo
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ contract DispatchTest over TemplateModel {
return Response{ message: "I am a request4" }
}

clause clause6(request : Request6) : Response {
return Response{ message: "I am a request6" }
}
}
4 changes: 4 additions & 0 deletions examples/dispatchtest/logic3.ergo
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
namespace org.accordproject.dispatchtest

contract DispatchTest over TemplateModel {
clause clause6(request : Request6) : Response {
return Response{ message: "I am a request6" }
}

clause clause4(request : Request4) : Response {
return Response{ message: "I am a request4" }
}
Expand Down
6 changes: 4 additions & 2 deletions mechanization/Common/Result.v
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Section Result.
End Lift.

Section Fmt.
Definition format_error (name : string) (prov : provenance) (msg : string) :=
Definition format_error (name : string) (prov : provenance) (msg : string) : string :=
let loc := loc_of_provenance prov in
(name ++ " at " ++ (string_of_location_no_file loc) ++ " '" ++ msg ++ "'")%string.
End Fmt.
Expand Down Expand Up @@ -217,14 +217,16 @@ Section Result.
ErgoData.dbrand (default_error_absolute_name::nil)
(ErgoData.drec (("message"%string, ErgoData.dstring message)::nil)).
Definition default_match_error_content (prov:provenance) : ErgoData.data :=
let message := format_error "Dispatch Error" prov ("Found no clause in contract to match the request") in
let message := "Dispatch Error: no clause in the contract matches the request"%string in
ErgoData.dbrand (default_error_absolute_name::nil)
(ErgoData.drec (("message"%string, ErgoData.dstring message)::nil)).

Definition unresolved_name_error {A} prov : eresult A :=
efailure (ECompilationError prov "Unresolved name").
Definition should_have_one_contract_error {A} prov : eresult A :=
efailure (ECompilationError prov "Should have exactly one contract").
Definition duplicate_clause_for_request_error {A} prov : eresult A :=
efailure (ECompilationError prov "Duplicate clauses for the same request type").

Definition contract_in_calculus_error {A} prov : eresult A :=
efailure (ESystemError prov "Should not find 'contract' in Ergo Calculus").
Expand Down
24 changes: 15 additions & 9 deletions mechanization/ErgoC/Lang/ErgoCExpand.v
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,13 @@ Section ErgoCExpand.

Definition make_cases
(order:list laergo_type_declaration)
(xy:list (absolute_name * (laergo_pattern * ergoc_expr))) :=
(prov:provenance)
(xy:list (absolute_name * (laergo_pattern * ergoc_expr)))
: eresult (list (laergo_pattern * ergoc_expr)) :=
let oxy := sort_given_topo_order order fst xy in
map snd oxy.
if NoDup_dec (map fst oxy)
then esuccess (map snd oxy)
else duplicate_clause_for_request_error prov.

Definition match_of_sigs
(order:list laergo_type_declaration)
Expand All @@ -84,13 +88,15 @@ Section ErgoCExpand.
(effparam0:ergoc_expr)
(effparamrest:list ergoc_expr)
(ss:list (string * sigc)) : eresult ergoc_expr :=
elift (fun (xy:list (absolute_name * (ergo_pattern * ergoc_expr))) =>
let cases := make_cases order xy in
EMatch prov effparam0
cases
(EError prov
(EConst prov (default_match_error_content prov))))
(eflatmaplift (case_of_sig prov coname v0 effparam0 effparamrest) ss).
eolift (fun (xy:list (absolute_name * (ergo_pattern * ergoc_expr))) =>
let ecases := make_cases order prov xy in
elift (fun cases =>
EMatch prov effparam0
cases
(EError prov
(EConst prov (default_match_error_content prov))))
ecases)
(eflatmaplift (case_of_sig prov coname v0 effparam0 effparamrest) ss).

Definition match_of_sigs_top
(order:list laergo_type_declaration)
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6,188 changes: 3,097 additions & 3,091 deletions packages/ergo-cli/lib/ergoc-lib.js

Large diffs are not rendered by default.

5,478 changes: 2,742 additions & 2,736 deletions packages/ergo-cli/lib/ergotop-lib.js

Large diffs are not rendered by default.

6,246 changes: 3,126 additions & 3,120 deletions packages/ergo-compiler/lib/ergo-core.js

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions packages/ergo-engine/test/features/dispatchtest1.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Feature: Dispatch Tests
This describe the expected behavior when dispatching requests to a clause

Background:
Given the Ergo contract "org.accordproject.dispatchtest.DispatchTest" in file "data/dispatchtest/logic.ergo"
And the model in file "data/dispatchtest/model.cto"
And the contract data
"""
{
"$class": "org.accordproject.dispatchtest.TemplateModel"
}
"""

Scenario: The contract should fail executing when an import is missing
When it receives the request
"""
{
"$class": "org.accordproject.dispatchtest.Request1"
}
"""
Then it should fail with the error
"""
{
"kind": "CompilationError",
"message": "Duplicate clauses for the same request type",
"locstart": {
"line": 16,
"character": 0
},
"locend": {
"line": 37,
"character": 1
}
}
"""

111 changes: 111 additions & 0 deletions packages/ergo-engine/test/features/dispatchtest2.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
Feature: Dispatch Tests
This describe the expected behavior when dispatching requests to a clause

Background:
Given the Ergo contract "org.accordproject.dispatchtest.DispatchTest" in file "data/dispatchtest/logic2.ergo"
And the model in file "data/dispatchtest/model.cto"
And the contract data
"""
{
"$class": "org.accordproject.dispatchtest.TemplateModel"
}
"""

Scenario: The contract should initialize
Then the initial state should be the default state

Scenario: The contract should return the correct response for a Request1
When it receives the request
"""
{
"$class": "org.accordproject.dispatchtest.Request1"
}
"""
Then it should respond with
"""
{
"message": "I am a request1",
"$class": "org.accordproject.dispatchtest.Response"
}
"""

Scenario: The contract should return the correct response for a Request2
When it receives the request
"""
{
"$class": "org.accordproject.dispatchtest.Request2"
}
"""
Then it should respond with
"""
{
"message": "I am a request1",
"$class": "org.accordproject.dispatchtest.Response"
}
"""

Scenario: The contract should return the correct response for a Request3
When it receives the request
"""
{
"$class": "org.accordproject.dispatchtest.Request3"
}
"""
Then it should respond with
"""
{
"message": "I am a request3",
"$class": "org.accordproject.dispatchtest.Response"
}
"""

Scenario: The contract should return the correct response for a Request4
When it receives the request
"""
{
"$class": "org.accordproject.dispatchtest.Request4"
}
"""
Then it should respond with
"""
{
"message": "I am a request4",
"$class": "org.accordproject.dispatchtest.Response"
}
"""

Scenario: The contract should return the correct response for a Request5
When it receives the request
"""
{
"$class": "org.accordproject.dispatchtest.Request5"
}
"""
Then it should fail with the error
"""
{
"kind": "ErgoError",
"message": {
"type": ["org.accordproject.ergo.stdlib.ErgoErrorResponse"],
"data": {
"message": "Dispatch Error: no clause in the contract matches the request"
}
}
}
"""

Scenario: The contract should return the correct response for a Request6
When it receives the request
"""
{
"$class": "org.accordproject.dispatchtest.Request6"
}
"""
Then it should respond with
"""
{
"message": "I am a request6",
"$class": "org.accordproject.dispatchtest.Response"
}
"""

111 changes: 111 additions & 0 deletions packages/ergo-engine/test/features/dispatchtest3.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
Feature: Dispatch Tests
This describe the expected behavior when dispatching requests to a clause

Background:
Given the Ergo contract "org.accordproject.dispatchtest.DispatchTest" in file "data/dispatchtest/logic3.ergo"
And the model in file "data/dispatchtest/model.cto"
And the contract data
"""
{
"$class": "org.accordproject.dispatchtest.TemplateModel"
}
"""

Scenario: The contract should initialize
Then the initial state should be the default state

Scenario: The contract should return the correct response for a Request1
When it receives the request
"""
{
"$class": "org.accordproject.dispatchtest.Request1"
}
"""
Then it should respond with
"""
{
"message": "I am a request1",
"$class": "org.accordproject.dispatchtest.Response"
}
"""

Scenario: The contract should return the correct response for a Request2
When it receives the request
"""
{
"$class": "org.accordproject.dispatchtest.Request2"
}
"""
Then it should respond with
"""
{
"message": "I am a request1",
"$class": "org.accordproject.dispatchtest.Response"
}
"""

Scenario: The contract should return the correct response for a Request3
When it receives the request
"""
{
"$class": "org.accordproject.dispatchtest.Request3"
}
"""
Then it should respond with
"""
{
"message": "I am a request3",
"$class": "org.accordproject.dispatchtest.Response"
}
"""

Scenario: The contract should return the correct response for a Request4
When it receives the request
"""
{
"$class": "org.accordproject.dispatchtest.Request4"
}
"""
Then it should respond with
"""
{
"message": "I am a request4",
"$class": "org.accordproject.dispatchtest.Response"
}
"""

Scenario: The contract should return the correct response for a Request5
When it receives the request
"""
{
"$class": "org.accordproject.dispatchtest.Request5"
}
"""
Then it should fail with the error
"""
{
"kind": "ErgoError",
"message": {
"type": ["org.accordproject.ergo.stdlib.ErgoErrorResponse"],
"data": {
"message": "Dispatch Error: no clause in the contract matches the request"
}
}
}
"""

Scenario: The contract should return the correct response for a Request6
When it receives the request
"""
{
"$class": "org.accordproject.dispatchtest.Request6"
}
"""
Then it should respond with
"""
{
"message": "I am a request6",
"$class": "org.accordproject.dispatchtest.Response"
}
"""

0 comments on commit 76d737d

Please sign in to comment.