/* ----------------------------------------------------------------------------- Copyright 2022-2023 Kevin P. Barry Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ----------------------------------------------------------------------------- */ // Author: Kevin P. Barry [ta0kira@gmail.com] testcase "Pointer passing" { success } concrete CallHelper { @type call (String) -> (String) @type call2 (String) -> (String) } define CallHelper { call (data) { Request request <- Request.create(data) Response response <- Response.create("") // This is just meant to test code generated for passing Pointer as a // function arg or return. return Response.getData(callWithPointers(request.asRequest(), response.asResponse())) } call2 (data) { Request request <- Request.create(data) Response response <- Response.create("") \ Service.send2(request.asMessage(), response.asResponse()) return response.formatted() } @type callWithPointers (Pointer, Pointer) -> (Pointer) callWithPointers (request, response) { \ Service.send(request, response) return response } } unittest test { scoped { String value <- CallHelper.call("DATA") } in if (value != "DATA has been processed as Request") { fail(value) } scoped { String value <- CallHelper.call2("DATA") } in if (value != "REQUEST has been processed as Message") { fail(value) } } testcase "Pointer storage" { success } concrete Helper { @type new () -> (Helper) @value call () -> () } define Helper { @category Request request <- Request.create("request") @category Pointer requestPointer <- request.asRequest() @value Response response @value Pointer responsePointer new () { Response response <- Response.create("response") return #self{ response, response.asResponse() } } call () { scoped { String value <- Request.getData(requestPointer) } in if (value != "request") { fail(value) } scoped { String value <- Response.getData(responsePointer) } in if (value != "response") { fail(value) } Pointer localRequest <- requestPointer Pointer localResponse <- responsePointer scoped { String value <- Request.getData(localRequest) } in if (value != "request") { fail(value) } scoped { String value <- Response.getData(localResponse) } in if (value != "response") { fail(value) } localRequest <- request.asRequest() localResponse <- response.asResponse() scoped { String value <- Request.getData(localRequest) } in if (value != "request") { fail(value) } scoped { String value <- Response.getData(localResponse) } in if (value != "response") { fail(value) } requestPointer <- localRequest responsePointer <- localResponse scoped { String value <- Request.getData(requestPointer) } in if (value != "request") { fail(value) } scoped { String value <- Response.getData(responsePointer) } in if (value != "response") { fail(value) } // Test swapping. Request request2 <- Request.create("request2") Pointer requestPointer2 <- request2.asRequest() localRequest <-> requestPointer2 scoped { String value <- Request.getData(localRequest) } in if (value != "request2") { fail(value) } scoped { String value <- Request.getData(requestPointer2) } in if (value != "request") { fail(value) } } } unittest test { \ Helper.new().call() }