Context.behavior.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const { contract } = require('@openzeppelin/test-environment');
  2. const { BN, expectEvent } = require('@openzeppelin/test-helpers');
  3. const ContextMock = contract.fromArtifact('ContextMock');
  4. function shouldBehaveLikeRegularContext (sender) {
  5. describe('msgSender', function () {
  6. it('returns the transaction sender when called from an EOA', async function () {
  7. const { logs } = await this.context.msgSender({ from: sender });
  8. expectEvent.inLogs(logs, 'Sender', { sender });
  9. });
  10. it('returns the transaction sender when from another contract', async function () {
  11. const { tx } = await this.caller.callSender(this.context.address, { from: sender });
  12. await expectEvent.inTransaction(tx, ContextMock, 'Sender', { sender: this.caller.address });
  13. });
  14. });
  15. describe('msgData', function () {
  16. const integerValue = new BN('42');
  17. const stringValue = 'OpenZeppelin';
  18. let callData;
  19. beforeEach(async function () {
  20. callData = this.context.contract.methods.msgData(integerValue.toString(), stringValue).encodeABI();
  21. });
  22. it('returns the transaction data when called from an EOA', async function () {
  23. const { logs } = await this.context.msgData(integerValue, stringValue);
  24. expectEvent.inLogs(logs, 'Data', { data: callData, integerValue, stringValue });
  25. });
  26. it('returns the transaction sender when from another contract', async function () {
  27. const { tx } = await this.caller.callData(this.context.address, integerValue, stringValue);
  28. await expectEvent.inTransaction(tx, ContextMock, 'Data', { data: callData, integerValue, stringValue });
  29. });
  30. });
  31. }
  32. module.exports = {
  33. shouldBehaveLikeRegularContext,
  34. };