Context.behavior.js 1.6 KB

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